Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Storage Node Script
- -- Variables
- local PROTOCOL = "mobFarm"
- local LOCAL_ID = "STORAGE_1" -- Change for unique IDs
- -- Peripherals
- local modem = peripheral.find("modem", rednet.open)
- if not modem then
- error("Modem not found!")
- end
- -- Constants
- local FLUID_XP = "mob_grinding_utils:fluid_xp"
- -- Functions
- local function getInventoryType()
- local frontBlock = peripheral.wrap("front")
- if not frontBlock then
- return nil, "No inventory detected in front."
- end
- local methods = peripheral.getMethods("front")
- if table.concat(methods):find("list") then
- return "chest", frontBlock
- elseif table.concat(methods):find("tanks") then
- return "tank", frontBlock
- else
- return nil, "Unknown inventory type."
- end
- end
- local function getChestStoragePercentage(chest)
- local items = chest.list()
- local totalSlots = chest.size()
- local usedSlots = 0
- for slot, _ in pairs(items) do
- usedSlots = usedSlots + 1
- end
- return (usedSlots / totalSlots) * 100
- end
- local function getTankFluidAmount(tank)
- local tanks = tank.tanks()
- for _, fluid in ipairs(tanks) do
- if fluid.name == FLUID_XP then
- return fluid.amount / 1000 -- Convert to thousands of mB
- end
- end
- return 0
- end
- local function sendStorageUpdate()
- local inventoryType, inventory = getInventoryType()
- if not inventoryType then
- print(inventory) -- Log error
- return
- end
- local data = {}
- if inventoryType == "chest" then
- data.type = "chest"
- data.percentage = getChestStoragePercentage(inventory)
- elseif inventoryType == "tank" then
- data.type = "tank"
- data.amount = getTankFluidAmount(inventory)
- end
- rednet.send(rednet.lookup(PROTOCOL), {
- type = "storageUpdate",
- hostName = LOCAL_ID,
- storage = data
- })
- print("Sent storage update:", textutils.serialize(data))
- end
- -- Main Loop
- while true do
- sendStorageUpdate()
- sleep(5) -- Update every 5 seconds
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement