Advertisement
xX-AAAAAAAAAA-Xx

f

Nov 1st, 2024 (edited)
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.37 KB | None | 0 0
  1. local saveFilePath = "EMC_Monitor_Save.json"
  2.  
  3. if fs.open("EMC_Values.json", "r") == nil then
  4.     shell.run("pastebin get mjdF8LBR EMC_Values.json")
  5. end
  6.  
  7. local json = fs.open("EMC_Values.json", "r")
  8. local jsonData = json.readAll()
  9. json.close()
  10. local itemData = textutils.unserialiseJSON(jsonData)
  11.  
  12. local function loadSavedData()
  13.     if fs.exists(saveFilePath) then
  14.         local file = fs.open(saveFilePath, "r")
  15.         local data = textutils.unserialiseJSON(file.readAll())
  16.         file.close()
  17.         return data.time, data.initStorage, data.totalEMCReceived, data.lastUpdateTime, data.emcPerSecond
  18.     else
  19.         return 0, 0, 0, os.clock(), 0
  20.     end
  21. end
  22.  
  23. local function saveData(time, initStorage, totalEMCReceived, lastUpdateTime, emcPerSecond)
  24.     local data = {
  25.         time = time,
  26.         initStorage = initStorage,
  27.         totalEMCReceived = totalEMCReceived,
  28.         lastUpdateTime = lastUpdateTime,
  29.         emcPerSecond = emcPerSecond
  30.     }
  31.     local file = fs.open(saveFilePath, "w")
  32.     file.write(textutils.serialiseJSON(data))
  33.     file.close()
  34. end
  35.  
  36. local unit = peripheral.find("industrialforegoing:black_hole_unit_tile")
  37. local monitor = peripheral.find("monitor")
  38.  
  39. local time, initStorage, totalEMCReceived, lastUpdateTime, emcPerSecond = loadSavedData()
  40.  
  41. for slot, item in pairs(unit.list()) do
  42.     local itemName = item.name .. "|" .. item.damage
  43.     local emcValue = itemData[itemName] or 0
  44.     emcValue = emcValue * item.count
  45.     initStorage = emcValue
  46. end
  47.  
  48. local function getEMCValues()
  49.     local items = unit.list()
  50.     local storage = 0
  51.  
  52.     for slot, item in pairs(items) do
  53.         local itemName = item.name .. "|" .. item.damage
  54.         local emcValue = itemData[itemName] or 0
  55.         emcValue = emcValue * item.count
  56.         storage = storage + emcValue
  57.     end
  58.  
  59.     return storage
  60. end
  61.  
  62. local function calculateNextShipmentTime(expectedEMC)
  63.     local currentTime = os.clock()
  64.     local timeSinceLastUpdate = currentTime - lastUpdateTime
  65.     if expectedEMC > 0 then
  66.         return (expectedEMC - totalEMCReceived) / emcPerSecond
  67.     else
  68.         return math.huge
  69.     end
  70. end
  71.  
  72. local function updateMonitor(storage, emcPerSecond, timeUntilNextShipment)
  73.     monitor.setBackgroundColour(colours.black)
  74.     monitor.clear()
  75.     monitor.setBackgroundColor(colours.blue)
  76.     monitor.setCursorPos(3, 2)
  77.     monitor.write("MrOliPantz's EMC Monitor")
  78.     monitor.setBackgroundColor(colours.cyan)
  79.     monitor.setCursorPos(3, 3)
  80.     monitor.write("EMC In Storage: " .. storage)
  81.     monitor.setCursorPos(3, 4)
  82.     monitor.write("Total EMC Received: " .. totalEMCReceived)
  83.     monitor.setCursorPos(3, 5)
  84.     monitor.write("EMC/s: " .. emcPerSecond)
  85.     monitor.setCursorPos(3, 6)
  86.     monitor.write("Time Until Next Shipment: " .. string.format("%.2f", timeUntilNextShipment) .. "s")
  87. end
  88.  
  89. local expectedEMC = 1000
  90.  
  91. while true do
  92.     os.sleep(1)
  93.     time = time + 1
  94.     local storage = getEMCValues()
  95.  
  96.     if storage > totalEMCReceived then
  97.         emcPerSecond = (storage - totalEMCReceived) / (os.clock() - lastUpdateTime)
  98.         totalEMCReceived = storage
  99.         lastUpdateTime = os.clock()
  100.     end
  101.  
  102.     local timeUntilNextShipment = calculateNextShipmentTime(expectedEMC)
  103.     updateMonitor(storage, emcPerSecond, timeUntilNextShipment)
  104.     saveData(time, initStorage, totalEMCReceived, lastUpdateTime, emcPerSecond)
  105. end
  106.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement