cc-stuff/stock-manager.lua
2025-12-26 22:04:05 -05:00

47 lines
1.4 KiB
Lua

-- Program that manages a stock system
-- For now this just means removing "waste" but in the future it could involve
-- - Displaying information to the user
local STOCK_SOURCE = ""
local DISCARD_ADDR = ""
local LARGE_COMPACTING_ADDR = ""
local SMALL_COMPACTING_ADDR = ""
local KEEP_ONLY = {}
KEEP_ONLY["minecraft:quartz"] = {6 * 64, DISCARD_ADDR}
KEEP_ONLY["minecraft:prismarine_shard"] = {2 * 64, DISCARD_ADDR}
local DISCARD_REST = false
local SLEEP_T = 10
function remove_waste(source, discard_rest)
local amounts_to_keep = KEEP_ONLY
local discard = function (item, amt, dest)
peripheral.call(source, "requestFiltered", dest
{
name = item.name,
_requestCount = amt,
}
)
end
for slot, item in pairs(peripheral.call(source, "stock")) do
if amounts_to_keep[item.name] ~= nil then
local excess = item.count - amounts_to_keep[item.name][1]
if excess > 0 then
amounts_to_keep[item.name][1] = 0
discard(item, excess, amounts_to_keep[item.name][2])
else
amounts_to_keep[item.name][1] = -excess
end
elseif DISCARD_REST then
discard(item, amt, DISCARD_ADDR)
end
os.sleep(1)
end
end
while true do
remove_waste(STOCK_SOURCE, DISCARD_REST)
os.sleep(SLEEP_T)
end