Advertisement
Loneranger419

meDrives.lua

Feb 1st, 2025 (edited)
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.01 KB | Source Code | 0 0
  1. -- Find peripherals
  2. local monitor = peripheral.find("monitor")
  3. local ae2 = peripheral.find("meBridge")
  4. local modem = peripheral.find("modem", function(_, m) return m.isWireless() end)
  5.  
  6. -- Check for required peripherals
  7. if not monitor or not ae2 or not modem then
  8.     print("Missing peripherals:")
  9.     if not monitor then print("  - No monitor found!") end
  10.     if not ae2 then print("  - No meBridge found!") end
  11.     if not modem then print("  - No wireless modem found!") end
  12.     return
  13. end
  14.  
  15. -- Initialize rednet
  16. rednet.open(peripheral.getName(modem))
  17.  
  18. -- Setup monitor
  19. monitor.setTextScale(0.5)
  20. monitor.clear()
  21.  
  22. -- Create terminal and monitor windows
  23. local termWindow = window.create(term.current(), 1, 1, term.getSize())
  24. local monWindow = window.create(monitor, 1, 1, monitor.getSize())
  25. local monx, mony = monWindow.getSize()
  26. local termx, termy = termWindow.getSize()
  27.  
  28. local receivedItemStorage = 0 -- Storage data received from network
  29.  
  30. -- Utility functions
  31. local function clearWindow(win, width, height)
  32.     win.setBackgroundColor(colors.black)
  33.     for i = 1, height do
  34.         win.setCursorPos(1, i)
  35.         win.write(string.rep(' ', width))
  36.     end
  37. end
  38.  
  39. local function roundDec(num, decimals)
  40.     local mult = 10^(decimals or 0)
  41.     return math.floor(num * mult + 0.5) / mult
  42. end
  43.  
  44. local function formatNumber(n)
  45.     return tostring(n):reverse():gsub("(%d%d%d)", "%1,"):reverse():gsub("^,", "")
  46. end
  47.  
  48. local function drawBar(label, y, percent)
  49.     local barlen = monx - 13
  50.     local loadlen = math.floor(barlen * percent)
  51.    
  52.     monWindow.setCursorPos(1, y)
  53.     monWindow.setBackgroundColor(colors.black)
  54.     monWindow.write(label .. " ")
  55.     if label == "Items" then
  56.         monWindow.write(" ")
  57.     end
  58.    
  59.     monWindow.setBackgroundColor(colors.gray)
  60.     monWindow.write(string.rep(' ', barlen))
  61.    
  62.     monWindow.setBackgroundColor(colors.black)
  63.     monWindow.write(" " .. roundDec(percent * 100, 1) .. "%")
  64.    
  65.     monWindow.setCursorPos(8, y)
  66.     monWindow.setBackgroundColor(colors.green)
  67.     monWindow.write(string.rep(' ', loadlen))
  68. end
  69.  
  70. local function getRealItemStorage(maxStorage)
  71.     local cells = ae2.listCells() or {}
  72.     local totalStorage = 0
  73.    
  74.     for _, cell in ipairs(cells) do
  75.         if cell.type == "item" then
  76.             totalStorage = totalStorage + (cell.totalBytes or 0)
  77.         end
  78.     end
  79.    
  80.     local byteconvert = math.floor((maxStorage - totalStorage) / 8)
  81.     return byteconvert + totalStorage
  82. end
  83.  
  84. local function debugInfo(max, used, percent)
  85.     clearWindow(termWindow, termx, termy)
  86.     local debugData = {
  87.         {"Max", max},
  88.         {"Used", used},
  89.         {"Percentage", roundDec(percent * 100, 2) .. "%"},
  90.         {"Received Storage", receivedItemStorage}
  91.     }
  92.    
  93.     for i, data in ipairs(debugData) do
  94.         termWindow.setCursorPos(1, i)
  95.         termWindow.write(data[1] .. ": " .. formatNumber(data[2]))
  96.     end
  97. end
  98.  
  99. local function receiveUpdates()
  100.     while true do
  101.         local _, message = rednet.receive("drawers_update")
  102.         if type(message) == "number" then
  103.             receivedItemStorage = message
  104.         end
  105.     end
  106. end
  107.  
  108. local function main()
  109.     parallel.waitForAny(function()
  110.         while true do
  111.             -- Get storage info
  112.             local itemMax = getRealItemStorage(math.abs(ae2.getTotalItemStorage()))
  113.             local itemUsed = (ae2.getUsedItemStorage() - (receivedItemStorage * 8)) + receivedItemStorage
  114.             local itemPercent = itemUsed / itemMax
  115.            
  116.             local fluidMax = math.abs(ae2.getTotalFluidStorage())
  117.             local fluidUsed = fluidMax - math.abs(ae2.getAvailableFluidStorage() / 1000)
  118.             local fluidPercent = fluidUsed / fluidMax
  119.            
  120.             -- Update display
  121.             clearWindow(monWindow, monx, mony)
  122.             drawBar("Items", 2, itemPercent)
  123.             drawBar("Fluids", 4, fluidPercent)
  124.             debugInfo(itemMax, itemUsed, itemPercent)
  125.            
  126.             sleep(3)
  127.         end
  128.     end, receiveUpdates)
  129. end
  130.  
  131. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement