--Finally we're opening up the gates
--//Init
local sensor = peripheral.wrap("left")
--//Variables
local transmitter = "right" -- Set this to the side that controls the gate open/close
local receiver = "back" -- Set this to the side you want to check redstone on to keep the gate open
local delay = 10 -- The frequency of checking for players
local whiteList = {
[1] = "Queen Elsa of Arendelle",
[2] = "Princess Anna of Arendelle",
[3] = "Kristoff",
[4] = "Olaf",
[5] = "Sven",
[6] = "Wandering Oaken",
[7] = "Marshmallow",
}
local blackList = {
[1] = "Hans of the Southern Isles",
[2] = "The Duke of Weselton",
}
--//postInit
--//Functions
function checkPlayerPriveleges(id) -- Scans player list against blacklist and then whitelist to see if the door can be opened
print("Blacklist Check:")
for i,v in pairs(blackList) do
print("\tChecking for "..v)
for k,w in pairs(id) do
if v == w then
print("\tNaughty people detected, locking gate")
return false
end
end
end
print("Whitelist Check:")
for i,v in pairs(whiteList) do
print("\tChecking for "..v)
for k,w in pairs(id) do
if v == w then
print("\tNice person detected, finalising security check")
return true, w
end
end
end
return false
end
function isInRange(player) -- Ensures that the player is in range (necessary in my case because of the way the door was laid out. Range is determined as distance from the sensor in meters.
print(player)
print("Hello")
local playerData = sensor.getPlayerData(player)
local position = playerData["position"]
if position["y"] == 2 and position["z"] > -4 then
if (position["x"] < -2 or position["x"] > 4) and position["z"] < 2 then
return false
else
return true
end
else
return false
end
end
function openDoor(player) -- Opens door for up to 10 seconds
print("Opening door for "..player)
redstone.setOutput(transmitter, true)
sleep(5)
local playerIDs = sensor.getPlayerNames() -- I should probably have deleted this line it seems unnecessary
for i = 5,1,-1 do
local playerIDs = sensor.getPlayerNames()
local stillInRange = false
for i,v in pairs(playerIDs) do
if v == player then
stillInRange = true
end
end
if stillInRange then
print(player.." still in range; keeping door open")
sleep(1)
else
print("Player out of range; closing gate")
redstone.setOutput(transmitter, false)
return
end
print("Countdown remaining: "..i)
sleep(1)
end
print("Countdown elapsed; auto closing gate")
redstone.setOutput(transmitter, false)
end
function mainProg() -- Main handler for getting nearby player list and getting there privleges and ensuring they are in range
print("Scanning the gate zone...")
local playerIDs = sensor.getPlayerNames()
if #playerIDs > 0 then
local playerCheck = {checkPlayerPriveleges(playerIDs)}
if playerCheck[1] then
local finalCheck = isInRange(playerCheck[2])
if finalCheck then
print(playerCheck[2].." is in range")
openDoor(playerCheck[2])
else
print(playerCheck[2].." is not in range")
end
end
else
print("No players in range")
end
end
--//Main Prog
--sleep(8)
while true do
term.clear()
term.setCursorPos(1,1)
print("Gate Controller")
if redstone.getInput(receiver) then
mainProg()
else -- This will leave the door open based on the way I had redstone set up.
print("Under Queen Elsa's and Princess Anna's orders, the gates are to remain open for the foreseeable future.")
end
sleep(delay)
end