isama92

OpenPeripheral Automatic Door Detecting Players

Jun 3rd, 2016
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.27 KB | None | 0 0
  1. -- MODIFY IF YOU NEED TO:
  2.  
  3. -- If your door get opened when redstone signal is
  4. -- applied, then set doorOpenState to true, in the
  5. -- other case, set it false.
  6. local doorOpenedState = true
  7. -- Set sensorPosition with the side where the
  8. -- sensor is.
  9. -- Available: top, bottom, left, right, front and back
  10. local sensorPosition  = "right"
  11. -- Set doorPosition with the side where you want
  12. -- the redstone signal to be outputted.
  13. -- Available: top, bottom, left, right, front and back
  14. local doorPosition    = "left"
  15. -- These variables regulate how much close you
  16. -- have to be to the sensor to get the door opened.
  17. -- In this case, if you are more than 3 block away
  18. -- from the sensor, the door will stay closed.
  19. xDistance1 = -3
  20. xDistance2 = 3
  21. yDistance1 = -3
  22. yDistance2 = 3
  23. zDistance1 = -3
  24. zDistance2 = 3
  25. -- If a player is in this list, the door
  26. -- will get opened for him.
  27. -- These names are examples, you should delete
  28. -- them and add only the names you want.
  29. local playersAllowed =  {
  30.                             "isama92",
  31.                             "Al",
  32.                             "John",
  33.                             "Jack",
  34.                             "Peter",
  35.                         }
  36.  
  37.  
  38. -- DON'T MODIFY UNLESS YOU KNOW WHAT YOU ARE DOING!
  39.  
  40. -- INIZIALIZATIONS
  41. local p = peripheral.wrap(sensorPosition)
  42. local door = false  -- if false the door is closed
  43. local found
  44. local playerName
  45. redstone.setOutput(doorPosition, (not doorOpenedState))
  46.  
  47. -- CHECK DOOR
  48. function CheckDoor()
  49.     if door == false then
  50.         redstone.setOutput(doorPosition, (not doorOpenedState))
  51.     else
  52.         redstone.setOutput(doorPosition, doorOpenedState)
  53.     end
  54. end
  55.  
  56. -- CHECK PLAYER POSITION
  57. function CheckPlayerPosition()
  58.     pos = p.getPlayerByName(playerName).all().position
  59.     if pos.x >= xDistance1 and pos.x <= xDistance2
  60.     and pos.y >= yDistance1 and pos.y <= yDistance2
  61.     and pos.z >= zDistance1 and pos.z <= zDistance2 then
  62.         for i, playerAllowed in ipairs(playersAllowed) do
  63.             if playerAllowed == playerName then
  64.                 found = true
  65.             end
  66.         end
  67.     end
  68. end
  69.  
  70. -- CHECK PLAYER
  71. function CheckPlayers()
  72.     found = false
  73.     if #p.getPlayers() > 0 then
  74.         for i,player in ipairs(p.getPlayers()) do
  75.             playerName = player.name
  76.             pcall(CheckPlayerPosition)
  77.         end
  78.     end
  79.     if found == true then
  80.         door = true
  81.     else
  82.         door = false
  83.     end
  84.     CheckDoor()
  85. end
  86.  
  87. -- MAIN
  88. while true do
  89.     sleep(0.1)
  90.     CheckPlayers()
  91. end
Advertisement
Add Comment
Please, Sign In to add comment