How can I Listen A event then cancel B event


#1

I am working on my new plugin(FarmProtect). I need to Listen to CollideBlockEvent to know whether the Farmland is being steeped. Then I need to cancel the InteractBlockEvent to protect the farmland.
How can I cancel the InteractBlockEvent when CollideBlockEvent triggered


#2

Use a static field (also known as global variables) to share the data that you require to check that the InteractBlockEvent is targeting your farmland (such as location or something).

After that listen to InteractBlockEvent and make sure the static field data meets the requirements, cancel the event and then reset the values from the static fields


#3

To add on to MoseMister, you can also setup a map with a player name and maybe just a boolean if you’re only doing it for one event. That way you can keep track of who did what event to avoid others messing with it with their event activation


#4

Here is another approach that works really well:

@Listener
public void onChangeBlock(ChangeBlockEvent.Place e, @Root Player p) {
	for (Transaction<BlockSnapshot> t : e.getTransactions()) {
		if (t.getOriginal().getState().getType() == BlockTypes.FARMLAND
				&& t.getDefault().getState().getType() == BlockTypes.DIRT
				&& t.getOriginal().getPosition().add(0, 1, 0).equals(p.getPosition().toInt())) {
			t.setValid(false);
		}
	}
}