Search code examples
javabukkit

Bukkit - How to check if a player is attacking


How would I check if a player is swinging a sword in a if() statement?


Solution

  • You can listen to the PlayerInteractEvent and check if said player is holding a sword.

    This example can be used for 1.9:

    if (p.getInventory().getItemInMainHand().getType() == Material.DIAMOND_SWORD)
    

    For 1.8 and below you can simply use: player.getItemInHand().getType()

    Make sure to check if the Action is LeftClickBlock or LeftClickAir(Attacking): if (e.getAction() == Action.LEFT_CLICK_AIR && e.getAction() == Action.LEFT_CLICK_BLOCK)