Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- AE2 System Monitor for CC: Tweaked with Advanced Peripherals
- -- This was tested in ATM10 v3.2 modpack
- -- IMPORTANT: Older/Newer versions of Advanced Peripherals have different functions!
- -- This script will likely not work unless you are using Advanced Peripherals 0.7.49a
- local MONITOR_SIDE = "left"
- local AE2_BRIDGE_SIDE = "right"
- local REFRESH_RATE = 1
- local colors = colors
- local WHITE, BLACK = colors.white, colors.black
- local BLUE, GREEN, RED = colors.blue, colors.green, colors.red
- local YELLOW, GRAY, DARK_GRAY = colors.yellow, colors.lightGray, colors.gray
- local monitor, ae2_bridge = nil, nil
- local currentTab = 1
- local tabNames = { "Power", "Usage", "Bytes", "Types" }
- -- Cached values to prevent flickering
- local cache = {
- energy = { stored = nil, max = nil },
- usage = nil,
- bytes = { used = nil, max = nil },
- types = { used = nil, max = nil },
- }
- local function initPeripherals()
- monitor = peripheral.wrap(MONITOR_SIDE)
- if not monitor or not monitor.isColor then error("No valid Advanced Monitor at " .. MONITOR_SIDE) end
- ae2_bridge = peripheral.wrap(AE2_BRIDGE_SIDE)
- if not ae2_bridge then error("No AE2 ME Bridge at " .. AE2_BRIDGE_SIDE) end
- monitor.setTextScale(1)
- end
- local function clearMain()
- local width, height = monitor.getSize()
- monitor.setBackgroundColor(BLACK)
- monitor.setTextColor(WHITE)
- for y = 1, height - 3 do
- monitor.setCursorPos(1, y)
- monitor.write(string.rep(" ", width))
- end
- end
- local function formatNumber(n)
- if n >= 1e12 then return string.format("%.2fT", n/1e12)
- elseif n >= 1e9 then return string.format("%.2fG", n/1e9)
- elseif n >= 1e6 then return string.format("%.2fM", n/1e6)
- elseif n >= 1e3 then return string.format("%.2fK", n/1e3)
- else return tostring(math.floor(n)) end
- end
- local function drawBar(x, y, width, progress, max, fg, bg)
- local filled = math.floor(width * (progress / max))
- monitor.setCursorPos(x, y)
- monitor.setBackgroundColor(fg)
- monitor.write(string.rep(" ", filled))
- monitor.setBackgroundColor(bg)
- monitor.write(string.rep(" ", width - filled))
- end
- local function centerText(y, text, color)
- local width = monitor.getSize()
- monitor.setCursorPos(math.floor((width - #text) / 2), y)
- monitor.setTextColor(color or WHITE)
- monitor.write(text)
- end
- local function drawPowerTab()
- local ok1, stored = pcall(ae2_bridge.getEnergyStorage)
- local ok2, max = pcall(ae2_bridge.getMaxEnergyStorage)
- if not (ok1 and ok2 and stored and max) then
- if cache.energy.stored ~= "error" then
- clearMain()
- centerText(1, tabNames[1])
- centerText(3, "Error: Failed to read AE2 energy.", YELLOW)
- cache.energy.stored = "error"
- end
- return
- end
- if stored == cache.energy.stored and max == cache.energy.max then return end
- cache.energy.stored, cache.energy.max = stored, max
- clearMain()
- centerText(1, tabNames[1])
- local pct = (stored / max) * 100
- local color = pct < 25 and RED or pct < 50 and YELLOW or GREEN
- centerText(3, string.format("Stored: %s AE / %s AE", formatNumber(stored), formatNumber(max)))
- centerText(4, string.format("Percentage: %.2f%%", pct))
- local w = math.floor(monitor.getSize() * 0.8)
- drawBar(math.floor((monitor.getSize() - w)/2), 6, w, stored, max, color, GRAY)
- end
- local function drawUsageTab()
- local ok, usage = pcall(ae2_bridge.getEnergyUsage)
- if not ok or not usage then
- if cache.usage ~= "error" then
- clearMain()
- centerText(1, tabNames[2])
- centerText(3, "Error: Failed to read AE2 usage.", YELLOW)
- cache.usage = "error"
- end
- return
- end
- if usage == cache.usage then return end
- cache.usage = usage
- clearMain()
- centerText(1, tabNames[2])
- centerText(3, string.format("Current Usage: %s AE/t", formatNumber(usage)))
- centerText(5, "This is an instantaneous reading.")
- centerText(6, "Actual usage fluctuates with activity.")
- end
- local function drawBytesTab()
- local ok1, used = pcall(ae2_bridge.getUsedItemStorage)
- local ok2, max = pcall(ae2_bridge.getTotalItemStorage)
- if not (ok1 and ok2 and used and max) then
- if cache.bytes.used ~= "error" then
- clearMain()
- centerText(1, tabNames[3])
- centerText(3, "Error: Failed to read AE2 bytes.", YELLOW)
- cache.bytes.used = "error"
- end
- return
- end
- if used == cache.bytes.used and max == cache.bytes.max then return end
- cache.bytes.used, cache.bytes.max = used, max
- clearMain()
- centerText(1, tabNames[3])
- local pct = (used / max) * 100
- local color = pct > 75 and RED or pct > 50 and YELLOW or GREEN
- centerText(3, string.format("Used: %s B / %s B", formatNumber(used), formatNumber(max)))
- centerText(4, string.format("Percentage: %.2f%%", pct))
- local w = math.floor(monitor.getSize() * 0.8)
- drawBar(math.floor((monitor.getSize() - w)/2), 6, w, used, max, color, GRAY)
- end
- local function drawTypesTab()
- local ok, items = pcall(ae2_bridge.listItems)
- if not ok or not items then
- if cache.types.used ~= "error" then
- clearMain()
- centerText(1, tabNames[4])
- centerText(3, "Error: Failed to list items.", YELLOW)
- cache.types.used = "error"
- end
- return
- end
- local types = {}
- for _, item in ipairs(items) do
- if item and item.fingerprint then
- types[item.fingerprint] = true
- end
- end
- local used = 0
- for _ in pairs(types) do
- used = used + 1
- end
- --[[
- local ok1, used = pcall(ae2_bridge.getItemTypes)
- local ok2, max = pcall(ae2_bridge.getMaxItemTypes)
- if not (ok1 and ok2) then
- if cache.types.used ~= "error" then
- clearMain()
- centerText(1, tabNames[4])
- centerText(3, "Error: Failed to get types.", YELLOW)
- cache.types.used = "error"
- end
- return
- end
- --]]
- clearMain()
- centerText(1, tabNames[4])
- --local pct = (used / max) * 100
- --local color = pct > 75 and RED or pct > 50 and YELLOW or GREEN
- --centerText(3, string.format("Used: %s Types / %s Types", formatNumber(used), formatNumber(max)))
- centerText(2, string.format("Unique fingerprints or types: %d", used))
- --centerText(4, string.format("Percentage: %.2f%%", pct))
- --local w = math.floor(monitor.getSize() * 0.8)
- --drawBar(math.floor((monitor.getSize() - w)/2), 6, w, used, max, color, GRAY)
- end
- local function drawNavigation()
- local width, height = monitor.getSize()
- local tabY, spacing = height - 2, 1
- local totalWidth = 0
- for _, name in ipairs(tabNames) do totalWidth = totalWidth + #name + 2 + spacing end
- local x = math.floor((width - totalWidth) / 2)
- for i, name in ipairs(tabNames) do
- local w = #name + 2
- monitor.setCursorPos(x, tabY)
- monitor.setBackgroundColor(i == currentTab and BLUE or DARK_GRAY)
- monitor.setTextColor(i == currentTab and YELLOW or WHITE)
- monitor.write(" " .. name .. " ")
- x = x + w + spacing
- end
- end
- local function handleTouch(x, y)
- local width, height = monitor.getSize()
- local tabY = height - 2
- if y ~= tabY then return end
- local spacing = 1
- local totalWidth = 0
- for _, name in ipairs(tabNames) do totalWidth = totalWidth + #name + 2 + spacing end
- local xPos = math.floor((width - totalWidth) / 2)
- for i, name in ipairs(tabNames) do
- local w = #name + 2
- if x >= xPos and x <= xPos + w - 1 then
- if currentTab ~= i then
- currentTab = i
- drawUI()
- drawNavigation()
- end
- return
- end
- xPos = xPos + w + spacing
- end
- end
- function drawUI()
- if currentTab == 1 then drawPowerTab()
- elseif currentTab == 2 then drawUsageTab()
- elseif currentTab == 3 then drawBytesTab()
- elseif currentTab == 4 then drawTypesTab() end
- end
- local function main()
- initPeripherals()
- drawUI()
- drawNavigation()
- local lastTouch = 0
- local timer = os.startTimer(REFRESH_RATE)
- while true do
- local e, p1, p2, p3 = os.pullEvent()
- if e == "monitor_touch" and p1 == MONITOR_SIDE then
- local now = os.clock()
- if now - lastTouch > 0.25 then
- handleTouch(p2, p3)
- lastTouch = now
- end
- elseif e == "timer" and p1 == timer then
- drawUI()
- drawNavigation()
- timer = os.startTimer(REFRESH_RATE)
- end
- end
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement