From 89a09f326e5e0b18db40b594a4a0beff1f5c9fb6 Mon Sep 17 00:00:00 2001 From: Konstantin Shandurenko Date: Sat, 30 Jul 2022 19:54:10 +0300 Subject: [PATCH] Fix for inventory window id generation (#1292) --- .../java/net/minestom/server/inventory/Inventory.java | 6 +++++- .../java/net/minestom/server/inventory/InventoryTest.java | 8 ++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/main/java/net/minestom/server/inventory/Inventory.java b/src/main/java/net/minestom/server/inventory/Inventory.java index 1a0df8c04..9bfa8617d 100644 --- a/src/main/java/net/minestom/server/inventory/Inventory.java +++ b/src/main/java/net/minestom/server/inventory/Inventory.java @@ -58,7 +58,11 @@ public non-sealed class Inventory extends AbstractInventory implements Viewable } private static byte generateId() { - return (byte) Math.abs((byte) ID_COUNTER.incrementAndGet()); + final byte id = (byte) Math.abs((byte) ID_COUNTER.incrementAndGet()); + if (id == 0) { // zero is player's inventory id + return generateId(); + } + return id; } /** diff --git a/src/test/java/net/minestom/server/inventory/InventoryTest.java b/src/test/java/net/minestom/server/inventory/InventoryTest.java index fa3f976bc..f8cb90317 100644 --- a/src/test/java/net/minestom/server/inventory/InventoryTest.java +++ b/src/test/java/net/minestom/server/inventory/InventoryTest.java @@ -75,4 +75,12 @@ public class InventoryTest { assertTrue(inventory.addItemStack(ItemStack.of(Material.ANDESITE, 32), TransactionOption.ALL_OR_NOTHING)); assertFalse(inventory.addItemStack(ItemStack.of(Material.BLUE_CONCRETE, 32), TransactionOption.ALL_OR_NOTHING)); } + + @Test + public void testIds() { + for (int i = 0; i <= 256; ++i) { + final Inventory inventory = new Inventory(InventoryType.CHEST_1_ROW, "title"); + assertTrue(inventory.getWindowId() != 0); + } + } }