Skip to content

Comments

Fix inventory desync by waiting 1 tick.#53

Open
DungeonDev wants to merge 2 commits intolMartin3:masterfrom
DungeonDev:fix_inventory_desync
Open

Fix inventory desync by waiting 1 tick.#53
DungeonDev wants to merge 2 commits intolMartin3:masterfrom
DungeonDev:fix_inventory_desync

Conversation

@DungeonDev
Copy link

@DungeonDev DungeonDev commented Mar 23, 2023

Fixes #52

Reading the following from the Bukkit API:

The following should never be invoked by an EventHandler for InventoryClickEvent using the HumanEntity or InventoryView associated with this event:

HumanEntity.closeInventory()
HumanEntity.openInventory(Inventory)
HumanEntity.openWorkbench(Location, boolean)
HumanEntity.openEnchanting(Location, boolean)
InventoryView.close()
To invoke one of these methods, schedule a task using BukkitScheduler.runTask(Plugin, Runnable), which will run the task on the next tick. Also be aware that this is not an exhaustive list, and other methods could potentially create issues as well.

I'm personally not the most experienced when it comes to working with Bukkit, but the current InventoryClickEvent handler does in fact call both closeInventory and openInventory, which is why I followed the API:s recommendation and delayed execution of openShulkerBoxInventory by 1 tick. For safety, I also added a check to make sure that the item right-clicked has not changed since the click, as clients otherwise have a 1 tick window to do bad stuff, such as replacing the shulker being opened.

This is the solution used by me on my personal server, if there is a better solution someone more experienced can come up with, I'd be all ears.

All in all I'd still recommend more testing to make sure that there isn't any edge-cases where items can be duplicated.

@Oryfox
Copy link
Contributor

Oryfox commented May 28, 2023

Tested it just now. Works fine!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Inventory desync (visual)

2 participants