Minecraft – How to track player’s height level

minecraft-commandsminecraft-java-edition

I would really like to know if it's possible to track "how high" the player is and link it to another command.

For example, if the player is above or crossed height level of 240, it would trigger a set of commands (tp ~ ~-100 ~ as an example).

Best Answer

You must change the origin to that of the player using /execute and then modify the Y value, while providing a selection limiter such as a radius.

Without the limiter, it will select all players in existence, as an origin change (only modifying x, y, or x parameters) does not equate to a limiter. The origin is used to sort targets by distance before running commands affecting them.

Without /execute, the X and Z coordinates will be that of the command block, which would prevent usage anywhere except at the same X and Z of the command block. For example, given the following command:

/tp @a[y=-64,dy=64] X Y Z

This would only teleport players at the command block's X and Z, but with a modified Y. Example image, where the black square is the command block and the shaded area is where a player must be to match:

Command block with shaded area beneath it

However, with /execute, the origin will be that of the targets. This allows usage anywhere in the world since the targets will always match their own X and Z coordinates.

For example, using the following command:

/execute @a ~ 64 ~ /tp @a[y=-64,dy=64] X Y Z

The following image represents where players (represented by circles) need to be to match:

Origin at players rather than command block


You can either change the Y using /execute's syntax:

/execute @a ~ 240 ~ /tp @a[r=1,c=1] ~ ~-100 ~

Or change it using the y parameter of the nested selector:

/execute @a ~ ~ ~ /tp @a[y=240,r=1,c=1] ~ ~-100 ~