Solved Item only removed when block clicked

Discussion in 'Plugin Development' started by 567legodude, Mar 25, 2015.

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

    567legodude

    I have a listener for when you right click with an item in your hand.
    When you right click, I have it set to remove the item in your hand and run a command.
    The problem is that it only removes the item if you right click on a block. Right clicking in the air does not remove the item but still runs the command. Anyone know a way to fix this?

    I am just simply doing player.setItemInHand(null); to remove the item. (updateInventory() doesn't help).
     
  2. Offline

    567legodude

    @CodePlaysMinecraft Thats not the problem, the problem is that when you click in the air the setItemInHand(null) does nothing.
     
  3. @567legodude Try Material.AIR also, please post full code and any relative classes.
     
  4. Offline

    Konato_K

    @567legodude When you click the air the event fires in a cancelled state, so if your event handler ignores cancelled events it won't remove the item.
     
  5. Offline

    567legodude

    @CodePlaysMinecraft My code is not the problem, it works just fine, it's just that the item is not removed.

    @Konato_K It does listen to the event because it runs the command, but the item is not removed.

    EDIT: I was able to get it to work by calling this function when they right clicked.

    Code:
    public void remove(final Player player) {
            Bukkit.getScheduler().runTask(plugin, new Runnable() {
                @Override
                public void run() {
                    int slot = player.getInventory().getHeldItemSlot();
                    ItemStack[] items = player.getInventory().getContents();
                    items[slot] = null;
                    player.getInventory().setContents(items);
                }
            });
        }
     
    Last edited: Mar 25, 2015
Thread Status:
Not open for further replies.

Share This Page