Commands/execute

From Minecraft Wiki
< Commands(Redirected from Execute)
Jump to: navigation, search

/execute is a versatile command that modifies variables or procedures in the execution of another command, such as giving the server a false impression of the entity that's executing the command, its current position, how it's rotated, etc.

All commands end up having a success status and some quantifiable value they can return to a target, as might be specified by /execute store <arguments>. All commands issued from command blocks return the success status to that issuing command block, which can trigger chained command blocks.

Java Edition[edit]

Syntax

There are twelve instructions (or subcommands) for the /execute command, and each has its own special syntax, so describing syntax takes a largely branching tree.

Full syntax tree
/execute . . .
  • align <axes> -> execute
  • anchored (eyes|feet) -> execute
  • as <targets> -> execute
  • at <targets> -> execute
  • facing (<pos>|entity <targets'> (eyes|feet)) -> execute
  • in <dimension'> -> execute
  • positioned (<pos>|as <targets>) -> execute
  • rotated (<rot>|as <targets>) -> execute
  • store (result|success) . . .
    • … block <targetPos> <path> (byte|short|int|long|float|double) <scale> -> execute
    • … bossbar <id> (max|value) -> execute
    • … entity <target> <path> (byte|short|int|long|float|double) <scale> -> execute
    • … score <targets> <objective> -> execute
  • … (if|unless) . . .
    • … block <pos> <block> -> [execute]
    • … blocks <start> <end> <destination> (all|masked) -> [execute]
    • … data . . .
      • … block <sourcePos> <path> -> [execute]
      • … entity <source> <path> -> [execute]
    • … entity <entities> -> [execute]
    • … score <target> <targetObjective> . . .
      • … (<|<=|=|>|>=) <source> <sourceObjective> -> [execute]
      • … matches <range> -> [execute]
  • run <command>
… where -> execute represents the start of another execution instruction. A number of instructions can be strung end-to-end, where run is the only instruction that can terminate the command—An exception is that including the if or unless instructions allows this terminator to be left out.
Details
There are 11 chainable instructions—9 instructions that modify command variables and 2 conditional instructions—and a final run instruction for carrying out some following command. A number of instructions can be chained together, but run finalises the command. The simplest usage for this command is: /execute run <command>.
Alternatively, using the conditional instructions if or unless without run is still valid syntax, in which case the result of the test is returned: /execute if entity @e[name="Ender Dragon"]
The conditional instructions if and unless can prevent a command from running at all. All specified conditions must be met for the test to pass and the command to run.
The 11 chainable instructions are:
  1. align <axes> -> execute
    Align the current position to the block grid, rounding down (towards negative infinity).
    <axes>—Any non-repeating combination of the characters 'x', 'y', and 'z'. (For example, x, xz, zyx, or yz.)
    Example: Given the position (-1.8, +2.3, +5.9), the command "/execute align xz run summon zombie ~ ~ ~" summons a zombie at (-2.0, +2.3, +5.0).
    Example: Given (+2.4, -1.1, +3.8), using "/execute align yxz run spawnpoint @p ~ ~ ~" sets the player's spawnpoint to (2, -2, 3).
  2. anchored (feet|eyes) -> execute
    Use feet/eye height for ~Δx ~Δy ~Δz and ^ΔT ^ΔN ^ΔB coordinates and facing commands.
  3. as <targets> -> execute
    Execute the command as the targeted entity/entities, but don't change position. If an entity doesn't have an eye anchor point, the command anchors to the feet of the previous executor (not the specified entity).
    [These details need fact checking! These statements seem conflicting. [citation needed]]
    Changes the function of the @s target selector variable
  4. at <target> -> execute
    Use the position, rotation, and dimension of the target, but don't change the current entity (@s).
    Changes the function of ~Δx ~Δy ~Δz and ^ΔT ^ΔN ^ΔB positioning
  5. facing (<pos>|entity <target> (eyes|feet)) -> execute
    Use an execution position that faces either the given position or the targeted entity's eyes or feet.
    Changes the function of ^ΔT ^ΔN ^ΔB positioning
  6. in <dimension> -> execute
    Execute as though the executor is in the given dimension.
    Changes command dimension, but does not effect X- Y- Z-positions
    <dimension>—The standard dimensions in the Minecraft namespace are "overworld", "the_nether", and "the_end".
  7. positioned (<pos>|as <targets>) -> execute
    Use either the given coordinates or the targeted entity's coordinates for the "current position."
    Changes the function of ^ΔT ^ΔN ^ΔB and ~Δx ~Δy ~Δz positioning
    [Editor's note: Does this change the command dimension or @s?]]
  8. rotated (<rot>|as <targets>) -> execute
    Execute as though the executor is rotated either in the given direction or the same the targeted entity is facing.
    Changes command rotation
  9. store (result|success) (block|bossbar|entity|score) <arguments> -> execute
    Store the result or success of a command somewhere. This instruction replaced the "/stats" command.
    result—Return the result of the command. (This replaces the old stats AffectedBlocks, AffectedEntities, AffectedItems, QueryResult.)
    success—Return how many times the command was successful. This is usually 0 or 1, but if the command split up (as perhaps with "/execute as @a -> execute") then success can be a number higher than 1. (This replaces the old stat SuccessCount.)
    If a command fails, "success" and "result" both return 0. The return value is stored when the full command has finished executing.
    • … block <pos> <path> (byte|short|int|long|float|double) <scale> -> execute
      Saves the return value in NBT data at the given block position.
      <path>—Name of the data tag to save return value in. See 10. (if|unless) data below for more details.
    • … bossbar <id> (value|max) -> execute
      Saves the return value in either the current value or the maximum value of the given bossbar
      <id>—ID of the bossbar to store return value in.
      value—Specifies that the bossbar's "value" is to be overwritten with the execution's return value
      max—Specifies that the bossbar's "max" is to be overwritten with the execution's return value
    • … entity <target> <path> (byte|short|int|long|float|double) <scale> -> execute
      Use the data tag path from one targeted entity to save the return value. Store as a byte, short, int, long, float, or double.
      Like the "/data" command, "/execute store <arguments>" cannot modify player NBT. NBT data inside the tag key of items in the player's Inventory or EnderItemsare an exception and can be modified by "/execute store <arguments>".
    • … score <name> <objective> -> execute
      Use the scoreboard under <name> and <objective> to save the return value.
      <objective> must exist, but unlike with "/stats" you don't need to set an initial value for <name>.
  10. (if|unless) (block|blocks|data|entity|score) <arguments> -> [execute]
    Test for some condition before choosing whether to carry out the command.
    • … block <pos> <block> -> [execute]
      Compares the block at the given position to the given block ID
    • … blocks <start> <end> <destination> (all|masked) -> [execute]
      Compare region between the <start> and <end> positions to the block located at the <destination> position.
    • … data (entity <target>|block <pos>) <path> -> [execute]
      Execute if or unless the targeted entity or block has any data for the tag named by <path>.
      <path>—Names an NBT to check for. This is a string of parent tag names of the Java-like form highestTag.child.childsChild…parent.targetDataTag.
      See Commands/data#What is an NBT path? for more on paths
      Example: /execute if data entity @a ArmorItems[0].id would test for if any entity has an "id" for the item in their boot armour slot. Note that this returns true for all mobs, armoured or not.
    • … entity <targets> -> [execute]
      Execute if or unless one or more <targets> exist.
    • … score <target> <targetObjective> (<|<=|=|>=|>) <source> <sourceObjective> -> [execute]
      Execute if or unless the target entity's score accurately relates to source entity's score, given some conditional operator.
    • … score <target> <targetObjective> matches <range> -> [execute]
      Execute if or unless target entity's score is within given range (e.g. 1, 1..5).
  11. unless <arguments> -> [execute]
    See 10. (if|unless) above.
Beyond those 11 instructions, there's the run instruction.
  • run <command>
    Specifies the command to be executed, the variables of which may be modified by the subcommands used
    Can be any Minecraft command
    Technical: Resets the command node to the root of the command dispatcher
    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 your cursor is editing, either a command or an argument.
    The root node comes before the first word in your command.
Examples
  • /execute store success score @a foo run say hi
  • /execute as @e[type=pig] at @s store success entity @s Saddle byte 1 if entity @p[distance=..5]
  • /execute as somebody at somebody run say "Hello world!"

Bedrock Edition[edit]

Syntax
execute <origin: target> <position: x y z> <command: command>
An alternate syntax allows the command to be executed only if a specific block is detected:
execute <origin: target> <position: x y z> detect <detectPos: x y z> <block: Block> <data: int> <command: command>

Color added to make reading easier.

Arguments
origin: target
Specifies the target to be the command's executor. Must be a player name or target selector. If more than one entity is selected, the command will be run once as each of them.
position: x y z
Specifies the position from which to run the command. x and z must be within the range -30,000,000 to 30,000,000 (inclusive). Coordinates specified with tilde notation are relative to the target, not to the position of the command's execution.
command: command
Specifies the command to be run. Must be a valid command.
detectPos: x y z (detect mode only)
Specifies the position of the block to check. x2 and z2 must be within the range -30,000,000 to 30,000,000 (inclusive). May use tilde notation to specify distances relative to (x,y,z).
block: Block (detect mode only)
Specifies the block ID which the block at (x2,y2,z2) must match for the command to run. block must be a valid block id (for example, stone)
data: int (detect mode only)
Must be a valid block data for that type of block or -1 to match any block data.
Result
Fails if arguments are not specified correctly, if <entity> fails to resolve to one or more valid entities (named players must be online), if the checked block is not of the correct block id, data or state, or if the specified command fails.
On success, executes the specified command as if executed by the specified target(s), done with operator-level permission at the specified coordinate.
Examples
To summon lightning bolts at the positions of every zombie:
execute @e[type=zombie] ~ ~ ~ summon lightning_bolt
To summon lightning bolts at the positions of every zombie standing on any type of sand:
execute @e[type=zombie] ~ ~ ~ detect ~ ~-1 ~ minecraft:sand 0 summon lightning_bolt
To summon 10 creepers at the nearest player's position (only works as long as there are at least 10 entities in the loaded world):
execute @e[limit=10] ~ ~ ~ execute @p ~ ~ ~ summon creeper ~ ~ ~

History[edit]

Java Edition
1.814w07aAdded /execute.
14w08aCommands run will now pass their success value back to the command block running them.
1.1116w32aAdded block state support.
1.1317w45aThe syntax has been split off.
17w45bReworked /execute store.
18w02aAdded new subcommands to allow for more control over commands.
18w05aAdded /execute store subcommand.
1.1418w43aAdded /execute (if|unless) data.
Pocket Edition Alpha
0.16.0build 1Added /execute.
Functionality is equivalent to Java Edition 1.10 usage, but the entity target is optional in /execute.