[Solved] ChangeBlockEvent.Break Cause Error?

So I’m working on a listener that prevents non players from breaking blocks on the server. If a player breaks a block, I want to send the player a message. For some reason, fire is being considered as an instance of a player and I’m getting endless “Cause is a player” messages. Is this intentional or a bug?

@Listener
public void onDestroyBlock(ChangeBlockEvent.Break event) {
	
	Optional<Player> cause = event.getCause().first(Player.class);
	
	if(cause.isPresent()){
		
		Sponge.getServer().getBroadcastChannel().send(Text.of("Cause is a player"));
	}
	event.setCancelled(true);
}

I believe that Player is a notifier, but not the cause. Try instead checking whether the root cause is a player.

1 Like

Try this:

@Listener
public void onDestroyBlock(ChangeBlockEvent.Break event, @Root Player player) {
  Sponge.getServer.getBroadcastChannel().send.......you get the rest of it.
}

Because yes, the player in other cases can be the notifier of the block changes (when they’ve placed blocks or placed blocks next to other blocks that cause a break/change).

1 Like

Yeah, you should probably be using filters anyway.

I’ve thought about adding a @Root filter, which would only filter out block destructions caused by Players, but I’d be ignoring all the block destructions caused by entities like creepers. Thanks for your suggestion though!

Then why not do a @Root Entity, and check if it’s a player?

1 Like

Oh! :joy: palmface I didn’t realize I could do that haha. Thanks!