Fix inventory id overflow

This commit is contained in:
TheMode 2021-04-14 05:03:37 +02:00
parent c57e35c58a
commit 97f2f3f06c

View File

@ -19,7 +19,6 @@ import java.util.Collections;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet; import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.atomic.AtomicInteger;
/** /**
* Represents an inventory which can be viewed by a collection of {@link Player}. * Represents an inventory which can be viewed by a collection of {@link Player}.
@ -30,7 +29,7 @@ import java.util.concurrent.atomic.AtomicInteger;
public class Inventory extends AbstractInventory implements Viewable { public class Inventory extends AbstractInventory implements Viewable {
// incremented each time an inventory is created (used in the window packets) // incremented each time an inventory is created (used in the window packets)
private static final AtomicInteger LAST_INVENTORY_ID = new AtomicInteger(); private static byte LAST_INVENTORY_ID = 1;
// the id of this inventory // the id of this inventory
private final byte id; private final byte id;
@ -60,11 +59,13 @@ public class Inventory extends AbstractInventory implements Viewable {
this(inventoryType, Component.text(title)); this(inventoryType, Component.text(title));
} }
private static byte generateId() { private static synchronized byte generateId() {
byte newInventoryId = (byte) LAST_INVENTORY_ID.incrementAndGet(); if (LAST_INVENTORY_ID != Byte.MAX_VALUE) {
if (newInventoryId == Byte.MAX_VALUE) return ++LAST_INVENTORY_ID;
newInventoryId = 1; } else {
return newInventoryId; LAST_INVENTORY_ID = 1;
return LAST_INVENTORY_ID;
}
} }
/** /**