## Looping clock checking to see if a player is in range to use the teleport pad
## Runs from controller.animation.teleport_pad.detect_player
## @s = ldz:teleport_pad


scoreboard objectives add TPInRange dummy
scoreboard objectives add TPTimer dummy
scoreboard objectives add TPBuffer dummy
scoreboard players add @a TPBuffer 0
#scoreboard objectives setdisplay sidebar TPTimer

scoreboard players set @s TPInRange 0
execute positioned ~-1.5 ~ ~-1.5 if entity @a[dx=2,dy=2,dz=2,scores={TPBuffer=0}] run scoreboard players set @s TPInRange 1

scoreboard players add @s[scores={TPInRange=1}] TPTimer 1

execute as @s[scores={TPTimer=1}] run stopsound @a[r=16] entity.teleport_pad.trigger
execute as @s[scores={TPTimer=5}] run playsound entity.teleport_pad.trigger @a ~ ~ ~
execute as @s[scores={TPTimer=5}] run event entity @s set_trigger_to_1
execute as @s[scores={TPTimer=60}] run playsound entity.teleport_pad.warp @a[r=16] ~ ~ ~
execute as @s[scores={TPTimer=60}] run event entity @s set_trigger_to_0
execute positioned ~-1.5 ~ ~-1.5 as @s[scores={TPTimer=60}] run scoreboard players set @a[dx=2,dy=3,dz=2] TPBuffer 1

execute positioned ~-1.5 ~ ~-1.5 as @s[family=beast_pad_hub,scores={TPTimer=60}] run teleport @a[dx=2,dy=3,dz=2] 73 160.5 -475 0 0
execute positioned ~-1.5 ~ ~-1.5 as @s[family=beast_pad_dungeon,scores={TPTimer=60}] run teleport @a[dx=2,dy=3,dz=2] 104 149.5 7 0 0

execute positioned ~-1.5 ~ ~-1.5 as @s[family=mystic_pad_hub,scores={TPTimer=60}] run teleport @a[dx=2,dy=2,dz=2] -512 169.5 -196 45 0
execute positioned ~-1.5 ~ ~-1.5 as @s[family=mystic_pad_dungeon,scores={TPTimer=60}] run teleport @a[dx=2,dy=2,dz=2] 70 157.5 31 270 0

execute positioned ~-1.5 ~ ~-1.5 as @s[family=scourge_pad_hub,scores={TPTimer=60}] run teleport @a[dx=2,dy=2,dz=2] 6 171.5 436 270 0
execute positioned ~-1.5 ~ ~-1.5 as @s[family=scourge_pad_dungeon,scores={TPTimer=60}] run teleport @a[dx=2,dy=2,dz=2] 104 149.5 55 180 0

execute positioned ~-1.5 ~ ~-1.5 as @s[family=wraith_pad_hub,scores={TPTimer=60}] run teleport @a[dx=2,dy=3,dz=2] 591 171.5 50 90 0
execute positioned ~-1.5 ~ ~-1.5 as @s[family=wraith_pad_dungeon,scores={TPTimer=60}] run teleport @a[dx=2,dy=3,dz=2] 138 157.5 31 90 0

#execute as @s[scores={TPTimer=60}] at @a[scores={TPBuffer=1}] run teleport @a[scores={TPBuffer=1}] ~ ~0.5 ~

execute as @s[scores={TPTimer=60}] run scoreboard players set @s TPTimer 61

scoreboard players add @a[scores={TPBuffer=1..}] TPBuffer 1
execute positioned ~-1.5 ~ ~-1.5 run scoreboard players set @a[scores={TPBuffer=1..},dx=2,dy=2,dz=2] TPBuffer 1
scoreboard players reset @a[scores={TPBuffer=10..}] TPBuffer

scoreboard players reset @s[scores={TPTimer=70..}] TPTimer
execute as @s[scores={TPTimer=1..,TPInRange=0}] run event entity @s set_trigger_to_0
execute as @s[scores={TPTimer=5..,TPInRange=0}] run stopsound @a[r=16] entity.teleport_pad.trigger
scoreboard players reset @s[scores={TPInRange=0}] TPTimer