snirkimmington

Sneak Door (OpenPeripherals)

Mar 9th, 2014
1,010
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.59 KB | None | 0 0
  1. -- Sneak [Proximity] Door by snirkimmington. Doors that you can sneak in front of to open them!
  2. -- Requires OpenPeripheral and Sensor block. If you want regular proximity doors, see http://pastebin.com/N5TgZh0x.
  3. -- To use, edit the next 5 lines. (If you want, you can add to the sleep times.)
  4. -- The min/max X and Y is a <= or >= check and the numbers are relative to the player.
  5. -- Output is where to output a signal to, and initial is what the signal should be when untoggled.
  6. local minX = -2 local maxX =  2
  7. local minY = -2 local maxY =  2
  8. local minZ = -2 local maxZ =  2
  9. local sensor = peripheral.wrap('left') -- Where the sensor is
  10. local output = 'right' -- Where to output
  11. local redstone_level = 15
  12. local initial_signal = true -- Default signal
  13. -- Begin code --
  14. -- If you would like, you can edit the check on line 39 to, say, data.isBlocking or something. You could even check for holding a specific item in the inventory.
  15. local toggle = false
  16. local current_signal = initial_signal
  17. -- Set initial redstone level
  18. if initial_signal then
  19.     redstone.setAnalogOutput(output, redstone_level)
  20. end
  21. -- Infinite loop
  22. while true do
  23.     -- Get the players from the sensor.
  24.     local player_namess = sensor.getPlayers()
  25.     -- If there are no players around
  26.     if #players == 0 then
  27.         -- If there are no players, it shouldn't be toggled or do any checks.
  28.         toggle = false
  29.         sleep(2)
  30.     else -- There are players
  31.         -- We need to make sure anyone can open the doors.
  32.         local any = false
  33.         for _, player in pairs(players) do
  34.             -- Yes, a null check is required.
  35.             -- You can replace with "if name == your name" to be you-only.
  36.             if player ~= '' then
  37.                 -- Get position data
  38.                 -- So this method fails sometimes.
  39.                 -- pcall() doesn't seem to work.
  40.                 --- You will need to call a wrapper script.
  41.                 local data = sensor.getPlayerByUUID(player.uuid)
  42.                 if data ~= nil and data.all ~= nil then
  43.                     local pos = data.all().position
  44.                     -- Check if the player is in range
  45.                     if pos.x >= minX and pos.x <= maxX
  46.                         and pos.y >= minY and pos.y <= maxY
  47.                         and pos.z >= minZ and pos.z <= maxZ
  48.                         and data.living.isSneaking == true then
  49.                         any = true
  50.                         break
  51.                     end
  52.                 end
  53.             end
  54.         end
  55.         if any then -- shifting players found
  56.             if not toggle then
  57.                 toggle = true
  58.                 if current_signal then
  59.                     redstone.setAnalogOutput(output, 0)
  60.                 else
  61.                     redstone.setAnalogOutput(output, redstone_level)
  62.                 end
  63.                 current_signal = not current_signal
  64.             end
  65.         else -- no shifting
  66.             toggle = false
  67.         end
  68.         -- If we detect any players, we should sleep less between sweeps.
  69.         sleep(0.5)
  70.     end
  71. end
Advertisement
Add Comment
Please, Sign In to add comment