Right click event (with item name)

Discussion in 'Plugin Development' started by joshzen, Mar 31, 2015.

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

    joshzen

    Hey guys.
    I've had some truble with making a right click event.
    I can make a event that does something after you right clicked with for example a piece of sugar.
    but i can't require it to have a name. This is my code:
    Code:
        @EventHandler
        public void druginjection(PlayerInteractEvent event) {
          
          
            Player player = event.getPlayer();
            if(player.getInventory().getItemInHand().getType() == Material.STICK && player.getItemInHand().getItemMeta().getDisplayName() == "Heroin injection") {
              
                if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
          
                    player.sendMessage(ChatColor.BOLD + "Hmm that feels good!");
                  
                    player.addPotionEffect(new PotionEffect(PotionEffectType.ABSORPTION, 100, 2));  
                  
                    player.setItemInHand(null);
                  
                }
              
            }
          
          
          
          
          
        }
    
     
    Last edited: Mar 31, 2015
  2. Offline

    Signatured

    Are you getting any errors in console? Have you registered your events? I would check to see if Action == your action first before checking the item in hand.
     
  3. Offline

    EpicCraft21

    Show the code where you made the heroin injection item please.
     
  4. Offline

    joshzen

    I don't get any errors from the console the only thing it sais is that the plugin is enabled

    I don't make the item with code, I'm trying to make it possible that i can make it myself ingame or with a anvil
     
  5. Offline

    Zombie_Striker

    Don't set the Item to Null, That will cause a NullPointerException! You are also using == instead of .equals(); , which is a bad habit of getting into. You use player.getInventory().getItemInHand() instead of player.getItemInHand(); , I would suggest you change that. Are you registering Events? does your class implement Listener?
     
  6. Offline

    SuperOriginal

    What do you mean by "bad habit"? == is actually preferred when comparing enumerators.
     
    bcohen9685 likes this.
  7. Offline

    bcohen9685

    Change null to "new ItemStack(Material.AIR);
     
  8. Offline

    nverdier

    For Strings, sure. For enums, nope.
     
  9. Offline

    Zombie_Striker

    @nverdier @SuperOriginal I meant that .equeals() should be used here;
     
  10. Offline

    SuperOriginal

  11. Offline

    joshzen

    I used .equals() and it works perfect now!
    Thx so much!
     
Thread Status:
Not open for further replies.

Share This Page