From 4eaa15477ba38bfb34380377205fb375a3754a22 Mon Sep 17 00:00:00 2001 From: TheMode Date: Sat, 24 Apr 2021 19:14:19 +0200 Subject: [PATCH] Potentially fix cursor ghost item when opening a new inventory --- .../java/net/minestom/server/entity/Player.java | 6 +++--- .../net/minestom/server/inventory/Inventory.java | 15 ++++++++------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index b9818c1cc..f2b0c45a9 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -1928,9 +1928,9 @@ public class Player extends LivingEntity implements CommandSender, Localizable, InventoryOpenEvent inventoryOpenEvent = new InventoryOpenEvent(inventory, this); callCancellableEvent(InventoryOpenEvent.class, inventoryOpenEvent, () -> { - - if (getOpenInventory() != null) { - getOpenInventory().removeViewer(this); + Inventory openInventory = getOpenInventory(); + if (openInventory != null) { + openInventory.removeViewer(this); } Inventory newInventory = inventoryOpenEvent.getInventory(); diff --git a/src/main/java/net/minestom/server/inventory/Inventory.java b/src/main/java/net/minestom/server/inventory/Inventory.java index c61aeb7c9..728209009 100644 --- a/src/main/java/net/minestom/server/inventory/Inventory.java +++ b/src/main/java/net/minestom/server/inventory/Inventory.java @@ -174,7 +174,7 @@ public class Inventory extends AbstractInventory implements Viewable { @Override public boolean removeViewer(@NotNull Player player) { final boolean result = this.viewers.remove(player); - this.cursorPlayersItem.remove(player); + setCursorItem(player, ItemStack.AIR); this.clickProcessor.clearCache(player); return result; } @@ -198,18 +198,19 @@ public class Inventory extends AbstractInventory implements Viewable { * @param cursorItem the new player cursor item */ public void setCursorItem(@NotNull Player player, @NotNull ItemStack cursorItem) { - if (!isViewer(player)) - return; - - final ItemStack currentCursorItem = cursorPlayersItem.get(player); - final boolean similar = currentCursorItem != null && currentCursorItem.isSimilar(cursorItem); + final ItemStack currentCursorItem = cursorPlayersItem.getOrDefault(player, ItemStack.AIR); + final boolean similar = currentCursorItem.isSimilar(cursorItem); if (!similar) { final SetSlotPacket setSlotPacket = SetSlotPacket.createCursorPacket(cursorItem); player.getPlayerConnection().sendPacket(setSlotPacket); } - this.cursorPlayersItem.put(player, cursorItem); + if (!cursorItem.isAir()) { + this.cursorPlayersItem.put(player, cursorItem); + } else { + this.cursorPlayersItem.remove(player); + } } /**