101 lines
2.7 KiB
Lua
101 lines
2.7 KiB
Lua
-- 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
|
|
|
|
-- Mark as group when at least one chest AND one dropper share the item
|
|
local isGroup = #chests > 0 and #droppers > 0
|
|
local tag = isGroup and "[GROUP]" or "[-----]"
|
|
|
|
print(tag .. " " .. itemName)
|
|
|
|
for _, device in ipairs(devices) do
|
|
local label = device.type == "minecraft:chest" and "CHEST " or "DROPPER"
|
|
print(string.format(" %s %-35s x%d", label, device.name, device.count))
|
|
end
|
|
|
|
print("")
|
|
end
|
|
end
|
|
|
|
-- Main
|
|
local devices = getInventoryDevices()
|
|
print("Scanning " .. #devices .. " inventories...")
|
|
print("")
|
|
|
|
if #devices == 0 then
|
|
print("No chests or droppers found on the network.")
|
|
return
|
|
end
|
|
|
|
local itemMap = buildItemMap(devices)
|
|
displayGroups(itemMap)
|