-- energyMonitor.lua
-- Zeigt den Energieinhalt eines Speichers (rechts) auf einem Monitor (oben) an.
-- Entwickelt für Advanced Computer (CC: Tweaked)
-- Peripheriegeräte abrufen
local battery = peripheral.wrap("right")
local monitor = peripheral.wrap("top")
if not battery then
error("❌ Kein Energiespeicher rechts gefunden!")
end
if not monitor then
error("❌ Kein Monitor oben gefunden!")
end
-- Monitor vorbereiten
monitor.setTextScale(1)
monitor.setBackgroundColor(colors.black)
monitor.clear()
monitor.setCursorPos(1,1)
-- Funktion zum Zeichnen des Fortschrittsbalkens
local function drawBar(x, y, width, percent)
local filled = math.floor(width * percent)
monitor.setCursorPos(x, y)
monitor.setBackgroundColor(colors.green)
monitor.write(string.rep(" ", filled))
monitor.setBackgroundColor(colors.gray)
monitor.write(string.rep(" ", width - filled))
monitor.setBackgroundColor(colors.black)
end
-- Funktion, um Energie aus verschiedenen Mods zu lesen
local function getEnergyData(periph)
local stored, max = 0, 0
if periph.getEnergyStored and periph.getMaxEnergyStored then
stored = periph.getEnergyStored()
max = periph.getMaxEnergyStored()
elseif periph.getEnergy and periph.getMaxEnergy then
stored = periph.getEnergy()
max = periph.getMaxEnergy()
elseif periph.getStored and periph.getCapacity then
stored = periph.getStored()
max = periph.getCapacity()
else
error("⚠️ Unbekannte Energie-API – Block unterstützt keine Energieabfrage.")
end
return stored, max
end
-- Hauptschleife
while true do
monitor.clear()
monitor.setCursorPos(1,1)
monitor.setTextColor(colors.white)
local ok, energyStored, energyCapacity = pcall(function()
local s, m = getEnergyData(battery)
return s, m
end)
if not ok then
monitor.setTextColor(colors.red)
monitor.write("Fehler beim Lesen der Energie!")
sleep(2)
else
local percent = (energyCapacity > 0) and (energyStored / energyCapacity) or 0
local percentText = string.format("%.1f%%", percent * 100)
monitor.setTextColor(colors.cyan)
monitor.write("Energie-Speicher")
monitor.setCursorPos(1,2)
monitor.setTextColor(colors.white)
monitor.write(string.format("%d / %d RF", energyStored, energyCapacity))
monitor.setCursorPos(1,3)
drawBar(1, 3, 25, percent)
monitor.setCursorPos(1,5)
monitor.setTextColor(colors.yellow)
monitor.write("Fuellstand: " .. percentText)
end
sleep(2)
end