I am trying to find the easiest way to delete for example a rail, when placed on top of a slime block. I don't want to scan for all blocks within certain radius, because I don't want the plugin to use much ram.
I would like the rail to get deleted only when placed on top of a slime block.
Thanks for any answers.
Like Benjamin Urquhart has done, always divide your problem into smaller sub-problems, these can be answered much easier if you search for them rather than searching for the main problem, because the main is most likely unique to you whereas the sub-problems are not.
With this in mind, we need to start off listening for an event. More specifically the BlockPlaceEvent. Set up your class as a listener and register it.
Inside the event we need to check if the block placed is a rail and the block under is a slime, if so, cancel the event to prevent it from being placed. Note that cancelling the event will only prevent rail from being placed, not deleted. If you want to delete the rail you have to set its type to AIR instead of cancelling the event.
You will end up with something like this:
@EventHandler
public void onBlockPlace(BlockPlaceEvent event) {
Block block = event.getBlock();
Block against = event.getBlockAgainst();
if(block.getType().equals(Material.RAIL) && against.getType().equals(Material.SLIME_BLOCK)) {
event.setCancelled(true);
}
}
You have to put this code into a registered listener class for it to be called. I choose to use #getBlockAgainst() to check for our slime block. Note that this method returns the block that our rail was placed on, in our case the rail can only be placed on top of blocks meaning the method must return the block below. However, remember the method does not ensure that with blocks other than rail.
In other cases you might have to get the block at coordinate block.getY()-1
, that will always return the block below but requires more performance. I choose event.getBlockAgainst()
because you had performance in mind.