Cancel InventoryClickEvent Deletes Item

Discussion in 'Plugin Development' started by Jmbeard96, Mar 12, 2018.

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


    I'm trying to prevent users from holding an item in the off hand when they are holding a custom weapon so I used the InventoryClickEvent to catch when they manually put an item in the off hand slot in the inventory and cancel it if they are holding the custom weapon in their main hand. However, when the event fires, the message displays as it should, but the item that you try to put in the off hand disappears. I tried adding the item back to the player's inventory before cancelling the event, but that duplicates the item. What am I doing wrong?
    public void itemClick(InventoryClickEvent e) {
            if (e.getSlot() == 40) { //offhand slot
                Player p = (Player)e.getWhoClicked();
                if(Staff.isInHand(p.getInventory().getItemInMainHand())) {
                    //ItemStack item = e.getCursor();
                    p.sendMessage(ChatColor.RED + "The Dragon Staff is a two-handed weapon.");
  2. Offline


    If it's duplicating you need to update the inventory after you cancelled the event. Maybe that will help.
    1. p.updateInventory();
  3. Offline


    I'm not 100% sure if this will fix it but the first thing I would try is doing player.updateInventory() after e.setCancelled(true); normally when I have issues when cancelling inventory click events that fixes it.
  4. Offline


    I just tried that and it did not work. The first time you try to move something over to your offhand, it jumps back down to the inventory like it should. The second time you try with the same item, it jumps back down, but the amount on the stack doubles. The third time it doubles again and so on.
    1. public void itemClick(InventoryClickEvent e) {
    3. if (e.getSlot() == 40) { //offhand slot
    5. Player p = (Player)e.getWhoClicked();
    7. if(Staff.isInHand(p.getInventory().getItemInMainHand())) {
    9. ItemStack item = e.getCursor();
    11. p.getInventory().addItem(item);
    13. p.sendMessage(ChatColor.RED + "The Dragon Staff is a two-handed weapon.");
    15. e.setCancelled(true);
    17. p.updateInventory();
    18. }
    19. }
    20. }
  5. Offline


    The problem probably lays in you adding an item. There should be no other reason why it's duplicating honestly.

    Here let me take a step back. Give us a detailed explanation of what you want your code to do so we can solve it accordingly.
  6. Offline


    It is supposed to prevent people from moving an item to the offhand slot in the inventory. Doing that by clicking an item in the inventory (moving the item to the cursor) then clicking the offhand slot. If a player does that while holding the custom weapon in the main hand, the code should cancel the event (moving the item they tried to put in the offhand back to an inventory slot) and send the message to the player saying you can’t use offhand with the custom weapon
Thread Status:
Not open for further replies.

Share This Page