AnonymusHochgenuss

playerdetectlog

Mar 23rd, 2026 (edited)
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. -- Konfiguration
  2. local logFile = "player_log.txt"
  3. local radius = 20
  4. local interval = 0.5
  5.  
  6. local detector = peripheral.find("playerDetector")
  7. if not detector then error("Player Detector nicht gefunden!") end
  8.  
  9. local lastPlayers = {}
  10.  
  11. -- Funktion: In Datei schreiben
  12. local function writeToLog(message)
  13. local file = fs.open(logFile, "a") -- "a" für Append (Anhängen)
  14. file.writeLine(message)
  15. file.close()
  16. end
  17.  
  18. -- Funktion: Monitor aktualisieren
  19. local function updateMonitor()
  20. local mon = peripheral.find("monitor")
  21. if not mon then return end -- Kein Monitor? Dann einfach nichts tun.
  22.  
  23. mon.clear()
  24. local w, h = mon.getSize()
  25.  
  26. -- Datei einlesen
  27. if not fs.exists(logFile) then return end
  28. local file = fs.open(logFile, "r")
  29. local lines = {}
  30. local line = file.readLine()
  31. while line do
  32. table.insert(lines, line)
  33. line = file.readLine()
  34. end
  35. file.close()
  36.  
  37. -- Nur die letzten 'h' Zeilen anzeigen (passend zur Monitorhöhe)
  38. local startLine = math.max(1, #lines - h + 1)
  39. local currentY = 1
  40.  
  41. mon.setCursorPos(1, 1)
  42. for i = startLine, #lines do
  43. mon.setCursorPos(1, currentY)
  44. mon.write(lines[i]:sub(1, w)) -- Text kürzen, falls Monitor zu schmal
  45. currentY = currentY + 1
  46. end
  47. end
  48.  
  49. -- Hilfsfunktion: Spieler-Check
  50. local function isNameInList(list, name)
  51. for _, n in ipairs(list) do if n == name then return true end end
  52. return false
  53. end
  54.  
  55. -- Start-Setup
  56. term.clear()
  57. term.setCursorPos(1,1)
  58. print("Logger gestartet. Datei: " .. logFile)
  59. updateMonitor() -- Initialanzeige
  60.  
  61. while true do
  62. local currentPlayers = detector.getPlayersInRange(radius)
  63. local realTime = os.date("%H:%M:%S")
  64. local mcDay = os.day()
  65. local changed = false
  66.  
  67. -- Beitritt prüfen
  68. for _, name in ipairs(currentPlayers) do
  69. if not isNameInList(lastPlayers, name) then
  70. local msg = ("[%s] [Tag %d] + %s"):format(realTime, mcDay, name)
  71. print(msg)
  72. writeToLog(msg)
  73. changed = true
  74. end
  75. end
  76.  
  77. -- Verlassen prüfen
  78. for _, name in ipairs(lastPlayers) do
  79. if not isNameInList(currentPlayers, name) then
  80. local msg = ("[%s] [Tag %d] - %s"):format(realTime, mcDay, name)
  81. print(msg)
  82. writeToLog(msg)
  83. changed = true
  84. end
  85. end
  86.  
  87. -- Wenn sich etwas geändert hat, Monitor sofort aktualisieren
  88. if changed then
  89. updateMonitor()
  90. end
  91.  
  92. lastPlayers = currentPlayers
  93. sleep(interval)
  94. end
Advertisement
Add Comment
Please, Sign In to add comment