[SPIGOT-6145] InventoryClickEvent does not get correctly cancelled when action is HOTBAR_SWAP Created: 19/Sep/20  Updated: 16/Nov/21  Resolved: 01/Oct/20

Status: Resolved
Project: Spigot
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Minor
Reporter: Shynixn Assignee: Unassigned
Resolution: Invalid Votes: 1
Labels: None
Environment:
  • No other plugins and code except described below.

Attachments: PNG File gui-1.PNG     PNG File gui-2.PNG     PNG File gui-3.PNG    
Issue Links:
Duplicate
is duplicated by SPIGOT-6291 Ghost item in inventory after cancell... Resolved
Version: CraftBukkit version git-Spigot-17d78db-f8d4da0 (MC: 1.16.3) (Implementing API version 1.16.3-R0.1-SNAPSHOT)
Guidelines Read: Yes

 Description   

How to replicate

  • Create a custom inventory with the following code

 

Inventory inventory = Bukkit.getServer().createInventory(player, 54, "DemoGui");
inventory.setItem(0, new ItemStack(Material.APPLE));
player.openInventory(inventory);

  • Cancel every type of InventoryClickEvent 

 

 

@EventHandler
public void inventoryClickEvent(InventoryClickEvent event) {
    event.setCancelled(true);
    System.out.println(event.getAction().toString() + " cancelled.");
}
  • Observe that clicking on the apple with your mouse is correctly cancelled and the item does not move.

 

  • Move your mouse of the item and press "F"
  • Observe that the inventory item is copied to the offhand slot

Expected behaviour:

The item does not get copied to the offhand slot if the InventoryClickEvent is cancelled.

 

 



 Comments   
Comment by osipxd [ 16/Nov/21 ]

As a workaround it is possible to send a packet with the offhand item on the next tick after event using the following code:

if (event.getClick() == ClickType.SWAP_OFFHAND) {
    final PlayerInventory playerInventory = event.getWhoClicked().getInventory();
    Bukkit.getScheduler().runTask(plugin, () -> playerInventory.setItemInOffHand(playerInventory.getItemInOffHand()));
}

 

 

Comment by Black Hole [ 10/Mar/21 ]

This will be fixed in 1.17:
https://bugs.mojang.com/browse/MC-87019

Comment by md_5 [ 01/Oct/20 ]

There is a more general bug somewhere about how updateInventory doesn't affect the clients offhand slot, though I can't find it right now.
This is the same thing, and cannot be fixed by Spigot, it is a client issue and the client simply ignores any update packets spigot sends because the offhand is not part of these guis

Comment by Shynixn [ 19/Sep/20 ]

Has also been discussed in https://www.spigotmc.org/threads/how-to-cancel-pressing-f-in-gui.463831/#post-3950201.

Generated at Sat Jun 06 05:02:40 UTC 2026 using Jira 10.3.21#10030021-sha1:1b57c64b79893799c54a69ac24cc209127fad1fe.