diff --git a/src/main/java/fr/themode/demo/PlayerInit.java b/src/main/java/fr/themode/demo/PlayerInit.java index 1f58b4af9..b4fb96f54 100644 --- a/src/main/java/fr/themode/demo/PlayerInit.java +++ b/src/main/java/fr/themode/demo/PlayerInit.java @@ -155,16 +155,17 @@ public class PlayerInit { }); player.addEventCallback(PlayerSpawnEvent.class, event -> { - player.setGameMode(GameMode.SURVIVAL); + player.setGameMode(GameMode.CREATIVE); player.teleport(new Position(0, 75, 0)); ItemStack item = new ItemStack(Material.STONE, (byte) 43); item.setDisplayName("Item name"); item.getLore().add("a lore line"); + //item.setEnchantment(Enchantment.SHARPNESS, 2); player.getInventory().addItemStack(item); Inventory inventory = new Inventory(InventoryType.CHEST_1_ROW, "Test inventory"); - inventory.setInventoryCondition((p, slot, clickType, inventoryConditionResult) -> { + inventory.addInventoryCondition((p, slot, clickType, inventoryConditionResult) -> { player.sendMessage("click type: " + clickType); inventoryConditionResult.setCancel(false); }); diff --git a/src/main/java/net/minestom/server/inventory/Inventory.java b/src/main/java/net/minestom/server/inventory/Inventory.java index 2329cd58d..f610dc467 100644 --- a/src/main/java/net/minestom/server/inventory/Inventory.java +++ b/src/main/java/net/minestom/server/inventory/Inventory.java @@ -13,8 +13,10 @@ import net.minestom.server.network.packet.server.play.WindowPropertyPacket; import java.util.Arrays; import java.util.Collections; +import java.util.List; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArraySet; public class Inventory implements InventoryModifier, InventoryClickHandler, Viewable { @@ -33,7 +35,7 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View private Set viewers = new CopyOnWriteArraySet<>(); private ConcurrentHashMap cursorPlayersItem = new ConcurrentHashMap<>(); - private InventoryCondition inventoryCondition; + private List inventoryConditions = new CopyOnWriteArrayList<>(); private InventoryClickProcessor clickProcessor = new InventoryClickProcessor(); public Inventory(InventoryType inventoryType, String title) { @@ -95,13 +97,13 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View } @Override - public InventoryCondition getInventoryCondition() { - return inventoryCondition; + public List getInventoryConditions() { + return inventoryConditions; } @Override - public void setInventoryCondition(InventoryCondition inventoryCondition) { - this.inventoryCondition = inventoryCondition; + public void addInventoryCondition(InventoryCondition inventoryCondition) { + this.inventoryConditions.add(inventoryCondition); } public void update() { @@ -174,7 +176,7 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View ItemStack clicked = isInWindow ? getItemStack(slot) : playerInventory.getItemStack(slot, offset); - InventoryClickResult clickResult = clickProcessor.leftClick(getInventoryCondition(), player, slot, clicked, cursor); + InventoryClickResult clickResult = clickProcessor.leftClick(getInventoryConditions(), player, slot, clicked, cursor); if (clickResult.doRefresh()) player.getPlayerConnection().sendPacket(getWindowItemsPacket()); @@ -195,7 +197,7 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View boolean isInWindow = isClickInWindow(slot); ItemStack clicked = isInWindow ? getItemStack(slot) : playerInventory.getItemStack(slot, offset); - InventoryClickResult clickResult = clickProcessor.rightClick(getInventoryCondition(), player, slot, clicked, cursor); + InventoryClickResult clickResult = clickProcessor.rightClick(getInventoryConditions(), player, slot, clicked, cursor); if (clickResult.doRefresh()) player.getPlayerConnection().sendPacket(getWindowItemsPacket()); @@ -220,7 +222,7 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View InventoryClickResult clickResult; if (isInWindow) { - clickResult = clickProcessor.shiftClick(getInventoryCondition(), player, slot, clicked, cursor, + clickResult = clickProcessor.shiftClick(getInventoryConditions(), player, slot, clicked, cursor, // Player inventory loop new InventoryClickLoopHandler(0, PlayerInventory.INVENTORY_SIZE, 1, i -> playerInventory.convertToPacketSlot(i), @@ -233,7 +235,7 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View } })); } else { - clickResult = clickProcessor.shiftClick(getInventoryCondition(), player, slot, clicked, cursor, + clickResult = clickProcessor.shiftClick(getInventoryConditions(), player, slot, clicked, cursor, // Window loop new InventoryClickLoopHandler(0, itemStacks.length, 1, i -> i, @@ -265,7 +267,7 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View ItemStack clicked = isInWindow ? getItemStack(slot) : playerInventory.getItemStack(slot, offset); ItemStack heldItem = playerInventory.getItemStack(key); - InventoryClickResult clickResult = clickProcessor.changeHeld(getInventoryCondition(), player, slot, clicked, heldItem); + InventoryClickResult clickResult = clickProcessor.changeHeld(getInventoryConditions(), player, slot, clicked, heldItem); if (clickResult.doRefresh()) player.getPlayerConnection().sendPacket(getWindowItemsPacket()); @@ -291,7 +293,7 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View null : (isInWindow ? getItemStack(slot) : playerInventory.getItemStack(slot, offset)); ItemStack cursor = getCursorItem(player); - InventoryClickResult clickResult = clickProcessor.drop(getInventoryCondition(), player, + InventoryClickResult clickResult = clickProcessor.drop(getInventoryConditions(), player, mode, slot, button, clicked, cursor); if (clickResult.doRefresh()) @@ -319,7 +321,7 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View if (slot != -999) clicked = isInWindow ? getItemStack(slot) : playerInventory.getItemStack(slot, offset); - InventoryClickResult clickResult = clickProcessor.dragging(getInventoryCondition(), player, + InventoryClickResult clickResult = clickProcessor.dragging(getInventoryConditions(), player, slot, button, clicked, cursor, @@ -349,7 +351,7 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View ItemStack cursor = getCursorItem(player); - InventoryClickResult clickResult = clickProcessor.doubleClick(getInventoryCondition(), player, slot, cursor, + InventoryClickResult clickResult = clickProcessor.doubleClick(getInventoryConditions(), player, slot, cursor, // Start by looping through the opened inventory new InventoryClickLoopHandler(0, itemStacks.length, 1, i -> i, diff --git a/src/main/java/net/minestom/server/inventory/InventoryModifier.java b/src/main/java/net/minestom/server/inventory/InventoryModifier.java index 7856b6a33..0fd9031d6 100644 --- a/src/main/java/net/minestom/server/inventory/InventoryModifier.java +++ b/src/main/java/net/minestom/server/inventory/InventoryModifier.java @@ -3,6 +3,8 @@ package net.minestom.server.inventory; import net.minestom.server.inventory.condition.InventoryCondition; import net.minestom.server.item.ItemStack; +import java.util.List; + public interface InventoryModifier { void setItemStack(int slot, ItemStack itemStack); @@ -13,7 +15,7 @@ public interface InventoryModifier { ItemStack[] getItemStacks(); - InventoryCondition getInventoryCondition(); + List getInventoryConditions(); - void setInventoryCondition(InventoryCondition inventoryCondition); + void addInventoryCondition(InventoryCondition inventoryCondition); } diff --git a/src/main/java/net/minestom/server/inventory/PlayerInventory.java b/src/main/java/net/minestom/server/inventory/PlayerInventory.java index 7c60db4c6..9a32fa05f 100644 --- a/src/main/java/net/minestom/server/inventory/PlayerInventory.java +++ b/src/main/java/net/minestom/server/inventory/PlayerInventory.java @@ -14,6 +14,8 @@ import net.minestom.server.network.packet.server.play.WindowItemsPacket; import net.minestom.server.network.player.PlayerConnection; import java.util.Arrays; +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; import static net.minestom.server.utils.inventory.PlayerInventoryUtils.*; @@ -25,7 +27,7 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler private ItemStack[] items = new ItemStack[INVENTORY_SIZE]; private ItemStack cursorItem = ItemStack.getAirItem(); - private InventoryCondition inventoryCondition; + private List inventoryConditions = new CopyOnWriteArrayList<>(); private InventoryClickProcessor clickProcessor = new InventoryClickProcessor(); public PlayerInventory(Player player) { @@ -47,13 +49,13 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler } @Override - public InventoryCondition getInventoryCondition() { - return inventoryCondition; + public List getInventoryConditions() { + return inventoryConditions; } @Override - public void setInventoryCondition(InventoryCondition inventoryCondition) { - this.inventoryCondition = inventoryCondition; + public void addInventoryCondition(InventoryCondition inventoryCondition) { + this.inventoryConditions.add(inventoryCondition); } @Override @@ -278,7 +280,7 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler ItemStack cursor = getCursorItem(); ItemStack clicked = getItemStack(convertSlot(slot, OFFSET)); - InventoryClickResult clickResult = clickProcessor.leftClick(getInventoryCondition(), player, slot, clicked, cursor); + InventoryClickResult clickResult = clickProcessor.leftClick(getInventoryConditions(), player, slot, clicked, cursor); if (clickResult.doRefresh()) sendSlotRefresh((short) slot, clicked); @@ -292,7 +294,7 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler ItemStack cursor = getCursorItem(); ItemStack clicked = getItemStack(slot, OFFSET); - InventoryClickResult clickResult = clickProcessor.rightClick(getInventoryCondition(), player, slot, clicked, cursor); + InventoryClickResult clickResult = clickProcessor.rightClick(getInventoryConditions(), player, slot, clicked, cursor); if (clickResult.doRefresh()) sendSlotRefresh((short) slot, clicked); @@ -311,7 +313,7 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler ItemStack cursor = getCursorItem(); ItemStack clicked = slot == -999 ? null : getItemStack(slot, OFFSET); - InventoryClickResult clickResult = clickProcessor.drop(getInventoryCondition(), player, + InventoryClickResult clickResult = clickProcessor.drop(getInventoryConditions(), player, mode, slot, button, clicked, cursor); if (clickResult.doRefresh()) @@ -329,7 +331,7 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler ItemStack clicked = getItemStack(slot, OFFSET); boolean hotbarClick = convertToPacketSlot(slot) < 9; - InventoryClickResult clickResult = clickProcessor.shiftClick(getInventoryCondition(), player, slot, clicked, cursor, + InventoryClickResult clickResult = clickProcessor.shiftClick(getInventoryConditions(), player, slot, clicked, cursor, new InventoryClickLoopHandler(0, items.length, 1, i -> { if (hotbarClick) { @@ -358,7 +360,7 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler ItemStack heldItem = getItemStack(key); ItemStack clicked = getItemStack(slot, OFFSET); - InventoryClickResult clickResult = clickProcessor.changeHeld(getInventoryCondition(), player, slot, clicked, heldItem); + InventoryClickResult clickResult = clickProcessor.changeHeld(getInventoryConditions(), player, slot, clicked, heldItem); if (clickResult.doRefresh()) sendSlotRefresh((short) slot, clicked); @@ -374,7 +376,7 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler if (slot != -999) clicked = getItemStack(slot, OFFSET); - InventoryClickResult clickResult = clickProcessor.dragging(getInventoryCondition(), player, + InventoryClickResult clickResult = clickProcessor.dragging(getInventoryConditions(), player, slot, button, clicked, cursor, s -> getItemStack(s, OFFSET), (s, item) -> setItemStack(s, OFFSET, item)); @@ -392,7 +394,7 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler public void doubleClick(Player player, int slot) { ItemStack cursor = getCursorItem(); - InventoryClickResult clickResult = clickProcessor.doubleClick(getInventoryCondition(), player, slot, cursor, + InventoryClickResult clickResult = clickProcessor.doubleClick(getInventoryConditions(), player, slot, cursor, new InventoryClickLoopHandler(0, items.length, 1, i -> i < 9 ? i + 9 : i - 9, index -> items[index], 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 afb7e9be7..4cab967ad 100644 --- a/src/main/java/net/minestom/server/inventory/click/InventoryClickProcessor.java +++ b/src/main/java/net/minestom/server/inventory/click/InventoryClickProcessor.java @@ -6,10 +6,7 @@ import net.minestom.server.inventory.condition.InventoryConditionResult; import net.minestom.server.item.ItemStack; import net.minestom.server.item.StackingRule; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; +import java.util.*; import java.util.function.BiConsumer; import java.util.function.Function; @@ -19,8 +16,8 @@ public class InventoryClickProcessor { private Map> leftDraggingMap = new HashMap<>(); private Map> rightDraggingMap = new HashMap<>(); - public InventoryClickResult leftClick(InventoryCondition inventoryCondition, Player player, int slot, ItemStack clicked, ItemStack cursor) { - InventoryClickResult clickResult = startCondition(inventoryCondition, player, slot, ClickType.LEFT_CLICK, clicked, cursor); + public InventoryClickResult leftClick(List inventoryConditions, Player player, int slot, ItemStack clicked, ItemStack cursor) { + InventoryClickResult clickResult = startCondition(inventoryConditions, player, slot, ClickType.LEFT_CLICK, clicked, cursor); if (clickResult.isCancel()) { return clickResult; @@ -62,8 +59,8 @@ public class InventoryClickProcessor { return clickResult; } - public InventoryClickResult rightClick(InventoryCondition inventoryCondition, Player player, int slot, ItemStack clicked, ItemStack cursor) { - InventoryClickResult clickResult = startCondition(inventoryCondition, player, slot, ClickType.RIGHT_CLICK, clicked, cursor); + public InventoryClickResult rightClick(List inventoryConditions, Player player, int slot, ItemStack clicked, ItemStack cursor) { + InventoryClickResult clickResult = startCondition(inventoryConditions, player, slot, ClickType.RIGHT_CLICK, clicked, cursor); if (clickResult.isCancel()) { return clickResult; @@ -119,8 +116,8 @@ public class InventoryClickProcessor { return clickResult; } - public InventoryClickResult changeHeld(InventoryCondition inventoryCondition, Player player, int slot, ItemStack clicked, ItemStack cursor) { - InventoryClickResult clickResult = startCondition(inventoryCondition, player, slot, ClickType.CHANGE_HELD, clicked, cursor); + public InventoryClickResult changeHeld(List inventoryConditions, Player player, int slot, ItemStack clicked, ItemStack cursor) { + InventoryClickResult clickResult = startCondition(inventoryConditions, player, slot, ClickType.CHANGE_HELD, clicked, cursor); if (clickResult.isCancel()) { return clickResult; @@ -156,7 +153,7 @@ public class InventoryClickProcessor { return clickResult; } - public InventoryClickResult shiftClick(InventoryCondition inventoryCondition, Player player, int slot, + public InventoryClickResult shiftClick(List inventoryConditions, Player player, int slot, ItemStack clicked, ItemStack cursor, InventoryClickLoopHandler... loopHandlers) { InventoryClickResult clickResult = new InventoryClickResult(clicked, cursor); @@ -186,7 +183,7 @@ public class InventoryClickProcessor { StackingRule itemRule = item.getStackingRule(); if (itemRule.canBeStacked(item, clicked)) { - clickResult = startCondition(clickResult, inventoryCondition, player, index, ClickType.SHIFT_CLICK, item, cursor); + clickResult = startCondition(clickResult, inventoryConditions, player, index, ClickType.SHIFT_CLICK, item, cursor); if (clickResult.isCancel()) continue; @@ -212,7 +209,7 @@ public class InventoryClickProcessor { } } else if (item.isAir()) { - clickResult = startCondition(clickResult, inventoryCondition, player, index, ClickType.SHIFT_CLICK, item, cursor); + clickResult = startCondition(clickResult, inventoryConditions, player, index, ClickType.SHIFT_CLICK, item, cursor); if (clickResult.isCancel()) continue; @@ -231,7 +228,7 @@ public class InventoryClickProcessor { return clickResult; } - public InventoryClickResult dragging(InventoryCondition inventoryCondition, Player player, + public InventoryClickResult dragging(List inventoryConditions, Player player, int slot, int button, ItemStack clicked, ItemStack cursor, Function itemGetter, @@ -265,7 +262,7 @@ public class InventoryClickProcessor { ItemStack draggedItem = cursor.clone(); ItemStack slotItem = itemGetter.apply(s); - clickResult = startCondition(clickResult, inventoryCondition, player, s, ClickType.DRAGGING, slotItem, cursor); + clickResult = startCondition(clickResult, inventoryConditions, player, s, ClickType.DRAGGING, slotItem, cursor); if (clickResult.isCancel()) continue; @@ -303,7 +300,7 @@ public class InventoryClickProcessor { ItemStack draggedItem = cursor.clone(); ItemStack slotItem = itemGetter.apply(s); - clickResult = startCondition(clickResult, inventoryCondition, player, s, ClickType.DRAGGING, slotItem, cursor); + clickResult = startCondition(clickResult, inventoryConditions, player, s, ClickType.DRAGGING, slotItem, cursor); if (clickResult.isCancel()) continue; @@ -345,7 +342,7 @@ public class InventoryClickProcessor { return clickResult; } - public InventoryClickResult doubleClick(InventoryCondition inventoryCondition, Player player, int slot, + public InventoryClickResult doubleClick(List inventoryConditions, Player player, int slot, ItemStack cursor, InventoryClickLoopHandler... loopHandlers) { InventoryClickResult clickResult = new InventoryClickResult(ItemStack.getAirItem(), cursor); @@ -377,7 +374,7 @@ public class InventoryClickProcessor { if (!cursorRule.canApply(cursor, amount + 1)) break; if (cursorRule.canBeStacked(cursor, item)) { - clickResult = startCondition(clickResult, inventoryCondition, player, index, ClickType.DOUBLE_CLICK, item, cursor); + clickResult = startCondition(clickResult, inventoryConditions, player, index, ClickType.DOUBLE_CLICK, item, cursor); if (clickResult.isCancel()) continue; @@ -401,10 +398,10 @@ public class InventoryClickProcessor { return clickResult; } - public InventoryClickResult drop(InventoryCondition inventoryCondition, Player player, + public InventoryClickResult drop(List inventoryConditions, Player player, int mode, int slot, int button, ItemStack clicked, ItemStack cursor) { - InventoryClickResult clickResult = startCondition(inventoryCondition, player, slot, ClickType.DROP, clicked, cursor); + InventoryClickResult clickResult = startCondition(inventoryConditions, player, slot, ClickType.DROP, clicked, cursor); if (clickResult.isCancel()) { return clickResult; @@ -461,27 +458,29 @@ public class InventoryClickProcessor { return clickResult; } - private InventoryClickResult startCondition(InventoryClickResult clickResult, InventoryCondition inventoryCondition, Player player, int slot, ClickType clickType, ItemStack clicked, ItemStack cursor) { - if (inventoryCondition != null) { - InventoryConditionResult result = new InventoryConditionResult(clicked, cursor); - inventoryCondition.accept(player, slot, clickType, result); + private InventoryClickResult startCondition(InventoryClickResult clickResult, List inventoryConditions, Player player, int slot, ClickType clickType, ItemStack clicked, ItemStack cursor) { + if (!inventoryConditions.isEmpty()) { + for (InventoryCondition inventoryCondition : inventoryConditions) { + InventoryConditionResult result = new InventoryConditionResult(clicked, cursor); + inventoryCondition.accept(player, slot, clickType, result); - cursor = result.getCursorItem(); - clicked = result.getClickedItem(); + cursor = result.getCursorItem(); + clicked = result.getClickedItem(); - clickResult.setCancel(result.isCancel()); - if (result.isCancel()) { - clickResult.setClicked(clicked); - clickResult.setCursor(cursor); - clickResult.setRefresh(true); + clickResult.setCancel(result.isCancel()); + if (result.isCancel()) { + clickResult.setClicked(clicked); + clickResult.setCursor(cursor); + clickResult.setRefresh(true); + } } } return clickResult; } - private InventoryClickResult startCondition(InventoryCondition inventoryCondition, Player player, int slot, ClickType clickType, ItemStack clicked, ItemStack cursor) { + private InventoryClickResult startCondition(List inventoryConditions, Player player, int slot, ClickType clickType, ItemStack clicked, ItemStack cursor) { InventoryClickResult clickResult = new InventoryClickResult(clicked, cursor); - return startCondition(clickResult, inventoryCondition, player, slot, clickType, clicked, cursor); + return startCondition(clickResult, inventoryConditions, player, slot, clickType, clicked, cursor); } } diff --git a/src/main/java/net/minestom/server/utils/NbtReaderUtils.java b/src/main/java/net/minestom/server/utils/NbtReaderUtils.java index 9bcc31b94..4022a8104 100644 --- a/src/main/java/net/minestom/server/utils/NbtReaderUtils.java +++ b/src/main/java/net/minestom/server/utils/NbtReaderUtils.java @@ -91,6 +91,7 @@ public class NbtReaderUtils { } reader.readByte(); + readItemStackNBT(reader, item); } break; diff --git a/src/main/java/net/minestom/server/utils/Utils.java b/src/main/java/net/minestom/server/utils/Utils.java index 7fec70633..7ab3faed5 100644 --- a/src/main/java/net/minestom/server/utils/Utils.java +++ b/src/main/java/net/minestom/server/utils/Utils.java @@ -138,10 +138,8 @@ public class Utils { // FIXME: Enchantment { - System.out.println("ENCODAGE"); Map enchantmentMap = itemStack.getEnchantmentMap(); if (!enchantmentMap.isEmpty()) { - System.out.println("write enchant"); packet.writeByte((byte) 0x09); // list packet.writeShortSizedString("StoredEnchantments");