Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local saveFilePath = "EMC_Monitor_Save.json"
- if fs.open("EMC_Values.json", "r") == nil then
- shell.run("pastebin get mjdF8LBR EMC_Values.json")
- end
- local json = fs.open("EMC_Values.json", "r")
- local jsonData = json.readAll()
- json.close()
- local itemData = textutils.unserialiseJSON(jsonData)
- local function loadSavedData()
- if fs.exists(saveFilePath) then
- local file = fs.open(saveFilePath, "r")
- local data = textutils.unserialiseJSON(file.readAll())
- file.close()
- return data.time, data.initStorage, data.totalEMCReceived, data.lastUpdateTime, data.emcPerSecond
- else
- return 0, 0, 0, os.clock(), 0
- end
- end
- local function saveData(time, initStorage, totalEMCReceived, lastUpdateTime, emcPerSecond)
- local data = {
- time = time,
- initStorage = initStorage,
- totalEMCReceived = totalEMCReceived,
- lastUpdateTime = lastUpdateTime,
- emcPerSecond = emcPerSecond
- }
- local file = fs.open(saveFilePath, "w")
- file.write(textutils.serialiseJSON(data))
- file.close()
- end
- local unit = peripheral.find("industrialforegoing:black_hole_unit_tile")
- local monitor = peripheral.find("monitor")
- local time, initStorage, totalEMCReceived, lastUpdateTime, emcPerSecond = loadSavedData()
- for slot, item in pairs(unit.list()) do
- local itemName = item.name .. "|" .. item.damage
- local emcValue = itemData[itemName] or 0
- emcValue = emcValue * item.count
- initStorage = emcValue
- end
- local function getEMCValues()
- local items = unit.list()
- local storage = 0
- for slot, item in pairs(items) do
- local itemName = item.name .. "|" .. item.damage
- local emcValue = itemData[itemName] or 0
- emcValue = emcValue * item.count
- storage = storage + emcValue
- end
- return storage
- end
- local function calculateNextShipmentTime(expectedEMC)
- local currentTime = os.clock()
- local timeSinceLastUpdate = currentTime - lastUpdateTime
- if expectedEMC > 0 then
- return (expectedEMC - totalEMCReceived) / emcPerSecond
- else
- return math.huge
- end
- end
- local function updateMonitor(storage, emcPerSecond, timeUntilNextShipment)
- monitor.setBackgroundColour(colours.black)
- monitor.clear()
- monitor.setBackgroundColor(colours.blue)
- monitor.setCursorPos(3, 2)
- monitor.write("MrOliPantz's EMC Monitor")
- monitor.setBackgroundColor(colours.cyan)
- monitor.setCursorPos(3, 3)
- monitor.write("EMC In Storage: " .. storage)
- monitor.setCursorPos(3, 4)
- monitor.write("Total EMC Received: " .. totalEMCReceived)
- monitor.setCursorPos(3, 5)
- monitor.write("EMC/s: " .. emcPerSecond)
- monitor.setCursorPos(3, 6)
- monitor.write("Time Until Next Shipment: " .. string.format("%.2f", timeUntilNextShipment) .. "s")
- end
- local expectedEMC = 1000
- while true do
- os.sleep(1)
- time = time + 1
- local storage = getEMCValues()
- if storage > totalEMCReceived then
- emcPerSecond = (storage - totalEMCReceived) / (os.clock() - lastUpdateTime)
- totalEMCReceived = storage
- lastUpdateTime = os.clock()
- end
- local timeUntilNextShipment = calculateNextShipmentTime(expectedEMC)
- updateMonitor(storage, emcPerSecond, timeUntilNextShipment)
- saveData(time, initStorage, totalEMCReceived, lastUpdateTime, emcPerSecond)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement