From 4365467ba0d7ed589596f92357c8b63e6351059d Mon Sep 17 00:00:00 2001 From: Phillipp Glanz Date: Sun, 3 Nov 2024 15:08:25 +0100 Subject: [PATCH 1/2] Implement a way to override the prediction of start slot for inventory --- .../server/inventory/AbstractInventory.java | 28 +++++++++++++++++++ .../minestom/server/inventory/Inventory.java | 27 ++++++++++++++++-- .../server/inventory/PlayerInventory.java | 3 +- .../click/InventoryClickProcessor.java | 4 +-- 4 files changed, 57 insertions(+), 5 deletions(-) diff --git a/src/main/java/net/minestom/server/inventory/AbstractInventory.java b/src/main/java/net/minestom/server/inventory/AbstractInventory.java index 1d239416120..db3e6164d3a 100644 --- a/src/main/java/net/minestom/server/inventory/AbstractInventory.java +++ b/src/main/java/net/minestom/server/inventory/AbstractInventory.java @@ -1,5 +1,6 @@ package net.minestom.server.inventory; +import net.minestom.server.entity.Player; import net.minestom.server.event.EventDispatcher; import net.minestom.server.event.inventory.InventoryItemChangeEvent; import net.minestom.server.event.inventory.PlayerInventoryItemChangeEvent; @@ -56,6 +57,33 @@ public synchronized void setItemStack(int slot, @NotNull ItemStack itemStack) { safeItemInsert(slot, itemStack); } + + // Microtus - fix shift click for inventory + /** + * Gets the slot for a shift click operation. + *

+ * This is used to determine where the item should be placed when shift clicking. + * + * @param index the slot index + * @return the slot where the item should be placed when shift clicking + */ + int getStartSlotForShiftClick(int index, Player player) { + return 0; + } + + /** + * Gets the slot for a double click operation. + *

+ * This is used to determine where the item should be placed when double-clicking. + * + * @param index the slot index + * @return the slot where the item should be placed when double-clicking + */ + int getStartSlotForDoubleClick(int index, Player player) { + return 0; + } + // Microtus - fix shift click for inventory + /** * Inserts safely an item into the inventory. *

diff --git a/src/main/java/net/minestom/server/inventory/Inventory.java b/src/main/java/net/minestom/server/inventory/Inventory.java index ace374daffc..d9f88e80db4 100644 --- a/src/main/java/net/minestom/server/inventory/Inventory.java +++ b/src/main/java/net/minestom/server/inventory/Inventory.java @@ -246,7 +246,7 @@ public boolean shiftClick(@NotNull Player player, int slot, int button) { // Mic final InventoryClickResult clickResult = clickProcessor.shiftClick( isInWindow ? this : playerInventory, isInWindow ? playerInventory : this, - 0, isInWindow ? playerInventory.getInnerSize() : getInnerSize(), 1, + getStartSlotForShiftClick(slot, player), isInWindow ? playerInventory.getInnerSize() : getInnerSize(), 1, // Microtus - fix shift click for inventory player, clickSlot, clicked, cursor, button); // Microtus if (clickResult.isCancel()) { updateAll(player); @@ -262,6 +262,29 @@ public boolean shiftClick(@NotNull Player player, int slot, int button) { // Mic return true; } + // Microtus - fix shift click for inventory + @Override + int getStartSlotForShiftClick(int index, Player player) { + if (index == 0) { + return 9; + } + if (index >= 1 && index < 5) { + return 9; + } + // TODO: Add armor slot support + if (index >= 5 && index < 9) { + return 9; + } + if (index >= 9 && index < 36) { + return 36; + } + if (index >= 36 && index < 45) { + return 9; + } + return 9; + } + // Microtus - fix shift click for inventory + @Override public boolean changeHeld(@NotNull Player player, int slot, int key) { final int convertedKey = key == 40 ? PlayerInventoryUtils.OFFHAND_SLOT : key; @@ -352,7 +375,7 @@ public boolean doubleClick(@NotNull Player player, int slot) { ItemStack.AIR; final ItemStack cursor = playerInventory.getCursorItem(); final InventoryClickResult clickResult = clickProcessor.doubleClick(isInWindow ? this : playerInventory, - this, player, clickSlot, clicked, cursor); + this, player, clickSlot, clicked, cursor, getStartSlotForDoubleClick(slot, player)); // Microtus - fix shift click for inventory if (clickResult.isCancel()) { updateAll(player); return false; diff --git a/src/main/java/net/minestom/server/inventory/PlayerInventory.java b/src/main/java/net/minestom/server/inventory/PlayerInventory.java index 0515694bc51..60c2cd76eb7 100644 --- a/src/main/java/net/minestom/server/inventory/PlayerInventory.java +++ b/src/main/java/net/minestom/server/inventory/PlayerInventory.java @@ -273,7 +273,8 @@ public boolean doubleClick(@NotNull Player player, int slot) { final int convertedSlot = convertPlayerInventorySlot(slot, OFFSET); final ItemStack cursor = getCursorItem(); final ItemStack clicked = getItemStack(convertedSlot); - final InventoryClickResult clickResult = clickProcessor.doubleClick(this, this, player, convertedSlot, clicked, cursor); + final InventoryClickResult clickResult = clickProcessor.doubleClick(this, this, player, convertedSlot, clicked, cursor, getStartSlotForDoubleClick(slot, player)); + // Microtus - fix shift click for inventory if (clickResult.isCancel()) { update(); return false; 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 72f17ffaa32..ccd7bebc5d0 100644 --- a/src/main/java/net/minestom/server/inventory/click/InventoryClickProcessor.java +++ b/src/main/java/net/minestom/server/inventory/click/InventoryClickProcessor.java @@ -289,7 +289,7 @@ public final class InventoryClickProcessor { } public @NotNull InventoryClickResult doubleClick(@NotNull AbstractInventory clickedInventory, @NotNull AbstractInventory inventory, @NotNull Player player, int slot, - @NotNull ItemStack clicked, @NotNull ItemStack cursor) { + @NotNull ItemStack clicked, @NotNull ItemStack cursor, int startSlot) { // Microtus - fix shift click for inventory InventoryClickResult clickResult = startCondition(player, clickedInventory, slot, ClickType.START_DOUBLE_CLICK, clicked, cursor); if (clickResult.isCancel()) return clickResult; if (cursor.isAir()) return clickResult.cancelled(); @@ -307,7 +307,7 @@ public final class InventoryClickProcessor { return false; final InventoryClickResult result = startCondition(player, inv, index, ClickType.DOUBLE_CLICK, itemStack, cursor); return !result.isCancel(); - }); + }, startSlot, inventory.getInnerSize(), 1); // Microtus - fix shift click for inventory final ItemStack itemResult = pair.left(); var itemChangesMap = pair.right(); itemChangesMap.forEach((Integer s, ItemStack itemStack) -> { From d99be421381a75ccaada181c6ef51fb1be7d57c8 Mon Sep 17 00:00:00 2001 From: Phillipp Glanz Date: Sun, 3 Nov 2024 16:08:52 +0100 Subject: [PATCH 2/2] Switch to converted slot id --- src/main/java/net/minestom/server/inventory/Inventory.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/net/minestom/server/inventory/Inventory.java b/src/main/java/net/minestom/server/inventory/Inventory.java index d9f88e80db4..9a06a388a75 100644 --- a/src/main/java/net/minestom/server/inventory/Inventory.java +++ b/src/main/java/net/minestom/server/inventory/Inventory.java @@ -246,7 +246,7 @@ public boolean shiftClick(@NotNull Player player, int slot, int button) { // Mic final InventoryClickResult clickResult = clickProcessor.shiftClick( isInWindow ? this : playerInventory, isInWindow ? playerInventory : this, - getStartSlotForShiftClick(slot, player), isInWindow ? playerInventory.getInnerSize() : getInnerSize(), 1, // Microtus - fix shift click for inventory + getStartSlotForShiftClick(clickSlot, player), isInWindow ? playerInventory.getInnerSize() : getInnerSize(), 1, // Microtus - fix shift click for inventory player, clickSlot, clicked, cursor, button); // Microtus if (clickResult.isCancel()) { updateAll(player);