Skip to content

Instantly share code, notes, and snippets.

@sudomaxime
Last active May 3, 2022 18:25
Show Gist options
  • Save sudomaxime/12c0be876b414556eb687cc805e3941e to your computer and use it in GitHub Desktop.
Save sudomaxime/12c0be876b414556eb687cc805e3941e to your computer and use it in GitHub Desktop.
get a table struct with informations about a bunch of containers
function tablelength(T)
local count = 0
for _ in pairs(T) do count = count + 1 end
return count
end
function makeInventoriesHash (containers)
inputHash = {
-- Inventory by inputs
inputs = {},
-- Consolidated inventories
global = {},
-- All item names found
names = {},
-- Containers with more than one type of items, by index
multiItemContainers = {}
}
for i, input in pairs(containers) do
inventoryHash = {}
inventory = input.chest:getInventories()[1]
size = inventory.size - 1;
for i = 0, size, 1 do
stack = inventory:getstack(i)
if stack.item.type then
name = stack.item.type.name
qty = stack.count
if not inventoryHash[name] then
inventoryHash[name] = 0
table.insert(inputHash.names, name)
end
if not inputHash.global[name] then
inputHash.global[name] = 0
end
inventoryHash[name] = inventoryHash[name] + qty
inputHash.global[name] = inputHash.global[name] + qty
end
end
inputHash.inputs[i] = inventoryHash;
if tablelength(inventoryHash) > 1 then
table.insert(inputHash.multiItemContainers, i)
end
end
return inputHash;
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment