Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local monitor = peripheral.find("monitor")
- local ae2 = peripheral.find("meBridge")
- local modem = peripheral.find("modem", function(_, m) return m.isWireless() end) -- Find wireless modem
- if not monitor then
- print("No monitor found!")
- return
- end
- if not ae2 then
- print("No meBridge found!")
- return
- end
- if not modem then
- print("No wireless modem found!")
- return
- end
- rednet.open(peripheral.getName(modem)) -- Open rednet for receiving data
- monitor.setTextScale(0.5)
- monitor.clear()
- local termWindow = window.create(term.current(), 1, 1, term.getSize()) -- Save terminal state
- local monWindow = window.create(monitor, 1, 1, monitor.getSize()) -- Create monitor window
- local monx, mony = monWindow.getSize()
- local termx, termy = termWindow.getSize()
- local receivedItemStorage = 0 -- Variable to store received data
- local function clearMonitor()
- for i = 1, mony do
- monWindow.setBackgroundColor(colors.black)
- monWindow.setCursorPos(1, i)
- monWindow.write(string.rep(' ', monx))
- end
- end
- local function clearTerminal()
- for i = 1, termy do
- termWindow.setBackgroundColor(colors.black)
- termWindow.setCursorPos(1, i)
- termWindow.write(string.rep(' ', termx))
- end
- end
- local function roundDec(num, numDecimalPlaces)
- local mult = 10^(numDecimalPlaces or 0)
- return math.floor(num * mult + 0.5) / mult
- end
- local function round(n)
- return math.floor(n + 0.5)
- end
- local function formatNumberWithCommas(n)
- local formatted = tostring(n):reverse():gsub("(%d%d%d)", "%1,"):reverse()
- return formatted:match("^,") and formatted:sub(2) or formatted
- end
- local function itemBar(percent) -- Draw loading bar for Items
- local barlen = monx - 13
- local loadlen = barlen * percent
- -- Items tag at beginning
- monWindow.setCursorPos(1, 2)
- monWindow.setBackgroundColor(colors.black)
- monWindow.write("Items ")
- -- Bar Background
- monWindow.setBackgroundColor(colors.gray)
- monWindow.write(string.rep(' ', barlen))
- -- Percentage
- monWindow.setBackgroundColor(colors.black)
- monWindow.write(" " .. roundDec(percent * 100, 1) .. "%")
- -- Bar Progress
- monWindow.setCursorPos(8, 2)
- monWindow.setBackgroundColor(colors.green)
- monWindow.write(string.rep(' ', loadlen))
- end
- local function fluidBar(percent) -- Draw loading bar for Fluids
- local barlen = monx - 13
- local loadlen = barlen * percent
- -- Fluids tag at beginning
- monWindow.setCursorPos(1, 4)
- monWindow.setBackgroundColor(colors.black)
- monWindow.write("Fluids ")
- -- Bar Background
- monWindow.setBackgroundColor(colors.gray)
- monWindow.write(string.rep(' ', barlen))
- -- Percentage
- monWindow.setBackgroundColor(colors.black)
- monWindow.write(" " .. roundDec(percent * 100, 1) .. "%")
- -- Bar Progress
- monWindow.setCursorPos(8, 4)
- monWindow.setBackgroundColor(colors.green)
- monWindow.write(string.rep(' ', loadlen))
- end
- local function getRealItemStorage(itemmax)
- local cells = ae2.listCells() -- Get storage cells
- if not cells or #cells == 0 then
- print("No storage cells found.")
- return 0 -- Return 0 if no cells exist
- end
- local totalStorage = 0
- for _, cell in ipairs(cells) do
- if cell.type == "item" then -- Only sum item storage cells
- totalStorage = totalStorage + (cell.totalBytes or 0)
- end
- end
- local nonByteStorage = itemmax - totalStorage
- local byteconvert = round(nonByteStorage / 8)
- local realStorage = byteconvert + totalStorage
- return realStorage
- end
- local function debug(max, avail, used, per)
- clearTerminal()
- termWindow.setCursorPos(1, 1)
- termWindow.write("Max: " .. formatNumberWithCommas(max))
- termWindow.setCursorPos(1, 2)
- termWindow.write("Free: " .. formatNumberWithCommas(avail))
- termWindow.setCursorPos(1, 3)
- termWindow.write("Used: " .. formatNumberWithCommas(used))
- termWindow.setCursorPos(1, 4)
- termWindow.write("Percentage: " .. roundDec(per * 100, 2) .. "%")
- termWindow.setCursorPos(1, 5)
- termWindow.write("Received Storage: " .. formatNumberWithCommas(receivedItemStorage))
- end
- local function receiveUpdates()
- while true do
- local senderID, message, protocol = rednet.receive("drawers_update")
- if type(message) == "number" then
- receivedItemStorage = message
- end
- end
- end
- local function main()
- parallel.waitForAny(function()
- while true do
- -- Set item variables
- local itemmax = math.abs(ae2.getTotalItemStorage())
- itemmax = getRealItemStorage(itemmax)
- local itemavailable = math.abs(ae2.getAvailableItemStorage())
- local itemused = (ae2.getUsedItemStorage() - (receivedItemStorage * 8)) + receivedItemStorage
- local itempercent = itemused / itemmax
- -- Set fluid variables
- local fluidmax = math.abs(ae2.getTotalFluidStorage())
- local fluidavailable = math.abs(ae2.getAvailableFluidStorage() / 1000)
- local fluidused = fluidmax - fluidavailable
- local fluidpercent = fluidused / fluidmax
- clearMonitor()
- itemBar(itempercent)
- fluidBar(fluidpercent)
- debug(itemmax, itemavailable, itemused, itempercent)
- sleep(3)
- end
- end, receiveUpdates)
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment