Minecraft:Commands/execute: Difference between revisions
More actions
Correct aligns function as flooring instead of rounding |
Remove broken links to missing pages |
||
| Line 1: | Line 1: | ||
{{about|the command in the current version (since | {{about|the command in the current version (since Java Edition 1.13 (17w45a) and [[Minecraft:Bedrock Edition 1.19.50]] (beta 1.19.50.23))|the command in former game versions|Commands/execute (old)}} | ||
{{Infobox command | {{Infobox command | ||
|name=execute | |name=execute | ||
| Line 5: | Line 5: | ||
* 2 {{only|java|short=1}} | * 2 {{only|java|short=1}} | ||
* 1 {{only|bedrock|short=1}} | * 1 {{only|bedrock|short=1}} | ||
|others= | |others=Cheats only{{only|bedrock|short=1}} | ||
}} | }} | ||
{{TOC|right}} | {{TOC|right}} | ||
Executes another | Executes another command but allows changing the executor, changing the position and angle it is executed at, adding preconditions, and storing its result. | ||
== Usage == | == Usage == | ||
| Line 14: | Line 14: | ||
* '''Modifier subcommands''' modify execution context variables to change the context the command is executed in. Subcommands following it execute with specific executor(s), execution position(s), execution dimension(s), execution rotation(s) and execution anchor. | * '''Modifier subcommands''' modify execution context variables to change the context the command is executed in. Subcommands following it execute with specific executor(s), execution position(s), execution dimension(s), execution rotation(s) and execution anchor. | ||
* '''Condition subcommands''' are used to test whether certain conditions are met, and either output the result or limit the execution of following subcommands. | * '''Condition subcommands''' are used to test whether certain conditions are met, and either output the result or limit the execution of following subcommands. | ||
* '''Store subcommands'''{{only|java|short=1}} can store the output values of the command in a | * '''Store subcommands'''{{only|java|short=1}} can store the output values of the command in a scoreboard or the data of an entity, [[Minecraft:block entity]], or command storage, and can also change the maximum or current value of a bossbar. | ||
* '''<code>run</code> subcommand''' is used for carrying out another command. | * '''<code>run</code> subcommand''' is used for carrying out another command. | ||
| Line 72: | Line 72: | ||
* The success counts of all branches are summed up. | * The success counts of all branches are summed up. | ||
* It is an integer greater than or equal to 0. | * It is an integer greater than or equal to 0. | ||
* When executing it with a | * When executing it with a command block, the success count is passed into the command block, which can be checked with a chained conditional command block or be read with a redstone comparator. | ||
== Syntax == | == Syntax == | ||
| Line 178: | Line 178: | ||
: Sets the ''execution anchor'' to the eyes or feet. Defaults to feet. | : Sets the ''execution anchor'' to the eyes or feet. Defaults to feet. | ||
: Running <code>positioned <pos> -> execute</code> resets to feet. | : Running <code>positioned <pos> -> execute</code> resets to feet. | ||
: Effectively recenters | : Effectively recenters local coordinates on either the eyes or feet, also changing the angle of the <code>facing</code> subcommand (of {{cmd|execute|link=none}} and {{cmd|teleport}}) works off of. | ||
: See also {{bug|MCPE-162681}} and {{bug|MCPE-165051}} for issues about {{el|bedrock}}. | : See also {{bug|MCPE-162681}} and {{bug|MCPE-165051}} for issues about {{el|bedrock}}. | ||
:;Syntax | :;Syntax | ||
| Line 271: | Line 271: | ||
==== in ==== | ==== in ==== | ||
: Sets the ''execution dimension'' and ''execution position''. | : Sets the ''execution dimension'' and ''execution position''. | ||
: It respects dimension scaling for relative and local coordinates: the ''execution position'' (only the X/Z part) is divided by 8 when changing from the Overworld to the Nether, and is multiplied by 8 when vice versa. Applies to | : It respects dimension scaling for relative and local coordinates: the ''execution position'' (only the X/Z part) is divided by 8 when changing from the Overworld to the Nether, and is multiplied by 8 when vice versa. Applies to custom dimensions as well. | ||
:;Syntax | :;Syntax | ||
: {{el|java}}: | : {{el|java}}: | ||
| Line 307: | Line 307: | ||
:: {{el|je|short=1}}: {{Argument|''relation''|}} | :: {{el|je|short=1}}: {{Argument|''relation''|}} | ||
::: A relation to the current executor entity. | ::: A relation to the current executor entity. | ||
:::* <code>attacker</code>: the last entity that damaged the current executor entity in the previous 5 seconds. Note that damage types in <code>minecraft:no_anger</code> tag bypass the record of attacker. | :::* <code>attacker</code>: the last entity that damaged the current executor entity in the previous 5 seconds. Note that damage types in <code>minecraft:no_anger</code> tag bypass the record of attacker. Interaction entities do not forget attacker after 5 seconds. Some mobs forget the attacker when ceasing their aggression. | ||
:::* <code>controller</code>: the entity that is riding and controlling the current executor entity. See | :::* <code>controller</code>: the entity that is riding and controlling the current executor entity. See Riding for details. | ||
:::* <code>leasher</code>: the entity leading the current executor entity with a leash. | :::* <code>leasher</code>: the entity leading the current executor entity with a leash. | ||
:::* <code>origin</code>: the entity that cause the summon of the current executor entity. For example, the shooter of an arrow, the primer of a primed TNT entity. | :::* <code>origin</code>: the entity that cause the summon of the current executor entity. For example, the shooter of an arrow, the primer of a primed TNT entity. | ||
:::* <code>owner</code>: the owner of the current executor entity if it is a tameable animal. | :::* <code>owner</code>: the owner of the current executor entity if it is a tameable animal. | ||
:::* <code>passengers</code>: all entities that are directly riding the current executor entity, no sub-passengers. | :::* <code>passengers</code>: all entities that are directly riding the current executor entity, no sub-passengers. | ||
:::* <code>target</code>: the target that the current executor entity intends on attacking. | :::* <code>target</code>: the target that the current executor entity intends on attacking. Interaction entities can select the last entity that interacted with them. | ||
:::* <code>vehicle</code>: the entity ridden by the current executor entity. | :::* <code>vehicle</code>: the entity ridden by the current executor entity. | ||
:;Result | :;Result | ||
| Line 325: | Line 325: | ||
==== positioned ==== | ==== positioned ==== | ||
: Sets the ''execution position'', without changing ''execution rotation'' or ''dimension''; can match an entity's position, or at one block above the Y-level stored in the specified | : Sets the ''execution position'', without changing ''execution rotation'' or ''dimension''; can match an entity's position, or at one block above the Y-level stored in the specified heightmap{{only|java|short=1}}. | ||
:;Syntax | :;Syntax | ||
: {{el|java}}: | : {{el|java}}: | ||
| Line 345: | Line 345: | ||
:: '''Option:''' <code>positioned over <heightmap></code> | :: '''Option:''' <code>positioned over <heightmap></code> | ||
::: {{el|je|short=1}}: {{Argument|heightmap|heightmap}} | ::: {{el|je|short=1}}: {{Argument|heightmap|heightmap}} | ||
:::: Specifies the | :::: Specifies the heightmap. | ||
:::: Must be one of <code>world_surface</code>, <code>motion_blocking</code>, <code>motion_blocking_no_leaves</code>, and <code>ocean_floor</code>. | :::: Must be one of <code>world_surface</code>, <code>motion_blocking</code>, <code>motion_blocking_no_leaves</code>, and <code>ocean_floor</code>. | ||
:;Result | :;Result | ||
| Line 424: | Line 424: | ||
# [[#ifunlessdimension|(if|unless) dimension]] - Tests the dimension of the execution{{only|java}} | # [[#ifunlessdimension|(if|unless) dimension]] - Tests the dimension of the execution{{only|java}} | ||
# [[#ifunlessentity|(if|unless) entity]] – Tests whether an entity like the one given is real | # [[#ifunlessentity|(if|unless) entity]] – Tests whether an entity like the one given is real | ||
# [[#ifunlessfunction|(if|unless) function]] – Runs a function or function | # [[#ifunlessfunction|(if|unless) function]] – Runs a function or function tag and tests its return value{{only|java}} | ||
# [[#ifunlessitems|(if|unless) items]] – Tests an inventory slot for a given item{{only|java}} | # [[#ifunlessitems|(if|unless) items]] – Tests an inventory slot for a given item{{only|java}} | ||
# [[#ifunlessloaded|(if|unless) loaded]] – Tests if a given position is loaded in the world{{only|java}} | # [[#ifunlessloaded|(if|unless) loaded]] – Tests if a given position is loaded in the world{{only|java}} | ||
# [[#ifunlesspredicate|(if|unless) predicate]] – Tests a | # [[#ifunlesspredicate|(if|unless) predicate]] – Tests a predicate{{only|java}} | ||
# [[#ifunlessscore|(if|unless) score]] - Tests a target's score | # [[#ifunlessscore|(if|unless) score]] - Tests a target's score | ||
| Line 523: | Line 523: | ||
==== (if|unless) data ==== | ==== (if|unless) data ==== | ||
{{exclusive|section=1|java}} | {{exclusive|section=1|java}} | ||
: Checks whether the targeted block, entity or | : Checks whether the targeted block, entity or storage has any data tag for a given path. | ||
:;Syntax | :;Syntax | ||
| Line 547: | Line 547: | ||
:: '''Option:''' <code>(if|unless) data '''storage <source> <path>'''</code> | :: '''Option:''' <code>(if|unless) data '''storage <source> <path>'''</code> | ||
::: {{Argument|source|resource_location}} | ::: {{Argument|source|resource_location}} | ||
:::: The | :::: The resource location of the storage for data testing. | ||
:::: {{arg desc|je=resource_location}} | :::: {{arg desc|je=resource_location}} | ||
::: {{Argument|path|nbt_path}} | ::: {{Argument|path|nbt_path}} | ||
| Line 652: | Line 652: | ||
::: Slots to test. A string that consists of a "slot type" and an optional "slot number", in the format of <code><slot_type></code>, <code><slot_type>.<slot_number></code> or <code><slot_type>.*</code>. See [[Minecraft:Slot]] for details. | ::: Slots to test. A string that consists of a "slot type" and an optional "slot number", in the format of <code><slot_type></code>, <code><slot_type>.<slot_number></code> or <code><slot_type>.*</code>. See [[Minecraft:Slot]] for details. | ||
:: {{Argument|item_predicate|item_predicate}} | :: {{Argument|item_predicate|item_predicate}} | ||
::: Item or item | ::: Item or item tag to test for. | ||
::: {{arg desc|je=item_predicate}} | ::: {{arg desc|je=item_predicate}} | ||
| Line 670: | Line 670: | ||
==== (if|unless) loaded ==== | ==== (if|unless) loaded ==== | ||
{{exclusive|section=1|java}} | {{exclusive|section=1|java}} | ||
: Checks if | : Checks if chunks at a given position is fully loaded (Entity Ticking). | ||
:;Syntax | :;Syntax | ||
| Line 690: | Line 690: | ||
==== (if|unless) predicate ==== | ==== (if|unless) predicate ==== | ||
{{exclusive|section=1|java}} | {{exclusive|section=1|java}} | ||
: Checks whether the predicate successes. See | : Checks whether the predicate successes. See Predicate for more information. | ||
:;Syntax | :;Syntax | ||
| Line 697: | Line 697: | ||
:: {{Argument|predicate|resource_location}} or inline definition | :: {{Argument|predicate|resource_location}} or inline definition | ||
::: The predicate to be checked whether it evaluates to a positive result. | ::: The predicate to be checked whether it evaluates to a positive result. | ||
::: Must either be a | ::: Must either be a resource location or an inline definition, following the same structure as predicates encoded in the SNBT format. | ||
::: The command is resolved during command execution into unregistered content, or sent to the client to resolve into a client-side content. | ::: The command is resolved during command execution into unregistered content, or sent to the client to resolve into a client-side content. | ||
| Line 810: | Line 810: | ||
{{Anchor|storeentity}} | {{Anchor|storeentity}} | ||
==== store (result|success) entity ==== | ==== store (result|success) entity ==== | ||
: Save the final command's output value in one of an entity's data tags. Store as a byte, short, int, long, float, or double. If the output value is a decimal, it is rounded first and then multiplied by <code><scale></code>. Like the {{cmd|data}} command, "/execute store" cannot modify | : Save the final command's output value in one of an entity's data tags. Store as a byte, short, int, long, float, or double. If the output value is a decimal, it is rounded first and then multiplied by <code><scale></code>. Like the {{cmd|data}} command, "/execute store" cannot modify player NBT. | ||
:;Syntax | :;Syntax | ||
| Line 851: | Line 851: | ||
==== store (result|success) storage ==== | ==== store (result|success) storage ==== | ||
: Uses the {{cd|<path>}} within | : Uses the {{cd|<path>}} within storage {{cd|<target>}} to store the output value in. Store as a byte, short, int, long, float, or double. If the output value is a decimal, it is rounded first and then multiplied by {{cd|<scale>}}. If the storage does not yet exist, it gets created. | ||
:;Syntax | :;Syntax | ||
| Line 858: | Line 858: | ||
:;Arguments | :;Arguments | ||
:: {{Argument|target|resource_location}} | :: {{Argument|target|resource_location}} | ||
::: Target storage container, as a | ::: Target storage container, as a resource location. | ||
::: {{arg desc|je=resource_location}} | ::: {{arg desc|je=resource_location}} | ||
:: {{Argument|path|nbt_path}} | :: {{Argument|path|nbt_path}} | ||
| Line 889: | Line 889: | ||
:;Technical{{only|java}} | :;Technical{{only|java}} | ||
:: Resets the command node to the root of the | :: Resets the command node to the root of the command dispatcher | ||
:: Information on modelling commands as chat text: | :: Information on modelling commands as chat text: | ||
::* The ''command dispatcher'' is what starts when the player begins a message with a forward-slash (<code>/</code>). | ::* The ''command dispatcher'' is what starts when the player begins a message with a forward-slash (<code>/</code>). | ||
| Line 899: | Line 899: | ||
=== More examples === | === More examples === | ||
* Teleport all players who have an item enchanted with | * Teleport all players who have an item enchanted with Efficiency in their first hotbar slot to coordinates (0, 64, 0): | ||
*: <code>/execute as @a if data entity @s Inventory[{Slot:0b}].tag.Enchantments[{id:"minecraft:efficiency"}] run tp @s 0 64 0</code>{{only|java}} | *: <code>/execute as @a if data entity @s Inventory[{Slot:0b}].tag.Enchantments[{id:"minecraft:efficiency"}] run tp @s 0 64 0</code>{{only|java}} | ||
* Create a smoke | * Create a smoke particle three blocks in front of all players: | ||
*: <code>/execute as @a at @s anchored eyes run particle smoke ^ ^ ^3</code>{{only|java}} | *: <code>/execute as @a at @s anchored eyes run particle smoke ^ ^ ^3</code>{{only|java}} | ||
* Place a saddle on [[Minecraft:pig]]s located within 5 blocks of the executing player, and remove saddles from pigs located over 5 blocks away from the executing player: | * Place a saddle on [[Minecraft:pig]]s located within 5 blocks of the executing player, and remove saddles from pigs located over 5 blocks away from the executing player: | ||
| Line 909: | Line 909: | ||
* Make a player say "Score is reset" in chat if the score <code>test</code> is not set, eg by doing "scoreboard players reset @s test": | * Make a player say "Score is reset" in chat if the score <code>test</code> is not set, eg by doing "scoreboard players reset @s test": | ||
*:<code>/execute as @a unless score @s test = @s test run say "Score is reset"</code> | *:<code>/execute as @a unless score @s test = @s test run say "Score is reset"</code> | ||
* Hurl all | * Hurl all skeletons skyward who are within 3 blocks of the nearest player: | ||
*:<code>/execute at @p as @e[type=skeleton,distance=..3] run data merge entity @s {Motion:[0.0,2.0,0.0]}</code>{{only|java}} | *:<code>/execute at @p as @e[type=skeleton,distance=..3] run data merge entity @s {Motion:[0.0,2.0,0.0]}</code>{{only|java}} | ||
* Kill all [[Minecraft:zombie]]s who have no headgear: | * Kill all [[Minecraft:zombie]]s who have no headgear: | ||
*:<code>/execute as @e[type=zombie] unless data entity @s ArmorItems[3].id run kill @s</code>{{only|java}} | *:<code>/execute as @e[type=zombie] unless data entity @s ArmorItems[3].id run kill @s</code>{{only|java}} | ||
* Set every player's <code>nearbyRedSheep</code> | * Set every player's <code>nearbyRedSheep</code> scoreboard objective equal to the amount of red sheep within twenty blocks: | ||
*:<code>/execute as @a at @s store result score @s nearbyRedSheep if entity @e[type=sheep,nbt={Color:14},distance=..20]</code> | *:<code>/execute as @a at @s store result score @s nearbyRedSheep if entity @e[type=sheep,nbt={Color:14},distance=..20]</code> | ||
| Line 961: | Line 961: | ||
{{Navbox commands}} | {{Navbox commands}} | ||
de:Befehl/execute | |||
es:Comandos/execute | |||
fr:Commandes | |||
ja:コマンド/execute | |||
ko:명령어/execute | |||
pl:Polecenia/execute | |||
pt:Comandos/execute | |||
ru:Команды консоли/execute | |||
uk:Команди консолі/execute | |||
zh:命令/execute | |||
Revision as of 15:44, 9 April 2026
|
For the {{{Description}}} of the same name, see [[{{{Destination}}}]]. |
Template:Infobox command Template:TOC Executes another command but allows changing the executor, changing the position and angle it is executed at, adding preconditions, and storing its result.
Usage
There are fourteenTemplate:Only / elevenTemplate:Only subcommands (aka. instructions) for the /execute command. Multiple subcommands can be chained after /execute. Subcommands are divided into 4Template:Only / 3Template:Only categories: modifier subcommands, condition subcommands, store subcommandsTemplate:Only, and the run subcommand.
- Modifier subcommands modify execution context variables to change the context the command is executed in. Subcommands following it execute with specific executor(s), execution position(s), execution dimension(s), execution rotation(s) and execution anchor.
- Condition subcommands are used to test whether certain conditions are met, and either output the result or limit the execution of following subcommands.
- Store subcommandsTemplate:Only can store the output values of the command in a scoreboard or the data of an entity, Minecraft:block entity, or command storage, and can also change the maximum or current value of a bossbar.
runsubcommand is used for carrying out another command.
All needed subcommands can be concatenated together. Subcommands other than the run subcommand can be arranged arbitrarily and used multiple times. The run subcommand can be used only once, at the end of the chain. Only a run subcommand or a condition subcommand may finalize the chain; otherwise, the command is unparseable.
Subcommands and forking
The game processes the subcommand chain in order — from left to right. For example, the following commands are different:
- All entities move one block forward:
execute as @e at @s run tp ^ ^ ^1 - All entities are teleported to one block in front of the executor:
execute at @s as @e run tp ^ ^ ^1
Some subcommands can fork the command execution into multiple branches, causing subcommands following it to be executed multiple times. For example, when the Template:Cd subcommand selects multiple entities, the subcommands following it execute once per entity.
If the Template:Cd command doesn't fork, it has only one branch — the main branch.
A branch may terminate halfway if, for example, the condition in a condition subcommand isn't met, or the Template:Cd subcommand selects zero entities. When terminating, a red message is shown.Template:Only If all branches terminate, the /execute command is said to have terminated.
Forking is different between Template:El and Template:El (see also Template:Bug and Template:Bug):
- Template:IN, the game processes subcommands in breadth-first, that is, executes subcommands one by one. So the game processes other subcommands before processing
runsubcommands, so therunsubcommand cannot affect other subcommands. - Template:IN, the game processes subcommands in depth-first. That is, after forking, all the following subcommands are considered as a whole and are executed once for each branch.
- For example, when there are two armor stands (A and B) in the world, and a player executes
execute as @e[type=armor_stand] as @e[type=armor_stand] run summon armor_stand, the behaviors of Template:El and Template:El are shown in the following pictures:
-
Java Edition
-
Bedrock Edition
Note that ... run execute ... has no overall effect in both versions. For example, the following commands are identical:
execute as @e[type=armor_stand] as @e[type=armor_stand] run summon armor_standexecute as @e[type=armor_stand] run execute as @e[type=armor_stand] run execute run execute run summon armor_stand
Template:IN, depth-first can be achieved via Template:Cmd, for example:
- When there are two armor stands (A and B) in the world, and a player executes
execute as @e[type=armor_stand] run function test, and in the function file is:execute as @e[type=armor_stand] run summon armor_stand
- It behaves like
/execute as @e[type=armor_stand] as @e[type=armor_stand] run summon armor_standin Template:El.
Template:IN, there is no way to achieve breadth-first.
Output values
Stored values
Template:Exclusive
Template:IN, a branch outputs a success value and a result value (see also Minecraft:Commands#Output) after it is fully executed (does not terminate halfway). These two output values:
- Are the outputs of each execution branch, rather than the Template:Cd command itself;
- Come from the last subcommand (may be a condition subcommand or a
runsubcommand). - Both are integers. The
successvalue is always 0 or 1. Theresultvalue is rounded down if not an integer. - If in a branch the last subcommand fails, both the two values are 0 in the branch.
- The two values can be stored through store subcommandsTemplate:Only.
Note that these two values are from the conditional subcommand at the end or from the command in the Template:Cd subcommand, and are the output values of each branch, rather than of the whole Template:Cd command.
If executing Template:Cmd command in the Template:Cd subcommand, these two output values are not available under certain conditions. See the Template:Cmd for details.
If the command execution is forked after a store subcommand, the storage operation is applied on each branch. The output value of each branch is stored after the branch is fully executed. If the store locations are the same between all branches, the output value of a later-executing branch directly overwrites the output value of the earlier-executed branch, rather than being accumulated. So, after the whole Template:Cd command is executed, the value at this storage location is the output of the last branch executed.
Success count
Like most commands, /execute command itself also has a success count (whether or not terminates halfway):
- It is the output value of the Template:Cd command itself, rather than of each execution branch.
- It comes from the last subcommand (may be a condition subcommand or a
runsubcommand). - The success counts of all branches are summed up.
- It is an integer greater than or equal to 0.
- When executing it with a command block, the success count is passed into the command block, which can be checked with a chained conditional command block or be read with a redstone comparator.
Syntax
There are fourteenTemplate:Only / elevenTemplate:Only instructions (aka. subcommands) for the /execute command, and each has its own special syntax, so describing syntax takes a large branching tree.
- Full syntax tree
- Java Edition
/execute ...... align <axes> -> execute... anchored <anchor> -> execute... as <targets> -> execute... at <targets> -> execute... facing (<pos>|entity <targets> <anchor>) -> execute... in <dimension> -> execute... on <relation> -> execute... positioned (<pos>|as <targets>|over <heightmap>) -> execute... rotated (<rot>|as <targets>) -> execute... store (result|success) ...... block <targetPos> <path> <type> <scale> -> execute... bossbar <id> (max|value) -> execute... entity <target> <path> <type> <scale> -> execute... score <targets> <objective> -> execute... storage <target> <path> <type> <scale> -> execute
... summon <entity> -> execute... (if|unless) ...... biome <pos> <biome> -> [execute]... block <pos> <block> -> [execute]... blocks <start> <end> <destination> (all|masked) -> [execute]... data ...... block <sourcePos> <path> -> [execute]... entity <source> <path> -> [execute]... storage <source> <path> -> [execute]
... dimension <dimension> -> [execute]... entity <entities> -> [execute]... function <function> -> execute... items ...... block <sourcePos> <slots> <item_predicate> -> [execute]... entity <source> <slots> <item_predicate> -> [execute]
... loaded <pos> -> [execute]... predicate <predicate> -> [execute]... score <target> <targetObjective> ...... (<|<=|=|>|>=) <source> <sourceObjective> -> [execute]... matches <range> -> [execute]
... run <command>
- where
-> executerepresents the start of another subcommand that is required;-> [execute]represents the start of another subcommand that is optional.
- Bedrock Edition
/execute ...... align <axes: string> <chainedCommand: ExecuteChainedOption_0>... anchored <eyes|feet> <chainedCommand: ExecuteChainedOption_0>... as <origin: target> <chainedCommand: ExecuteChainedOption_0>... at <origin: target> <chainedCommand: ExecuteChainedOption_0>... facing ...... <position: x y z> <chainedCommand: ExecuteChainedOption_0>... entity <origin: target> <eyes|feet> <chainedCommand: ExecuteChainedOption_0>
... in <dimension: Dimension> <chainedCommand: ExecuteChainedOption_0>... positioned ...... <position: x y z> <chainedCommand: ExecuteChainedOption_0>... as <origin: target> <chainedCommand: ExecuteChainedOption_0>
... rotated ...... <yaw: value> <pitch: value> <chainedCommand: ExecuteChainedOption_0>... as <origin: target> <chainedCommand: ExecuteChainedOption_0>
... <subcommand: Option_If_Unless> ...... block <position: x y z> <block: Block> ...... <blockStates: block states> [chainedCommand: ExecuteChainedOption_0]... [chainedCommand: ExecuteChainedOption_0]
... blocks <begin: x y z> <end: x y z> <destination: x y z> <scan mode: BlocksScanMode> [chainedCommand: ExecuteChainedOption_0]... entity <target: target> [chainedCommand: ExecuteChainedOption_0]... score <target: target> <objective: string> ...... <operation: compare operator> <source: target> <objective: string> [chainedCommand: ExecuteChainedOption_0]... matches <range: integer range> [chainedCommand: ExecuteChainedOption_0]
... run <command: command>
- where
chainedCommand: ExecuteChainedOption_0represents the start of another subcommand.
Modifier subcommands
align
- Updates the execution position, aligning to its current block position (integer coordinates). Applies only along specified axes.
- Effectively Template:Wikipedia the coordinates (i.e., Template:Wikipedia the coordinates down)
- Syntax
- Template:El:
align <axes> -> execute
- Template:El:
align <axes: string> <chainedCommand: ExecuteChainedOption_0>- Arguments
- Template:Argument
- Result
- Execution position in the given axes are floored, changing by less than 1 block.
- Unparseable if the argument is not specified correctly.
- Example
- Given (-1.8, 2.3, 5.9),
execute align xzchanges the position to (-2, 2.3, 5). - Given (2.4, -1.1, 3.8),
execute align yxz run spawnpoint @p ~ ~ ~sets the player's spawnpoint to (2, -2, 3).
anchored
- Sets the execution anchor to the eyes or feet. Defaults to feet.
- Running
positioned <pos> -> executeresets to feet. - Effectively recenters local coordinates on either the eyes or feet, also changing the angle of the
facingsubcommand (of Template:Cmd and Template:Cmd) works off of. - See also Template:Bug and Template:Bug for issues about Template:El.
- Syntax
- Template:El:
anchored <anchor> -> execute
- Template:El:
anchored <eyes|feet> <chainedCommand: ExecuteChainedOption_0>- Arguments
- Template:Argument
- Whether to anchor the executed command to eyes or feet.
- Must be either
eyesorfeet.
- Result
- Execution anchor is set to either the eyes or the feet.
- Unparseable if the argument is not specified correctly.
- Example
- The effect of Template:Command is to move a target by placing its feet at the given position.
execute anchored eyes run tp ^ ^ ^effectively teleports the executor's feet to where its eyes are.execute anchored eyes run tp ^5 ^ ^means "place the executor's feet 5 blocks left of where its eyes are"
as
- Sets the executor to target entity, without changing execution position, rotation, dimension, and anchor.
- Syntax
- Template:El:
as <targets> -> execute
- Template:El:
as <origin: target> <chainedCommand: ExecuteChainedOption_0>- Arguments
- Template:Argument
- Target entity/entities to become the new executor.
- Template:Arg desc
- Result
- Executor is updated to target entity (which changes the meaning of
@s). - Unparseable if the argument is not specified correctly.
- Forks if
<targets>ororigin: targetselects multiple entities. - Terminates current branch if
<targets>ororigin: targetfails to resolve to one or more entities (named players must be online). - Example
- Kill all sheep:
execute as @e[type=sheep] run kill @s - Make all villagers in loaded chunks invincible:
execute as @e[type=villager] run data merge entity @s {Invulnerable:1}Template:Only
at
- Sets the execution position, rotation, and dimension to match those of an entity; does not change executor.
- Syntax
- Template:El:
at <targets> -> execute
- Template:El:
at <origin: target> <chainedCommand: ExecuteChainedOption_0>- Arguments
- Template:Argument
- Target entity/entities to match position, rotation, and dimension with.
- Template:Arg desc
- Result
- Execution position, rotation, and dimension are updated to match target entity.
- Unparseable if the argument is not specified correctly.
- Forks if
<targets>ororigin: targetselects multiple entities. - Terminates current branch if
<targets>ororigin: targetfails to resolve to one or more entities (named players must be online). - Example
- Move all sheep upward 1 block:
execute as @e[type=sheep] at @s run tp ~ ~1 ~ - Kill the player running the command, because "
at" does not change the executor:execute at @e[type=sheep] run kill @s
facing
- Sets the execution rotation to face a given point, as viewed from its anchor (either the eyes or the feet).
- Syntax
- Template:El:
facing <pos> -> executefacing entity <targets> <anchor> -> execute
- Template:El:
facing <position: x y z> <chainedCommand: ExecuteChainedOption_0>facing entity <origin: target> <eyes|feet> <chainedCommand: ExecuteChainedOption_0>- Arguments
- Options:
facing <pos>andfacing <position: x y z>- Template:Argument
- Coordinates to rotate toward.
- Template:Arg desc
- Template:Argument
- Options:
facing entity <targets> <anchor>andfacing entity <origin: target> <eyes|feet>- Template:Argument
- The target(s) to rotate toward.
- Template:Arg desc
- Template:Argument
- Whether to face the target's eyes or feet
- Must be either
eyesorfeet.
- Template:Argument
- Result
- Execution rotation is updated to face given position or targets.
- Unparseable if the argument is not specified correctly.
- Forks if
<targets>ororigin: targetselects multiple entities. - Terminates current branch if
<targets>ororigin: targetfails to resolve to one or more entities (named players must be online). - Example
- Executor rotates once to the left:
execute facing ^1 ^ ^ run tp @s ~ ~ ~ ~ ~ - All entities move one block in the direction of (0, 64, 0) (without changing their rotation):
execute as @e at @s facing 0 64 0 run tp @s ^ ^ ^1 - All entities move one block in the direction of (0, 64, 0) (with changing their rotation):
execute as @e at @s facing 0 64 0 run tp ^ ^ ^1 ~ ~ - All non player entities move one space in the direction of their nearest player (without changing their rotation):
execute as @e[type=!player] at @s facing entity @p feet run tp @s ^ ^ ^1
in
- Sets the execution dimension and execution position.
- It respects dimension scaling for relative and local coordinates: the execution position (only the X/Z part) is divided by 8 when changing from the Overworld to the Nether, and is multiplied by 8 when vice versa. Applies to custom dimensions as well.
- Syntax
- Template:El:
in <dimension> -> execute
- Template:El:
in <dimension: Dimension> <chainedCommand: ExecuteChainedOption_0>- Arguments
- Template:Argument
- ID of the new execution dimension.
- Template:Arg desc
- Result
- Execution dimension and position is updated.
- Unparseable if the argument is not specified correctly.
- Example
- Looking for an end city (from the overworld):
execute in minecraft:the_end run locate structure minecraft:end_cityTemplate:Onlyexecute in the_end run locate structure end_cityTemplate:Only
- If a player at position Template:Cd in Overworld runs the following command, the player is teleported to Template:Cd in the Nether:
execute in minecraft:the_nether positioned as @s run tp ~ ~ ~Template:Onlyexecute in nether positioned as @s run tp ~ ~ ~Template:Only
- If a player at position Template:Cd in Overworld runs the following command, the player is teleported to Template:Cd in the Nether.
execute in minecraft:the_nether run tp ~ ~ ~Template:Onlyexecute in nether run tp ~ ~ ~Template:Only
- If a player at position Template:Cd in Overworld runs the following command, the player is teleported to Template:Cd in the Nether.
execute in minecraft:the_nether run tp ~ ~ ~5Template:Onlyexecute in nether run tp ~ ~ ~5Template:Only
on
- Updates the executor to entities selected based on relation to the current executor entity. without changing execution position, rotation, dimension, and anchor.
- Syntax
- Template:El:
on <relation> -> execute- Arguments
- Template:El: Template:Argument
- A relation to the current executor entity.
attacker: the last entity that damaged the current executor entity in the previous 5 seconds. Note that damage types inminecraft:no_angertag bypass the record of attacker. Interaction entities do not forget attacker after 5 seconds. Some mobs forget the attacker when ceasing their aggression.controller: the entity that is riding and controlling the current executor entity. See Riding for details.leasher: the entity leading the current executor entity with a leash.origin: the entity that cause the summon of the current executor entity. For example, the shooter of an arrow, the primer of a primed TNT entity.owner: the owner of the current executor entity if it is a tameable animal.passengers: all entities that are directly riding the current executor entity, no sub-passengers.target: the target that the current executor entity intends on attacking. Interaction entities can select the last entity that interacted with them.vehicle: the entity ridden by the current executor entity.
- A relation to the current executor entity.
- Result
- Executor is updated based on the relation with the executor entity (which changes the meaning of
@s). - Forks if
passengersselects multiple entities. (Other relations can select only at most one entities.) - Terminates current branch if the current executor is not an entity.
- Terminates current branch if the relation is not applicable to the current executor entity or there are no entities matching it.
- Example
- Damage 1 heart the entity that is leashing a cat:
execute as @e[type=cat] on leasher run damage @s 2 generic - Give all players riding a boat a fishing rod:
execute as @e[type=boat] on passengers run give @s fishing_rod
positioned
- Sets the execution position, without changing execution rotation or dimension; can match an entity's position, or at one block above the Y-level stored in the specified heightmapTemplate:Only.
- Syntax
- Template:El:
positioned <pos> -> executepositioned as <targets> -> executepositioned over <heightmap> -> execute
- Template:El:
positioned <position: x y z> <chainedCommand: ExecuteChainedOption_0>positioned as <origin: target> <chainedCommand: ExecuteChainedOption_0>- Arguments
- Option:
positioned <pos>orpositioned <position: x y z>- Template:Argument
- New position.
- Template:Arg desc
- Template:Argument
- Option:
positioned as <targets>orpositioned as <origin: target>- Template:Argument
- Target entity/entities to match position with.
- Template:Arg desc
- Template:Argument
- Option:
positioned over <heightmap>- Template:El: Template:Argument
- Specifies the heightmap.
- Must be one of
world_surface,motion_blocking,motion_blocking_no_leaves, andocean_floor.
- Template:El: Template:Argument
- Result
- Execution position is updated. And
positioned <pos>also resets execution anchor to feet. - Unparseable if the argument is not specified correctly.
- Forks if
<targets>ororigin: targetselects multiple entities. - Terminates current branch if
<targets>ororigin: targetfails to resolve to one or more entities (named players must be online), orover <heightmap>is specified when current execution position is not loaded. - Example
- Look for a village near (0, 64, 0):
execute positioned 0 64 0 run locate structure #villageTemplate:Onlyexecute positioned 0 64 0 run locate structure villageTemplate:Only
rotated
- Sets the execution rotation; can match an entity's rotation.
- Syntax
- Template:El:
rotated <rot> -> executerotated as <targets> -> execute
- Template:El:
rotated <yaw: value> <pitch: value> <chainedCommand: ExecuteChainedOption_0>rotated as <origin: target> <chainedCommand: ExecuteChainedOption_0>- Arguments
- Option:
rotated <rot>orrotated <yaw: value> <pitch: value>- Template:El: Template:Argument (
<yaw> <pitch>) - Template:El: Template:Argument and Template:Argument
- Angles of rotation.
- Template:Arg desc
- Template:El: Template:Argument (
- Option:
rotated as <targets>orrotated as <origin: target>- Template:Argument
- Target entity/entities to match rotation with.
- Template:Arg desc
- Template:Argument
- Result
- Execution rotation is updated.
- Unparseable if the argument is not specified correctly.
- Forks if
<targets>ororigin: targetselects multiple entities. - Terminates current branch if
<targets>ororigin: targetfails to resolve to one or more valid entities (named players must be online). - Example
- Move every sheep 1 block in the direction that the player closest to it is facing:
execute as @e[type=sheep] at @s rotated as @p run tp @s ^ ^ ^1execute as @e[type=sheep] positioned as @s rotated as @p run tp @s ^ ^ ^1
summon
- Summons a new entity at execution position and changes the executor to this summoned entity.
- Syntax
- Template:El:
summon <entity> -> execute- Arguments
- Template:El: Template:Argument
- The entity to be summoned and become the new executor.
- Template:Arg desc
- Result
- Summons the entity and changes the executor.
- Unparseable if the argument is not specified correctly.
- Terminates current branch if execution position's <x> or <z> exceeds the range of [-30000000, 30000000), or <y> exceeds the range of [-20000000, 20000000).
- Terminates the whole Template:Cmd command if the specified entity is
minecraft:playerorminecraft:fishing_bobber. - Example
- Summon a sheep with the tag "Test":
execute summon sheep run tag @s add Test - Summon a block display entiy and scale it:
execute summon block_display run data merge entity @s {transformation:{scale:[4.0,4.0,4.0]},block_state:{Name:"minecraft:dirt"}}
Template:AnchorTemplate:Anchor
Condition subcommands
The particular use of the if and unless subcommands are to restrict command execution to happen only under specified conditions. In most cases, unless is a negation of if, equivalent to "if not...". The two subcommands have identical argument structures.
- Result of condition subcommands
- Unparseable if the arguments are not specified correctly.
- In some cases, for example, testing a block outside the world, both
ifandunlessterminates or fails. - When not at the end of the subcommands chain, only if the condition tests pass does the branch continue; otherwise it terminates.
- Template:IN, if it is executed in multiple branches, it acts as a context filter (only branches with contexts that match the condition continue).
- When at the end of the subcommands chain, it checks whether the condition is met and then outputs. Template:IN, it has output values that can be stored by store subcommands.
There are elevenTemplate:Only / fourTemplate:Only different types of conditions:
- (if|unless) biome – Tests the biome in a positionTemplate:Only
- (if|unless) block – Tests a single block
- (if|unless) blocks – Tests blocks in a 3D rectangular volume against another
- (if|unless) data – Tests for the data held by a block entity, an entity, or a storageTemplate:Only
- (if|unless) dimension - Tests the dimension of the executionTemplate:Only
- (if|unless) entity – Tests whether an entity like the one given is real
- (if|unless) function – Runs a function or function tag and tests its return valueTemplate:Only
- (if|unless) items – Tests an inventory slot for a given itemTemplate:Only
- (if|unless) loaded – Tests if a given position is loaded in the worldTemplate:Only
- (if|unless) predicate – Tests a predicateTemplate:Only
- (if|unless) score - Tests a target's score
(if|unless) biome
- Checks for a specific biome in a given position.
- Syntax
(if|unless) biome <pos> <biome> -> [execute]
- Arguments
- Template:Argument
- Position to test.
- Template:Arg desc
- Template:Argument
- Biome(s) to test for.
- Template:Arg desc
- Result
- Unparseable if the argument is not specified correctly.
- In following conditions, terminates current branch when this subcommand is not at the end of the subcommand chain; fails when at the end:
- If
<pos>is unloaded or out of the world. - If test doesn't pass.
- If
- When at the end of the subcommand chain, if the command is successful, both
successvalue andresultvalue are 1.
(if|unless) block
- Compares the block at a given position to a given block ID or a block tagTemplate:Only
- Syntax
- Template:El:
(if|unless) block <pos> <block> -> [execute]
- Template:El:
<subcommand: Option_If_Unless> block <position: x y z> <block: Block> [chainedCommand: ExecuteChainedOption_0]<subcommand: Option_If_Unless> block <position: x y z> <block: Block> <blockStates: block states> [chainedCommand: ExecuteChainedOption_0]
- Arguments
- Template:Argument
- Position of a target block to test.
- Template:Arg desc
- Template:Argument
- Specifies the block to test for.
- Template:Arg desc
- Template:El: Template:Argument
- Specifies the block state for the specified block to test for.
- Template:Arg desc
- Result
- Unparseable if the argument is not specified correctly.
- In following conditions, terminates current branch when this subcommand is not at the end of the subcommand chain; fails when at the end:
- If
<pos>orposition: x y zis unloaded or out of the world. - If test doesn't pass.
- If
- Template:IN, when at the end of the subcommand chain, if the command is successful, both
successvalue andresultvalue are 1.
(if|unless) blocks
- Compares the blocks in two equally sized volumes.
- Syntax
- Template:El:
(if|unless) blocks <start> <end> <destination> (all|masked) -> [execute]
- Template:El:
<subcommand: Option_If_Unless> blocks <begin: x y z> <end: x y z> <destination: x y z> <scan mode: BlocksScanMode> [chainedCommand: ExecuteChainedOption_0]
- Arguments
- Template:Argument
- Template:Argument
- Positions of any two diagonal corners of the source volume (the volume to compare).
- Template:Arg desc
- Template:Argument
- Position of the lower northwest corner of the destination volume (the volume to compare to).
- Assumed to be of the same size as the source volume
- Template:Arg desc
- Template:El: Template:Code
- Template:El: Template:Argument
- Specifies whether all blocks in the source volume should be compared, or if air blocks should be masked/ignored.
- Must be either
allormasked.
- Result
- Unparseable if the argument is not specified correctly.
- In following conditions, terminates current branch when this subcommand is not at end of the subcommand chain; fails when at the end:
- If
<start>/begin: x y z,<end>/end: x y z, or<destination>/destination: x y zis unloaded or out of the world. - If the volume of the source region is greater than 32768 (the equivalent of 8 chunk sections).
- If test doesn't pass.
- If
- Template:IN, when at the end of the subcommand chain, if the command is successful:
- The
successvalue is 1. - For
ifmode,resultvalue is the number of matching blocks (the total number of blocks inallmode, or the number of source region non-air blocks inmaskedmode). - For
unlessmode,resultvalue is 1.
- The
(if|unless) data
- Checks whether the targeted block, entity or storage has any data tag for a given path.
- Syntax
(if|unless) data block <pos> <path> -> [execute]– for data checking a block(if|unless) data entity <target> <path> -> [execute]– for data checking an entity(if|unless) data storage <source> <path> -> [execute]– for data checking a storage
- Arguments
- Option:
(if|unless) data block <pos> <path>- Template:Argument
- Position of the block for data testing.
- Template:Arg desc
- Template:Argument
- Data path to check whether the block has.
- Template:Arg desc
- Template:Argument
- Option:
(if|unless) data entity <target> <path>- Template:Argument
- A single entity for data testing.
- Template:Arg desc
- Template:Argument
- Data path to check whether the entity has.
- Template:Arg desc
- Template:Argument
- Option:
(if|unless) data storage <source> <path>- Template:Argument
- The resource location of the storage for data testing.
- Template:Arg desc
- Template:Argument
- Data path to check whether the storage has.
- Template:Arg desc
- Template:Argument
- Result
- Unparseable if the argument is not specified correctly.
- In following conditions, terminates current branch when this subcommand is not at the end of the subcommand chain; fails when at the end:
- If
<pos>is unloaded or out of the world. - If block at
<pos>isn't a block entity. - If
<target>fails to resolve to one or more valid entities (named players must be online). - If test doesn't pass.
- If
- When at the end of the subcommand chain, if the command is successful:
- The
successvalue is 1. - For
ifmode,resultvalue is the number of matching data tags. - For
unlessmode,resultvalue is 1.
- The
(if|unless) dimension
- Checks if the execution is in a matching dimension.
- Syntax
(if|unless) dimension <dimension> -> [execute]
- Arguments
- Template:Argument
- Dimension to test for.
- Template:Arg desc
- Result
- Unparseable if the argument is not specified correctly.
- In following conditions, terminates current branch when this subcommand is not at the end of the subcommand chain; fails when at the end:
- If test doesn't pass.
- When at the end of the subcommand chain, if the command is successful, both
successvalue andresultvalue are 1.
(if|unless) entity
- Checks whether one or more entities exist.
- Syntax
- Template:El:
(if|unless) entity <entities> -> [execute]
- Template:El:
<subcommand: Option_If_Unless> entity <target: target> [chainedCommand: ExecuteChainedOption_0]- Arguments
- Template:Argument
- The target entity/ies to test for.
- Template:Arg desc
- Result
- Unparseable if the argument is not specified correctly.
- In following conditions, terminates current branch when this subcommand is not at the end of the subcommand chain; fails when at the end:
- If test doesn't pass.
- Template:IN, when at the end of the subcommand chain, if the command is successful:
- The
successvalue is 1. - For
ifmode,resultvalue is the number of matching entities. - For
unlessmode,resultvalue is 1.
- The
(if|unless) function
- Checks if function(s) are non-void and the return value is non-zero. Terminates current branch unless the function's return value is non-zero. Doesn't change any execution context.
- Unlike other conditional subcommands, this subcommand can modify the world depending on the function(s) that are tested. It also cannot be placed at the end of the subcommand chain.<ref>Template:Bug</ref>
- Syntax
(if|unless) function <function> -> execute
- Arguments
- Template:Argument
- Function to test for.
- Template:Arg desc
- Result
- Unparseable if the argument is not specified correctly.
- If the given function or function tag does not exist, or a function tag is given but there's no function in it, terminates the whole
/executecommand. - When only one function is given, if it is not macro function; and it is not void; and its return value is not 0:
ifdoes nothing andunlessterminates current branch.
- When multiple functions are given, each function is checked and called one by one in the defined order in the tag file:
- If the function is a macro function: Ends this subcommand execution, and
ifterminates current branch andunlessdoes nothing. - If a function is void: Checks and calls the next function. If there's no next function,
ifterminates current branch andunlessdoes nothing. - If a function returns a failure, or returns a success but its return value is 0: Ends this subcommand execution, and
ifterminates current branch andunlessdoes nothing. - If a function returns a success and its return value is not 0: Ends this subcommand execution, and
ifdoes nothing andunlessterminates current branch.
- If the function is a macro function: Ends this subcommand execution, and
(if|unless) items
- Checks for a matching item in the provided inventory Minecraft:slots.
- Syntax
(if|unless) items block <sourcePos> <slots> <item_predicate>(if|unless) items entity <source> <slots> <item_predicate>
- Arguments
- Template:Argument
- Location of a block entity to test.
- Template:Arg desc
- Template:Argument
- Entity to test.
- Template:Arg desc
- Template:Argument
- Slots to test. A string that consists of a "slot type" and an optional "slot number", in the format of
<slot_type>,<slot_type>.<slot_number>or<slot_type>.*. See Minecraft:Slot for details.
- Slots to test. A string that consists of a "slot type" and an optional "slot number", in the format of
- Template:Argument
- Item or item tag to test for.
- Template:Arg desc
- Result
- Unparseable if the argument is not specified correctly.
- In following conditions, terminates current branch when this subcommand is not at the end of the subcommand chain; fails when at the end:
- If
<source_pos>is unloaded or out of the world. - If block at
<source_pos>isn't a block entity. - If
<source>fails to resolve to one or more valid entities (named players must be online). - If test doesn't pass.
- If
- When at the end of the subcommand chain, if the command is successful:
- The
successvalue is1. - For
ifmode,resultvalue is the number of matching items. - For
unlessmode,resultvalue is 1.
- The
(if|unless) loaded
- Checks if chunks at a given position is fully loaded (Entity Ticking).
- Syntax
(if|unless) loaded <pos> -> [execute]- Arguments
- Template:Argument
- Position to test
- Template:Arg desc
- Result
- Unparseable if the argument is not specified correctly.
- In following conditions, terminates current branch when this subcommand is not at the end of the subcommand chain; fails when at the end:
- If
<pos>is out of the world. - If test doesn't pass.
- If
- When at the end of the subcommand chain, if the command is successful, both
successvalue andresultvalue are 1.
(if|unless) predicate
- Checks whether the predicate successes. See Predicate for more information.
- Syntax
(if|unless) predicate <predicate> -> [execute]- Arguments
- Template:Argument or inline definition
- The predicate to be checked whether it evaluates to a positive result.
- Must either be a resource location or an inline definition, following the same structure as predicates encoded in the SNBT format.
- The command is resolved during command execution into unregistered content, or sent to the client to resolve into a client-side content.
- Result
- Unparseable if the argument is not specified correctly.
- In following conditions, terminates current branch when this subcommand is not at the end of the subcommand chain; fails when at the end:
- If the predicate doesn't exist.
- If test doesn't pass.
- When at the end of the subcommand chain, if the command is successful, both
successvalue andresultvalue are 1.
(if|unless) score
- Check whether a score has the specific relation to another score, or whether it is in a given range.
- Syntax
- Template:El:
(if|unless) score <target> <targetObjective> (<|<=|=|>=|>) <source> <sourceObjective> -> [execute]– for comparing two scores(if|unless) score <target> <targetObjective> matches <range> -> [execute]– for comparing one score against a range
- Template:El:
<subcommand: Option_If_Unless> score <target: target> <objective: string> <operation: compare operator> <source: target> <objective: string> [chainedCommand: ExecuteChainedOption_0]- for comparing two scores<subcommand: Option_If_Unless> score <target: target> <objective: string> matches <range: integer range> [chainedCommand: ExecuteChainedOption_0]- for comparing one score against a range
- Arguments
- Template:Argument
- A single score holder.
- Template:IN, Template:Arg desc
- Template:IN, Template:Arg desc It can also be quoted unique id to specified non-player entities in the objective.
- Template:Argument
- The scoreboard objective to check under. The score of <target> or <target: target> in this objective is checked.
- Template:Arg desc
- Option:
(if|unless) score ... (<|<=|=|>=|>) ...or<subcommand: Option_If_Unless> score ... <operation: compare operator> ...- Template:El:
(<|<=|=|>=|>) - Template:El: Template:Argument
- Specifies a compare operator.
- Must be one of
<,<=,=,>=, and>.
- Template:Argument
- A single score holder to compare against.
- Template:IN, Template:Arg desc
- Template:IN, Template:Arg desc It can also be quoted unique id to specified non-player entities in the objective.
- Template:Argument
- A scoreboard objective. The score of <source> or <source: target> in this objective is checked.
- Template:Arg desc
- Template:El:
- Option:
(if|unless) score ... matches ...or<subcommand: Option_If_Unless> score ... matches ...- Template:Argument
- Range to compare score against.
- Template:Arg desc
- Template:Argument
- Result
- Unparseable if the argument is not specified correctly.
- In following conditions, terminates current branch when this subcommand is not at the end of the subcommand chain; fails when at the end:
- Template:IN, if
<target>/target: targetor<source>/source: targetis*. - If test doesn't pass.
- Template:IN, if
- Template:IN, when at the end of the subcommand chain, if the command is successful, both
successvalue andresultvalue are 1.
Example
- Kill all players standing on a wool:
execute as @a at @s if block ~ ~-1 ~ #wool run kill @sTemplate:Onlyexecute as @a at @s if block ~ ~-1 ~ wool run kill @sTemplate:Only
- Check whether scores A and B are equal:
execute if score @s A = @s B
Store subcommand
Template:Exclusive
Stores the final subcommand's result or success value somewhere. It is first processed along with other subcommands in the subcommand chain, recording the location to store in. After the last subcommand (may be a condition subcommand or a run subcommand) is executed, output values are stored in the recorded location. Note that the output values of commands are always an integer. If not, they are rounded down.
There are five different modes of storage:
- store (result|success) block – Stores output value under one of a block's NBTs
- store (result|success) bossbar – Stores output value as a bossbar data
- store (result|success) entity – Stores output value under one of an entity's NBTs
- store (result|success) score – Stores output value under a target's score on an objective
- store (result|success) storage – Stores output value under one of a storage's NBTs
store (result|success) block
- Saves the final command's output value as tag data within a block entity. Store as a byte, short, int, long, float, or double. If the output value is a decimal, it is rounded first and then multiplied by
<scale>.
- Syntax
store (result|success) block <targetPos> <path> <type> <scale> -> execute
- Arguments
- Template:Argument
- Position of target block.
- Template:Arg desc
- Template:Argument
- Location of the desired tag to hold the value in.
- Template:Code
- Desired data size/type.
- Must be one of
byte,short,int,long,float, anddouble.
- Template:Argument
- Multiplier to apply before storing value, may be negative.
- Template:Arg desc
- Result
- Unparseable if the argument is not specified correctly.
store (result|success) bossbar
- Saves the final command's output value in either a bossbar's current value or its maximum value
- Syntax
store (result|success) bossbar <id> (value|max) -> execute- Arguments
- Template:Argument
- ID of the bossbar to target for saving.
- Template:Arg desc
- Template:Code
- Whether to overwrite the bossbar's current value or its max value
- Result
- Unparseable if the argument is not specified correctly.
store (result|success) entity
- Save the final command's output value in one of an entity's data tags. Store as a byte, short, int, long, float, or double. If the output value is a decimal, it is rounded first and then multiplied by
<scale>. Like the Template:Cmd command, "/execute store" cannot modify player NBT.
- Syntax
store (result|success) entity <target> <path> <type> <scale> -> execute
- Arguments
- Template:Argument
- A single entity to store under.
- Template:Arg desc
- Template:Argument
- Location of the desired tag to hold the value in.
- Template:Code
- Desired data size/type
- Must be one of
byte,short,int,long,float, anddouble.
- Template:Argument
- Multiplier to apply before storing value, may be negative.
- Template:Arg desc
- Result
- Unparseable if the argument is not specified correctly.
store (result|success) score
- Overrides the score held by
<targets>on the given<objective>with the final command's output value.
- Syntax
store (result|success) score <targets> <objective> -> execute
- Arguments
- Template:Argument
- Specifies score holder(s) whose score is to be overridden.
- Template:Arg desc
- Template:Argument
- A scoreboard objective.
- Template:Arg desc
- Result
- Unparseable if the argument is not specified correctly.
store (result|success) storage
- Uses the Template:Cd within storage Template:Cd to store the output value in. Store as a byte, short, int, long, float, or double. If the output value is a decimal, it is rounded first and then multiplied by Template:Cd. If the storage does not yet exist, it gets created.
- Syntax
store (result|success) storage <target> <path> <type> <scale> -> execute
- Arguments
- Template:Argument
- Target storage container, as a resource location.
- Template:Arg desc
- Template:Argument
- Location of the desired tag to hold the value in.
- Template:Arg desc
- Template:Code
- Desired data size/type.
- Must be one of
byte,short,int,long,float, anddouble.
- Template:Argument
- Multiplier to apply before storing value, may be negative.
- Template:Arg desc
- Result
- Unparseable if the argument is not specified correctly.
Run subcommand
The Template:Cd command's single argument is the command to be executed, the context variables of which may be modified by the subcommands used.
- Syntax
- Template:El:
run --><command>
- Template:El:
run <command: command>
- Arguments
- Template:Argument
- Can be any command
- TechnicalTemplate:Only
- Resets the command node to the root of the command dispatcher
- Information on modelling commands as chat text:
- The command dispatcher is what starts when the player begins a message with a forward-slash (
/). - A command node is the specific word/entry the cursor is editing, either a command or an argument.
- The root node comes before the first word in the current command.
- The command dispatcher is what starts when the player begins a message with a forward-slash (
- Result
- Execute this command. Fails if
<command>orcommand: commandis failure.
More examples
- Teleport all players who have an item enchanted with Efficiency in their first hotbar slot to coordinates (0, 64, 0):
/execute as @a if data entity @s Inventory[{Slot:0b}].tag.Enchantments[{id:"minecraft:efficiency"}] run tp @s 0 64 0Template:Only
- Create a smoke particle three blocks in front of all players:
/execute as @a at @s anchored eyes run particle smoke ^ ^ ^3Template:Only
- Place a saddle on Minecraft:pigs located within 5 blocks of the executing player, and remove saddles from pigs located over 5 blocks away from the executing player:
/execute as @e[type=pig] at @s store success entity @s Saddle byte 1 if entity @p[distance=..5]Template:Only
- Make a player say "My feet are soaked!" in chat if they are located in a block of Minecraft:water:
/execute as @a at @s if block ~ ~ ~ water run say "My feet are soaked!"
- Make a player say "Score is reset" in chat if the score
testis not set, eg by doing "scoreboard players reset @s test":/execute as @a unless score @s test = @s test run say "Score is reset"
- Hurl all skeletons skyward who are within 3 blocks of the nearest player:
/execute at @p as @e[type=skeleton,distance=..3] run data merge entity @s {Motion:[0.0,2.0,0.0]}Template:Only
- Kill all Minecraft:zombies who have no headgear:
/execute as @e[type=zombie] unless data entity @s ArmorItems[3].id run kill @sTemplate:Only
- Set every player's
nearbyRedSheepscoreboard objective equal to the amount of red sheep within twenty blocks:/execute as @a at @s store result score @s nearbyRedSheep if entity @e[type=sheep,nbt={Color:14},distance=..20]
History
Java Edition
Bedrock Edition
References
<references/>
External links
de:Befehl/execute es:Comandos/execute fr:Commandes ja:コマンド/execute ko:명령어/execute pl:Polecenia/execute pt:Comandos/execute ru:Команды консоли/execute uk:Команди консолі/execute zh:命令/execute