mojira.dev
MC-125067

Command context/selectors in /execute are evaluated too early

The new /execute command gives detailed control over the command context given to a command that is to be executed. However, the way command contexts are computed doesn't account for the fact that command context may change in iterations of the outer loop and should then be different in subsequent iterations.

For many use-cases this is perfectly fine because the command context rarely (intentionally) changes. However, for some applications this is not sufficient. Consider the following command, ran when exactly one player and a couple of other entities are in the game:

/execute as @e at @p run tp @p ~ ~ ~1

Expected behavior: the player is teleported one block for each entity in the world. If there are 10 armor stands and the player, the player should be teleported 11 blocks. Actual behavior: the player is teleported one block. A reproduction case not involving teleportation can be found in this comment.


The following is a simplified, pseudo code interpretation of how /execute looping is understood to work with this command:

compute contexts for all loops
loop over @e
	loop over @p
		stitch together computed command context
		run "tp @p ~ ~ ~1" with computed command context

Instead, it should work somewhat like this:

calculate contexts for @e loop
loop over @e
	calculate contexts for @p loop
	loop over @p
		stitch together context for this iteration
		run tp @p ~ ~ ~1 with computed command context

Note that the implementation of /execute wasn't looked at in detail for this ticket, so it contains a healthy dose of speculation.

Linked issues

Comments 12

execute @ e [c = 2] ~ ~ ~ tp @ p ~ 1 ~ ~ ~ 90 ~

This is not a valid command, neither in 1.12, nor in 1.13.

execute positioned as @e[limit=2] positioned as @p run tp @p ~1 ~ ~ ~90 ~

this will only move one block and rotate only 90 °

That's what the command is supposed to do, what did you expect?

@violine1101
I'm sorry. There was wasted space in the command.

execute @e[c=2] ~ ~ ~ tp @p ~1 ~ ~ ~90 ~

This command is valid in 1.12.

With this command, you will move two blocks and rotate 180 °.

No, that command will move you one block and rotate you 90° as well. Maybe the command was simply run twice?

@violine1101
Perhaps you are doing it in a world where there is no one other than yourself?
My writing is based on the assumption that there are two or more entities.

Oh. I misunderstood your commands. Stupid me.

Now, this is what the command does in 18w06a: It teleports you one block and rotates you 180°. Apart from myself, there's only an armor stand in the world. I'm not exactly sure why I got a different result than you.

2 more comments

@violine1101
I hope that a way to rotate and move twice even if the bug is fixed be added.
In the current specification, I have to create a function each time.

@@unknown: Updated summary and description. Let us know if we missed any important information from the old description.

@violine1101
It's a great!
I thank you for being writen appropriate sentences instead of me.

Here is a repro without teleport:

/scoreboard objectives add TEMP dummy
/scoreboard objectives setdisplay sidebar TEMP
/scoreboard players set @p TEMP 1
/execute at @e[limit=2] as @p at @s[scores={TEMP=1}] run scoreboard players add @s TEMP 1

EXPECTED: final score of 2

ACTUAL: final score of 3

This demonstrates that the second 'at' is evaluating @s only once at the start, rather than in each iteration of the outer limit=2 loop.

Can confirm for 1.21. The famous Vanilla Tweaks team had trouble updating their datapacks because of this bug. I hope it will be looked at in the future.

komaramune

(Unassigned)

Confirmed

(Unassigned)

/execute

Minecraft 18w06a, Minecraft 18w07c, Minecraft 1.13-pre1, Minecraft 1.13, Minecraft 1.13.1, 1.15.1, 1.15.2 Pre-release 2, 1.16.3, 1.17.1

Retrieved