mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-06 16:37:38 +01:00
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:
parent
3ca74e00fa
commit
7b0b314707
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user