From cb5122d36c27988b6c1cd882cdae7dd100ca33b3 Mon Sep 17 00:00:00 2001 From: MayaTheShy Date: Sun, 15 Mar 2026 15:10:05 -0400 Subject: [PATCH] Add inventory management functions to scan and group items by devices --- inventoryManager.lua | 75 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 inventoryManager.lua diff --git a/inventoryManager.lua b/inventoryManager.lua new file mode 100644 index 0000000..a582eca --- /dev/null +++ b/inventoryManager.lua @@ -0,0 +1,75 @@ +-- Inventory Manager: Scan & Group by shared items + +-- Scan all peripherals and categorize them +local function getInventoryDevices() + local devices = {} + for _, name in ipairs(peripheral.getNames()) do + local pType = peripheral.getType(name) + if pType == "minecraft:chest" or pType == "minecraft:dropper" then + table.insert(devices, { + name = name, + type = pType, + }) + end + end + return devices +end + +-- Scan the contents of a single inventory peripheral +local function scanInventory(deviceName) + local items = {} + local inventory = peripheral.wrap(deviceName) + if not inventory then return items end + + local contents = inventory.list() + for slot, item in pairs(contents) do + if items[item.name] then + items[item.name] = items[item.name] + item.count + else + items[item.name] = item.count + end + end + return items +end + +-- Build a map of item -> list of devices that contain it +local function buildItemMap(devices) + local itemMap = {} + + for _, device in ipairs(devices) do + device.items = scanInventory(device.name) + + for itemName, count in pairs(device.items) do + if not itemMap[itemName] then + itemMap[itemName] = {} + end + table.insert(itemMap[itemName], { + name = device.name, + type = device.type, + count = count, + }) + end + end + + return itemMap +end + +-- Display grouped results +local function displayGroups(itemMap) + print("=== Inventory Groups ===") + print("") + + for itemName, devices in pairs(itemMap) do + local chests = {} + local droppers = {} + + for _, device in ipairs(devices) do + if device.type == "minecraft:chest" then + table.insert(chests, device) + else + table.insert(droppers, device) + end + end + + -- Only show groups where at least one chest AND one dropper share the item + local isGroup = #_