Minecraft – Test for approximate rotation

minecraft-commandsminecraft-java-edition

So lets say you have an entity which happens to be of type=Player.
Such that a command block, when powered

testfor @e[r=15,type=Player] {}

will output true.

Now, lets teleport this player

tp @e[r=15,type=Player] ~ ~ ~ -90 45

They should now be facing due east and at the ground.

Now if I test for this:

testfor @e[r=15,type=Player] {Rotation:[0:-90f,1:45f]}

I'll output true as well.

Now the player turns around.
the same command block will now output false (Proving that the command block has successfully tested for a players rotation)

but the rotation is a floating point number, having many decimals of accuracy, and the only way to have it tested and it be true (unless you get realllllly lucky and land on exactly -90.000000000) is to tp the player to have that rotation.

I'd like to be able to test for a player's rotation, but in a natural state.

So the fundamental question is… Can i test for a data tag which is Close to a given value?

Ideally it would look like this:

testfor @e[type=Player,r=15] {Rotation:[0:{min:-45,max:-125}]}

but I know this is wrong. So anyone, has anyone found a solution to testing for approximate data tags?

Best Answer

Here's a video by MNSweet demonstrating rotational direction detection:

Rather than checking the NBT data (i.e. using {}), rotation can be tested for using the target selector arguments ry, rym, rx, and rxm, denoting the maximum and minimum view angle in the horizontal (east, south, etc.) and vertical (up, down) direction, respectively.

For example, to test for someone looking South (=0±22), use

testfor @a[ry=22,rym=-22]

To test for someone looking 45° upwards (±10) and to the northeast (=135±22)

testfor @a[ry=-113,rym=-157,rx=55,rxm=35]

See Commands wiki page for more information.