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