Derek1017

Door Lock - CC Sensor

Jun 19th, 2015
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.57 KB | None | 0 0
  1. -- When this program gets a redstone signal, it
  2. -- scans for players. It continues scanning until
  3. -- the redstone has been off for the configured
  4. -- number of seconds.
  5. --
  6. -- While scanning, users on the whitelist will
  7. -- cause a redstone signal to be emitted. Users on
  8. -- the blacklist will prevent the redstone from
  9. -- activating, even when whitelisted players are
  10. -- near.
  11. --
  12. -- Author: MyrddinE
  13. -- 2013-10-16
  14.  
  15. -- Configuration
  16. local side = {sensor="bottom",input="right",output="back"}
  17. local active_when = true
  18. local open_emit = false
  19. local continue_for = 30 -- seconds
  20.  
  21. local file = fs.open("whitelist.dat","r")
  22. local whitelist = textutils.unserialize(file.readAll())
  23. file.close()
  24.  
  25. file = fs.open("blacklist.dat","r")
  26. local blacklist = textutils.unserialize(file.readAll())
  27. file.close()
  28.  
  29. function roomOpen(open)
  30.   if open then
  31.     print("Opening door.")
  32.   else
  33.     print("Closing door.")
  34.   end
  35.  
  36.   -- The '(open == open_emit)' is a logical filter, setting
  37.   -- the output based on whether open = true, or open = false.
  38.   rs.setOutput(side.output,(open == open_emit))
  39. end
  40.  
  41. -- Load sensor api, and connect to it.
  42. os.loadAPI("ocs/apis/sensor")
  43. local players = sensor.wrap(side.sensor)
  44.  
  45. roomOpen(false)
  46.  
  47. -- Infinite loop waiting for redstone change.
  48. while true do
  49.   os.pullEvent("redstone")
  50.  
  51.   -- Initially just wait for 1 second when it
  52.   -- detects a redstone change on any side.
  53.   local stopTime = os.clock() + 1
  54.  
  55.   while os.clock() < stopTime do
  56.     -- If the input side is in 'on mode' (as defined
  57.     -- by the active_when var) then set the end time
  58.     -- to be now + continue_for seconds (default of
  59.     -- 30 seconds).
  60.     if rs.getInput(side.input) == active_when then stopTime = os.clock() + continue_for end
  61.  
  62.     -- Compare all the names of nearby players to the
  63.     -- lists.
  64.     good = false
  65.     bad = false
  66.     targets = players.getTargets()
  67.     for username,_ in pairs(targets) do
  68.       write(username.." is ")
  69.       if whitelist[username] then
  70.         write("whitelisted. ")
  71.         good = true
  72.       elseif blacklist[username] then
  73.         write("blacklisted. ")
  74.         bad = true
  75.       else
  76.         write("unknown player. ")
  77.       end
  78.     end
  79.    
  80.     -- Open/close the door based on nearby listed
  81.     -- players
  82.     if bad then
  83.       roomOpen(false)
  84.     elseif good then
  85.       roomOpen(true)
  86.     else
  87.       roomOpen(false)
  88.     end
  89.    
  90.     write(os.time().." ")
  91.     sleep(1)
  92.   end
  93.  
  94.   -- Rebooting when the detection turns off allows dynamic
  95.   -- re-loading of the software when updated.
  96.   os.reboot()
  97. end
Add Comment
Please, Sign In to add comment