mc-scripts/storage-net/common.lua

56 lines
2.1 KiB
Lua
Raw Normal View History

2024-02-25 22:44:46 -06:00
-- Salt's ComputerCraft Storage Network Script
--
-- For information on this script, physical in-world setup, and configuration,
-- see: https://git.desu.ltd/salt/mc-scripts/src/branch/master/storage-net
-- Startup diagnostics
print("Salt's CC Storage Net")
2024-02-25 23:33:32 -06:00
print("Computer " .. os.getComputerID() .. " configured as " .. mode)
2024-02-25 22:44:46 -06:00
-- Global scope locals
local output = peripheral.wrap("top") or error("Put a chest on top of this terminal for output items", 0)
2024-02-25 22:47:00 -06:00
-- Helper functions
function getDepositHoppers()
-- Get all connected inventories that push items into the system
return {peripheral.find("inventory", function(name,i)
return string.find(name, "minecraft:hopper")
end)}
2024-02-25 23:33:32 -06:00
end
function getConnectedChests()
-- Get all connected inventories that store items
return {peripheral.find("inventory", function(name,i)
return string.find(name, "minecraft:chest")
end)}
2024-02-25 23:33:32 -06:00
end
-- Common functions
function pushDepositsToChests()
-- Take all the contents of all connected hoppers and stuff them into any other connected inventory
-- These things are wrapped into tables because that's how you take multiple return values and stuff them in a table, apparently
local hoppers = getDepositHoppers()
local chests = {peripheral.find("inventory", function(name,i) return string.find(name, "minecraft:chest") end)}
-- For each hopper connected to the network...
for k,hopper in ipairs(hoppers) do
-- For each item in that hopper's inventory...
for hslot,hitem in pairs(hopper.list()) do
-- For each connected "chest"...
for k,chest in ipairs(chests) do
-- Attempt to push our items in
hopper.pushItems(peripheral.getName(chest),hslot)
end
end
end
2024-02-25 22:44:46 -06:00
end
-- Application entrypoint
function main()
while true do
-- Manage inventory cleanup tasks
pushDepositsToChests() -- Take all deposit terminals and push them into the inventory network
-- Sleep for a tick
sleep(0.05) -- This is to stop CC from killing us
2024-02-25 22:44:46 -06:00
end
end
main()