How to check if a player right-clicked another player

Viewed 1708

Looking for the right event to work with. I want to check if a player right-clicked another player.

This is what i have so far (doesn't work. Not getting into the if or the else statement:

public void onPlayerRightClicks(PlayerInteractEntityEvent e) {
    Player p=e.getPlayer();
    if(e.getRightClicked() instanceof Player) p.sendMessage("You have rightclicked a player.");
    else p.sendMessage("You didn't hit anyone with your spell");
}
2 Answers

Try this:

@EventHandler
public void onPlayerInteractEntity(final PlayerInteractAtEntityEvent e) {
    final Player p = e.getPlayer();
    if (e.getRightClicked() instanceof Player) {
        final Player clicked = (Player) e.getRightClicked();
        // DO STUFF
    }
}
Related