From 0fb639ff1f0041c1a3908e5a5ed291e08c65f21f Mon Sep 17 00:00:00 2001 From: MayaTheShy Date: Sun, 15 Mar 2026 16:31:43 -0400 Subject: [PATCH] Refactor inventory helper functions for improved clarity and organization --- inventoryManager.lua | 90 ++++++++++++++++++++++---------------------- 1 file changed, 45 insertions(+), 45 deletions(-) diff --git a/inventoryManager.lua b/inventoryManager.lua index 8e5fe6f..099dc34 100644 --- a/inventoryManager.lua +++ b/inventoryManager.lua @@ -7,6 +7,51 @@ local POLL_INTERVAL = 2 -- seconds between barrel checks local MONITOR_SIDE = "left" local DASH_REFRESH = 3 -- seconds between dashboard refreshes +------------------------------------------------- +-- Inventory helpers +------------------------------------------------- + +-- Get all chest peripheral names on the network +local function getChests() + local chests = {} + for _, name in ipairs(peripheral.getNames()) do + if peripheral.getType(name) == "minecraft:chest" then + table.insert(chests, name) + end + end + return chests +end + +-- Scan a single inventory: returns { [itemName] = { total=N, slots={ [slot]={name,count} } } } +local function scanInventory(deviceName) + local inv = peripheral.wrap(deviceName) + if not inv then return {} end + local result = {} + for slot, item in pairs(inv.list()) do + if not result[item.name] then + result[item.name] = { total = 0, slots = {} } + end + result[item.name].total = result[item.name].total + item.count + result[item.name].slots[slot] = { name = item.name, count = item.count } + end + return result +end + +-- Build a full catalogue: itemName -> { {chest=name, total=N}, ... } +local function buildCatalogue() + local catalogue = {} -- itemName -> list of {chest, total} + for _, chest in ipairs(getChests()) do + local contents = scanInventory(chest) + for itemName, info in pairs(contents) do + if not catalogue[itemName] then + catalogue[itemName] = {} + end + table.insert(catalogue[itemName], { chest = chest, total = info.total }) + end + end + return catalogue +end + ------------------------------------------------- -- Monitor Dashboard ------------------------------------------------- @@ -178,51 +223,6 @@ local function drawDashboard() monCenter(h, " \4 Auto-Sort Active \4 ", colors.lightBlue, colors.blue) end -------------------------------------------------- --- Inventory helpers -------------------------------------------------- - --- Get all chest peripheral names on the network -local function getChests() - local chests = {} - for _, name in ipairs(peripheral.getNames()) do - if peripheral.getType(name) == "minecraft:chest" then - table.insert(chests, name) - end - end - return chests -end - --- Scan a single inventory: returns { [itemName] = { total=N, slots={ [slot]={name,count} } } } -local function scanInventory(deviceName) - local inv = peripheral.wrap(deviceName) - if not inv then return {} end - local result = {} - for slot, item in pairs(inv.list()) do - if not result[item.name] then - result[item.name] = { total = 0, slots = {} } - end - result[item.name].total = result[item.name].total + item.count - result[item.name].slots[slot] = { name = item.name, count = item.count } - end - return result -end - --- Build a full catalogue: itemName -> { {chest=name, total=N}, ... } -local function buildCatalogue() - local catalogue = {} -- itemName -> list of {chest, total} - for _, chest in ipairs(getChests()) do - local contents = scanInventory(chest) - for itemName, info in pairs(contents) do - if not catalogue[itemName] then - catalogue[itemName] = {} - end - table.insert(catalogue[itemName], { chest = chest, total = info.total }) - end - end - return catalogue -end - ------------------------------------------------- -- Barrel auto-sort -------------------------------------------------