Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Minecraft:Command context: Difference between revisions

From SAS Gaming Wiki
SyncBot (talk | contribs)
Fix template calls: add MC/ prefix
SyncBot (talk | contribs)
Sync: updated from Minecraft
 
Line 1: Line 1:
'''Command context''' (also know as '''execution context''', '''command origin''', '''command source stack''') is a set of parameters like the executor and execution environment. It is used to provide context for command execution or raw JSON text resolution.
'''Command context''' (also know as '''execution context''', '''command origin''', '''command source stack''') is a set of parameters like the executor and execution environment. It is used to provide context for [[Minecraft:command]] execution or [[Minecraft:raw JSON text]] resolution.


== Behaviors ==
== Behaviors ==
Line 14: Line 14:
* '''[[#Execution Output Callback|Execution Output Callback]]'''
* '''[[#Execution Output Callback|Execution Output Callback]]'''


When commands are executed or raw JSON texts are resolved in different scenarios, corresponding contexts are provided, to allow them to work in different scenarios.
When [[Minecraft:command]]s are executed or [[Minecraft:raw JSON text]]s are resolved in different scenarios, corresponding contexts are provided, to allow them to work in different scenarios.


When running a function with the {{MC/Cmd|function}} command, the context for commands inside the function inherits the Executor Parameters, Execution Environment Parameters, and Execution Permission Level from the context of {{MC/Cmd|function}} command, while the Execution Output Callback is not inherited. {{MC/In|java}}, the Execution Permission Level of commands in a function is limited to 2 or below, so, if the {{MC/Cmd|function}} command is executed with 2 or higher Execution Permission Level, the command inside the function is executed with Execution Permission Level being 2.
When running a function with the {{cmd|function}} command, the context for commands inside the function inherits the Executor Parameters, Execution Environment Parameters, and Execution Permission Level from the context of {{cmd|function}} command, while the Execution Output Callback is not inherited. {{IN|java}}, the Execution Permission Level of commands in a function is limited to 2 or below, so, if the {{cmd|function}} command is executed with 2 or higher Execution Permission Level, the command inside the function is executed with Execution Permission Level being 2.


{{MC/In|bedrock}}, the context for commands in functions scheduled by {{MC/Cmd|schedule}} command also inherits the Executor Parameters, Execution Environment Parameters, and Execution Permission Level from the context of {{MC/Cmd|schedule}} command.
{{IN|bedrock}}, the context for commands in functions scheduled by {{cmd|schedule}} command also inherits the Executor Parameters, Execution Environment Parameters, and Execution Permission Level from the context of {{cmd|schedule}} command.


=== Static values and Dynamic values ===
=== Static values and Dynamic values ===
{{MC/In|java}}, the Executor Name and each Execution Environment Parameters in a context are '''static''', and each of them exists independently. In other words, if these parameters are not modified with {{MC/Cmd|execute}}, they do not change during the command execution, or during the execution of each command in a function. For example, even if the name of the Executor Entity is modified in a function, it does not change the Executor Name in the context of subsequent commands in the function; For another example, for {{MC/Cmd|1=execute at @n[type=sheep] run function test}}, even if a {{MC/Cmd|tp}} command in a function {{MC/Cd|test}} changes the position, rotation, or dimension of the sheep entity, it does not affect the Execution Position, Execution Rotation, and Execution Dimension in the context of subsequent commands in the function, since the values of all these parameters are static after being modified by the {{MC/Cd|at}} subcommand and they are no longer related to the sheep entity itself.
{{IN|java}}, the Executor Name and each Execution Environment Parameters in a context are '''static''', and each of them exists independently. In other words, if these parameters are not modified with {{cmd|execute}}, they do not change during the command execution, or during the execution of each command in a function. For example, even if the name of the Executor Entity is modified in a function, it does not change the Executor Name in the context of subsequent commands in the function; For another example, for {{cmd|1=execute at @n[type=sheep] run function test}}, even if a {{cmd|tp}} command in a function {{cd|test}} changes the position, rotation, or dimension of the sheep entity, it does not affect the Execution Position, Execution Rotation, and Execution Dimension in the context of subsequent commands in the function, since the values of all these parameters are static after being modified by the {{cd|at}} subcommand and they are no longer related to the sheep entity itself.


{{MC/In|bedrock}} parameter values are static in some cases but '''dynamic''' in other cases. When dynamic, the value of a parameter does not exist independently, but rather it is a reference to an entity and fetches values from the entity in real time. For example, when an Execution Position is dynamic, it depends on an entity and obtains its position in real time, i.e., if the entity is moved in a function, the Execution Position in the context of subsequent commands in the function is the position of the entity after the move.<ref>{{bug|MCPE-179363}}</ref>
{{IN|bedrock}} parameter values are static in some cases but '''dynamic''' in other cases. When dynamic, the value of a parameter does not exist independently, but rather it is a reference to an entity and fetches values from the entity in real time. For example, when an Execution Position is dynamic, it depends on an entity and obtains its position in real time, i.e., if the entity is moved in a function, the Execution Position in the context of subsequent commands in the function is the position of the entity after the move.<ref>{{bug|MCPE-179363}}</ref>


Specifically, dynamic values exist in the following cases:
Specifically, dynamic values exist in the following cases:
Line 29: Line 29:
* When a command block minecart executes the command in it, the Execution Position and Execution Rotation are dynamic, referring to this minecart entity.
* When a command block minecart executes the command in it, the Execution Position and Execution Rotation are dynamic, referring to this minecart entity.
* When an entity event response defined in a behavior pack executes a command, or a script executes a command with entity class, or an animation controller in a behavior pack executes a command, or an NPC executes a command, the Executor Name, Execution Position, Execution Rotation, and Execution Dimension are dynamic and referring to the entity.
* When an entity event response defined in a behavior pack executes a command, or a script executes a command with entity class, or an animation controller in a behavior pack executes a command, or an NPC executes a command, the Executor Name, Execution Position, Execution Rotation, and Execution Dimension are dynamic and referring to the entity.
* {{MC/Cmd|execute}} command converts the Executor Name to a dynamic value referring to the Executor Entity. That is, when a command is executed in a {{MC/Cmd|execute}} command, the Executor Name of it is always dynamic, fetched from the Executor Entity in real time, regardless of whether there is a static Executor Name in the context of the {{MC/Cmd|execute}}.
* {{cmd|execute}} command converts the Executor Name to a dynamic value referring to the Executor Entity. That is, when a command is executed in a {{cmd|execute}} command, the Executor Name of it is always dynamic, fetched from the Executor Entity in real time, regardless of whether there is a static Executor Name in the context of the {{cmd|execute}}.
* {{MC/Cd|at ...}} and {{MC/Cd|positioned as ...}} subcommands in {{MC/Cmd|execute}} set the Execution Position to a dynamic value, referring to the specified entity.
* {{cd|at ...}} and {{cd|positioned as ...}} subcommands in {{cmd|execute}} set the Execution Position to a dynamic value, referring to the specified entity.
* {{MC/Cd|at ...}} and {{MC/Cd|rotated as ...}}  subcommands in {{MC/Cmd|execute}} set the Execution Rotation to a dynamic value, referring to the specified entity.
* {{cd|at ...}} and {{cd|rotated as ...}}  subcommands in {{cmd|execute}} set the Execution Rotation to a dynamic value, referring to the specified entity.


If the Execution Position is dynamic, it fetches the position from an entity in real time. But, if this entity is riding on another entity, there is a special treatment:
If the Execution Position is dynamic, it fetches the position from an entity in real time. But, if this entity is [[Minecraft:riding]] on another entity, there is a special treatment:
* If the hitbox bottom of this entity is lower than the hitbox bottom of the mount, the mount's hitbox bottom is used as the Y coordinate of the Execution Position.
* If the hitbox bottom of this entity is lower than the hitbox bottom of the mount, the mount's hitbox bottom is used as the Y coordinate of the Execution Position.
** For example, if a player is riding on a minecart, the player's X and Z coordinates and the minecart's Y coordinate are used as the Execution Position.
** For example, if a player is riding on a minecart, the player's X and Z coordinates and the minecart's Y coordinate are used as the Execution Position.
Line 42: Line 42:
* Execution Position defaults to <code>(0, 0, 0)</code>
* Execution Position defaults to <code>(0, 0, 0)</code>
* Execution Rotation defaults to <code>(0, 0)</code>
* Execution Rotation defaults to <code>(0, 0)</code>
For example, when {{MC/Cmd|execute run say hi}} is executed in the command block, as mentioned above, the Executor Name converts to a dynamic value by the {{MC/Cmd|execute}} command, referring to the Executor Entity. Since there is no Executor Entity in the context, the Executor Name of {{MC/Cmd|say hi}} is an empty string.
For example, when {{cmd|execute run say hi}} is executed in the command block, as mentioned above, the Executor Name converts to a dynamic value by the {{cmd|execute}} command, referring to the Executor Entity. Since there is no Executor Entity in the context, the Executor Name of {{cmd|say hi}} is an empty string.


For another example, if the Executor Entity is removed before a function schedule (scheduled by {{MC/Cmd|schedule}}) is executed, the schedule cannot be executed because the Execution Dimension is missing in the context.
For another example, if the Executor Entity is removed before a function schedule (scheduled by {{cmd|schedule}}) is executed, the schedule cannot be executed because the Execution Dimension is missing in the context.


A dynamic value is converted to a static value in the following cases:
A dynamic value is converted to a static value in the following cases:
* {{MC/Cmd|execute}} command converts the Execution Dimension to a static value.
* {{cmd|execute}} command converts the Execution Dimension to a static value.
* {{MC/Cmd|execute}} command converts the Execution Rotation to a static value.
* {{cmd|execute}} command converts the Execution Rotation to a static value.
* {{MC/Cd|align ...}} subcommand in {{MC/Cmd|execute}} converts the Execution Position to a static value.
* {{cd|align ...}} subcommand in {{cmd|execute}} converts the Execution Position to a static value.
* {{MC/Cd|in ...}} subcommand in {{MC/Cmd|execute}} converts the Execution Position to a static value.
* {{cd|in ...}} subcommand in {{cmd|execute}} converts the Execution Position to a static value.
* {{MC/Cd|positioned <''coordinates''>}} subcommand in {{MC/Cmd|execute}} converts the Execution Position to a static value.
* {{cd|positioned <''coordinates''>}} subcommand in {{cmd|execute}} converts the Execution Position to a static value.
* {{MC/Cd|facing ...}} subcommand in {{MC/Cmd|execute}} converts the Execution Rotation to a static value.
* {{cd|facing ...}} subcommand in {{cmd|execute}} converts the Execution Rotation to a static value.
* {{MC/Cd|rotated <''angles''>}} subcommand in {{MC/Cmd|execute}} converts the Execution Rotation to a static value.
* {{cd|rotated <''angles''>}} subcommand in {{cmd|execute}} converts the Execution Rotation to a static value.
* If the Execution Position is a dynamic value, then {{MC/Cd|anchored ...}} subcommand in {{MC/Cmd|execute}} converts the Execution Position to a static value, being the position at the referred entity's feet or eyes; If the Execution Position is a static value, {{MC/Cd|anchored ...}} subcommand does not take any effect.<ref>{{bug|MCPE-162681}} and {{bug|MCPE-165051}}</ref>
* If the Execution Position is a dynamic value, then {{cd|anchored ...}} subcommand in {{cmd|execute}} converts the Execution Position to a static value, being the position at the referred entity's feet or eyes; If the Execution Position is a static value, {{cd|anchored ...}} subcommand does not take any effect.<ref>{{bug|MCPE-162681}} and {{bug|MCPE-165051}}</ref>


For example:
For example:
* When a player executes {{MC/Cmd|function ...}} in the chat bar, the Execution Position, Execution Rotation and Execution Dimension of commands in the function are dynamic, referring to the player.
* When a player executes {{cmd|function ...}} in the chat bar, the Execution Position, Execution Rotation and Execution Dimension of commands in the function are dynamic, referring to the player.
* When a player executes {{MC/Cmd|execute run <''command''>}} in the chat bar, the Execution Position of the {{MC/Cd|<''command''>}} is dynamic, while the Execution Rotation and Execution Dimension are static.
* When a player executes {{cmd|execute run <''command''>}} in the chat bar, the Execution Position of the {{cd|<''command''>}} is dynamic, while the Execution Rotation and Execution Dimension are static.
* When a player executes {{MC/Cmd|execute at @s run <''command''>}} in the chat bar, the Execution Position and Execution Rotation of the {{MC/Cd|<''command''>}}} are dynamic, while the Execution Dimension is static.
* When a player executes {{cmd|execute at @s run <''command''>}} in the chat bar, the Execution Position and Execution Rotation of the {{cd|<''command''>}}} are dynamic, while the Execution Dimension is static.
* When a player executes {{MC/Cmd|execute at @s rotated ~ ~ run <''command''>}} in the chat bar, the Execution Position of the {{MC/Cd|<''command''>}} is dynamic, while the Execution Rotation and Execution Dimension are static.
* When a player executes {{cmd|execute at @s rotated ~ ~ run <''command''>}} in the chat bar, the Execution Position of the {{cd|<''command''>}} is dynamic, while the Execution Rotation and Execution Dimension are static.
* When a player executes {{MC/Cmd|execute at @s rotated ~ ~ positioned ~ ~ ~ run <''command''>}} in the chat bar, the Execution Position, Execution Rotation and Execution Dimension of the {{MC/Cd|<''command''>}} are all static.
* When a player executes {{cmd|execute at @s rotated ~ ~ positioned ~ ~ ~ run <''command''>}} in the chat bar, the Execution Position, Execution Rotation and Execution Dimension of the {{cd|<''command''>}} are all static.
* When a player executes {{MC/Cmd|execute positioned ~ ~ ~ run <''command''>}} in the chat bar, the Execution Position, Execution Rotation and Execution Dimension of the {{MC/Cd|<''command''>}} are all static.
* When a player executes {{cmd|execute positioned ~ ~ ~ run <''command''>}} in the chat bar, the Execution Position, Execution Rotation and Execution Dimension of the {{cd|<''command''>}} are all static.
* If there are at least three entities in a dimension, and a player in the dimension executes {{MC/Cmd|1=execute rotated as @e[c=3] run tp @s ~ ~1 ~}} in the chat bar, the player is teleported to 3 blocks above its original position.
* If there are at least three entities in a dimension, and a player in the dimension executes {{cmd|1=execute rotated as @e[c=3] run tp @s ~ ~1 ~}} in the chat bar, the player is teleported to 3 blocks above its original position.


== Parameters ==
== Parameters ==
=== Execution Permission Level ===
=== Execution Permission Level ===
{{MC/Main|Permission level}}
{{main|Permission level}}
The Execution Permission Level cannot be modified by {{MC/Cmd|execute}} command.
The Execution Permission Level cannot be modified by {{cmd|execute}} command.


{{MC/In|java}}, in the context of commands in a function called by a {{MC/Cmd|function}} command, the Execution Permission Level is limited to 2 or less. If the Execution Permission Level of the {{MC/Cmd|function}} command is greater than 2, the commands in the function is executed with Execution Permission Level of 2.
{{IN|java}}, in the context of commands in a function called by a {{cmd|function}} command, the Execution Permission Level is limited to 2 or less. If the Execution Permission Level of the {{cmd|function}} command is greater than 2, the commands in the function is executed with Execution Permission Level of 2.


=== Executor Parameters ===
=== Executor Parameters ===
There are two Executor Parameters, Executor Name and Executor Entity, where the Executor Entity parameter may be empty (e.g. when a command is executed by a command block). Executor parameters are used in the following scenarios:
There are two Executor Parameters, Executor Name and Executor Entity, where the Executor Entity parameter may be empty (e.g. when a command is executed by a command block). Executor parameters are used in the following scenarios:
* When sending a message using commands such as {{MC/Cmd|tell}} or {{MC/Cmd|say}}, the Executor Name is used as the sender name of the message.
* When sending a message using commands such as {{cmd|tell}} or {{cmd|say}}, the Executor Name is used as the sender name of the message.
* <code>@s</code> target selector directly selects the Executor Entity.
* <code>@s</code> [[Minecraft:target selector]] directly selects the Executor Entity.
* By default, the target of commands such as {{MC/Cmd|tp}} and {{MC/Cmd|kill}} is the Executor Entity.
* By default, the target of commands such as {{cmd|tp}} and {{cmd|kill}} is the Executor Entity.
* By default, the target player of commands such as {{MC/Cmd|clear}}, {{MC/Cmd|gamemode}}, and {{MC/Cmd|playsound}} is the Executor Entity, if it is a player entity.
* By default, the target player of commands such as {{cmd|clear}}, {{cmd|gamemode}}, and {{cmd|playsound}} is the Executor Entity, if it is a player entity.
* {{MC/In|je}}, commands such as {{MC/Cmd|trigger}} and {{MC/Cmd|teammsg}} take effect on the Executor Entity, if the Executor Entity is a player.
* {{IN|je}}, commands such as {{cmd|trigger}} and {{cmd|teammsg}} take effect on the Executor Entity, if the Executor Entity is a player.
* {{MC/In|je}}, [[#Execution Anchor|Execution Anchor]] modifies the Y coordinate based on the eye height of the Executor Entity.
* {{IN|je}}, [[#Execution Anchor|Execution Anchor]] modifies the Y coordinate based on the eye height of the Executor Entity.


They can be modified using {{MC/Cd|as}}, {{MC/Cd|on}}, and {{MC/Cd|summon}} subcommands in {{MC/Cmd|execute}} command. {{MC/In|je}}, the Executor Name and Executor Entity are always modified at the same time. {{MC/In|be}}, only the Executor Entity is modified, and as mentioned above, the Executor Name remains a dynamic value referring to the Executor Entity.
They can be modified using {{cd|as}}, {{cd|on}}, and {{cd|summon}} subcommands in {{cmd|execute}} command. {{IN|je}}, the Executor Name and Executor Entity are always modified at the same time. {{IN|be}}, only the Executor Entity is modified, and as mentioned above, the Executor Name remains a dynamic value referring to the Executor Entity.


=== Execution Dimension ===
=== Execution Dimension ===
Execution Dimension is the [[Minecraft:dimension]] in which the command takes effect. Execution Dimension is used in the following scenarios:
Execution Dimension is the [[Minecraft:dimension]] in which the command takes effect. Execution Dimension is used in the following scenarios:
* Whenever [[Minecraft:coordinates]] are specified, the coordinates are specified within the Execution Dimension.
* Whenever [[Minecraft:coordinates]] are specified, the coordinates are specified within the Execution Dimension.
* Target selectors with <code>distance</code>{{only|je|short=1}}, <code>r</code>{{only|be|short=1}}, <code>rm</code>{{only|be|short=1}}, <code>dx</code>, <code>dy</code>, or <code>dz</code> being specified, only entities in the Execution Dimension are selected.
* [[Minecraft:Target selector]]s with <code>distance</code>{{only|je|short=1}}, <code>r</code>{{only|be|short=1}}, <code>rm</code>{{only|be|short=1}}, <code>dx</code>, <code>dy</code>, or <code>dz</code> being specified, only entities in the Execution Dimension are selected.
* {{MC/In|java}}, target selectors with <code>x</code>, <code>y</code>, or <code>z</code> being specified, only entities in the Execution Dimension are selected, too.
* {{IN|java}}, [[Minecraft:target selector]]s with <code>x</code>, <code>y</code>, or <code>z</code> being specified, only entities in the Execution Dimension are selected, too.
* Commands such as {{MC/Cmd|locate}} works within the Execution Dimension.
* Commands such as {{cmd|locate}} works within the Execution Dimension.


Execution Dimension can be modified using {{MC/Cd|at}} and {{MC/Cd|in}} subcommands in the {{MC/Cmd|execute}} command. {{MC/In|bedrock}}, the {{MC/Cmd|execute}} command itself converts the Execution Dimension to a static value.
Execution Dimension can be modified using {{cd|at}} and {{cd|in}} subcommands in the {{cmd|execute}} command. {{IN|bedrock}}, the {{cmd|execute}} command itself converts the Execution Dimension to a static value.


=== Execution Position ===
=== Execution Position ===
Execution Position is the [[Minecraft:coordinates]] that the command takes effect from. Execution Position is used in the following scenarios:
Execution Position is the [[Minecraft:coordinates]] that the command takes effect from. Execution Position is used in the following scenarios:
* [[Minecraft:Coordinates#Commands|Relative coordinates]] are relative to the Execution Position.
* [[Minecraft:Coordinates#Commands|Relative coordinates]] are relative to the Execution Position.
* {{MC/In|java}}, the origin of [[Minecraft:Coordinates#Commands|local coordinates]] is based on the Execution Position and affected by the Executor Anchor.
* {{IN|java}}, the origin of [[Minecraft:Coordinates#Commands|local coordinates]] is based on the Execution Position and affected by the Executor Anchor.
* {{MC/In|bedrock}}, the origin of [[Minecraft:Coordinates#Commands|local coordinates]] is the Execution Position.
* {{IN|bedrock}}, the origin of [[Minecraft:Coordinates#Commands|local coordinates]] is the Execution Position.
* {{MC/In|java}}, the start point of {{MC/Cmd|execute facing ...}} and {{MC/Cmd|tp ... facing ...}} is based on the Execution Position and affected by the Executor Anchor.
* {{IN|java}}, the start point of {{cmd|execute facing ...}} and {{cmd|tp ... facing ...}} is based on the Execution Position and affected by the Executor Anchor.
* {{MC/In|bedrock}}, the start point of {{MC/Cmd|execute facing ...}} and {{MC/Cmd|tp ... facing ...}} is the Execution Position.
* {{IN|bedrock}}, the start point of {{cmd|execute facing ...}} and {{cmd|tp ... facing ...}} is the Execution Position.
* Commands such as {{MC/Cmd|locate}} use the Execution Position as its start point.
* Commands such as {{cmd|locate}} use the Execution Position as its start point.
* By default, the target position of commands such as {{MC/Cmd|locate}} is the Execution Position.
* By default, the target position of commands such as {{cmd|locate}} is the Execution Position.


Execution Position can be modified using {{MC/Cd|align}}, {{MC/Cd|at}}, {{MC/Cd|positioned}} subcommands in {{MC/Cmd|execute}} command, and {{MC/Cd|in}} subcommand also affects the Execution Position. {{MC/In|be}}, {{MC/Cd|at}} and {{MC/Cd|positioned as}} subcommands set the Execution Position to a dynamic value referring to the specified entity. {{MC/In|bedrock}}, {{MC/Cd|align}}, {{MC/Cd|anchored}}, {{MC/Cd|in}}, and {{MC/Cd|positioned <''coordinates''>}} subcommands convert the Execution Position to a static value.
Execution Position can be modified using {{cd|align}}, {{cd|at}}, {{cd|positioned}} subcommands in {{cmd|execute}} command, and {{cd|in}} subcommand also affects the Execution Position. {{IN|be}}, {{cd|at}} and {{cd|positioned as}} subcommands set the Execution Position to a dynamic value referring to the specified entity. {{IN|bedrock}}, {{cd|align}}, {{cd|anchored}}, {{cd|in}}, and {{cd|positioned <''coordinates''>}} subcommands convert the Execution Position to a static value.


=== Execution Rotation ===
=== Execution Rotation ===
Execution Rotation is the rotation that the command is executed with. Execution Rotation is used in the following scenarios:
Execution Rotation is the rotation that the command is executed with. Execution Rotation is used in the following scenarios:
* The coordinate system of [[Minecraft:Coordinates#Commands|local Coordinates]] is based on the Execution Rotation.
* The coordinate system of [[Minecraft:Coordinates#Commands|local Coordinates]] is based on the Execution Rotation.
* Relative rotation specified with a tilde ({{MC/Cd|~}}) in commands such as {{MC/Cmd|tp}} and {{MC/Cmd|spawnpoint}} is relative to the Execution Rotation.
* Relative rotation specified with a tilde ({{cd|~}}) in commands such as {{cmd|tp}} and {{cmd|spawnpoint}} is relative to the Execution Rotation.


Execution Rotation can be modified using {{MC/Cd|at}}, {{MC/Cd|facing}}, and {{MC/Cd|rotated}} subcommands in the {{MC/Cmd|execute}} command. {{MC/In|be}}, {{MC/Cd|as}} and {{MC/Cd|rotated as}} subcommands set the Execution Rotation to a dynamic value referring to the specified entity. {{MC/In|bedrock}}, {{MC/Cmd|execute}} command itself and its {{MC/Cd|facing}}, {{MC/Cd|rotated <''angle''>}} subcommands converts the Execution Rotation to a static value.
Execution Rotation can be modified using {{cd|at}}, {{cd|facing}}, and {{cd|rotated}} subcommands in the {{cmd|execute}} command. {{IN|be}}, {{cd|as}} and {{cd|rotated as}} subcommands set the Execution Rotation to a dynamic value referring to the specified entity. {{IN|bedrock}}, {{cmd|execute}} command itself and its {{cd|facing}}, {{cd|rotated <''angle''>}} subcommands converts the Execution Rotation to a static value.


=== Execute Anchor ===
=== Execute Anchor ===
{{MC/Exclusive|java|section=1}}
{{exclusive|java|section=1}}
Execute Anchor determines whether the position of eyes or feet of the Executor Entity is used as the origin of local coordinates, and the start point of {{MC/Cd|facing}} in {{MC/Cmd|teleport}} and of {{MC/Cd|facing}} subcommand in {{MC/Cmd|execute}}.
Execute Anchor determines whether the position of eyes or feet of the Executor Entity is used as the origin of [[Minecraft:local coordinates]], and the start point of {{cd|facing}} in {{cmd|teleport}} and of {{cd|facing}} subcommand in {{cmd|execute}}.
* If it is foot, uses the Execution Position directly.
* If it is foot, uses the Execution Position directly.
* If it is eyes, based on the Execution Position, the eye height of the Executor Entity is added into the Y coordinate.
* If it is eyes, based on the Execution Position, the eye height of the Executor Entity is added into the Y coordinate.


Execution Anchor are used only in the following scenarios:
Execution Anchor are used only in the following scenarios:
* Local coordinates.
* [[Minecraft:Local coordinates]].
* The start point of {{MC/Cd|facing ...}} subcommand in {{MC/Cmd|execute}}.
* The start point of {{cd|facing ...}} subcommand in {{cmd|execute}}.
* The start point of {{MC/Cmd|tp ... facing ...}}.
* The start point of {{cmd|tp ... facing ...}}.


Execute Anchor can be modified using {{MC/Cd|anchored}} subcommand in {{MC/Cmd|execute}} command.
Execute Anchor can be modified using {{cd|anchored}} subcommand in {{cmd|execute}} command.


Executor Anchor parameter does not exist {{MC/In|be}}. Instead, {{MC/In|be}}, the {{MC/Cd|anchored}} subcommand in {{MC/Cmd|execute}} directly modifies the Execution Position. If the Execution Position is originally dynamic, the {{MC/Cd|anchored}} subcommand converts it to a static value, being the position of feet or eyes of the entity being referred by the dynamic value; If the Execution Position is static, {{MC/Cd|anchored}} subcommand does not have any effect at all.
Executor Anchor parameter does not exist {{in|be}}. Instead, {{in|be}}, the {{cd|anchored}} subcommand in {{cmd|execute}} directly modifies the Execution Position. If the Execution Position is originally dynamic, the {{cd|anchored}} subcommand converts it to a static value, being the position of feet or eyes of the entity being referred by the dynamic value; If the Execution Position is static, {{cd|anchored}} subcommand does not have any effect at all.


=== Execution Output Callback ===
=== Execution Output Callback ===
Execution Output Callbacks are used to receive [[Minecraft:Commands#Output|output values]] of a command.
Execution Output Callbacks are used to receive [[Minecraft:Commands#Output|output values]] of a command.


A command executed in a command block, a minecart with command block, or a script{{only|be}} has a callback that sends the success count to the command block or minecart.
A command executed in a [[Minecraft:command block]], a [[Minecraft:minecart with command block]], or a script{{only|be}} has a callback that sends the success count to the command block or minecart.


{{MC/In|java}}, {{MC/Cd|store}} subcommand in {{MC/Cmd|execute}} command can be used to add a new callback while the existing callbacks are retained. Existing callbacks cannot be cleared by {{MC/Cmd|execute}} command.
{{IN|java}}, {{cd|store}} subcommand in {{cmd|execute}} command can be used to add a new callback while the existing callbacks are retained. Existing callbacks cannot be cleared by {{cmd|execute}} command.


The context of commands in a function called by a {{MC/Cmd|function}} or {{MC/Cmd|schedule}} does not inherit the Execution Output Callbacks of the {{MC/Cmd|function}} or {{MC/Cmd|schedule}} command.
The context of commands in a function called by a {{cmd|function}} or {{cmd|schedule}} does not inherit the Execution Output Callbacks of the {{cmd|function}} or {{cmd|schedule}} command.


== Contexts in various scenarios ==
== Contexts in various scenarios ==
Line 149: Line 149:


==== Functions executed by the server-side ====
==== Functions executed by the server-side ====
A function is executed by the server-side when:
A [[Minecraft:function]] is executed by the server-side when:
* {{MC/In|je}}, the function is called via <code>load</code> or <code>tick</code> tags.
* {{IN|je}}, the function is called via <code>load</code> or <code>tick</code> tags.
* {{MC/In|be}}, the function is called via <code>load.json</code> or <code>tick.json</code>.
* {{IN|be}}, the function is called via <code>tick.json</code>.
* {{MC/In|je}}, the function is scheduled by {{MC/Cmd|schedule}} command.
* {{IN|je}}, the function is scheduled by {{cmd|schedule}} command.


The context of commands in a function executed by the server-side is:
The context of commands in a function executed by the server-side is:
Line 165: Line 165:


==== Player ====
==== Player ====
The context of commands that are entered in the chat bar, or requested via a WebSocket server connected with the client {{MC/In|be}}, is:
The context of commands that are entered in the chat bar, or requested via a WebSocket server connected with the client {{in|be}}, is:
* Execution Permission Level: The permission level of the player
* Execution Permission Level: The permission level of the player
* Executor Name: The name of the player; or <code>External</code> for commands requested by a WebSocket {{MC/In|be}}
* Executor Name: The name of the player; or <code>External</code> for commands requested by a WebSocket {{in|be}}
* Executor Entity: The player
* Executor Entity: The player
* Execution Dimension: The dimension the player is in (Dynamic value {{MC/In|bedrock}})
* Execution Dimension: The dimension the player is in (Dynamic value {{in|bedrock}})
* Execution Position: The position of the player (Dynamic value {{MC/In|bedrock}})
* Execution Position: The position of the player (Dynamic value {{in|bedrock}})
* Execution Rotation: The rotation of the player (Dynamic value {{MC/In|bedrock}})
* Execution Rotation: The rotation of the player (Dynamic value {{in|bedrock}})
* Execution Anchor{{only|je|short=1}}: Feet
* Execution Anchor{{only|je|short=1}}: Feet
* Execution Output Callback: None
* Execution Output Callback: None


==== Command block ====
==== Command block ====
The context of commands that are executed in a command block is:
The context of commands that are executed in a [[Minecraft:command block]] is:
* Execution Permission Level: 2{{only|java}}/1{{only|bedrock}}
* Execution Permission Level: 2{{only|java}}/1{{only|bedrock}}
* Executor Name: The custom name of the command block. If it has no custom name, defaults to <code>@</code>{{only|java}}/<code>!</code>{{only|bedrock}}.
* Executor Name: The custom name of the command block. If it has no custom name, defaults to <code>@</code>{{only|java}}/<code>!</code>{{only|bedrock}}.
Line 187: Line 187:


==== Minecart with command block ====
==== Minecart with command block ====
The context of commands that are executed in a minecart with command block is:
The context of commands that are executed in a [[Minecraft:minecart with command block]] is:
* Execution Permission Level: 2{{only|java}}/1{{only|bedrock}}
* Execution Permission Level: 2{{only|java}}/1{{only|bedrock}}
* Executor Name: The custom name of the minecart. If it has no custom name, defaults to <code>@</code>{{only|java}}/<code>!</code>{{only|bedrock}}.
* Executor Name: The custom name of the minecart. If it has no custom name, defaults to <code>@</code>{{only|java}}/<code>!</code>{{only|bedrock}}.
* Executor Entity: The minecart with command block
* Executor Entity: The minecart with command block
* Execution Dimension: The dimension the minecart is in
* Execution Dimension: The dimension the minecart is in
* Execution Position: The position of the minecart (Dynamic value {{MC/In|bedrock}})
* Execution Position: The position of the minecart (Dynamic value {{in|bedrock}})
* Execution Rotation: The rotation of the minecart (Dynamic value {{MC/In|bedrock}})
* Execution Rotation: The rotation of the minecart (Dynamic value {{in|bedrock}})
* Execution Anchor{{only|je|short=1}}: Feet
* Execution Anchor{{only|je|short=1}}: Feet
* Execution Output Callback: Sends the success count to the command block
* Execution Output Callback: Sends the success count to the command block


==== Sign ====
==== Sign ====
{{MC/Exclusive|java|section=1}}
{{exclusive|java|section=1}}
The context of commands executed when a player clicks a [[Minecraft:sign]] block is:
The context of commands executed when a player clicks a [[Minecraft:sign]] block is:
* Execution Permission Level: 2
* Execution Permission Level: 2
Line 210: Line 210:


==== Advancement reward ====
==== Advancement reward ====
{{MC/Exclusive|java|section=1}}
{{exclusive|java|section=1}}
The context of commands in the function called by a {{MC/Nbt|string|function}} reward in a custom advancement is:  
The context of commands in the [[Minecraft:function (Java Edition)|function]] called by a {{nbt|string|function}} reward in a [[Minecraft:Advancement definition|custom advancement]] is:  
* Execution Permission Level: 2
* Execution Permission Level: 2
* Executor Name: The name of the player that gets the advancement
* Executor Name: The name of the player that gets the advancement
Line 222: Line 222:


==== Enchantment effect ====
==== Enchantment effect ====
{{MC/Exclusive|java|section=1}}
{{exclusive|java|section=1}}
The context of commands in the function called by a <code>run_function</code> enchantment effect is:
The context of commands in the [[Minecraft:function (Java Edition)|function]] called by a <code>run_function</code> [[Minecraft:Enchantment definition#run_function|enchantment effect]] is:
* Execution Permission Level: 2
* Execution Permission Level: 2
* Executor Name: The name of the affected entity of the enchantment effect
* Executor Name: The name of the affected entity of the enchantment effect
Line 234: Line 234:


==== Entity in behavior packs ====
==== Entity in behavior packs ====
{{MC/Exclusive|bedrock|section=1}}
{{exclusive|bedrock|section=1}}


The context of commands executed via an entity event response defined in a behavior pack, or via entity class in a script, or an animation controller in a behavior pack, or a dialogue of an [[Minecraft:NPC]] entity is:
The context of commands executed via an entity event response defined in a behavior pack, or via entity class in a script, or an animation controller in a behavior pack, or a dialogue of an [[Minecraft:NPC]] entity is:
* Execution Permission Level: 1
* Execution Permission Level: 1
* Executor Name: The name of the entity (Dynamic value {{MC/In|bedrock}})
* Executor Name: The name of the entity (Dynamic value {{in|bedrock}})
* Executor Entity: The entity
* Executor Entity: The entity
* Execution Dimension: The dimension the entity is in (Dynamic value {{MC/In|bedrock}})
* Execution Dimension: The dimension the entity is in (Dynamic value {{in|bedrock}})
* Execution Position: The position of the entity (Dynamic value {{MC/In|bedrock}})
* Execution Position: The position of the entity (Dynamic value {{in|bedrock}})
* Execution Rotation: The rotation of the rotation (Dynamic value {{MC/In|bedrock}})
* Execution Rotation: The rotation of the rotation (Dynamic value {{in|bedrock}})
* Execution Output Callback: If via entity class in a script, returns the success count
* Execution Output Callback: If via entity class in a script, returns the success count


==== Dimension in scripts ====
==== Dimension in scripts ====
{{MC/Exclusive|bedrock|section=1}}
{{exclusive|bedrock|section=1}}
The context of commands executed via Dimension class in a script:  
The context of commands executed via Dimension class in a script:  
* Execution Permission Level: 1
* Execution Permission Level: 1
Line 257: Line 257:


=== Raw JSON text resolution ===
=== Raw JSON text resolution ===
When resolving a raw JSON text, relative coordinates, local coordinates, and target selectors in it are resolved based on a command context. The Execution Output Callback parameter in the command context is not used by raw JSON text.
When resolving a [[Minecraft:raw JSON text]], relative coordinates, local coordinates, and target selectors in it are resolved based on a command context. The Execution Output Callback parameter in the command context is not used by raw JSON text.


Note that if the {{MC/Nbt|string|name}} field is <code>*</code> in a <code>score</code> type text, the reader's own score is displayed. In addition to the command context, the readers of the raw JSON text are also listed below.
Note that if the {{nbt|string|name}} field is <code>*</code> in a <code>score</code> type text, the reader's own score is displayed. In addition to the command context, the readers of the raw JSON text are also listed below.


==== Called by a command ====
==== Called by a command ====
A raw JSON text called by a command inherits the context of the command. Including:
A [[Minecraft:raw JSON text]] called by a command inherits the context of the command. Including:
* {{MC/Cmd|bossbar}}{{only|je}}
* {{cmd|bossbar}}{{only|je}}
** Reader: None
** Reader: None
* {{MC/Cmd|tellraw}}
* {{cmd|tellraw}}
** Reader: Each player that receives the message
** Reader: Each player that receives the message
* {{MC/Cmd|title}}
* {{cmd|title}}
** Reader: Each player that receives the title
** Reader: Each player that receives the title


==== Written book being opened by a player ====
==== Written book being opened by a player ====
{{MC/Exclusive|java|section=1}}
{{exclusive|java|section=1}}
The context when a written book is being opened by a player is:
The context when a [[Minecraft:written book]] is being opened by a player is:
* Execution Permission Level: The permission level of the player
* Execution Permission Level: The permission level of the player
* Executor Name: The name of the player
* Executor Name: The name of the player
Line 283: Line 283:


==== Written book being placed on a lectern ====
==== Written book being placed on a lectern ====
{{MC/Exclusive|java|section=1}}
{{exclusive|java|section=1}}
The context when a written book is being placed on a [[Minecraft:lectern]] is:
The context when a written book is being placed on a [[Minecraft:lectern]] is:
* Execution Permission Level: 2
* Execution Permission Level: 2
Line 295: Line 295:


==== Sign ====
==== Sign ====
{{MC/Exclusive|java|section=1}}
{{exclusive|java|section=1}}
The context when the text in a [[Minecraft:sign]] block is being changed via NBT is:
The context when the text in a [[Minecraft:sign]] block is being changed via NBT is:
* Execution Permission Level: 2
* Execution Permission Level: 2
Line 307: Line 307:


==== Text display entity ====
==== Text display entity ====
{{MC/Exclusive|java|section=1}}
{{exclusive|java|section=1}}
The context when the text in a text display entity is being changed  
The context when the text in a [[Minecraft:text display]] entity is being changed  
* Execution Permission Level: 2
* Execution Permission Level: 2
* Executor Name: The name of the text display entity
* Executor Name: The name of the text display entity
Line 319: Line 319:


==== Item modifier ====
==== Item modifier ====
{{MC/Exclusive|java|section=1}}
{{exclusive|java|section=1}}
In item modifier, <code>set_name</code> and <code>set_lore</code> functions resolve the raw JSON text only if the entity specified with {{MC/Nbt|string|entity}} does exist, with the following context:
In [[Minecraft:item modifier]], <code>set_name</code> and <code>set_lore</code> functions resolve the raw JSON text only if the entity specified with {{nbt|string|entity}} does exist, with the following context:
* Execution Permission Level: 2
* Execution Permission Level: 2
* Executor Name: The name of the entity specified with {{MC/Nbt|string|entity}}
* Executor Name: The name of the entity specified with {{nbt|string|entity}}
* Executor Entity: The entity specified with {{MC/Nbt|string|entity}}
* Executor Entity: The entity specified with {{nbt|string|entity}}
* Execution Dimension: The dimension that the entity specified with {{MC/Nbt|string|entity}} is in
* Execution Dimension: The dimension that the entity specified with {{nbt|string|entity}} is in
* Execution Position: The position of the entity specified with {{MC/Nbt|string|entity}}
* Execution Position: The position of the entity specified with {{nbt|string|entity}}
* Execution Rotation: The rotation of the entity specified with {{MC/Nbt|string|entity}}
* Execution Rotation: The rotation of the entity specified with {{nbt|string|entity}}
* Execution Anchor: Feet
* Execution Anchor: Feet
* Reader: The entity specified with {{MC/Nbt|string|entity}}
* Reader: The entity specified with {{nbt|string|entity}}


== History ==
== History ==
{{HistoryTable
{{HistoryTable
|{{MC/HistoryLine|java}}
|{{HistoryLine|java}}
|{{HistoryLine||1.13|dev=17w45a|Reworked {{MC/Cmd|execute}} command. Now can modify each context parameter independently.
|{{HistoryLine||1.13|dev=17w45a|Reworked {{cmd|execute}} command. Now can modify each context parameter independently.
|Removed the [[Minecraft:Commands/stats|command stat]] callback in command context.
|Removed the [[Minecraft:Commands/stats|command stat]] callback in command context.
|Added {{MC/Cmd|execute store ...}}, allowing to add custom output callbacks into the command context.}}
|Added {{cmd|execute store ...}}, allowing to add custom output callbacks into the command context.}}
|{{HistoryLine|||dev=18w02a|Added Execution Anchor parameter.
|{{HistoryLine|||dev=18w02a|Added Execution Anchor parameter.
|Added {{MC/Cmd|execute anchored ...}}, which can modify the execution anchor.}}
|Added {{cmd|execute anchored ...}}, which can modify the execution anchor.}}
|{{HistoryLine||1.20|dev=23w16a|Now {{MC/Cmd|return}} command sets the return values of a function via command context.}}
|{{HistoryLine||1.20|dev=23w16a|Now {{cmd|return}} command sets the return values of a [[Minecraft:function]] via command context.}}
|{{HistoryLine||1.20.3|dev=23w41a|Now functions called by a {{MC/Cmd|function}} command no longer inherit its output callbacks.}}
|{{HistoryLine||1.20.3|dev=23w41a|Now functions called by a {{cmd|function}} command no longer inherit its output callbacks.}}
|{{HistoryLine|||dev=23w44a|Now {{MC/Cmd|return}} command sets the return values of a function via new function system, instead of via command context.}}
|{{HistoryLine|||dev=23w44a|Now {{cmd|return}} command sets the return values of a [[Minecraft:function]] via new function system, instead of via command context.}}


|{{MC/HistoryLine|bedrock}}
|{{HistoryLine|bedrock}}
|{{HistoryLine||1.9.0|dev=beta 1.9.0.2|Commands from command blocks no longer extend to other dimensions.<ref>{{bug|MCPE-33892|||fixed}}</ref>}}
|{{HistoryLine||1.9.0|dev=beta 1.9.0.2|Commands from command blocks no longer extend to other dimensions.<ref>{{bug|MCPE-33892|||fixed}}</ref>}}
|{{HistoryLine||1.13.0|dev=beta 1.13.0.1|Now {{MC/Cd|detect}} option in {{MC/Cd|/execute}} uses the position and rotation of the executor entity.<ref>{{bug|MCPE-29942|||fixed}}</ref>}}
|{{HistoryLine||1.13.0|dev=beta 1.13.0.1|Now {{cd|detect}} option in {{cd|/execute}} uses the position and rotation of the executor entity.<ref>{{bug|MCPE-29942|||fixed}}</ref>}}
|{{HistoryLine||1.16.220|dev=beta 1.16.220.50|Using {{MC/Cmd|teleport}} with {{MC/Cd|/execute}} command now uses the dimension of the executor entity<ref>{{bug|MCPE-44104|||fixed}}</ref>}}
|{{HistoryLine||1.16.220|dev=beta 1.16.220.50|Using {{cmd|teleport}} with {{cd|/execute}} command now uses the dimension of the executor entity<ref>{{bug|MCPE-44104|||fixed}}</ref>}}
|{{HistoryLine||1.19.10|exp=Upcoming Creator Features|dev=Preview 1.19.10.20|Added new {{MC/Cmd|execute|link=None}} syntax, which can modify each context parameter independently.}}
|{{HistoryLine||1.19.10|exp=Upcoming Creator Features|dev=Preview 1.19.10.20|Added new {{cmd|execute|link=None}} syntax, which can modify each context parameter independently.}}
|{{HistoryLine||1.19.50|dev=Preview 1.19.50.23|The new {{MC/Cmd|execute|link=none}} syntax is no longer behind the "Upcoming Creator Features" experimental toggle.
|{{HistoryLine||1.19.50|dev=Preview 1.19.50.23|The new {{cmd|execute|link=none}} syntax is no longer behind the "Upcoming Creator Features" experimental toggle.
|Running {{MC/Cmd|execute as}} from Command Blocks no longer inherits rotation from the entity.<ref>{{bug|MCPE-162680|||fixed}}</ref>}}
|Running {{cmd|execute as}} from Command Blocks no longer inherits rotation from the entity.<ref>{{bug|MCPE-162680|||fixed}}</ref>}}
|{{HistoryLine||1.19.70|dev=beta 1.19.70.20|
|{{HistoryLine||1.19.70|dev=beta 1.19.70.20|
Rotation in the {{MC/Cmd|teleport}} command is now relative to the execution rotation instead of the rotation of the target entity.}}
Rotation in the {{cmd|teleport}} command is now relative to the execution rotation instead of the rotation of the target entity.}}
}}
}}


Line 357: Line 357:


== Navigation ==
== Navigation ==
{{MC/Navbox Java Edition}}
{{Navbox Java Edition}}
{{MC/Navbox Bedrock Edition}}
{{Navbox Bedrock Edition}}


ja:コマンド文脈
[[Minecraft:ja:コマンド文脈]]
zh:命令上下文
[[Minecraft:zh:命令上下文]]

Latest revision as of 11:08, 15 April 2026

Command context (also know as execution context, command origin, command source stack) is a set of parameters like the executor and execution environment. It is used to provide context for Minecraft:command execution or Minecraft:raw JSON text resolution.

Behaviors

There are mainly 8Template:Only/7Template:Only parameters in command context:

When Minecraft:commands are executed or Minecraft:raw JSON texts are resolved in different scenarios, corresponding contexts are provided, to allow them to work in different scenarios.

When running a function with the Template:Cmd command, the context for commands inside the function inherits the Executor Parameters, Execution Environment Parameters, and Execution Permission Level from the context of Template:Cmd command, while the Execution Output Callback is not inherited. Template:IN, the Execution Permission Level of commands in a function is limited to 2 or below, so, if the Template:Cmd command is executed with 2 or higher Execution Permission Level, the command inside the function is executed with Execution Permission Level being 2.

Template:IN, the context for commands in functions scheduled by Template:Cmd command also inherits the Executor Parameters, Execution Environment Parameters, and Execution Permission Level from the context of Template:Cmd command.

Static values and Dynamic values

Template:IN, the Executor Name and each Execution Environment Parameters in a context are static, and each of them exists independently. In other words, if these parameters are not modified with Template:Cmd, they do not change during the command execution, or during the execution of each command in a function. For example, even if the name of the Executor Entity is modified in a function, it does not change the Executor Name in the context of subsequent commands in the function; For another example, for Template:Cmd, even if a Template:Cmd command in a function Template:Cd changes the position, rotation, or dimension of the sheep entity, it does not affect the Execution Position, Execution Rotation, and Execution Dimension in the context of subsequent commands in the function, since the values of all these parameters are static after being modified by the Template:Cd subcommand and they are no longer related to the sheep entity itself.

Template:IN parameter values are static in some cases but dynamic in other cases. When dynamic, the value of a parameter does not exist independently, but rather it is a reference to an entity and fetches values from the entity in real time. For example, when an Execution Position is dynamic, it depends on an entity and obtains its position in real time, i.e., if the entity is moved in a function, the Execution Position in the context of subsequent commands in the function is the position of the entity after the move.<ref>Template:Bug</ref>

Specifically, dynamic values exist in the following cases:

  • When the player executes a command in the chat bar, or a command is requested through a WebSocket server connected with the client, the Execution Position, Execution Rotation, and Execution Dimension are dynamic and referring to the player entity.
  • When a command block minecart executes the command in it, the Execution Position and Execution Rotation are dynamic, referring to this minecart entity.
  • When an entity event response defined in a behavior pack executes a command, or a script executes a command with entity class, or an animation controller in a behavior pack executes a command, or an NPC executes a command, the Executor Name, Execution Position, Execution Rotation, and Execution Dimension are dynamic and referring to the entity.
  • Template:Cmd command converts the Executor Name to a dynamic value referring to the Executor Entity. That is, when a command is executed in a Template:Cmd command, the Executor Name of it is always dynamic, fetched from the Executor Entity in real time, regardless of whether there is a static Executor Name in the context of the Template:Cmd.
  • Template:Cd and Template:Cd subcommands in Template:Cmd set the Execution Position to a dynamic value, referring to the specified entity.
  • Template:Cd and Template:Cd subcommands in Template:Cmd set the Execution Rotation to a dynamic value, referring to the specified entity.

If the Execution Position is dynamic, it fetches the position from an entity in real time. But, if this entity is Minecraft:riding on another entity, there is a special treatment:

  • If the hitbox bottom of this entity is lower than the hitbox bottom of the mount, the mount's hitbox bottom is used as the Y coordinate of the Execution Position.
    • For example, if a player is riding on a minecart, the player's X and Z coordinates and the minecart's Y coordinate are used as the Execution Position.

If the entity which a dynamic value refers to has been removed from the world (e.g., an armor stand is killed, or a sheep finishes its death animation), the dynamic value cannot be fetched. In this case, default values of parameters are used:

  • Executor Name defaults to an empty string
  • Execution Dimension defaults to empty
  • Execution Position defaults to (0, 0, 0)
  • Execution Rotation defaults to (0, 0)

For example, when Template:Cmd is executed in the command block, as mentioned above, the Executor Name converts to a dynamic value by the Template:Cmd command, referring to the Executor Entity. Since there is no Executor Entity in the context, the Executor Name of Template:Cmd is an empty string.

For another example, if the Executor Entity is removed before a function schedule (scheduled by Template:Cmd) is executed, the schedule cannot be executed because the Execution Dimension is missing in the context.

A dynamic value is converted to a static value in the following cases:

For example:

  • When a player executes Template:Cmd in the chat bar, the Execution Position, Execution Rotation and Execution Dimension of commands in the function are dynamic, referring to the player.
  • When a player executes Template:Cmd in the chat bar, the Execution Position of the Template:Cd is dynamic, while the Execution Rotation and Execution Dimension are static.
  • When a player executes Template:Cmd in the chat bar, the Execution Position and Execution Rotation of the Template:Cd} are dynamic, while the Execution Dimension is static.
  • When a player executes Template:Cmd in the chat bar, the Execution Position of the Template:Cd is dynamic, while the Execution Rotation and Execution Dimension are static.
  • When a player executes Template:Cmd in the chat bar, the Execution Position, Execution Rotation and Execution Dimension of the Template:Cd are all static.
  • When a player executes Template:Cmd in the chat bar, the Execution Position, Execution Rotation and Execution Dimension of the Template:Cd are all static.
  • If there are at least three entities in a dimension, and a player in the dimension executes Template:Cmd in the chat bar, the player is teleported to 3 blocks above its original position.

Parameters

Execution Permission Level

Template:Main The Execution Permission Level cannot be modified by Template:Cmd command.

Template:IN, in the context of commands in a function called by a Template:Cmd command, the Execution Permission Level is limited to 2 or less. If the Execution Permission Level of the Template:Cmd command is greater than 2, the commands in the function is executed with Execution Permission Level of 2.

Executor Parameters

There are two Executor Parameters, Executor Name and Executor Entity, where the Executor Entity parameter may be empty (e.g. when a command is executed by a command block). Executor parameters are used in the following scenarios:

They can be modified using Template:Cd, Template:Cd, and Template:Cd subcommands in Template:Cmd command. Template:IN, the Executor Name and Executor Entity are always modified at the same time. Template:IN, only the Executor Entity is modified, and as mentioned above, the Executor Name remains a dynamic value referring to the Executor Entity.

Execution Dimension

Execution Dimension is the Minecraft:dimension in which the command takes effect. Execution Dimension is used in the following scenarios:

Execution Dimension can be modified using Template:Cd and Template:Cd subcommands in the Template:Cmd command. Template:IN, the Template:Cmd command itself converts the Execution Dimension to a static value.

Execution Position

Execution Position is the Minecraft:coordinates that the command takes effect from. Execution Position is used in the following scenarios:

Execution Position can be modified using Template:Cd, Template:Cd, Template:Cd subcommands in Template:Cmd command, and Template:Cd subcommand also affects the Execution Position. Template:IN, Template:Cd and Template:Cd subcommands set the Execution Position to a dynamic value referring to the specified entity. Template:IN, Template:Cd, Template:Cd, Template:Cd, and Template:Cd subcommands convert the Execution Position to a static value.

Execution Rotation

Execution Rotation is the rotation that the command is executed with. Execution Rotation is used in the following scenarios:

Execution Rotation can be modified using Template:Cd, Template:Cd, and Template:Cd subcommands in the Template:Cmd command. Template:IN, Template:Cd and Template:Cd subcommands set the Execution Rotation to a dynamic value referring to the specified entity. Template:IN, Template:Cmd command itself and its Template:Cd, Template:Cd subcommands converts the Execution Rotation to a static value.

Execute Anchor

Template:Exclusive Execute Anchor determines whether the position of eyes or feet of the Executor Entity is used as the origin of Minecraft:local coordinates, and the start point of Template:Cd in Template:Cmd and of Template:Cd subcommand in Template:Cmd.

  • If it is foot, uses the Execution Position directly.
  • If it is eyes, based on the Execution Position, the eye height of the Executor Entity is added into the Y coordinate.

Execution Anchor are used only in the following scenarios:

Execute Anchor can be modified using Template:Cd subcommand in Template:Cmd command.

Executor Anchor parameter does not exist Template:In. Instead, Template:In, the Template:Cd subcommand in Template:Cmd directly modifies the Execution Position. If the Execution Position is originally dynamic, the Template:Cd subcommand converts it to a static value, being the position of feet or eyes of the entity being referred by the dynamic value; If the Execution Position is static, Template:Cd subcommand does not have any effect at all.

Execution Output Callback

Execution Output Callbacks are used to receive output values of a command.

A command executed in a Minecraft:command block, a Minecraft:minecart with command block, or a scriptTemplate:Only has a callback that sends the success count to the command block or minecart.

Template:IN, Template:Cd subcommand in Template:Cmd command can be used to add a new callback while the existing callbacks are retained. Existing callbacks cannot be cleared by Template:Cmd command.

The context of commands in a function called by a Template:Cmd or Template:Cmd does not inherit the Execution Output Callbacks of the Template:Cmd or Template:Cmd command.

Contexts in various scenarios

Command execution

Server console

The context of commands entered in the server console is:

  • Execution Permission Level: 4
  • Executor Name: Server
  • Executor Entity: None
  • Execution Dimension: Overworld
  • Execution Position: Bottom northwest corner of the block at the Minecraft:world spawnTemplate:Only/(0, 0, 0)Template:Only
  • Execution Rotation: (0, 0)
  • Execution AnchorTemplate:Only: Feet
  • Execution Output Callback: None

Functions executed by the server-side

A Minecraft:function is executed by the server-side when:

The context of commands in a function executed by the server-side is:

Player

The context of commands that are entered in the chat bar, or requested via a WebSocket server connected with the client Template:In, is:

  • Execution Permission Level: The permission level of the player
  • Executor Name: The name of the player; or External for commands requested by a WebSocket Template:In
  • Executor Entity: The player
  • Execution Dimension: The dimension the player is in (Dynamic value Template:In)
  • Execution Position: The position of the player (Dynamic value Template:In)
  • Execution Rotation: The rotation of the player (Dynamic value Template:In)
  • Execution AnchorTemplate:Only: Feet
  • Execution Output Callback: None

Command block

The context of commands that are executed in a Minecraft:command block is:

  • Execution Permission Level: 2Template:Only/1Template:Only
  • Executor Name: The custom name of the command block. If it has no custom name, defaults to @Template:Only/!Template:Only.
  • Executor Entity: None
  • Execution Dimension: The dimension the command block is in
  • Execution Position: The center of the command block
  • Execution Rotation: The rotation of the command blockTemplate:Only/(0, 0)Template:Only
  • Execution AnchorTemplate:Only: Feet
  • Execution Output Callback: Sends the success count to the command block

Minecart with command block

The context of commands that are executed in a Minecraft:minecart with command block is:

  • Execution Permission Level: 2Template:Only/1Template:Only
  • Executor Name: The custom name of the minecart. If it has no custom name, defaults to @Template:Only/!Template:Only.
  • Executor Entity: The minecart with command block
  • Execution Dimension: The dimension the minecart is in
  • Execution Position: The position of the minecart (Dynamic value Template:In)
  • Execution Rotation: The rotation of the minecart (Dynamic value Template:In)
  • Execution AnchorTemplate:Only: Feet
  • Execution Output Callback: Sends the success count to the command block

Sign

Template:Exclusive The context of commands executed when a player clicks a Minecraft:sign block is:

  • Execution Permission Level: 2
  • Executor Name: The name of the player
  • Executor Entity: The player
  • Execution Dimension: The dimension the sign block is in
  • Execution Position: The center of the block the sign is in
  • Execution Rotation: (0, 0)
  • Execution Anchor: Feet
  • Execution Output Callback: None

Advancement reward

Template:Exclusive The context of commands in the function called by a Template:Nbt reward in a custom advancement is:

  • Execution Permission Level: 2
  • Executor Name: The name of the player that gets the advancement
  • Executor Entity: The player that gets the advancement
  • Execution Dimension: The dimension the player is in
  • Execution Position: The position of the player
  • Execution Rotation: The rotation of the player
  • Execution Anchor: Feet
  • Execution Output Callback: None

Enchantment effect

Template:Exclusive The context of commands in the function called by a run_function enchantment effect is:

  • Execution Permission Level: 2
  • Executor Name: The name of the affected entity of the enchantment effect
  • Executor Entity: The affected entity of the enchantment effect
  • Execution Dimension: The dimension the enchantment takes effects in
  • Execution Position: The position the enchantment takes effects at
  • Execution Rotation: The rotation of the affected entity of the enchantment effect
  • Execution Anchor: Feet
  • Execution Output Callback: None

Entity in behavior packs

Template:Exclusive

The context of commands executed via an entity event response defined in a behavior pack, or via entity class in a script, or an animation controller in a behavior pack, or a dialogue of an Minecraft:NPC entity is:

  • Execution Permission Level: 1
  • Executor Name: The name of the entity (Dynamic value Template:In)
  • Executor Entity: The entity
  • Execution Dimension: The dimension the entity is in (Dynamic value Template:In)
  • Execution Position: The position of the entity (Dynamic value Template:In)
  • Execution Rotation: The rotation of the rotation (Dynamic value Template:In)
  • Execution Output Callback: If via entity class in a script, returns the success count

Dimension in scripts

Template:Exclusive The context of commands executed via Dimension class in a script:

  • Execution Permission Level: 1
  • Executor Name: Script Engine
  • Executor Entity: None
  • Execution Dimension: The dimension
  • Execution Position: (0, 0, 0)
  • Execution Rotation: (0, 0)
  • Execution Output Callback: Returns the success count

Raw JSON text resolution

When resolving a Minecraft:raw JSON text, relative coordinates, local coordinates, and target selectors in it are resolved based on a command context. The Execution Output Callback parameter in the command context is not used by raw JSON text.

Note that if the Template:Nbt field is * in a score type text, the reader's own score is displayed. In addition to the command context, the readers of the raw JSON text are also listed below.

Called by a command

A Minecraft:raw JSON text called by a command inherits the context of the command. Including:

Written book being opened by a player

Template:Exclusive The context when a Minecraft:written book is being opened by a player is:

  • Execution Permission Level: The permission level of the player
  • Executor Name: The name of the player
  • Executor Entity: The player
  • Execution Dimension: The dimension the player is in
  • Execution Position: The position of the player
  • Execution Rotation: The rotation of the player
  • Execution Anchor: Feet
  • Reader: The player

Written book being placed on a lectern

Template:Exclusive The context when a written book is being placed on a Minecraft:lectern is:

  • Execution Permission Level: 2
  • Executor Name: Lectern
  • Executor Entity: None
  • Execution Dimension: The dimension the lectern is in
  • Execution Position: The center of the block the lectern is in
  • Execution Rotation: (0, 0)
  • Execution Anchor: Feet
  • Reader: None

Sign

Template:Exclusive The context when the text in a Minecraft:sign block is being changed via NBT is:

  • Execution Permission Level: 2
  • Executor Name: Sign
  • Executor Entity: None
  • Execution Dimension: The dimension the sign is in
  • Execution Position: The center of the block the sign is in
  • Execution Rotation: (0, 0)
  • Execution Anchor: Feet
  • Reader: None

Text display entity

Template:Exclusive The context when the text in a Minecraft:text display entity is being changed

  • Execution Permission Level: 2
  • Executor Name: The name of the text display entity
  • Executor Entity: The text display entity
  • Execution Dimension: The dimension the text display entity is in
  • Execution Position: The position of the text display entity
  • Execution Rotation: The rotation of the text display entity
  • Execution Anchor: Feet
  • Reader: The text display entity

Item modifier

Template:Exclusive In Minecraft:item modifier, set_name and set_lore functions resolve the raw JSON text only if the entity specified with Template:Nbt does exist, with the following context:

  • Execution Permission Level: 2
  • Executor Name: The name of the entity specified with Template:Nbt
  • Executor Entity: The entity specified with Template:Nbt
  • Execution Dimension: The dimension that the entity specified with Template:Nbt is in
  • Execution Position: The position of the entity specified with Template:Nbt
  • Execution Rotation: The rotation of the entity specified with Template:Nbt
  • Execution Anchor: Feet
  • Reader: The entity specified with Template:Nbt

History

Template:HistoryTable

References

<references/>

Navigation

Template:Navbox Java Edition Template:Navbox Bedrock Edition

Minecraft:ja:コマンド文脈 Minecraft:zh:命令上下文