Help?

Discussion in 'Spigot Plugin Development' started by Known, Apr 29, 2017.

  1. @EventHandler
    public void onInteract(PlayerInteractEvent e) {
    Player player = e.getPlayer();
    if(e.getPlayer().getItemInHand().getType() == Material.AIR){
    if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK) {
    player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 200, 4));

    }
    }

    }
    not really sure whats wrong with this code, someone help please.
     
  2. Well, we don't know either, since you haven't told us your issue...
    My guesses:
    - Nothing happens > the event is not registered
    - NullPointerException > getItemInHand is null (but if I remember correctly it's not the case here)
    - Your method may fire twice > PlayerInteractEvent: "potentially fired once for each hand"

    Also, please use code tags...
     
  3. what's your issue? maybe post your error
     
  4. Register it with:
    Bukkit.getPluginManager().registerEvents(this, this);
     
  5. You must check if getItemInHand() is null BEFORE you check its type. If you invoke getType() or any other method on a null object you will get a Null Pointer Exception.


    Sent from my iPhone using Tapatalk