Closing inventory by clicking outside of GUI doesn't close inventory properly
Description
When closing any container with a GUI (including the player inventory) by clicking outside of the container GUI in touchscreen mode, the container won't be closed properly and the items will stay in it. This could be abused to "gain" extra inventory space by filling the items in the crafting slots.
Reproduction steps
Unlisted video demonstrating these steps: https://youtu.be/WLilZsK7Q1Q
- Go to Options, Mouse Settings and enable touchscreen mode.
- Open your inventory (or another container) and place an itemstack in the crafting slot.
- Exit the inventory by clicking on the empty space around the GUI (not by pressing escape or E)
- Notice how the item hasn't gone back to your regular inventory space. When opening your inventory again, you will can see it's still in the crafting slot.
Code analysis
Inside of AbstractContainerScreen#mouseClicked, the current screen will be set to null when when the player is using a touch screen and clicks outside of the screen area:
boolean var12 = this.hasClickedOutside(var1, var3, var10, var11, var5); if (this.minecraft.options.touchscreen && var12 && this.menu.getCarried().isEmpty()) { this.minecraft.setScreen((Screen)null); return true; }
No additional container closing logic is called, causing no ServerboundContainerClosePacket to be sent, which is why the server doesn't know the client has closed their inventory, which is why it won't reset the itemstacks back into the regular inventory.
The easiest fix here would be to replace the setScreen call with `this.onClose();`
2022-03-14, 07:18 PM
2022-03-24, 10:32 AM
2022-03-24, 10:32 AM
4
5