Minecraft – How to you detect if a player is falling

minecraft-commandsminecraft-java-edition

I want to make a parkour map and when players fall they will be teleported back to the beginning of the parkour.

Is there a command or a set of commands to detect whether or not a player is falling?

Best Answer

This is actually not too difficult to implement by using scoreboard magic.

First, set up some scoreboard commands

scoreboard objectives add airborne dummy
scoreboard objectives add fallen dummy

Next, run the following commands on a fast clock (I suggest using a fill clock), in this order.

scoreboard players set @a airborne 0
execute @a ~ ~ ~ detect ~ ~-1 ~ minecraft:air -1 scoreboard players set @p airborne 1
scoreboard players set @a[score_airborne=0] fallen 0
scoreboard players add @a[score_airborne_min=1] fallen 1

The first two will set airborne to 1 for every player who is not standing on something, i.e. the block beneath him is air. The third command will reset the fallen score to 0 for everyone who is not airborne, the fourth increments the score 20 times a second instead. This means that fallen is 0 as long as you were standing on something since your last fall.

Now you can select the falling players for teleportation using @a[score_fallen_min=XYZ]. Replace XYZ with however many seconds a player has to be above air to be considered "falling", multiplied by 20.

There is an issue with this however: Standing on the edge of a block will make you be considered falling, and I can't think of an elegant solution to this, short of chaining execute detect commands to check a 3×3 area below the player for blocks.