SuperMcBrent

worker_energy_core_reader

Oct 30th, 2025 (edited)
1,260
1
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.86 KB | Gaming | 1 0
  1. -- Peripheral setup
  2. local core = peripheral.wrap("draconic_rf_storage_3")
  3. local inputdetector = peripheral.wrap("energy_detector_4")
  4. local outputdetector = peripheral.wrap("energy_detector_3")
  5. peripheral.find("modem", rednet.open)
  6.  
  7. local function getCoreTier(max)
  8.     if max < 50000000 then return 1
  9.     elseif max < 300000000 then return 2
  10.     elseif max < 2000000000 then return 3
  11.     elseif max < 10000000000 then return 4
  12.     elseif max < 50000000000 then return 5
  13.     elseif max < 400000000000 then return 6
  14.     elseif max < 3000000000000 then return 7
  15.     else return 8 end
  16. end
  17.  
  18. local function getCoreData()
  19.     local now = core.getEnergyStored()
  20.     local max = core.getMaxEnergyStored()
  21.     local data = {
  22.         now = now,
  23.         max = max,
  24.         rate = core.getTransferPerTick(),
  25.         tier = getCoreTier(max),
  26.         input = inputdetector.getTransferRate(),
  27.         output = outputdetector.getTransferRate()
  28.     }
  29.     return data
  30. end
  31.  
  32. local function HandleRequest(sender, message, protocol)
  33.     print("Handling: " .. tostring(message))
  34.     if message == "provideCoreData" then
  35.         local data = getCoreData()
  36.         print(os.time() .. ": " .. data.now .. " RF")
  37.         rednet.send(sender, data, protocol)
  38.     end
  39. end
  40.  
  41. local function requestListener()
  42.     while true do
  43.         local sender, message, protocol = rednet.receive()
  44.         if type(message) == "table" and protocol == "batchedRequests" then
  45.             print("Received batch of " .. #message .. " requests")
  46.             for _, req in ipairs(message) do
  47.                 HandleRequest(sender, req.message, req.protocol)
  48.             end
  49.         else
  50.             HandleRequest(sender, message, protocol)
  51.         end
  52.     end
  53. end
  54.  
  55. local function coreMonitor()
  56.     while true do
  57.         sleep(0.5)
  58.     end
  59. end
  60.  
  61. parallel.waitForAll(coreMonitor, requestListener)
  62.  
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment