Right click event works even if the item isn't in my hand

Discussion in 'Plugin Development' started by stimoze, Jul 9, 2016.

Thread Status:
Not open for further replies.
  1. So, i'm developing a plugin (HealingItem), this plugin adds a Healer item, and if you right click the item if you dont have full Health, it will heal you. But the problem is when my hand is not on the item. and i right click , it also works... And it works too if i give myself a single Blaze rod, and i want to block this.

    So these are my problems. There is a source code of the void

    Code:
        @EventHandler
        public void itemData(PlayerInteractEvent e){
            String prefix;
            prefix = translateColor(getConfig().getString("Prefix"));
                ItemStack HealItem = new ItemStack (Material.BLAZE_ROD);
                ItemMeta meta = HealItem.getItemMeta();
                meta.setDisplayName(prefix + " " + translateColor(getConfig().getString("ItemName")));
                List<String> lore = new ArrayList();
                lore.add(translateColor(getConfig().getString("ItemLore")));
                meta.setLore(lore);
                HealItem.setItemMeta(meta);
                if (e.getPlayer().hasPermission("itemheal.item.use"))
                {
                    if (e.getPlayer().getInventory().getItemInHand() == HealItem)
                    {
                        if (e.getPlayer().getInventory().getItemInHand() == HealItem)
                        {
                            if (e.getPlayer().getHealth() == 20)
                            {
                            e.getPlayer().sendMessage(prefix + " " + translateColor(getConfig().getString("Healdeny-message")));
                            }
                            else
                            {
                            e.getPlayer().getInventory().removeItem(HealItem);
                            e.getPlayer().setHealth(20);
                            e.getPlayer().sendMessage(prefix + " " + translateColor(getConfig().getString("Healsuccess-message")));
                            }
                        }
                    }
                    else if (e.getPlayer().getInventory().getItemInHand() == HealItem)
                    {
                        if (e.getPlayer().getHealth() == 20)
                        {
                        e.getPlayer().sendMessage(prefix + " " + translateColor(getConfig().getString("Healdeny-message")));
                        }
                        else
                        {
                        e.getPlayer().getInventory().removeItem(HealItem);
                        e.getPlayer().setHealth(20);
                        e.getPlayer().sendMessage(prefix + " " + translateColor(getConfig().getString("Healsuccess-message")));
                        }
                    }
                   
                    else
                    {
                            e.getPlayer().sendMessage(translateColor(getConfig().getString("Permissiondeny-message")));
                    }
                }
            }
     
  2. Offline

    ipodtouch0218

    Use .equals() instead of ==

    I don't see what you're doing. You have
    Code:
    if (e.getPlayer().getInventory().getItemInHand() == HealItem) {
    } else if (e.getPlayer().getInventory().getItemInHand() == HealItem) {
    }
    And you are also checking if the item in the hand is the HealItem twice?
    Code:
                    if (e.getPlayer().getInventory().getItemInHand() == HealItem)
                    {
                        if (e.getPlayer().getInventory().getItemInHand() == HealItem)
                        {
    e.getPlayer().getInventory().getItemInHand() also may return null if the player has nothing in their hand, causing a NullPointerException

    And if they don't have the item in their hand they don't have permission..?
     
    Last edited: Jul 9, 2016
Thread Status:
Not open for further replies.

Share This Page