diff --git a/src/main/java/fr/themode/demo/PlayerInit.java b/src/main/java/fr/themode/demo/PlayerInit.java index f429debd2..cc5a4f620 100644 --- a/src/main/java/fr/themode/demo/PlayerInit.java +++ b/src/main/java/fr/themode/demo/PlayerInit.java @@ -161,7 +161,7 @@ public class PlayerInit { Inventory inventory = new Inventory(InventoryType.CHEST_1_ROW, "Test inventory"); inventory.setInventoryCondition((p, slot, inventoryConditionResult) -> { - inventoryConditionResult.setCancel(true); + inventoryConditionResult.setCancel(false); }); inventory.setItemStack(0, item.clone()); diff --git a/src/main/java/fr/themode/minestom/event/PlayerSwapItemEvent.java b/src/main/java/fr/themode/minestom/event/PlayerSwapItemEvent.java new file mode 100644 index 000000000..1855cc742 --- /dev/null +++ b/src/main/java/fr/themode/minestom/event/PlayerSwapItemEvent.java @@ -0,0 +1,30 @@ +package fr.themode.minestom.event; + +import fr.themode.minestom.item.ItemStack; + +public class PlayerSwapItemEvent extends CancellableEvent { + + private ItemStack mainHandItem; + private ItemStack offHandItem; + + public PlayerSwapItemEvent(ItemStack mainHandItem, ItemStack offHandItem) { + this.mainHandItem = mainHandItem; + this.offHandItem = offHandItem; + } + + public ItemStack getMainHandItem() { + return mainHandItem; + } + + public void setMainHandItem(ItemStack mainHandItem) { + this.mainHandItem = mainHandItem; + } + + public ItemStack getOffHandItem() { + return offHandItem; + } + + public void setOffHandItem(ItemStack offHandItem) { + this.offHandItem = offHandItem; + } +} diff --git a/src/main/java/fr/themode/minestom/inventory/Inventory.java b/src/main/java/fr/themode/minestom/inventory/Inventory.java index 0eb09e17a..0ee527237 100644 --- a/src/main/java/fr/themode/minestom/inventory/Inventory.java +++ b/src/main/java/fr/themode/minestom/inventory/Inventory.java @@ -162,8 +162,6 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View if (clickResult.doRefresh()) player.getPlayerConnection().sendPacket(getWindowItemsPacket()); - System.out.println("cancel:" + clickResult.isCancel()); - if (isInWindow) { setItemStack(slot, clickResult.getClicked()); setCursorPlayerItem(player, clickResult.getCursor()); diff --git a/src/main/java/fr/themode/minestom/inventory/PlayerInventory.java b/src/main/java/fr/themode/minestom/inventory/PlayerInventory.java index 094748a0c..fe77a1eda 100644 --- a/src/main/java/fr/themode/minestom/inventory/PlayerInventory.java +++ b/src/main/java/fr/themode/minestom/inventory/PlayerInventory.java @@ -66,14 +66,12 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler for (int i = 0; i < items.length - 10; i++) { ItemStack item = items[i]; StackingRule itemStackingRule = item.getStackingRule(); - if (item.isAir()) { - setItemStack(i, itemStack); - return true; - } else if (itemStack.isSimilar(item)) { - int itemAmount = item.getAmount(); + if (itemStackingRule.canBeStacked(itemStack, item)) { + int itemAmount = itemStackingRule.getAmount(item); if (itemAmount == stackingRule.getMaxSize()) continue; - int totalAmount = itemStack.getAmount() + itemAmount; + int itemStackAmount = itemStackingRule.getAmount(itemStack); + int totalAmount = itemStackAmount + itemAmount; if (!stackingRule.canApply(itemStack, totalAmount)) { item = itemStackingRule.apply(item, itemStackingRule.getMaxSize()); @@ -84,6 +82,9 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler sendSlotRefresh((short) convertToPacketSlot(i), item); return true; } + } else if (item.isAir()) { + setItemStack(i, itemStack); + return true; } } } diff --git a/src/main/java/fr/themode/minestom/inventory/click/InventoryClickProcessor.java b/src/main/java/fr/themode/minestom/inventory/click/InventoryClickProcessor.java index eac109247..0f11aec95 100644 --- a/src/main/java/fr/themode/minestom/inventory/click/InventoryClickProcessor.java +++ b/src/main/java/fr/themode/minestom/inventory/click/InventoryClickProcessor.java @@ -274,6 +274,7 @@ public class InventoryClickProcessor { cursor = result.getCursorItem(); clicked = result.getClickedItem(); + clickResult.setCancel(result.isCancel()); if (result.isCancel()) { clickResult.setClicked(clicked); clickResult.setCursor(cursor); diff --git a/src/main/java/fr/themode/minestom/listener/PlayerDiggingListener.java b/src/main/java/fr/themode/minestom/listener/PlayerDiggingListener.java index 0b55e0f22..3e939e954 100644 --- a/src/main/java/fr/themode/minestom/listener/PlayerDiggingListener.java +++ b/src/main/java/fr/themode/minestom/listener/PlayerDiggingListener.java @@ -5,6 +5,7 @@ import fr.themode.minestom.entity.ItemEntity; import fr.themode.minestom.entity.Player; import fr.themode.minestom.event.ItemDropEvent; import fr.themode.minestom.event.PlayerStartDiggingEvent; +import fr.themode.minestom.event.PlayerSwapItemEvent; import fr.themode.minestom.instance.Instance; import fr.themode.minestom.instance.block.CustomBlock; import fr.themode.minestom.inventory.PlayerInventory; @@ -88,8 +89,11 @@ public class PlayerDiggingListener { PlayerInventory playerInventory = player.getInventory(); ItemStack mainHand = playerInventory.getItemInMainHand().clone(); ItemStack offHand = playerInventory.getItemInOffHand().clone(); - playerInventory.setItemInMainHand(offHand); - playerInventory.setItemInOffHand(mainHand); + PlayerSwapItemEvent swapItemEvent = new PlayerSwapItemEvent(offHand, mainHand); + player.callCancellableEvent(PlayerSwapItemEvent.class, swapItemEvent, () -> { + playerInventory.setItemInMainHand(swapItemEvent.getMainHandItem()); + playerInventory.setItemInOffHand(swapItemEvent.getOffHandItem()); + }); break; } } diff --git a/src/main/java/fr/themode/minestom/net/packet/client/handler/ClientPacketsHandler.java b/src/main/java/fr/themode/minestom/net/packet/client/handler/ClientPacketsHandler.java index 1e7b1b68c..5cfd7206a 100644 --- a/src/main/java/fr/themode/minestom/net/packet/client/handler/ClientPacketsHandler.java +++ b/src/main/java/fr/themode/minestom/net/packet/client/handler/ClientPacketsHandler.java @@ -15,7 +15,7 @@ public class ClientPacketsHandler { } public ClientPacket getPacketInstance(int id) { - // System.out.println("RECEIVED PACKET 0x" + Integer.toHexString(id)); + //System.out.println("RECEIVED PACKET 0x" + Integer.toHexString(id)); if (id > SIZE) throw new IllegalStateException("Packet ID 0x" + Integer.toHexString(id) + " has been tried to be parsed, debug needed");