mirror of
https://github.com/Minestom/Minestom.git
synced 2025-03-02 11:21:15 +01:00
Fix inventory click on size non-divisible by 9
This commit is contained in:
parent
023431e32a
commit
d83abcef85
@ -395,8 +395,8 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
|
||||
final PlayerInventory playerInventory = player.getInventory();
|
||||
final ItemStack cursor = getCursorItem(player);
|
||||
final boolean isInWindow = isClickInWindow(slot);
|
||||
final ItemStack clicked = isInWindow ? getItemStack(slot) : playerInventory.getItemStack(slot, offset);
|
||||
|
||||
final int clickSlot = isInWindow ? slot : PlayerInventoryUtils.convertSlot(slot, offset);
|
||||
final ItemStack clicked = isInWindow ? getItemStack(slot) : playerInventory.getItemStack(clickSlot);
|
||||
|
||||
final InventoryClickResult clickResult = clickProcessor.leftClick(this, player, slot, clicked, cursor);
|
||||
|
||||
@ -407,7 +407,7 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
|
||||
if (isInWindow) {
|
||||
setItemStack(slot, clickResult.getClicked());
|
||||
} else {
|
||||
playerInventory.setItemStack(slot, offset, clickResult.getClicked());
|
||||
playerInventory.setItemStack(clickSlot, clickResult.getClicked());
|
||||
}
|
||||
setCursorPlayerItem(player, clickResult.getCursor());
|
||||
|
||||
@ -422,7 +422,8 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
|
||||
final PlayerInventory playerInventory = player.getInventory();
|
||||
final ItemStack cursor = getCursorItem(player);
|
||||
final boolean isInWindow = isClickInWindow(slot);
|
||||
final ItemStack clicked = isInWindow ? getItemStack(slot) : playerInventory.getItemStack(slot, offset);
|
||||
final int clickSlot = isInWindow ? slot : PlayerInventoryUtils.convertSlot(slot, offset);
|
||||
final ItemStack clicked = isInWindow ? getItemStack(slot) : playerInventory.getItemStack(clickSlot);
|
||||
|
||||
final InventoryClickResult clickResult = clickProcessor.rightClick(this, player, slot, clicked, cursor);
|
||||
|
||||
@ -433,7 +434,7 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
|
||||
if (isInWindow) {
|
||||
setItemStack(slot, clickResult.getClicked());
|
||||
} else {
|
||||
playerInventory.setItemStack(slot, offset, clickResult.getClicked());
|
||||
playerInventory.setItemStack(clickSlot, clickResult.getClicked());
|
||||
}
|
||||
setCursorPlayerItem(player, clickResult.getCursor());
|
||||
|
||||
@ -447,7 +448,8 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
|
||||
public boolean shiftClick(@NotNull Player player, int slot) {
|
||||
final PlayerInventory playerInventory = player.getInventory();
|
||||
final boolean isInWindow = isClickInWindow(slot);
|
||||
final ItemStack clicked = isInWindow ? getItemStack(slot) : playerInventory.getItemStack(slot, offset);
|
||||
final int clickSlot = isInWindow ? slot : PlayerInventoryUtils.convertSlot(slot, offset);
|
||||
final ItemStack clicked = isInWindow ? getItemStack(slot) : playerInventory.getItemStack(clickSlot);
|
||||
final ItemStack cursor = getCursorItem(player); // Isn't used in the algorithm
|
||||
|
||||
|
||||
@ -458,12 +460,14 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
|
||||
// Player inventory loop
|
||||
new InventoryClickLoopHandler(0, PlayerInventory.INVENTORY_SIZE, 1,
|
||||
PlayerInventoryUtils::convertToPacketSlot,
|
||||
index -> isClickInWindow(index) ? getItemStack(index) : playerInventory.getItemStack(index, offset),
|
||||
index -> isClickInWindow(index) ?
|
||||
getItemStack(index) :
|
||||
playerInventory.getItemStack(PlayerInventoryUtils.convertSlot(index, offset)),
|
||||
(index, itemStack) -> {
|
||||
if (isClickInWindow(index)) {
|
||||
setItemStack(index, itemStack);
|
||||
} else {
|
||||
playerInventory.setItemStack(index, offset, itemStack);
|
||||
playerInventory.setItemStack(PlayerInventoryUtils.convertSlot(index, offset), itemStack);
|
||||
}
|
||||
}));
|
||||
} else {
|
||||
@ -471,12 +475,14 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
|
||||
// Window loop
|
||||
new InventoryClickLoopHandler(0, getSize(), 1,
|
||||
i -> i,
|
||||
index -> isClickInWindow(index) ? getItemStack(index) : playerInventory.getItemStack(index, offset),
|
||||
index -> isClickInWindow(index) ?
|
||||
getItemStack(index) :
|
||||
playerInventory.getItemStack(PlayerInventoryUtils.convertSlot(index, offset)),
|
||||
(index, itemStack) -> {
|
||||
if (isClickInWindow(index)) {
|
||||
setItemStack(index, itemStack);
|
||||
} else {
|
||||
playerInventory.setItemStack(index, offset, itemStack);
|
||||
playerInventory.setItemStack(PlayerInventoryUtils.convertSlot(index, offset), itemStack);
|
||||
}
|
||||
}));
|
||||
}
|
||||
@ -499,7 +505,8 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
|
||||
public boolean changeHeld(@NotNull Player player, int slot, int key) {
|
||||
final PlayerInventory playerInventory = player.getInventory();
|
||||
final boolean isInWindow = isClickInWindow(slot);
|
||||
final ItemStack clicked = isInWindow ? getItemStack(slot) : playerInventory.getItemStack(slot, offset);
|
||||
final int clickSlot = isInWindow ? slot : PlayerInventoryUtils.convertSlot(slot, offset);
|
||||
final ItemStack clicked = isInWindow ? getItemStack(slot) : playerInventory.getItemStack(clickSlot);
|
||||
final ItemStack heldItem = playerInventory.getItemStack(key);
|
||||
|
||||
final InventoryClickResult clickResult = clickProcessor.changeHeld(this, player, slot, key, clicked, heldItem);
|
||||
@ -511,7 +518,7 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
|
||||
if (isInWindow) {
|
||||
setItemStack(slot, clickResult.getClicked());
|
||||
} else {
|
||||
playerInventory.setItemStack(slot, offset, clickResult.getClicked());
|
||||
playerInventory.setItemStack(clickSlot, clickResult.getClicked());
|
||||
}
|
||||
playerInventory.setItemStack(key, clickResult.getCursor());
|
||||
|
||||
@ -535,8 +542,9 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
|
||||
final PlayerInventory playerInventory = player.getInventory();
|
||||
final boolean isInWindow = isClickInWindow(slot);
|
||||
final boolean outsideDrop = slot == -999;
|
||||
final int clickSlot = isInWindow ? slot : PlayerInventoryUtils.convertSlot(slot, offset);
|
||||
final ItemStack clicked = outsideDrop ?
|
||||
ItemStack.getAirItem() : (isInWindow ? getItemStack(slot) : playerInventory.getItemStack(slot, offset));
|
||||
ItemStack.getAirItem() : (isInWindow ? getItemStack(slot) : playerInventory.getItemStack(clickSlot));
|
||||
final ItemStack cursor = getCursorItem(player);
|
||||
|
||||
final InventoryClickResult clickResult = clickProcessor.drop(this, player,
|
||||
@ -551,7 +559,7 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
|
||||
if (isInWindow) {
|
||||
setItemStack(slot, resultClicked);
|
||||
} else {
|
||||
playerInventory.setItemStack(slot, offset, resultClicked);
|
||||
playerInventory.setItemStack(clickSlot, resultClicked);
|
||||
}
|
||||
}
|
||||
|
||||
@ -564,8 +572,9 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
|
||||
public boolean dragging(@NotNull Player player, int slot, int button) {
|
||||
final PlayerInventory playerInventory = player.getInventory();
|
||||
final boolean isInWindow = isClickInWindow(slot);
|
||||
final int clickSlot = isInWindow ? slot : PlayerInventoryUtils.convertSlot(slot, offset);
|
||||
final ItemStack clicked = slot != -999 ?
|
||||
(isInWindow ? getItemStack(slot) : playerInventory.getItemStack(slot, offset)) :
|
||||
(isInWindow ? getItemStack(slot) : playerInventory.getItemStack(clickSlot)) :
|
||||
ItemStack.getAirItem();
|
||||
final ItemStack cursor = getCursorItem(player);
|
||||
|
||||
@ -573,13 +582,14 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
|
||||
slot, button,
|
||||
clicked, cursor,
|
||||
|
||||
s -> isClickInWindow(s) ? getItemStack(s) : playerInventory.getItemStack(s, offset),
|
||||
s -> isClickInWindow(s) ? getItemStack(s) :
|
||||
playerInventory.getItemStack(PlayerInventoryUtils.convertSlot(s, offset)),
|
||||
|
||||
(s, item) -> {
|
||||
if (isClickInWindow(s)) {
|
||||
setItemStack(s, item);
|
||||
} else {
|
||||
playerInventory.setItemStack(s, offset, item);
|
||||
playerInventory.setItemStack(PlayerInventoryUtils.convertSlot(s, offset), item);
|
||||
}
|
||||
});
|
||||
|
||||
@ -601,7 +611,6 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
|
||||
final PlayerInventory playerInventory = player.getInventory();
|
||||
final ItemStack cursor = getCursorItem(player);
|
||||
|
||||
|
||||
final InventoryClickResult clickResult = clickProcessor.doubleClick(this, player, slot, cursor,
|
||||
// Start by looping through the opened inventory
|
||||
new InventoryClickLoopHandler(0, getSize(), 1,
|
||||
@ -609,7 +618,7 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
|
||||
this::getItemStack,
|
||||
this::setItemStack),
|
||||
// Looping through player inventory
|
||||
new InventoryClickLoopHandler(0, PlayerInventory.INVENTORY_SIZE - 9, 1,
|
||||
new InventoryClickLoopHandler(0, PlayerInventory.INVENTORY_SIZE, 1,
|
||||
PlayerInventoryUtils::convertToPacketSlot,
|
||||
index -> playerInventory.getItemStack(index, PlayerInventoryUtils.OFFSET),
|
||||
(index, itemStack) -> playerInventory.setItemStack(index, PlayerInventoryUtils.OFFSET, itemStack)));
|
||||
|
@ -43,7 +43,7 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
|
||||
|
||||
private Data data;
|
||||
|
||||
public PlayerInventory(Player player) {
|
||||
public PlayerInventory(@NotNull Player player) {
|
||||
this.player = player;
|
||||
|
||||
ArrayUtils.fill(items, ItemStack::getAirItem);
|
||||
@ -70,8 +70,8 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
|
||||
@Override
|
||||
public void addInventoryCondition(@NotNull InventoryCondition inventoryCondition) {
|
||||
InventoryCondition condition = (p, slot, clickType, inventoryConditionResult) -> {
|
||||
slot = convertSlot(slot, OFFSET);
|
||||
inventoryCondition.accept(p, slot, clickType, inventoryConditionResult);
|
||||
final int convertedSlot = convertPlayerInventorySlot(slot, OFFSET);
|
||||
inventoryCondition.accept(p, convertedSlot, clickType, inventoryConditionResult);
|
||||
};
|
||||
|
||||
this.inventoryConditions.add(condition);
|
||||
@ -320,8 +320,8 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
|
||||
* @param itemStack the item stack to set
|
||||
*/
|
||||
protected void setItemStack(int slot, int offset, ItemStack itemStack) {
|
||||
slot = convertSlot(slot, offset);
|
||||
setItemStack(slot, itemStack);
|
||||
final int convertedSlot = convertPlayerInventorySlot(slot, offset);
|
||||
setItemStack(convertedSlot, itemStack);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -332,8 +332,8 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
|
||||
* @return the item in the specified slot
|
||||
*/
|
||||
protected ItemStack getItemStack(int slot, int offset) {
|
||||
slot = convertSlot(slot, offset);
|
||||
return this.items[slot];
|
||||
final int convertedSlot = convertPlayerInventorySlot(slot, offset);
|
||||
return this.items[convertedSlot];
|
||||
}
|
||||
|
||||
/**
|
||||
@ -373,7 +373,7 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
|
||||
@Override
|
||||
public boolean leftClick(@NotNull Player player, int slot) {
|
||||
final ItemStack cursor = getCursorItem();
|
||||
final ItemStack clicked = getItemStack(convertSlot(slot, OFFSET));
|
||||
final ItemStack clicked = getItemStack(convertPlayerInventorySlot(slot, OFFSET));
|
||||
|
||||
final InventoryClickResult clickResult = clickProcessor.leftClick(null, player, slot, clicked, cursor);
|
||||
|
||||
@ -446,7 +446,7 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
|
||||
if (hotBarClick) {
|
||||
return i < 9 ? i + 9 : i - 9;
|
||||
} else {
|
||||
return convertSlot(i, OFFSET);
|
||||
return convertPlayerInventorySlot(i, OFFSET);
|
||||
}
|
||||
},
|
||||
index -> getItemStack(index, OFFSET),
|
||||
|
@ -18,7 +18,7 @@ public class CreativeInventoryActionListener {
|
||||
|
||||
if (slot != -1) {
|
||||
// Set item
|
||||
slot = (short) PlayerInventoryUtils.convertSlot(slot, PlayerInventoryUtils.OFFSET);
|
||||
slot = (short) PlayerInventoryUtils.convertPlayerInventorySlot(slot, PlayerInventoryUtils.OFFSET);
|
||||
PlayerInventory inventory = player.getInventory();
|
||||
inventory.setItemStack(slot, item);
|
||||
} else {
|
||||
|
@ -28,7 +28,7 @@ public final class PlayerInventoryUtils {
|
||||
* the offset for the player inventory is {@link #OFFSET}
|
||||
* @return a packet which can be use internally with Minestom
|
||||
*/
|
||||
public static int convertSlot(int slot, int offset) {
|
||||
public static int convertPlayerInventorySlot(int slot, int offset) {
|
||||
switch (slot) {
|
||||
case 0:
|
||||
return CRAFT_RESULT;
|
||||
@ -49,6 +49,11 @@ public final class PlayerInventoryUtils {
|
||||
case 8:
|
||||
return BOOTS_SLOT;
|
||||
}
|
||||
|
||||
return convertSlot(slot, offset);
|
||||
}
|
||||
|
||||
public static int convertSlot(int slot, int offset) {
|
||||
final int rowSize = 9;
|
||||
slot -= offset;
|
||||
if (slot >= rowSize * 3 && slot < rowSize * 4) {
|
||||
@ -59,6 +64,7 @@ public final class PlayerInventoryUtils {
|
||||
return slot;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Used to convert internal slot to one used in packets
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user