Item hit command

Discussion in 'Plugin Development' started by zioGiok, Aug 12, 2016.

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

    zioGiok

    I am working on a plugin and i need a command when someone is hit by an item (a blaze rod) but i have some errors with this part...I am not good creating plugin so i don't know where it's wrong.
    Code:
        @EventHandler
        public void blazerod(EntityDamageByEntityEvent e) {
                if (((e.getEntity() instanceof Player))
                        && ((e.getDamager() instanceof Player))) {
                    Player getdamager = (Player) e.getDamager();
                    Player hit = (Player) e.getEntity();
     
                    if (getdamager.getItemInHand().getType().equals(Material.BLAZE_ROD)) {
                        getdamager.sendMessage("ยง4Hit!");
                 }
                }
        }
    }
    The command sendMessage is only for try the plugin: It will do the real command when completed.

    Sorry for my bad English.
     
  2. Offline

    CaptainMike2828

    What do you exactly want to do?Could we have the Errors?
     
  3. Offline

    Zombie_Striker

    First, if the player is not holidng anything, getItemInHand will return a null object. Since you are not checking if it is null, this will thorw an error and break your event. Add the line below to your code (above the if statement)
    Code:
    if(getdamager.getItemInHand() == null) return;
    
    Also, use == to compare enums, so replace ".equals" with "=="
     
  4. Offline

    zioGiok

    @CaptainMike2828 I have not exported the plugin because eclipse give me some errors.
     
  5. Offline

    Marti201

    @zioGiok What are the exact errors eclipse is giving you? Can you post the whole code?
     
Thread Status:
Not open for further replies.

Share This Page