Resolving inventory- events and condition issues

This commit is contained in:
Шандуренко Константин Владимирович 2021-09-10 18:38:27 +03:00
parent 6355f06d53
commit e990283c40
2 changed files with 18 additions and 26 deletions

View File

@ -317,7 +317,7 @@ public class Inventory extends AbstractInventory implements Viewable {
isInWindow ? this : playerInventory,
isInWindow ? playerInventory : this,
0, isInWindow ? playerInventory.getInnerSize() : getInnerSize(), 1,
player, slot, clicked, cursor);
player, clickSlot, clicked, cursor);
if (clickResult.isCancel()) {
updateAll(player);
return false;
@ -421,7 +421,7 @@ public class Inventory extends AbstractInventory implements Viewable {
ItemStack.AIR;
final ItemStack cursor = getCursorItem(player);
final InventoryClickResult clickResult = clickProcessor.doubleClick(isInWindow ? this : playerInventory,
this, player, slot, clicked, cursor);
this, player, clickSlot, clicked, cursor);
if (clickResult.isCancel()) {
updateAll(player);
return false;

View File

@ -8,7 +8,6 @@ import net.minestom.server.event.inventory.PlayerInventoryItemChangeEvent;
import net.minestom.server.event.item.EntityEquipEvent;
import net.minestom.server.inventory.click.ClickType;
import net.minestom.server.inventory.click.InventoryClickResult;
import net.minestom.server.inventory.condition.InventoryCondition;
import net.minestom.server.item.ItemStack;
import net.minestom.server.network.packet.server.play.SetSlotPacket;
import net.minestom.server.network.packet.server.play.WindowItemsPacket;
@ -31,17 +30,6 @@ public class PlayerInventory extends AbstractInventory implements EquipmentHandl
this.player = player;
}
@Override
public void addInventoryCondition(@NotNull InventoryCondition inventoryCondition) {
// fix packet slot to inventory slot conversion
InventoryCondition condition = (p, slot, clickType, inventoryConditionResult) -> {
final int convertedSlot = convertPlayerInventorySlot(slot, OFFSET);
inventoryCondition.accept(p, convertedSlot, clickType, inventoryConditionResult);
};
super.addInventoryCondition(condition);
}
@Override
public synchronized void clear() {
super.clear();
@ -250,18 +238,19 @@ public class PlayerInventory extends AbstractInventory implements EquipmentHandl
@Override
public boolean drop(@NotNull Player player, boolean all, int slot, int button) {
final int convertedSlot = convertPlayerInventorySlot(slot, OFFSET);
final ItemStack cursor = getCursorItem();
final boolean outsideDrop = slot == -999;
final ItemStack clicked = outsideDrop ? ItemStack.AIR : getItemStackFromPacketSlot(slot);
final ItemStack clicked = outsideDrop ? ItemStack.AIR : getItemStack(convertedSlot);
final InventoryClickResult clickResult = clickProcessor.drop(player, this,
all, slot, button, clicked, cursor);
all, convertedSlot, button, clicked, cursor);
if (clickResult.isCancel()) {
update();
return false;
}
final ItemStack resultClicked = clickResult.getClicked();
if (resultClicked != null && !outsideDrop) {
setItemStackFromPacketSlot(slot, resultClicked);
setItemStack(convertedSlot, resultClicked);
}
setCursorItem(clickResult.getCursor());
return true;
@ -269,20 +258,21 @@ public class PlayerInventory extends AbstractInventory implements EquipmentHandl
@Override
public boolean shiftClick(@NotNull Player player, int slot) {
final int convertedSlot = convertPlayerInventorySlot(slot, OFFSET);
final ItemStack cursor = getCursorItem();
final ItemStack clicked = getItemStackFromPacketSlot(slot);
final ItemStack clicked = getItemStack(convertedSlot);
final boolean hotBarClick = convertSlot(slot, OFFSET) < 9;
final int start = hotBarClick ? 9 : 0;
final int end = hotBarClick ? getSize() - 9 : 8;
final InventoryClickResult clickResult = clickProcessor.shiftClick(
this, this,
start, end, 1,
player, slot, clicked, cursor);
player, convertedSlot, clicked, cursor);
if (clickResult.isCancel()) {
update();
return false;
}
setItemStackFromPacketSlot(slot, clickResult.getClicked());
setItemStack(convertedSlot, clickResult.getClicked());
setCursorItem(clickResult.getCursor());
update(); // FIXME: currently not properly client-predicted
return true;
@ -292,16 +282,17 @@ public class PlayerInventory extends AbstractInventory implements EquipmentHandl
public boolean changeHeld(@NotNull Player player, int slot, int key) {
final ItemStack cursorItem = getCursorItem();
if (!cursorItem.isAir()) return false;
final int convertedSlot = convertPlayerInventorySlot(slot, OFFSET);
final ItemStack heldItem = getItemStack(key);
final ItemStack clicked = getItemStackFromPacketSlot(slot);
final InventoryClickResult clickResult = clickProcessor.changeHeld(player, this, slot, key, clicked, heldItem);
final ItemStack clicked = getItemStack(convertedSlot);
final InventoryClickResult clickResult = clickProcessor.changeHeld(player, this, convertedSlot, key, clicked, heldItem);
if (clickResult.isCancel()) {
update();
return false;
}
setItemStackFromPacketSlot(slot, clickResult.getClicked());
setItemStack(convertedSlot, clickResult.getClicked());
setItemStack(key, clickResult.getCursor());
callClickEvent(player, null, slot, ClickType.CHANGE_HELD, clicked, cursorItem);
callClickEvent(player, null, convertedSlot, ClickType.CHANGE_HELD, clicked, cursorItem);
return true;
}
@ -322,9 +313,10 @@ public class PlayerInventory extends AbstractInventory implements EquipmentHandl
@Override
public boolean doubleClick(@NotNull Player player, int slot) {
final int convertedSlot = convertPlayerInventorySlot(slot, OFFSET);
final ItemStack cursor = getCursorItem();
final ItemStack clicked = getItemStackFromPacketSlot(slot);
final InventoryClickResult clickResult = clickProcessor.doubleClick(this, this, player, slot, clicked, cursor);
final ItemStack clicked = getItemStack(convertedSlot);
final InventoryClickResult clickResult = clickProcessor.doubleClick(this, this, player, convertedSlot, clicked, cursor);
if (clickResult.isCancel()) {
update();
return false;