Skip to content

Commit d0fbec6

Browse files
committed
Add bounds checks for compartment lookup
This *shouldn't* be needed, as the compartment index is always >= 0, and Inventory.getItem returns an empty stack if the item is not found. However, some mods pass a negative compartment index, which causes getItem to throw an AIOOB instead. Fixes #2267
1 parent a683697 commit d0fbec6

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

projects/common/src/main/java/dan200/computercraft/shared/util/InventoryUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public static int getHandSlot(Player player, InteractionHand hand) {
5454
*/
5555
public static int getInventorySlotFromCompartment(Player player, int slot, ItemStack stack) {
5656
if (stack.isEmpty()) throw new IllegalArgumentException("Cannot search for empty stack");
57-
if (player.getInventory().getItem(slot) == stack) return slot;
57+
if (slot >= 0 && slot < Inventory.INVENTORY_SIZE && player.getInventory().getItem(slot) == stack) return slot;
5858
if (player.getInventory().getItem(Inventory.SLOT_OFFHAND) == stack) return Inventory.SLOT_OFFHAND;
5959
return -1;
6060
}

0 commit comments

Comments
 (0)