Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Konfiguration
- local logFile = "player_log.txt"
- local radius = 20
- local interval = 0.5
- local detector = peripheral.find("playerDetector")
- if not detector then error("Player Detector nicht gefunden!") end
- local lastPlayers = {}
- -- Funktion: In Datei schreiben
- local function writeToLog(message)
- local file = fs.open(logFile, "a") -- "a" für Append (Anhängen)
- file.writeLine(message)
- file.close()
- end
- -- Funktion: Monitor aktualisieren
- local function updateMonitor()
- local mon = peripheral.find("monitor")
- if not mon then return end -- Kein Monitor? Dann einfach nichts tun.
- mon.clear()
- local w, h = mon.getSize()
- -- Datei einlesen
- if not fs.exists(logFile) then return end
- local file = fs.open(logFile, "r")
- local lines = {}
- local line = file.readLine()
- while line do
- table.insert(lines, line)
- line = file.readLine()
- end
- file.close()
- -- Nur die letzten 'h' Zeilen anzeigen (passend zur Monitorhöhe)
- local startLine = math.max(1, #lines - h + 1)
- local currentY = 1
- mon.setCursorPos(1, 1)
- for i = startLine, #lines do
- mon.setCursorPos(1, currentY)
- mon.write(lines[i]:sub(1, w)) -- Text kürzen, falls Monitor zu schmal
- currentY = currentY + 1
- end
- end
- -- Hilfsfunktion: Spieler-Check
- local function isNameInList(list, name)
- for _, n in ipairs(list) do if n == name then return true end end
- return false
- end
- -- Start-Setup
- term.clear()
- term.setCursorPos(1,1)
- print("Logger gestartet. Datei: " .. logFile)
- updateMonitor() -- Initialanzeige
- while true do
- local currentPlayers = detector.getPlayersInRange(radius)
- local realTime = os.date("%H:%M:%S")
- local mcDay = os.day()
- local changed = false
- -- Beitritt prüfen
- for _, name in ipairs(currentPlayers) do
- if not isNameInList(lastPlayers, name) then
- local msg = ("[%s] [Tag %d] + %s"):format(realTime, mcDay, name)
- print(msg)
- writeToLog(msg)
- changed = true
- end
- end
- -- Verlassen prüfen
- for _, name in ipairs(lastPlayers) do
- if not isNameInList(currentPlayers, name) then
- local msg = ("[%s] [Tag %d] - %s"):format(realTime, mcDay, name)
- print(msg)
- writeToLog(msg)
- changed = true
- end
- end
- -- Wenn sich etwas geändert hat, Monitor sofort aktualisieren
- if changed then
- updateMonitor()
- end
- lastPlayers = currentPlayers
- sleep(interval)
- end
Advertisement
Add Comment
Please, Sign In to add comment