Add inventory management functions to scan and group items by devices
This commit is contained in:
75
inventoryManager.lua
Normal file
75
inventoryManager.lua
Normal file
@@ -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 = #_
|
||||||
Reference in New Issue
Block a user