Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[**************************************************************************************
- ** **
- ** Visitor logger v1.1 **
- ** The script saves and display all players in configured range **
- ** **
- ** Author **
- ** discord: om_r#3567 **
- ** email: max.kvy@gmail.com **
- ** **
- ** --------------------------------------------------------------------------- **
- ** If you want to report a bug, suggest any ideas or just to support - use **
- ** credentials above. I appreciate any feedback! **
- ** --------------------------------------------------------------------------- **
- ** **
- ** Download **
- ** "pastebin get QKPAfPkU visitors" and "visitors" to execute **
- ** or "pastebin get QKPAfPkU startup" - to autorun **
- ** **
- ** Usage **
- ** Place "Player Detector" from any side **
- ** (optional) Place "Chat Box" from any side to receiv ig PMs **
- ** **
- ** Settings (After start you will'be asked if you want to change them) **
- ** Range => Player Detector scan range (default: 1000) **
- ** Refresh time => Delay between scans (default: 5) **
- ** Send PM's to username => Player login to whom it sends PM's **
- ** **
- **************************************************************************************--]]
- local scriptConfig = {
- version = 1.1,
- startDelay = 15,
- configPath = 'config',
- playersListPath = 'users'
- }
- local settings = {
- scanRange = 1000,
- refreshDelay = 5,
- sendToChat = false,
- messageToUser = 'none'
- }
- -- Support methods
- local utils = {}
- local detector = peripheral.find("playerDetector")
- local box = peripheral.find("chatBox")
- utils.checkRequirements = function ()
- if detector == nil then
- error("Player Detector required")
- end
- if box ~= nil then
- settings.sendToChat = true
- end
- end
- utils.getTablelength = function (T)
- local count = 0
- for _ in pairs(T) do count = count + 1 end
- return count
- end
- utils.loadTableFromFile = function (path)
- local file = fs.open(path , "r")
- if file == nil then return {} end
- local data = file.readAll()
- file.close()
- return textutils.unserialize(data)
- end
- utils.saveTableToFile = function (table, path)
- local file = fs.open(path , "w")
- file.write(textutils.serialize(table))
- file.close()
- end
- utils.restoreConfig = function ()
- local savedConfig = utils.loadTableFromFile(scriptConfig.configPath)
- if utils.getTablelength(savedConfig) ~= 0 then
- settings = savedConfig
- end
- end
- utils.resetTerminal = function ()
- term.setBackgroundColor(colours.cyan)
- term.clear()
- term.setCursorPos(1, 1)
- end
- utils.readOption = function (message, defaultValue)
- local x, y = term.getCursorPos()
- print(message .. "? Default: " .. defaultValue)
- input = read()
- term.setCursorPos(x, y + 1)
- term.clearLine()
- term.setCursorPos(x, y)
- term.clearLine()
- if input ~= '' then
- return input
- else
- return defaultValue
- end
- end
- -- Main methods
- local app = {}
- app.displayAllDetectedPlayers = function ()
- utils.resetTerminal()
- local savedPlayers = utils.loadTableFromFile(scriptConfig.playersListPath)
- print("Detected Players:")
- print("")
- for key, value in pairs(savedPlayers) do
- print(" => " .. key)
- end
- end
- app.displayConfiguration = function ()
- local x, y = term.getCursorPos()
- local w, h = term.getSize()
- term.setCursorPos(x, h - 2)
- local configure = utils.readOption("Do you want to configure (yes/no) (Use default after " .. scriptConfig.startDelay .. " seconds)", "no")
- if configure == 'yes' then
- settings.scanRange = tonumber(utils.readOption("Range", settings.scanRange))
- settings.refreshDelay = tonumber(utils.readOption("Refresh time", settings.refreshDelay))
- if settings.sendToChat ~= nil then
- settings.messageToUser = utils.readOption("Name of player to send notification", settings.messageToUser)
- end
- term.setCursorPos(x, y)
- utils.saveTableToFile(settings, scriptConfig.configPath)
- app.displayDescription()
- print("")
- print("Saving settings. Wait 3 seconds...")
- os.sleep(3)
- utils.resetTerminal()
- end
- end
- app.displayDescription = function ()
- utils.resetTerminal()
- print("Save all players who visited you base VERSION #" .. scriptConfig.version)
- print("")
- print("Discord: om_r#3567")
- print("--------------------------------------------------")
- print("Current settings:")
- print(" * Range => " .. settings.scanRange .. " blocks")
- print(" * Refresh time => " .. settings.refreshDelay .. " seconds")
- if settings.sendToChat ~= nil then
- settings.sendToChat = settings.messageToUser
- else
- settings.sendToChat = "disabled"
- end
- print(" * Sends PM's to (Chat Box required) => " .. tostring(settings.sendToChat))
- print("--------------------------------------------------")
- print("Use \"edit users.lua\" to show full list")
- end
- app.scanNewPlayers = function ()
- local playersNearby = detector.getPlayersInRange(settings.scanRange)
- local savedPlayers = utils.loadTableFromFile(scriptConfig.playersListPath)
- for k, playerName in pairs(playersNearby) do
- if not savedPlayers[playerName] then
- savedPlayers[playerName] = true
- print('New player on base: ' .. playerName)
- if settings.sendToChat ~= nil then
- box.sendMessageToPlayer('New player on base: ' .. playerName, settings.messageToUser, 'Base Protection Tool')
- end
- end
- end
- utils.saveTableToFile(savedPlayers, scriptConfig.playersListPath)
- end
- app.run = function()
- utils.checkRequirements()
- utils.restoreConfig()
- app.displayDescription()
- parallel.waitForAny(
- function()
- app.displayConfiguration()
- end,
- function()
- sleep(scriptConfig.startDelay)
- end
- )
- while true do
- app.displayAllDetectedPlayers()
- app.scanNewPlayers()
- os.sleep(settings.refreshDelay)
- end
- end
- app.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement