Refactor inventory helper functions for improved clarity and organization
This commit is contained in:
@@ -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
|
||||
-------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user