From c7876d648c951afc33ec5ef9f91a6970f1fdc8ad Mon Sep 17 00:00:00 2001 From: themode Date: Fri, 18 Dec 2020 04:05:29 +0100 Subject: [PATCH] Fix NPE when clicking outside of the inventory to drop --- .../java/net/minestom/server/inventory/Inventory.java | 7 ++++--- .../net/minestom/server/inventory/PlayerInventory.java | 5 +++-- .../server/inventory/click/InventoryClickProcessor.java | 8 ++++---- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/main/java/net/minestom/server/inventory/Inventory.java b/src/main/java/net/minestom/server/inventory/Inventory.java index 9390ce1ba..8e9e6d95a 100644 --- a/src/main/java/net/minestom/server/inventory/Inventory.java +++ b/src/main/java/net/minestom/server/inventory/Inventory.java @@ -534,8 +534,9 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View public boolean drop(@NotNull Player player, int mode, int slot, int button) { final PlayerInventory playerInventory = player.getInventory(); final boolean isInWindow = isClickInWindow(slot); - final ItemStack clicked = slot == -999 ? - null : (isInWindow ? getItemStack(slot) : playerInventory.getItemStack(slot, offset)); + final boolean outsideDrop = slot == -999; + final ItemStack clicked = outsideDrop ? + ItemStack.getAirItem() : (isInWindow ? getItemStack(slot) : playerInventory.getItemStack(slot, offset)); final ItemStack cursor = getCursorItem(player); final InventoryClickResult clickResult = clickProcessor.drop(this, player, @@ -546,7 +547,7 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View } ItemStack resultClicked = clickResult.getClicked(); - if (isInWindow) { + if (isInWindow && !outsideDrop) { if (resultClicked != null) setItemStack(slot, resultClicked); } else { diff --git a/src/main/java/net/minestom/server/inventory/PlayerInventory.java b/src/main/java/net/minestom/server/inventory/PlayerInventory.java index 0d36098d9..33b5916d4 100644 --- a/src/main/java/net/minestom/server/inventory/PlayerInventory.java +++ b/src/main/java/net/minestom/server/inventory/PlayerInventory.java @@ -414,7 +414,8 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler @Override public boolean drop(@NotNull Player player, int mode, int slot, int button) { final ItemStack cursor = getCursorItem(); - final ItemStack clicked = slot == -999 ? null : getItemStack(slot, OFFSET); + final boolean outsideDrop = slot == -999; + final ItemStack clicked = outsideDrop ? ItemStack.getAirItem() : getItemStack(slot, OFFSET); final InventoryClickResult clickResult = clickProcessor.drop(null, player, mode, slot, button, clicked, cursor); @@ -423,7 +424,7 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler sendSlotRefresh((short) slot, clicked); ItemStack resultClicked = clickResult.getClicked(); - if (resultClicked != null) + if (resultClicked != null && !outsideDrop) setItemStack(slot, OFFSET, resultClicked); setCursorItem(clickResult.getCursor()); 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 c6b2eb5f5..1490ddc19 100644 --- a/src/main/java/net/minestom/server/inventory/click/InventoryClickProcessor.java +++ b/src/main/java/net/minestom/server/inventory/click/InventoryClickProcessor.java @@ -425,17 +425,17 @@ public class InventoryClickProcessor { public InventoryClickResult drop(Inventory inventory, Player player, int mode, int slot, int button, - ItemStack clicked, ItemStack cursor) { + @NotNull ItemStack clicked, @NotNull ItemStack cursor) { final InventoryClickResult clickResult = startCondition(inventory, player, slot, ClickType.DROP, clicked, cursor); if (clickResult.isCancel()) { return clickResult; } - final StackingRule clickedRule = clicked == null ? null : clicked.getStackingRule(); + final StackingRule clickedRule = clicked.getStackingRule(); final StackingRule cursorRule = cursor.getStackingRule(); - ItemStack resultClicked = clicked == null ? null : clicked.clone(); + ItemStack resultClicked = clicked.clone(); ItemStack resultCursor = cursor.clone(); @@ -494,7 +494,7 @@ public class InventoryClickProcessor { @NotNull private InventoryClickResult startCondition(@NotNull InventoryClickResult clickResult, @Nullable Inventory inventory, @NotNull Player player, int slot, @NotNull ClickType clickType, - ItemStack clicked, ItemStack cursor) { + @NotNull ItemStack clicked, ItemStack cursor) { boolean isPlayerInventory = inventory == null; final int inventorySlot = isPlayerInventory ? 0 : inventory.getSize(); isPlayerInventory = isPlayerInventory ? isPlayerInventory : slot >= inventorySlot;