Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.unloadAPI("ocs/apis/sensor")
- os.loadAPI("ocs/apis/sensor")
- local prox = sensor.wrap("top")
- local modem = peripheral.wrap("right")
- local playerName = ""
- local playerNames = {}
- local secretPhrase = ""
- local portNum = ""
- local answer = ""
- local inputting
- local checking
- local heldItemCheck
- local item
- local heldItem
- local b = 0
- --This function checks what item the player is holding
- function itemCheck(name)
- local targets = prox.getTargets()
- --Read data from sensor
- for k, v in pairs(targets) do
- if ((v.Name) == "Player") then
- targetsDetail = prox.getTargetDetails(k)
- if not(targetsDetail == nil) then
- if name == targetsDetail.Username then
- heldItem = targetsDetail.HeldItem.Name
- return heldItem
- end
- end
- end
- end
- end
- --This functions checks our array "playerNames" which contains the players
- --we allowed access to this door
- --If the for loops finds a match, it will open the door
- function arrayCheck(playerNames,playerName,secretPhrase,portNum,item,heldItem)
- local port = tonumber(portNum)
- for key, value in pairs(playerNames) do
- if value == playerName and item == nil then
- modem.transmit(port,524,secretPhrase)
- elseif value == playerName and item == heldItem then
- modem.transmit(port,524,secretPhrase)
- end
- end
- modem.transmit(port,524,"not_player")
- end
- --This checks the names of the players near the sensor
- --and then sends this daya to the "arrayCheck" function
- function nameCheck(secretPhrase,playerNames,portNum,item)
- local targets = prox.getTargets()
- --Read data from sensor
- for k, v in pairs(targets) do
- if ((v.Name) == "Player") then
- targetsDetail = prox.getTargetDetails(k)
- if not(targetsDetail == nil) then
- playerName = targetsDetail.Username
- heldItem = targetsDetail.HeldItem.Name
- end
- end
- end
- arrayCheck(playerNames,playerName,secretPhrase,portNum,item,heldItem)
- targets = nil
- targetsDetail = nil
- playerName = nil
- end
- --This functions checks if there is a file named "proxInfo"
- --If there is, it will tell the program to just read from that
- --if there is no file it will tell the program we need to make one
- function fileCheck(name)
- local f = fs.open(name, "r")
- if f~=nil then
- repeat
- local line = f.readLine()
- if tostring(line) == "END" then
- f.close()
- sleep(1)
- return true
- end
- until line == nil
- f.close()
- sleep(1)
- return false
- else
- return false
- end
- end
- --The main loop of the program on startup
- while true do
- modem.closeAll()
- if fileCheck("proxInfo") then
- inputting = false
- answer = "y"
- checking = false
- else
- inputting = true
- checking = true
- end
- while inputting == true do
- term.clear()
- term.setCursorPos(1,1)
- write("Enter a secret phrase: ")
- secretPhrase = read()
- -----------------------------------
- term.clear()
- term.setCursorPos(1,1)
- write("Enter port number \n(can't be greater than 65535): ")
- portNum = read()
- while (tonumber(portNum) > 65535) do
- term.clear()
- term.setCursorPos(1,1)
- write("Please enter a vaild port number \n(can't be greater than 65535): ")
- portNum = read()
- end
- -----------------------------------
- term.clear()
- term.setCursorPos(1,1)
- write("Do you want to have the\ndoor open when holding a\nspecific item like a key (y/n)?: ")
- heldItemCheck = read()
- if heldItemCheck == "y" then
- term.clear()
- term.setCursorPos(1,1)
- write("Please enter your name so the\nsensor knows who to look for: ")
- local myName = read()
- term.clear()
- term.setCursorPos(1,1)
- write("Please hold the item you wish\nto be a 'key' for the door\nand then push enter...")
- sleep(1)
- local dummy = read()
- item = itemCheck(myName)
- end
- -----------------------------------
- term.clear()
- term.setCursorPos(1,1)
- print("Enter the names seperated by a space\nof the players you want to open this door")
- write("Player Names: ")
- local userInput = read()
- for i in string.gmatch(userInput, "%S+") do
- table.insert(playerNames, i)
- end
- sleep(1)
- term.clear()
- term.setCursorPos(1,1)
- print("Your secret phrase is: "..secretPhrase)
- print("Your port number is: "..portNum)
- if heldItemCheck == "y" then
- print("Your key is: "..item)
- end
- print("The player(s) allowed is/are:")
- for d=1,# playerNames do
- print(playerNames[d])
- end
- sleep(1)
- print("Is this the correct information (y/n)? ")
- answer = read()
- if answer == "y" then
- h = fs.open("proxInfo", "w")
- h.writeLine(secretPhrase)
- h.writeLine(portNum)
- if heldItemCheck == "y" then
- h.writeLine("ITEM")
- h.writeLine(item)
- else
- h.writeLine("")
- end
- for a=1,# playerNames do
- h.writeLine(playerNames[a])
- end
- h.writeLine("END")
- h.close()
- checking = false
- inputting = false
- else
- for d=1,# playerNames do
- table.remove(playerNames,d)
- end
- inputting = true
- checking = true
- end
- end
- -----------------------------------
- --We keep looping through this once we have a file
- while answer == "y" and checking == false do
- print("Starting the program...")
- h = fs.open("proxInfo", "r")
- secretPhrase = h.readLine()
- portNum = h.readLine()
- if h.readLine() == "ITEM" then
- item = h.readLine()
- else
- item = nil
- end
- local x = 0
- repeat
- x = x + 1
- ln = h.readLine()
- playerNames[x] = ln
- until ln == "END"
- -----------------------------------
- --print(secretPhrase)
- --print(portNum)
- --print(item)
- --for i=1,# playerNames do
- -- print(playerNames[i])
- --end
- -----------------------------------
- h.close()
- sleep(3)
- term.clear()
- term.setCursorPos(1,1)
- print("If you want to change any settings place")
- print("a lever in front of this computer and pull it.")
- print("You will be prompted if you wish to edit")
- print("the settings. Don't forget to remove")
- print("the lever or toggle it again when you are done!")
- while true do
- nameCheck(secretPhrase,playerNames,portNum,item)
- if rs.getInput("front") == true then
- term.clear()
- term.setCursorPos(1,1)
- print("Would you like to edit the settings (y/n)? ")
- sleep(1)
- local editCheck = read()
- if editCheck == "y" then
- sleep(1)
- fs.delete("proxInfo")
- os.reboot()
- else
- sleep(1)
- term.clear()
- term.setCursorPos(1,1)
- print("If you want to change any settings place")
- print("a lever in front of this computer and pull it.")
- print("You will be prompted if you wish to edit")
- print("the settings. Don't forget to remove")
- print("the lever or toggle it again when you are done!")
- end
- end
- sleep(2)
- end
- end
- end
- --Code created by ThatGamingGnome
- --Check out my youtube channel @ http://www.youtube.com/user/ThatGamingGnome
Add Comment
Please, Sign In to add comment