ComputerCraft/chest_monitor.lua

86 lines
2.9 KiB
Lua
Raw Normal View History

2024-03-10 23:45:44 +03:00
-- There are a lot of chest monitors beyond this one
-- Each branching into countless features of their own
-- But this monitor is being preserved because it's special
-- It's special, because despite being so basic...
-- it was the first practical CC program I ever wrote
local chests = {peripheral.find("minecraft:chest")}
local data_monitor = peripheral.find("monitor")
local sleep_time = sleep(#(chests)/20)
local function move_line()
data_monitor.setCursorPos(1, select(2, data_monitor.getCursorPos()) + 1)
if select(2, data_monitor.getCursorPos()) >= select(2, data_monitor.getSize()) then
data_monitor.scroll(1)
data_monitor.setCursorPos(1, select(2, data_monitor.getCursorPos()) - 1)
end
end
local function get_chest_data(chest)
local count = 0
local slots = 0
local calls = {}
local max_storage = 0
for i, item in pairs(chest.list()) do
table.insert(calls,function()
slots = slots + 1
count = count + item.count
max_storage = chest.getItemDetail(i).maxCount + max_storage
end)
end
parallel.waitForAll(table.unpack(calls))
return count, max_storage + (chest.size() - slots) * 64
end
local function make_progressbar(size, taken, max)
size = size - 2 -- make space for "[" and "]"
--[[
taken/max = x/size
taken * size = x * max
x = taken * size / max
]]
local progress = math.floor(size * taken / max)
local hash = ("#"):rep(progress)
local dots = ("."):rep(size - progress)
return ("[" .. hash .. dots .. "]")
end
local function write_progressbar(taken, max)
-- Progressbar
local percent = 100 - math.floor(100 * taken/max)
local taken_space = #tostring(percent) + #tostring(taken) + #tostring(max) + 11 -- " taken/free [PROGRESSBAR] percent% free "
local size = select(1, data_monitor.getSize()) - taken_space
data_monitor.write(" " .. tostring(taken) .. "/" .. tostring(max) .. " " .. make_progressbar(size, taken, max) .. " ".. percent .. "% free")
move_line()
end
local function write_chest_data(index, taken, max)
local start = " Chest " .. index .. (" "):rep(8 - #tostring(index) - #tostring(taken)) .. tostring(taken) .. "/" .. tostring(max) .. (" "):rep(5 - #tostring(max))
data_monitor.write(start .. make_progressbar(20, taken, max))
end
while true do
local max_storage = 0
local taken_storage = 0
local chests_list = {}
for i, chest in pairs(chests) do
local taken, max = get_chest_data(chest)
taken_storage = taken_storage + taken
max_storage = max_storage + max
table.insert(chests_list, {["taken"] = taken, ["max"] = max})
end
data_monitor.clear()
data_monitor.setCursorPos(1, 1)
write_progressbar(taken_storage, max_storage)
for i, chest in pairs(chests_list) do
write_chest_data(i, chest["taken"], chest["max"])
move_line()
end
sleep(sleep_time)
end