Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Programm: Tank-Daten empfangen und als Text anzeigen
- -- Voraussetzungen:
- -- 1. ComputerCraft Tweaked installiert.
- -- 2. Ein Modem (z. B. Wireless Modem) am Computer angebracht und aktiviert.
- -- 3. Ein Monitor (z. B. Advanced Monitor) am Computer angebracht und aktiviert.
- -- Konfiguration
- local modemSide = "right" -- Seite, an der das Modem angebracht ist
- local monitorSide = "top" -- Seite, an der der Monitor angeschlossen ist
- local channel = 1 -- Kanal, auf dem die Daten empfangen werden
- local monitorScale = 1 -- Textgröße des Monitors
- -- Globale Tabelle zur Speicherung aller Tanks, gruppiert nach Flüssigkeit
- local tanksByFluid = {}
- -- Funktionen
- local function getFluidType(fluidName)
- local separator = string.find(fluidName, ":")
- if separator then
- return string.sub(fluidName, separator + 1)
- end
- return fluidName
- end
- local function formatNumberWithThousandsSeparator(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
- local function displayTankData(monitor, tanksByFluid)
- monitor.setBackgroundColor(colors.black)
- monitor.clear()
- monitor.setTextColor(colors.white)
- local row = 1
- for fluid, tank in pairs(tanksByFluid) do
- monitor.setCursorPos(1, row)
- monitor.write(string.format("Flüssigkeit: %s", fluid))
- row = row + 1
- local percentage = (tank.amount / tank.capacity) * 100
- local amountFormatted = formatNumberWithThousandsSeparator(tank.amount)
- local capacityFormatted = formatNumberWithThousandsSeparator(tank.capacity)
- local text = string.format(" %s/%s mb (%.1f%%)", amountFormatted, capacityFormatted, percentage)
- monitor.setCursorPos(1, row)
- monitor.write(text)
- row = row + 1
- end
- end
- -- Hauptprogramm
- print("Starte Monitor...")
- local modem = peripheral.wrap(modemSide)
- local monitor = peripheral.wrap(monitorSide)
- if not modem then
- error("Modem nicht gefunden!")
- end
- if not monitor then
- error("Monitor nicht gefunden!")
- end
- monitor.setTextScale(monitorScale)
- modem.open(channel)
- while true do
- local event, _, senderChannel, _, message = os.pullEvent("modem_message")
- if senderChannel == channel then
- print("Nachricht empfangen: " .. tostring(message)) -- Debug-Ausgabe
- local success, data = pcall(function()
- return type(message) == "table" and message or textutils.unserialize(message)
- end)
- if success and data and type(data) == "table" then
- -- Verarbeitung der empfangenen Daten
- if data.capacity and data.amount and data.fluid then
- local fluidType = getFluidType(data.fluid)
- tanksByFluid[fluidType] = {
- capacity = data.capacity,
- amount = data.amount,
- fluid = data.fluid
- }
- else
- for key, value in pairs(data) do
- if type(value) == "table" and value.capacity and value.amount and value.fluid then
- local fluidType = getFluidType(value.fluid)
- tanksByFluid[fluidType] = {
- capacity = value.capacity,
- amount = value.amount,
- fluid = value.fluid
- }
- end
- end
- end
- displayTankData(monitor, tanksByFluid)
- else
- print("Ungültige Nachricht empfangen. Nachricht: " .. tostring(message)) -- Ausführlichere Fehlerausgabe
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment