Skip to content

Instantly share code, notes, and snippets.

@bananasov
Created July 6, 2022 18:24
Show Gist options
  • Save bananasov/3b8e30e41ad115fc8da04fde3a4ec4b7 to your computer and use it in GitHub Desktop.
Save bananasov/3b8e30e41ad115fc8da04fde3a4ec4b7 to your computer and use it in GitHub Desktop.
Get current items and max amount of items from connected chests in ComputerCraft
-- Tested on ComputerCraft: Restiched v1.101.0 on Minecraft 1.19
local function getChests()
local chests = peripheral.getNames()
local actual_chests = {}
for i, chest in pairs(chests) do
if chest:find("chest") or chest:find("barrel") then
table.insert(actual_chests, chest)
end
end
return actual_chests
end
local function getMaxSize()
local chests = getChests()
local max_size = 0
for i, chest in pairs(chests) do
local wrap = peripheral.wrap(chest)
local size = wrap.size()
max_size = max_size + (size * 64)
end
return max_size
end
local function getCurrentSize()
local chests = getChests()
local current_size = 0
for _, chest in pairs(chests) do
local list = peripheral.call(chest, "list")
for _, slot in pairs(list) do
current_size = current_size + slot.count
end
end
return current_size
end
local max_size = getMaxSize()
local current_size = getCurrentSize()
print(("Using %d items out of %d"):format(current_size, max_size))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment