Event to check player right clicking entity (another player)

Hello everyone,

I’ve recently moved from Bukkit/Spigot development here to Sponge to try out the API and see which one suits me more. So far so good and I love these builders, but when working with events I got stuck.

I’m trying to check if a player right clicks another player, the only event I could find was the InteractEntityEvent and I can indeed check if the entity is an instance of a player, but I can’t seem to get the player WHO right clicked the other entity.

Does somebody of you know of a way to do this? Any help is appreciated!

Thanks in advance,
Skrypt

That will be in the cause object.
try this:

Optional<Player> playerCause = event.getCause().first(Player.class);
if (playerCause.isPresent()) {
    // Player caused the interaction
} else {
    // Non-player caused interaction
}

1 Like

Oh, that seems to work! Thanks!

Also note you can use Cause filtering like so:

@Listener
public void onInteract(InteractEntityEvent event, @Root Player player) {
  // do stuff
}

More can be read on the docs.

2 Likes