execute unless entity @e[type=minecraft:minecart,distance=..1] run scoreboard players set @p in_minecart 0
advancement revoke @p only niknok:technical/on_a_rail_ad1



execute if score @p in_minecart matches 0 run execute as @e[type=minecart] at @e[distance=0.1..0.400] run scoreboard players set @p in_minecart 1


execute if score @p in_minecart matches 1..2 store result score @p minecart_input_x run data get entity @p Pos[0]
execute if score @p in_minecart matches 1 run execute store result score @p minecart_dest_x run data get entity @p Pos[0]
execute if score @p in_minecart matches 1 run execute store result score @p minecart_dest_x run data get entity @p Pos[0]

execute if score @p in_minecart matches 1..2 store result score @p minecart_input_z run data get entity @p Pos[2]
execute if score @p in_minecart matches 1 run execute store result score @p minecart_dest_z run data get entity @p Pos[2]
execute if score @p in_minecart matches 1 run execute store result score @p minecart_dest_z run data get entity @p Pos[2]

execute if score @p in_minecart matches 1..2 store result score @p minecart_inp_xn run data get entity @p Pos[0]
execute if score @p in_minecart matches 1 run execute store result score @p minecart_dest_xn run data get entity @p Pos[0]
execute if score @p in_minecart matches 1 run execute store result score @p minecart_dest_xn run data get entity @p Pos[0]

execute if score @p in_minecart matches 1..2 store result score @p minecart_inp_zn run data get entity @p Pos[2]
execute if score @p in_minecart matches 1 run execute store result score @p minecart_dest_zn run data get entity @p Pos[2]
execute if score @p in_minecart matches 1 run execute store result score @p minecart_dest_zn run data get entity @p Pos[2]

execute if score @p in_minecart matches 0 run scoreboard players set @p minecart_dest_x 0
execute if score @p in_minecart matches 0 run scoreboard players set @p minecart_dest_z 0

execute if score @p in_minecart matches 0 run scoreboard players set @p minecart_dest_xn 0
execute if score @p in_minecart matches 0 run scoreboard players set @p minecart_dest_zn 0

execute if score @p in_minecart matches 1 run scoreboard players remove @p minecart_dest_x 1000
execute if score @p in_minecart matches 1 run scoreboard players remove @p minecart_dest_z 1000
execute if score @p in_minecart matches 1 run scoreboard players add @p minecart_dest_xn 1000
execute if score @p in_minecart matches 1 run scoreboard players add @p minecart_dest_zn 1000

execute if score @p in_minecart matches 1 run scoreboard players set @p in_minecart 2

execute if score @p in_minecart matches 2 run execute if score @p minecart_input_x < @p minecart_dest_x run scoreboard players set @p in_minecart 3
execute if score @p in_minecart matches 2 run execute if score @p minecart_input_z < @p minecart_dest_z run scoreboard players set @p in_minecart 3
execute if score @p in_minecart matches 2 run execute if score @p minecart_inp_xn > @p minecart_dest_xn run scoreboard players set @p in_minecart 3
execute if score @p in_minecart matches 2 run execute if score @p minecart_inp_zn > @p minecart_dest_xn run scoreboard players set @p in_minecart 3

execute if score @p in_minecart matches 3 run advancement grant @p only niknok:minecraft/on_a_rail