Advertisement
zkb1325

Minecraft Computer Craft Visitor Log

Apr 4th, 2024 (edited)
607
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.45 KB | None | 0 0
  1. -- The two corner points of an area
  2. AreaCoords = {{x=6187, y=0, z=-10215}, {x=6078, y=255, z=-10116}}
  3. -- List of player usernames to not log
  4. BlackListPlayers = {"zkb1325"}
  5.  
  6. local pd = peripheral.find("playerDetector")
  7.  
  8. if fs.exists("visitorLog.txt") then
  9.     local visitorFile = fs.open("visitorLog.txt", "r")
  10.     VisitorLog = visitorFile.readAll()
  11.     visitorFile.close()
  12.     VisitorLog = textutils.unserialize(VisitorLog)
  13. else
  14.     VisitorLog = {}
  15. end
  16. CurrentlyVisiting = {}
  17.  
  18. local function inArray(value, table)
  19.     for i,v in ipairs(table) do
  20.         if v == value then return true end
  21.     end
  22.  
  23.     return false
  24. end
  25.  
  26. local function saveVisitorLog()
  27.     local visitorFile = fs.open("visitorLog.txt", "w")
  28.     visitorFile.write(textutils.serialize(VisitorLog))
  29.     visitorFile.close()
  30. end
  31.  
  32. local function currentVisitors()
  33.     -- return pd.getPlayersInRange(2)
  34.     return pd.getPlayersInCoords(AreaCoords[1], AreaCoords[2])
  35. end
  36.  
  37. local function updateActive()
  38.     for i,v in pairs(CurrentlyVisiting) do
  39.         if CurrentlyVisiting[i]["active"] and not inArray(i, currentVisitors()) then
  40.             CurrentlyVisiting[i]["active"] = false
  41.             table.insert(VisitorLog[i][CurrentlyVisiting[i]["lastIndex"]], 2, os.date("%b %d %G %I:%M:%S %p"))
  42.             table.insert(VisitorLog[i][CurrentlyVisiting[i]["lastIndex"]], 3, os.clock() - CurrentlyVisiting[i]["visitedAt"])
  43.             -- VisitorLog[i][CurrentlyVisiting[i]["lastDate"]] = os.date("%b %d %G %I:%M:%S %p")
  44.             saveVisitorLog()
  45.             print(i.." has left the area at "..os.date("%b %d %G %I:%M:%S %p"))
  46.         end
  47.     end
  48. end
  49.  
  50. while true do
  51.     sleep(1)
  52.     for i,v in ipairs(currentVisitors()) do
  53.         if not inArray(v, BlackListPlayers) then
  54.             if not CurrentlyVisiting[v] then
  55.                 CurrentlyVisiting[v] = {}
  56.             end
  57.             if not VisitorLog[v] then
  58.                 VisitorLog[v] = {}
  59.             end
  60.             if not CurrentlyVisiting[v]["active"] then
  61.                 CurrentlyVisiting[v]["active"] = true
  62.                 CurrentlyVisiting[v]["visitedAt"] = os.clock()
  63.                 CurrentlyVisiting[v]["lastIndex"] = #VisitorLog[v]+1
  64.                 VisitorLog[v][CurrentlyVisiting[v]["lastIndex"]] = {os.date("%b %d %G %I:%M:%S %p")}
  65.                 saveVisitorLog()
  66.                 print(v.." has entered the area at "..os.date("%b %d %G %I:%M:%S %p"))
  67.             end
  68.         end
  69.     end
  70.     updateActive()
  71. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement