Skip to content

Implement a way to override the prediction of start slot for inventory at shiftclick #89

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/main/java/net/minestom/server/inventory/AbstractInventory.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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.
* <p>
* 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.
* <p>
* 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.
* <p>
Expand Down
27 changes: 25 additions & 2 deletions src/main/java/net/minestom/server/inventory/Inventory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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(clickSlot, player), isInWindow ? playerInventory.getInnerSize() : getInnerSize(), 1, // Microtus - fix shift click for inventory
player, clickSlot, clicked, cursor, button); // Microtus
if (clickResult.isCancel()) {
updateAll(player);
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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) -> {
Expand Down
Loading