Wrong generated window id for newly created inventories. (#1784)

* chore(Inventory): Fixed generated window id sometimes becomes 0 or -128(out of int) which causes empty inventories.

* chore(InventoryTest): Updated inventory window id creation test.

* chore(InventoryTest): Updated inventory window id creation test. (iam's suggestion.)

* chore(Inventory): Updated inventory window id generation that is more thread safe compare to the old one. (iam's suggestion)

* chore(InventoryTest): int window id to byte.
This commit is contained in:
Obyvante 2023-03-15 20:44:41 +03:00 committed by GitHub
parent 3ca74e00fa
commit 7b0b314707
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 4 additions and 8 deletions

View File

@ -58,11 +58,7 @@ public non-sealed class Inventory extends AbstractInventory implements Viewable
}
private static byte generateId() {
final byte id = (byte) Math.abs((byte) ID_COUNTER.incrementAndGet());
if (id == 0) { // zero is player's inventory id
return generateId();
}
return id;
return (byte) ID_COUNTER.updateAndGet(i -> i + 1 >= 128 ? 1 : i + 1);
}
/**

View File

@ -78,9 +78,9 @@ public class InventoryTest {
@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);
for (int i = 0; i <= 1000; ++i) {
final byte windowId = new Inventory(InventoryType.CHEST_1_ROW, "title").getWindowId();
assertTrue(windowId > 0);
}
}
}