Loneranger419

sendDrawers.lua

Feb 3rd, 2025 (edited)
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.21 KB | None | 0 0
  1. local monitor = peripheral.find("monitor")
  2. local ae2 = peripheral.find("meBridge")
  3. local modem = peripheral.find("modem", function(_, m) return m.isWireless() end) -- Find wireless modem
  4.  
  5. if not monitor then
  6.   print("No monitor found!")
  7.   return
  8. end
  9.  
  10. if not ae2 then
  11.     print("No meBridge found!")
  12.     return
  13. end
  14.  
  15. if not modem then
  16.     print("No wireless modem found!")
  17.     return
  18. end
  19.  
  20. rednet.open(peripheral.getName(modem)) -- Open rednet for receiving data
  21.  
  22. monitor.setTextScale(0.5)
  23. monitor.clear()
  24.  
  25. local termWindow = window.create(term.current(), 1, 1, term.getSize()) -- Save terminal state
  26. local monWindow = window.create(monitor, 1, 1, monitor.getSize()) -- Create monitor window
  27.  
  28. local monx, mony = monWindow.getSize()
  29. local termx, termy = termWindow.getSize()
  30.  
  31. local receivedItemStorage = 0 -- Variable to store received data
  32.  
  33. local function clearMonitor()
  34.   for i = 1, mony do
  35.     monWindow.setBackgroundColor(colors.black)
  36.     monWindow.setCursorPos(1, i)
  37.     monWindow.write(string.rep(' ', monx))
  38.   end
  39. end
  40.  
  41. local function clearTerminal()
  42.   for i = 1, termy do
  43.     termWindow.setBackgroundColor(colors.black)
  44.     termWindow.setCursorPos(1, i)
  45.     termWindow.write(string.rep(' ', termx))
  46.   end
  47. end
  48.  
  49. local function roundDec(num, numDecimalPlaces)
  50.   local mult = 10^(numDecimalPlaces or 0)
  51.   return math.floor(num * mult + 0.5) / mult
  52. end
  53.  
  54. local function round(n)
  55.     return math.floor(n + 0.5)
  56. end
  57.  
  58. local function formatNumberWithCommas(n)
  59.     local formatted = tostring(n):reverse():gsub("(%d%d%d)", "%1,"):reverse()
  60.     return formatted:match("^,") and formatted:sub(2) or formatted
  61. end
  62.  
  63. local function itemBar(percent) -- Draw loading bar for Items
  64.   local barlen = monx - 13
  65.   local loadlen = barlen * percent
  66.  
  67.   -- Items tag at beginning
  68.   monWindow.setCursorPos(1, 2)
  69.   monWindow.setBackgroundColor(colors.black)
  70.   monWindow.write("Items  ")
  71.  
  72.   -- Bar Background
  73.   monWindow.setBackgroundColor(colors.gray)
  74.   monWindow.write(string.rep(' ', barlen))
  75.  
  76.   -- Percentage
  77.   monWindow.setBackgroundColor(colors.black)
  78.   monWindow.write(" " .. roundDec(percent * 100, 1) .. "%")
  79.  
  80.   -- Bar Progress
  81.   monWindow.setCursorPos(8, 2)
  82.   monWindow.setBackgroundColor(colors.green)
  83.   monWindow.write(string.rep(' ', loadlen))
  84. end
  85.  
  86. local function fluidBar(percent) -- Draw loading bar for Fluids
  87.   local barlen = monx - 13
  88.   local loadlen = barlen * percent
  89.  
  90.   -- Fluids tag at beginning
  91.   monWindow.setCursorPos(1, 4)
  92.   monWindow.setBackgroundColor(colors.black)
  93.   monWindow.write("Fluids ")
  94.  
  95.   -- Bar Background
  96.   monWindow.setBackgroundColor(colors.gray)
  97.   monWindow.write(string.rep(' ', barlen))
  98.  
  99.   -- Percentage
  100.   monWindow.setBackgroundColor(colors.black)
  101.   monWindow.write(" " .. roundDec(percent * 100, 1) .. "%")
  102.  
  103.   -- Bar Progress
  104.   monWindow.setCursorPos(8, 4)
  105.   monWindow.setBackgroundColor(colors.green)
  106.   monWindow.write(string.rep(' ', loadlen))
  107. end
  108.  
  109. local function getRealItemStorage(itemmax)
  110.     local cells = ae2.listCells() -- Get storage cells
  111.  
  112.     if not cells or #cells == 0 then
  113.         print("No storage cells found.")
  114.         return 0 -- Return 0 if no cells exist
  115.     end
  116.  
  117.     local totalStorage = 0
  118.  
  119.     for _, cell in ipairs(cells) do
  120.         if cell.type == "item" then -- Only sum item storage cells
  121.             totalStorage = totalStorage + (cell.totalBytes or 0)
  122.         end
  123.     end
  124.  
  125.     local nonByteStorage = itemmax - totalStorage
  126.     local byteconvert = round(nonByteStorage / 8)
  127.     local realStorage = byteconvert + totalStorage
  128.  
  129.     return realStorage
  130. end
  131.  
  132. local function debug(max, avail, used, per)
  133.   clearTerminal()
  134.   termWindow.setCursorPos(1, 1)
  135.   termWindow.write("Max: " .. formatNumberWithCommas(max))
  136.   termWindow.setCursorPos(1, 2)
  137.   termWindow.write("Free: " .. formatNumberWithCommas(avail))
  138.   termWindow.setCursorPos(1, 3)
  139.   termWindow.write("Used: " .. formatNumberWithCommas(used))
  140.   termWindow.setCursorPos(1, 4)
  141.   termWindow.write("Percentage: " .. roundDec(per * 100, 2) .. "%")
  142.   termWindow.setCursorPos(1, 5)
  143.   termWindow.write("Received Storage: " .. formatNumberWithCommas(receivedItemStorage))
  144. end
  145.  
  146. local function receiveUpdates()
  147.   while true do
  148.     local senderID, message, protocol = rednet.receive("drawers_update")
  149.     if type(message) == "number" then
  150.       receivedItemStorage = message
  151.     end
  152.   end
  153. end
  154.  
  155. local function main()
  156.   parallel.waitForAny(function()
  157.     while true do
  158.       -- Set item variables
  159.       local itemmax = math.abs(ae2.getTotalItemStorage())
  160.       itemmax = getRealItemStorage(itemmax)
  161.       local itemavailable = math.abs(ae2.getAvailableItemStorage())
  162.       local itemused = (ae2.getUsedItemStorage() - (receivedItemStorage * 8)) + receivedItemStorage
  163.       local itempercent = itemused / itemmax
  164.  
  165.       -- Set fluid variables
  166.       local fluidmax = math.abs(ae2.getTotalFluidStorage())
  167.       local fluidavailable = math.abs(ae2.getAvailableFluidStorage() / 1000)
  168.       local fluidused = fluidmax - fluidavailable
  169.       local fluidpercent = fluidused / fluidmax
  170.  
  171.       clearMonitor()
  172.       itemBar(itempercent)
  173.       fluidBar(fluidpercent)
  174.       debug(itemmax, itemavailable, itemused, itempercent)
  175.  
  176.       sleep(3)
  177.     end
  178.   end, receiveUpdates)
  179. end
  180.  
  181. main()
Advertisement
Add Comment
Please, Sign In to add comment