Solved How do you check if a player is holding x item?

Discussion in 'Plugin Development' started by Phantom_, Aug 28, 2016.

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

    Phantom_

    Code:
    @EventHandler
        public void onSwitch(PlayerItemHeldEvent e)    {
            final Player p = e.getPlayer();
            if (p.getItemInHand().getItemMeta().getLore().contains(ChatColor.GREEN + "Miner")){
                p.sendMessage("k");
                 p.addPotionEffect(new PotionEffect(PotionEffectType.FAST_DIGGING, 2000000000, 1));
            }
            else{
                p.removePotionEffect(PotionEffectType.FAST_DIGGING);
            }
               
        }
    It gives the user the potion effect when they move off of the item but not when they are on the item
     
  2. Offline

    HeartandSoul

    Instead of getting the lore, get the name. Then scan for a right click action. This would just, in my opinion, make it better.
     
  3. Agreed. Also, I don't mind if they hold or mine. And this is a matter of opinions but I like to use "equals" or more perferablly (with strings), "equalIgnoreCase" when I check for matching variables.
     
  4. Offline

    Lordloss

    No need to declare your player final. Also, check first if the item has meta and has lore before using them. It is gonna spam your console to death.
     
Thread Status:
Not open for further replies.

Share This Page