Advertisement
April_The_Sergal

Untitled

Nov 23rd, 2024
9
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. -- Storage Node Script
  2.  
  3. -- Variables
  4. local PROTOCOL = "mobFarm"
  5. local LOCAL_ID = "STORAGE_1" -- Change for unique IDs
  6.  
  7. -- Peripherals
  8. local modem = peripheral.find("modem", rednet.open)
  9. if not modem then
  10. error("Modem not found!")
  11. end
  12.  
  13. -- Constants
  14. local FLUID_XP = "mob_grinding_utils:fluid_xp"
  15.  
  16. -- Functions
  17. local function getInventoryType()
  18. local frontBlock = peripheral.wrap("front")
  19. if not frontBlock then
  20. return nil, "No inventory detected in front."
  21. end
  22.  
  23. local methods = peripheral.getMethods("front")
  24. if table.concat(methods):find("list") then
  25. return "chest", frontBlock
  26. elseif table.concat(methods):find("tanks") then
  27. return "tank", frontBlock
  28. else
  29. return nil, "Unknown inventory type."
  30. end
  31. end
  32.  
  33. local function getChestStoragePercentage(chest)
  34. local items = chest.list()
  35. local totalSlots = chest.size()
  36. local usedSlots = 0
  37. for slot, _ in pairs(items) do
  38. usedSlots = usedSlots + 1
  39. end
  40. return (usedSlots / totalSlots) * 100
  41. end
  42.  
  43. local function getTankFluidAmount(tank)
  44. local tanks = tank.tanks()
  45. for _, fluid in ipairs(tanks) do
  46. if fluid.name == FLUID_XP then
  47. return fluid.amount / 1000 -- Convert to thousands of mB
  48. end
  49. end
  50. return 0
  51. end
  52.  
  53. local function sendStorageUpdate()
  54. local inventoryType, inventory = getInventoryType()
  55. if not inventoryType then
  56. print(inventory) -- Log error
  57. return
  58. end
  59.  
  60. local data = {}
  61. if inventoryType == "chest" then
  62. data.type = "chest"
  63. data.percentage = getChestStoragePercentage(inventory)
  64. elseif inventoryType == "tank" then
  65. data.type = "tank"
  66. data.amount = getTankFluidAmount(inventory)
  67. end
  68.  
  69. rednet.send(rednet.lookup(PROTOCOL), {
  70. type = "storageUpdate",
  71. hostName = LOCAL_ID,
  72. storage = data
  73. })
  74. print("Sent storage update:", textutils.serialize(data))
  75. end
  76.  
  77. -- Main Loop
  78. while true do
  79. sendStorageUpdate()
  80. sleep(5) -- Update every 5 seconds
  81. end
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement