## Recursive function to find the nearest ground and teleport there
## Can be called from anywhere to bring the calling entity to the nearest ground
## @s = entity being brought to the ground

## Every iteration, first we reset the NotGrounded tag, then add it again if there is still air below.
tag @s remove NotGrounded
execute if block ~ ~-1 ~ air run tag @s add NotGrounded

## If there was no ground below, then teleport down 1 block and try run the function again in the same tick.
## Otherwise, we do nothing because we have made it to the ground.
tp @s[tag=NotGrounded] ~ ~-1 ~
execute as @s[tag=NotGrounded] run function bring_to_ground