Skip to content

Commit ec24887

Browse files
authored
fix: ensure that the npc inventory size does not exceed 54 (#1306)
### Motivation The npc inventory has a dynamic size feature that scales the inventory based on the amount of services to display but that feature misses a upper bound of 54 (which is needed as minecraft inventories cant be bigger) ### Modification Extracted the inventory size calculation into a separate method and set a upper bound of 54. ### Result The inventory is created with a maximum of 54 slots.
1 parent fe1e270 commit ec24887

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

modules/npcs/src/main/java/eu/cloudnetservice/modules/npc/platform/bukkit/entity/BukkitPlatformSelectorEntity.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ public abstract class BukkitPlatformSelectorEntity
6262
.findMethod("setColor", ChatColor.class)
6363
.orElse(null);
6464

65+
protected static final int MAX_INVENTORY_ROWS = 6;
66+
protected static final int MAX_INVENTORY_ROW_ITEMS = 9;
67+
protected static final int MAX_INVENTORY_SIZE = MAX_INVENTORY_ROW_ITEMS * MAX_INVENTORY_ROWS;
68+
6569
protected final NPC npc;
6670
protected final Plugin plugin;
6771
protected final Server server;
@@ -432,14 +436,7 @@ protected void rebuildInfoLines() {
432436

433437
protected void rebuildInventory(@NonNull InventoryConfiguration configuration) {
434438
// calculate the inventory size
435-
var inventorySize = configuration.inventorySize();
436-
if (configuration.dynamicSize()) {
437-
inventorySize = this.serviceItems.size();
438-
// try to make it to the next higher possible inventory size
439-
while (inventorySize == 0 || (inventorySize < 54 && inventorySize % 9 != 0)) {
440-
inventorySize++;
441-
}
442-
}
439+
var inventorySize = this.calculateInventorySize(configuration);
443440
// create the inventory
444441
var inventory = this.inventory;
445442
if (inventory == null || inventory.getSize() != inventorySize) {
@@ -470,6 +467,18 @@ protected void rebuildInventory(@NonNull InventoryConfiguration configuration) {
470467
}
471468
}
472469

470+
protected int calculateInventorySize(@NonNull InventoryConfiguration configuration) {
471+
var inventorySize = configuration.inventorySize();
472+
if (configuration.dynamicSize()) {
473+
// dynamic size: create the smallest possible inventory that can fit all items (one row can fit 9 items)
474+
inventorySize = this.serviceItems.size();
475+
inventorySize += MAX_INVENTORY_ROW_ITEMS - (inventorySize % MAX_INVENTORY_ROW_ITEMS);
476+
}
477+
478+
// minecraft inventories have a limit of 54 items
479+
return Math.min(MAX_INVENTORY_SIZE, inventorySize);
480+
}
481+
473482
protected @NonNull PlayerManager playerManager() {
474483
return this.playerManager;
475484
}

0 commit comments

Comments
 (0)