Pankola

AE2 Monitor

Dec 10th, 2023 (edited)
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.51 KB | None | 0 0
  1. --Based on POH18 code ; nHMYh5FV
  2.  
  3. -- Ensure the peripheral API is available
  4. local meBridgeSide = "top"  -- Replace with the correct side or network name for the ME Bridge
  5. local monitorSide = "left"  -- Replace with the correct side for the monitor
  6.  
  7. -- Wrapping peripherals
  8. local meBridge = peripheral.wrap(meBridgeSide)
  9. local monitor = peripheral.wrap(monitorSide)
  10.  
  11. -- Check if the ME Bridge is correctly wrapped
  12. if not meBridge then
  13.     print("ME Bridge not found on side: " .. meBridgeSide)
  14.     return
  15. end
  16.  
  17. -- Ensure the monitor is available
  18. if not monitor then
  19.     print("Monitor not found on side: " .. monitorSide)
  20.     return
  21. end
  22.  
  23. -- Colors
  24. local colorDefinitions = {
  25.     background = colors.black,
  26.     text = colors.white,
  27.     headerBackground = colors.blue,
  28.     headerText = colors.white,
  29.     progressBarGreen = colors.lime,
  30.     progressBarYellow = colors.yellow,
  31.     progressBarOrange = colors.orange,
  32.     progressBarRed = colors.red,
  33.     progressBarBackground = colors.gray,
  34.     boxBackground = colors.gray,
  35.     boxText = colors.lightGray
  36. }
  37.  
  38. -- Function to get item counts and storage information
  39. local function getItemAndStorageInfo()
  40.     print("Fetching item and storage info...")
  41.    
  42.     local items = meBridge.listItems()
  43.     if not items then
  44.         print("Error: meBridge.listItems() returned nil")
  45.         return 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  46.     end
  47.     print("Items fetched successfully")
  48.  
  49.     local usedItemStorage = meBridge.getUsedItemStorage() or 0
  50.     local itemStorage = meBridge.getTotalItemStorage() or 0
  51.     local usedFluidStorage = meBridge.getUsedFluidStorage() or 0
  52.     local fluidStorage = meBridge.getTotalFluidStorage() or 0
  53.     local maxEnergy = meBridge.getMaxEnergyStorage() or 0
  54.     local energyStorage = meBridge.getEnergyStorage() or 0
  55.     local energyUsage = meBridge.getEnergyUsage() or 0
  56.  
  57.     print("Storage info fetched successfully")
  58.  
  59.     local totalItemCount = 0
  60.     local nbtItemCount = 0
  61.     local itemTypes = {}
  62.  
  63.     for _, item in ipairs(items) do
  64.         totalItemCount = totalItemCount + item.amount
  65.         if item.nbt then
  66.             nbtItemCount = nbtItemCount + 1
  67.         end
  68.         itemTypes[item.name] = true
  69.     end
  70.  
  71.     local itemTypesUsed = 0
  72.     for _ in pairs(itemTypes) do
  73.         itemTypesUsed = itemTypesUsed + 1
  74.     end
  75.  
  76.     print("Total item count: " .. totalItemCount)
  77.     print("NBT item count: " .. nbtItemCount)
  78.     print("Item types used: " .. itemTypesUsed)
  79.  
  80.     -- Calculate total item type limit by counting the number of cells and multiplying by 63
  81.     print("Fetching list of cells...")
  82.     local cells = meBridge.listCells()
  83.     if not cells then
  84.         print("Error: meBridge.listCells() returned nil")
  85.         return 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  86.     end
  87.     print("List of cells fetched successfully")
  88.  
  89.     local itemTypeLimit = #cells * 63
  90.  
  91.     print("Total item type limit: " .. itemTypeLimit)
  92.  
  93.     return totalItemCount, nbtItemCount, itemStorage, fluidStorage, usedItemStorage, usedFluidStorage, maxEnergy, energyStorage, energyUsage, itemTypeLimit, itemTypesUsed
  94. end
  95.  
  96. -- Function to calculate required height for a box based on the number of lines of text
  97. local function calculateBoxHeight(textLines)
  98.     return #textLines + 7  -- 1 for the title, 1 for padding above, 2 for progress bar, 3 for padding below
  99. end
  100.  
  101. -- Function to draw a colored progress bar directly on the monitor
  102. local function drawProgressBar(x, y, value, max, length, height, reverseColors)
  103.     local percent = (max == 0) and 0 or (value / max)
  104.     local barLength = math.floor(percent * length)
  105.     local percentText = string.format("%.1f%%", percent * 100)
  106.  
  107.     -- Determine the color based on the percentage
  108.     local barColor
  109.     if reverseColors then
  110.         if percent <= 0.05 then
  111.             barColor = colorDefinitions.progressBarRed
  112.         elseif percent <= 0.25 then
  113.             barColor = colorDefinitions.progressBarOrange
  114.         elseif percent <= 0.50 then
  115.             barColor = colorDefinitions.progressBarYellow
  116.         else
  117.             barColor = colorDefinitions.progressBarGreen
  118.         end
  119.     else
  120.         if percent > 0.95 then
  121.             barColor = colorDefinitions.progressBarRed
  122.         elseif percent > 0.75 then
  123.             barColor = colorDefinitions.progressBarOrange
  124.         elseif percent > 0.50 then
  125.             barColor = colorDefinitions.progressBarYellow
  126.         else
  127.             barColor = colorDefinitions.progressBarGreen
  128.         end
  129.     end
  130.  
  131.     -- Draw the progress bar
  132.     for i = 0, height - 1 do
  133.         monitor.setCursorPos(x, y + i)
  134.         monitor.setBackgroundColor(barColor)
  135.         monitor.write(string.rep(" ", barLength))
  136.         monitor.setBackgroundColor(colorDefinitions.progressBarBackground)
  137.         monitor.write(string.rep(" ", length - barLength))
  138.     end
  139.  
  140.     -- Draw the percentage text to the right of the progress bar
  141.     monitor.setCursorPos(x + length + 2, y)
  142.     monitor.setBackgroundColor(colorDefinitions.background)
  143.     monitor.setTextColor(colors.white)
  144.     monitor.write(percentText)
  145. end
  146.  
  147. -- Function to format numbers with commas
  148. local function formatNumberWithCommas(number)
  149.     local formatted = tostring(number)
  150.     while true do
  151.         formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
  152.         if k == 0 then
  153.             break
  154.         end
  155.     end
  156.     return formatted
  157. end
  158.  
  159. -- Function to format numbers with two decimal places
  160. local function formatNumber(number)
  161.     return string.format("%.2f", number)
  162. end
  163.  
  164. -- Function to draw a box with a title inside the border and adjust height based on content
  165. local function drawBox(xMin, xMax, yMin, title, textLines, progressBarValue, progressBarMax, reverseColors)
  166.     local yMax = yMin + calculateBoxHeight(textLines) - 1
  167.     monitor.setBackgroundColor(colorDefinitions.boxBackground)
  168.     for xPos = xMin, xMax do
  169.         monitor.setCursorPos(xPos, yMin)
  170.         monitor.write(" ")
  171.     end
  172.     for yPos = yMin, yMax do
  173.         monitor.setCursorPos(xMin, yPos)
  174.         monitor.write(" ")
  175.         monitor.setCursorPos(xMax, yPos)
  176.         monitor.write(" ")
  177.     end
  178.     for xPos = xMin, xMax do
  179.         monitor.setCursorPos(xPos, yMax)
  180.         monitor.write(" ")
  181.     end
  182.     monitor.setCursorPos(xMin + 2, yMin)
  183.     monitor.setBackgroundColor(colorDefinitions.background)
  184.     monitor.setTextColor(colorDefinitions.boxText)
  185.     monitor.write(" " .. title .. " ")
  186.     monitor.setTextColor(colorDefinitions.text)
  187.  
  188.     for i, line in ipairs(textLines) do
  189.         monitor.setCursorPos(xMin + 2, yMin + i + 1)
  190.         monitor.write(line)
  191.     end
  192.  
  193.     drawProgressBar(xMin + 2, yMin + #textLines + 3, progressBarValue, progressBarMax, math.floor((xMax - xMin - 4) * 3 / 4), 2, reverseColors)
  194.  
  195.     return yMax + 1
  196. end
  197.  
  198. -- Function to display information on the monitor
  199. local function displayInfo(totalItemCount, nbtItemCount, usedItemStorage, itemStorage, usedFluidStorage, fluidStorage, maxEnergy, energyStorage, energyUsage, itemTypeLimit, itemTypesUsed)
  200.     monitor.setBackgroundColor(colorDefinitions.background)
  201.     monitor.setTextColor(colorDefinitions.text)
  202.     monitor.clear()
  203.     monitor.setTextScale(0.5)
  204.  
  205.     local width, height = monitor.getSize()
  206.     local padding = 2
  207.     local currentY = 3
  208.     local boxWidth = width - 2 * padding
  209.  
  210.     -- Draw header
  211.     monitor.setCursorPos(1, 1)
  212.     monitor.setBackgroundColor(colorDefinitions.headerBackground)
  213.     monitor.setTextColor(colorDefinitions.headerText)
  214.     monitor.clearLine()
  215.     monitor.setCursorPos(math.floor((width - string.len("AE2 Storage Monitor")) / 2) + 1, 1)
  216.     monitor.write("AE2 Storage Monitor")
  217.  
  218.     -- Draw Items Box
  219.     local itemLines = {
  220.         "Total Items: " .. formatNumberWithCommas(totalItemCount),
  221.         "Total NBT Items: " .. formatNumberWithCommas(nbtItemCount),
  222.         "Item Type Limit: " .. formatNumberWithCommas(itemTypeLimit),
  223.         "Item Types Used: " .. formatNumberWithCommas(itemTypesUsed),
  224.         "Item Storage Total: " .. formatNumberWithCommas(itemStorage) .. " bytes",
  225.         "Item Storage Used: " .. formatNumberWithCommas(usedItemStorage) .. " bytes"        
  226.     }
  227.     currentY = drawBox(padding, width - padding, currentY, "Items", itemLines, usedItemStorage, itemStorage, false)
  228.  
  229.     -- Draw Energy Box
  230.     local energyLines = {
  231.         "Energy Capacity: " .. formatNumberWithCommas(formatNumber(maxEnergy)) .. " FE",
  232.         "Energy Stored: " .. formatNumberWithCommas(formatNumber(energyStorage)) .. " FE",
  233.         "Energy Utilization: " .. formatNumberWithCommas(formatNumber(energyUsage)) .. " FE/t"
  234.     }
  235.     drawBox(padding, width - padding, currentY + 1, "Energy", energyLines, energyStorage, maxEnergy, true)
  236. end
  237.  
  238. -- Main loop
  239. while true do
  240.     local success, totalItemCount, nbtItemCount, itemStorage, fluidStorage, usedItemStorage, usedFluidStorage, maxEnergy, energyStorage, energyUsage, itemTypeLimit, itemTypesUsed
  241.     success = pcall(function()
  242.         totalItemCount, nbtItemCount, itemStorage, fluidStorage, usedItemStorage, usedFluidStorage, maxEnergy, energyStorage, energyUsage, itemTypeLimit, itemTypesUsed = getItemAndStorageInfo()
  243.     end)
  244.    
  245.     if success then
  246.         displayInfo(totalItemCount, nbtItemCount, usedItemStorage, itemStorage, usedFluidStorage, fluidStorage, maxEnergy, energyStorage, energyUsage, itemTypeLimit, itemTypesUsed)
  247.     else
  248.         monitor.clear()
  249.         monitor.setCursorPos(1, 1)
  250.         monitor.write("Error: Unable to fetch data from ME Bridge.")
  251.     end
  252.  
  253.     sleep(0.25)  -- Update every 5 ticks (0.25 seconds)
  254. end
Advertisement
Add Comment
Please, Sign In to add comment