Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local settings = {
- initialState = true;--Either normal or inverted
- mode = "whitelist";--Whitelist, locked, or unlocked
- redstoneSide = "bottom";--Which side the redstone connects on
- sensorSide = "right";--Which side your sensor is on.
- doorTimer = 3;--How long the door should alter state
- checkTimer = 0.5;--Interval to check for players
- configFile = config.cfg;--Config file to be saved for reboots
- bundledCable = true;--Use bundled cable or regular redstone output?
- }
- local accessList = {--Users that should have access when in whitelist mode
- "monsterror";
- }
- --Main functions
- function saveSettings() --Will save the different tables to a single file
- if fs.exists(settings.configFile) then
- fs.delete(settings.configFile)
- end
- cfg = fs.open(setting.configFile, "w")
- cfg.write(textutils.serialize(settings))
- cfg.write("\n")
- cft.write(textutils.serialize(accessList))
- cfg.write("\n")
- cfg.close()
- end
- function load() --Will load the config to keep information over reboot
- if fs.exists(settings.configFile) then
- cfg = fs.open(settings.configFile, "r")
- local content = cfg.readAll()
- cfg.close()
- local content = split(content)
- settings = textutils.unserialize(content[1])
- accessList = textutils.unserialize(content[2])
- end
- end
- function setSides() --Reads settings to determine sensor and output sides
- sensor = peripheral.wrap(settings.sensorSide)
- output = settings.redstoneSide
- end
- function getUsernames() --Returns a table of users close to the sensor
- return sensor.getPlayers()
- end
- function getUserData(name) --Returns data for a specific user
- return sensor.getPlayerByName(name)
- end
- function getUserLocation(name) --Returns a table of a given user's XYZ in relation to terminal
- return sensor.getPlayerByName(name).position
- end
- function hasAccess(name) --Returns true if player has access
- for index, username in pairs(accessList) do
- if username == name then
- return true
- end
- end
- return false
- end
- function addAccess(name) --Adds a username to the access table
- if hasAccess(name) then return true end
- accessList[#accessList+1] = name
- if hasAccess(name) then
- return true
- else
- return false
- end
- end
- function removeAccess(name) --Removes a username from the access table
- if hasAccess(name) then
- for index,username in pairs(accessList) do
- if username == name then
- table.remove(accessList, index)
- break
- end
- end
- if not hasAccess(name) then
- return true
- else
- return false
- end
- else
- return false
- end
- end
- function isOnLocation(user)--Returns true if a user is standing on a tile for the door
- local x = getUserLocation(user).x
- local z = getUserLocation(user).z
- local y = getUserLocation(user).y
- if y > 2 and y < 5 then
- if z > -1 and z < 1 then
- if x > -1 and x < 2 then
- return true
- else
- return false
- end
- else
- return false
- end
- else
- return false
- end
- end
- function open() -- Sets the output signal to 1 if initial state is true
- if settings.bundledCable then
- local num = settings.initialState and 0 or 1
- rs.setBundledOutput(settings.redstoneSide, num)
- else
- local num = settings.initialState and false or true
- rs.setOutput(settings.redstoneSide, num)
- end
- return os.startTimer(settings.doorTimer)
- end
- function close() -- Sets the bundled redstone to 1 if the initial state is true
- if settings.bundledCable then
- local num = settings.initialState and 1 or 0
- rs.setBundledOutput(settings.redstoneSide, num)
- else
- local num = settings.initialState and true or false
- rs.setOutput(settings.redstoneSide, num)
- end
- end
- function split(inputstr, sep) -- splits a string into a table
- if sep == nil then
- sep = "%s"
- end
- t={} ; i=1
- for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
- t[i] = str
- i = i + 1
- end
- return t
- end
- function doorCheck() -- Opens the door if the mode allows that player to open the door and player is on location
- users = getUsernames()
- if users then
- for index, username in pairs(users) do
- if settings.doorMode == "all" then
- timers.timerDoor = open()
- return true
- end
- end
- elseif hasAccess(username) then
- if isOnLocation(username) then
- timers.timerDoor = open()
- return true
- end
- else
- return false
- end
- end
- end
- load()
- close()
- timers = {
- timerDoor = false;--Will contain the os.timers so that we can check when we hit one of them
- timerCheck = false;
- }
- term.clear()
- term.setCursorPos(1,1)
- while true do
- if not timers.timerCheck then
- timers.timerCheck = os.startTimer(settings.checkDoorTimer)
- end
- e = {os.pullEvent()}
- if e[1] == "timer" and settings.doorMode ~= "disabled" then
- if e[2] == timers.timerDoor then
- close()
- timers.timerDoor = false
- elseif e[2] == timers.timerCheck then
- timers.timerCheck = false
- doorCheck()
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement