Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Based on POH18 code ; nHMYh5FV
- -- Ensure the peripheral API is available
- local meBridgeSide = "top" -- Replace with the correct side or network name for the ME Bridge
- local monitorSide = "left" -- Replace with the correct side for the monitor
- -- Wrapping peripherals
- local meBridge = peripheral.wrap(meBridgeSide)
- local monitor = peripheral.wrap(monitorSide)
- -- Check if the ME Bridge is correctly wrapped
- if not meBridge then
- print("ME Bridge not found on side: " .. meBridgeSide)
- return
- end
- -- Ensure the monitor is available
- if not monitor then
- print("Monitor not found on side: " .. monitorSide)
- return
- end
- -- Colors
- local colorDefinitions = {
- background = colors.black,
- text = colors.white,
- headerBackground = colors.blue,
- headerText = colors.white,
- progressBarGreen = colors.lime,
- progressBarYellow = colors.yellow,
- progressBarOrange = colors.orange,
- progressBarRed = colors.red,
- progressBarBackground = colors.gray,
- boxBackground = colors.gray,
- boxText = colors.lightGray
- }
- -- Function to get item counts and storage information
- local function getItemAndStorageInfo()
- print("Fetching item and storage info...")
- local items = meBridge.listItems()
- if not items then
- print("Error: meBridge.listItems() returned nil")
- return 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
- end
- print("Items fetched successfully")
- local usedItemStorage = meBridge.getUsedItemStorage() or 0
- local itemStorage = meBridge.getTotalItemStorage() or 0
- local usedFluidStorage = meBridge.getUsedFluidStorage() or 0
- local fluidStorage = meBridge.getTotalFluidStorage() or 0
- local maxEnergy = meBridge.getMaxEnergyStorage() or 0
- local energyStorage = meBridge.getEnergyStorage() or 0
- local energyUsage = meBridge.getEnergyUsage() or 0
- print("Storage info fetched successfully")
- local totalItemCount = 0
- local nbtItemCount = 0
- local itemTypes = {}
- for _, item in ipairs(items) do
- totalItemCount = totalItemCount + item.amount
- if item.nbt then
- nbtItemCount = nbtItemCount + 1
- end
- itemTypes[item.name] = true
- end
- local itemTypesUsed = 0
- for _ in pairs(itemTypes) do
- itemTypesUsed = itemTypesUsed + 1
- end
- print("Total item count: " .. totalItemCount)
- print("NBT item count: " .. nbtItemCount)
- print("Item types used: " .. itemTypesUsed)
- -- Calculate total item type limit by counting the number of cells and multiplying by 63
- print("Fetching list of cells...")
- local cells = meBridge.listCells()
- if not cells then
- print("Error: meBridge.listCells() returned nil")
- return 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
- end
- print("List of cells fetched successfully")
- local itemTypeLimit = #cells * 63
- print("Total item type limit: " .. itemTypeLimit)
- return totalItemCount, nbtItemCount, itemStorage, fluidStorage, usedItemStorage, usedFluidStorage, maxEnergy, energyStorage, energyUsage, itemTypeLimit, itemTypesUsed
- end
- -- Function to calculate required height for a box based on the number of lines of text
- local function calculateBoxHeight(textLines)
- return #textLines + 7 -- 1 for the title, 1 for padding above, 2 for progress bar, 3 for padding below
- end
- -- Function to draw a colored progress bar directly on the monitor
- local function drawProgressBar(x, y, value, max, length, height, reverseColors)
- local percent = (max == 0) and 0 or (value / max)
- local barLength = math.floor(percent * length)
- local percentText = string.format("%.1f%%", percent * 100)
- -- Determine the color based on the percentage
- local barColor
- if reverseColors then
- if percent <= 0.05 then
- barColor = colorDefinitions.progressBarRed
- elseif percent <= 0.25 then
- barColor = colorDefinitions.progressBarOrange
- elseif percent <= 0.50 then
- barColor = colorDefinitions.progressBarYellow
- else
- barColor = colorDefinitions.progressBarGreen
- end
- else
- if percent > 0.95 then
- barColor = colorDefinitions.progressBarRed
- elseif percent > 0.75 then
- barColor = colorDefinitions.progressBarOrange
- elseif percent > 0.50 then
- barColor = colorDefinitions.progressBarYellow
- else
- barColor = colorDefinitions.progressBarGreen
- end
- end
- -- Draw the progress bar
- for i = 0, height - 1 do
- monitor.setCursorPos(x, y + i)
- monitor.setBackgroundColor(barColor)
- monitor.write(string.rep(" ", barLength))
- monitor.setBackgroundColor(colorDefinitions.progressBarBackground)
- monitor.write(string.rep(" ", length - barLength))
- end
- -- Draw the percentage text to the right of the progress bar
- monitor.setCursorPos(x + length + 2, y)
- monitor.setBackgroundColor(colorDefinitions.background)
- monitor.setTextColor(colors.white)
- monitor.write(percentText)
- end
- -- Function to format numbers with commas
- local function formatNumberWithCommas(number)
- local formatted = tostring(number)
- while true do
- formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
- if k == 0 then
- break
- end
- end
- return formatted
- end
- -- Function to format numbers with two decimal places
- local function formatNumber(number)
- return string.format("%.2f", number)
- end
- -- Function to draw a box with a title inside the border and adjust height based on content
- local function drawBox(xMin, xMax, yMin, title, textLines, progressBarValue, progressBarMax, reverseColors)
- local yMax = yMin + calculateBoxHeight(textLines) - 1
- monitor.setBackgroundColor(colorDefinitions.boxBackground)
- for xPos = xMin, xMax do
- monitor.setCursorPos(xPos, yMin)
- monitor.write(" ")
- end
- for yPos = yMin, yMax do
- monitor.setCursorPos(xMin, yPos)
- monitor.write(" ")
- monitor.setCursorPos(xMax, yPos)
- monitor.write(" ")
- end
- for xPos = xMin, xMax do
- monitor.setCursorPos(xPos, yMax)
- monitor.write(" ")
- end
- monitor.setCursorPos(xMin + 2, yMin)
- monitor.setBackgroundColor(colorDefinitions.background)
- monitor.setTextColor(colorDefinitions.boxText)
- monitor.write(" " .. title .. " ")
- monitor.setTextColor(colorDefinitions.text)
- for i, line in ipairs(textLines) do
- monitor.setCursorPos(xMin + 2, yMin + i + 1)
- monitor.write(line)
- end
- drawProgressBar(xMin + 2, yMin + #textLines + 3, progressBarValue, progressBarMax, math.floor((xMax - xMin - 4) * 3 / 4), 2, reverseColors)
- return yMax + 1
- end
- -- Function to display information on the monitor
- local function displayInfo(totalItemCount, nbtItemCount, usedItemStorage, itemStorage, usedFluidStorage, fluidStorage, maxEnergy, energyStorage, energyUsage, itemTypeLimit, itemTypesUsed)
- monitor.setBackgroundColor(colorDefinitions.background)
- monitor.setTextColor(colorDefinitions.text)
- monitor.clear()
- monitor.setTextScale(0.5)
- local width, height = monitor.getSize()
- local padding = 2
- local currentY = 3
- local boxWidth = width - 2 * padding
- -- Draw header
- monitor.setCursorPos(1, 1)
- monitor.setBackgroundColor(colorDefinitions.headerBackground)
- monitor.setTextColor(colorDefinitions.headerText)
- monitor.clearLine()
- monitor.setCursorPos(math.floor((width - string.len("AE2 Storage Monitor")) / 2) + 1, 1)
- monitor.write("AE2 Storage Monitor")
- -- Draw Items Box
- local itemLines = {
- "Total Items: " .. formatNumberWithCommas(totalItemCount),
- "Total NBT Items: " .. formatNumberWithCommas(nbtItemCount),
- "Item Type Limit: " .. formatNumberWithCommas(itemTypeLimit),
- "Item Types Used: " .. formatNumberWithCommas(itemTypesUsed),
- "Item Storage Total: " .. formatNumberWithCommas(itemStorage) .. " bytes",
- "Item Storage Used: " .. formatNumberWithCommas(usedItemStorage) .. " bytes"
- }
- currentY = drawBox(padding, width - padding, currentY, "Items", itemLines, usedItemStorage, itemStorage, false)
- -- Draw Energy Box
- local energyLines = {
- "Energy Capacity: " .. formatNumberWithCommas(formatNumber(maxEnergy)) .. " FE",
- "Energy Stored: " .. formatNumberWithCommas(formatNumber(energyStorage)) .. " FE",
- "Energy Utilization: " .. formatNumberWithCommas(formatNumber(energyUsage)) .. " FE/t"
- }
- drawBox(padding, width - padding, currentY + 1, "Energy", energyLines, energyStorage, maxEnergy, true)
- end
- -- Main loop
- while true do
- local success, totalItemCount, nbtItemCount, itemStorage, fluidStorage, usedItemStorage, usedFluidStorage, maxEnergy, energyStorage, energyUsage, itemTypeLimit, itemTypesUsed
- success = pcall(function()
- totalItemCount, nbtItemCount, itemStorage, fluidStorage, usedItemStorage, usedFluidStorage, maxEnergy, energyStorage, energyUsage, itemTypeLimit, itemTypesUsed = getItemAndStorageInfo()
- end)
- if success then
- displayInfo(totalItemCount, nbtItemCount, usedItemStorage, itemStorage, usedFluidStorage, fluidStorage, maxEnergy, energyStorage, energyUsage, itemTypeLimit, itemTypesUsed)
- else
- monitor.clear()
- monitor.setCursorPos(1, 1)
- monitor.write("Error: Unable to fetch data from ME Bridge.")
- end
- sleep(0.25) -- Update every 5 ticks (0.25 seconds)
- end
Advertisement
Add Comment
Please, Sign In to add comment