Minecraft – Rotate Command Troubles

minecraft-commandsminecraft-java-edition

I went to a few other questions like this, regarding the rotate command, but none of them answered my question… I have the command:

/execute @e[r=10000] ~ ~ ~ detect ~ ~-2 ~ minecraft:coal_block 0 /tp @p ~ ~ ~ 360 0

And it is doing pretty much exactly what I want it to do – rotate a player when they walk 2 blocks above a coal block. HOWEVER, it is not rotating them 360 degrees from their current position, it is rotating them to the direction (which in this case is positive y/south) of the command block with the above command. Is there any way to navigate around this problem?

Thanks

Best Answer

You have to use a combination of relative rotational coordinates and a scoreboard objective. The latter is to ensure that players are only rotated once per stepping above a coal block.


Fancy single objective solution

Set up your objective as:

/scoreboard objectives add overCoal dummy

On a 20Hz. clock, run the following commands to set the score:

/execute @a[r=10000] ~ ~ ~ detect ~ ~-2 ~ minecraft:coal_block 0 scoreboard players add @a[c=1] overCoal 3
/scoreboard players remove @a[score_overCoal_min=1] overCoal 2
/scoreboard players set @a[score_overCoal_min=3] overCoal 2
/scoreboard players set @a[score_overCoal=-1] overCoal 0

This might look confusing at first, what with adding 3 and subtracting 2, but it does the trick. Basically, every tick the player is above a coal block, his score is incremented by +3-2=+1, but only to a maximum of 2 (3rd command). The moment the player steps off, the score decreases by 2 (thereby avoiding the value of 1), to a minimum of 0 (4th command).

This means that after these 4 commands are run, the score is:

  • 0 if the player is not above a coal block.
  • 1 if the player has been above a coal block for exactly 1 tick.
  • 2 if the player was above the coal block for longer.

All that is left is to put a command to rotate the players with a score of exactly 1:

/tp @a[score_overCoal=1,score_overCoal_min=1] ~ ~ ~ ~<R> ~

where <R> is the amount of degrees you want to rotate the player.


Simpler 2 objective solution

If the above solution was too confusing for your taste, a more intuitive way is to use 2 objectives:

/scoreboard objectives add overCoal dummy
/scoreboard objectives add overCoalTime dummy

On a 20Hz. clock, run the following commands to set the score:

/scoreboard players set @a overCoal 0
/execute @a[r=10000] ~ ~ ~ detect ~ ~-2 ~ minecraft:coal_block 0 scoreboard players set @a[c=1] overCoal 1
/scoreboard players set @a[score_overCoal=0] overCoalTime 0
/scoreboard players add @a[score_overCoal_min=1] overCoalTime 1

This achieves basically the same as above, but for overCoalTime instead.