From d6e3c9a7faa7766cd1e2f3a2070fa249ee831231 Mon Sep 17 00:00:00 2001 From: Jonathan Coates Date: Thu, 13 May 2021 18:12:49 +0100 Subject: [PATCH] Add inventory.getItemLimit Closes #781 --- .../generic/methods/InventoryMethods.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/main/java/dan200/computercraft/shared/peripheral/generic/methods/InventoryMethods.java b/src/main/java/dan200/computercraft/shared/peripheral/generic/methods/InventoryMethods.java index e86bc8901..8d044bf69 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/generic/methods/InventoryMethods.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/generic/methods/InventoryMethods.java @@ -135,6 +135,33 @@ public static int size( IItemHandler inventory ) return stack.isEmpty() ? null : ItemData.fill( new HashMap<>(), stack ); } + /** + * Get the maximum number of items which can be stored in this slot. + * + * Typically this will be limited to 64 items. However, some inventories (such as barrels or caches) can store + * hundreds or thousands of items in one slot. + * + * @param inventory Inventory to probe. + * @param slot The slot + * @return The maximum number of items in this slot. + * @throws LuaException If the slot is out of range. + * @cc.usage Count the maximum number of items an adjacent chest can hold. + *
{@code
+     * local chest = peripheral.find("minecraft:chest")
+     * local total = 0
+     * for i = 1, chest.size() do
+     *   total = total + chest.getItemLimit(i)
+     * end
+     * print(total)
+     * }
+ */ + @LuaFunction( mainThread = true ) + public static int getItemLimit( IItemHandler inventory, int slot ) throws LuaException + { + assertBetween( slot, 1, inventory.getSlots(), "Slot out of range (%s)" ); + return inventory.getSlotLimit( slot ); + } + /** * Push items from one inventory to another connected one. *