diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index f7b9774d9..afd25b2ff 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -942,12 +942,25 @@ public class Player extends LivingEntity { */ public void closeInventory() { Inventory openInventory = getOpenInventory(); + + // Drop cursor item when closing inventory + ItemStack cursorItem; + if (openInventory == null) { + cursorItem = getInventory().getCursorItem(); + getInventory().setCursorItem(ItemStack.getAirItem()); + } else { + cursorItem = openInventory.getCursorItem(this); + } + if (!cursorItem.isAir()) { + dropItem(cursorItem); + } + CloseWindowPacket closeWindowPacket = new CloseWindowPacket(); if (openInventory == null) { closeWindowPacket.windowId = 0; } else { closeWindowPacket.windowId = openInventory.getWindowId(); - openInventory.removeViewer(this); + openInventory.removeViewer(this); // Clear cache refreshOpenInventory(null); } playerConnection.sendPacket(closeWindowPacket); diff --git a/src/main/java/net/minestom/server/inventory/Inventory.java b/src/main/java/net/minestom/server/inventory/Inventory.java index 46f4cee2f..688e07172 100644 --- a/src/main/java/net/minestom/server/inventory/Inventory.java +++ b/src/main/java/net/minestom/server/inventory/Inventory.java @@ -127,6 +127,8 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View @Override public void removeViewer(Player player) { this.viewers.remove(player); + this.cursorPlayersItem.remove(player); + this.clickProcessor.clearCache(player); } public ItemStack getCursorItem(Player player) { diff --git a/src/main/java/net/minestom/server/inventory/click/InventoryClickProcessor.java b/src/main/java/net/minestom/server/inventory/click/InventoryClickProcessor.java index 228533098..9fffb39fd 100644 --- a/src/main/java/net/minestom/server/inventory/click/InventoryClickProcessor.java +++ b/src/main/java/net/minestom/server/inventory/click/InventoryClickProcessor.java @@ -532,4 +532,9 @@ public class InventoryClickProcessor { player.callEvent(InventoryClickEvent.class, inventoryClickEvent); } + public void clearCache(Player player) { + this.leftDraggingMap.remove(player); + this.rightDraggingMap.remove(player); + } + } diff --git a/src/main/java/net/minestom/server/storage/StorageFolder.java b/src/main/java/net/minestom/server/storage/StorageFolder.java index f4bd1ab86..2153e7541 100644 --- a/src/main/java/net/minestom/server/storage/StorageFolder.java +++ b/src/main/java/net/minestom/server/storage/StorageFolder.java @@ -153,18 +153,20 @@ public class StorageFolder { * @param key the specified cached data key */ public void saveAndRemoveCachedData(String key) { - SerializableData serializableData = cachedData.get(key); - if (serializableData == null) - return; + synchronized (cachedData) { + SerializableData serializableData = cachedData.get(key); + if (serializableData == null) + return; - try { - set(key, serializableData.getSerializedData()); - } catch (IOException e) { - e.printStackTrace(); + try { + set(key, serializableData.getSerializedData()); + } catch (IOException e) { + e.printStackTrace(); + } + + // Remove from map + this.cachedData.remove(key); } - - // Remove from map - this.cachedData.remove(key); } /**