Checking when a player hits another player with a certain item

Discussion in 'Spigot Plugin Development' started by BillyBob9876, May 16, 2016.

  1. Yup. Read the title. How do I check when a player hits another with a certain item for eg a blaze rod sets the "hit" player on fire?
     
  2. This is fairly simple. You would need to listen for the #EntityDamageByEntityEvent. After, some things to check for would be:

    • Get the entity from the event, in this case, that would be the victim
    • Instance the entity as a player
    • Get the damager from the event
    • Instance the attacker as a player
    • Check the type of the material in the damager's main hand and other conditions to apply further modifications to the event
    An example of this would be as shown.

    Code (Text):

            Entity entity = event.getEntity();
            Entity attacker = event.getDamager();
     
            if (entity instanceof Player)
            {
                if (attacker instanceof Player)
                {
                    if (((Player) attacker).getInventory().getItemInMainHand().getType().equals(...))
                    {
                        entity.setFireTicks(...);
                        entity.sendMessage("burn baby, burnn..");
                    }
                }
                else
                {
                    return;
                }
            }
            else
            {
                return;
            }
     
     
  3. please don't put an else return. please don't put two either
     
  4. I of course did not mean for the original post to intend on leaving it as I put it. It was simply a holder for checking other conditions.
     
  5. Thanks! Im new to plugins :p