Detecting Hit With Certain Item

Discussion in 'Plugin Development' started by Zachster, May 11, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    Zachster

    I want to detect if a player hurts a mob with a certain item in-hand (e.g. a gold sword) How do I do this?
     
  2. Offline

    ZeusAllMighty11

    EntityDamageByEntityEvent
     
  3. Offline

    Zachster

    I got that but how do I do the specific item in hand and must be a player thing?
     
  4. Offline

    Minecrell

    Zachster
    Code:java
    1. @EventHandler()
    2. public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
    3. if ((event.getEntity() instanceof <YourMob>) && (event.getDamager() instanceof Player)) {
    4. Player player = (Player) event.getDamager();
    5. ItemStack item = player.getItemInHand();
    6. }
    7. }

    Where item is the players item in his hand. Replace <YourMob> with the mob type you want.
     
  5. Offline

    Zachster

    YourMob is what was hurt, right?
     
  6. Offline

    Minecrell

    Zachster Yes. The entity damaged by an other entity. ;)
     
  7. Offline

    Zachster

    Thanks. I'll try this tomorrow.
     
Thread Status:
Not open for further replies.

Share This Page