Execute Commands
The execute command and its sub-commands for conditional and contextual command execution.
How the game reads a chain
/execute is read from left to right. Most subcommands change who runs the rest of the chain, where, or whether it runs at all, and the chain has to end with run plus a command, or with a condition. Order matters more than anything else here.
execute as @e at @s run tp ^ ^ ^1 moves every entity one block forward. Swap the first two parts to execute at @s as @e run tp ^ ^ ^1 and all of them land one block in front of you instead.
When as or at picks several entities, the chain forks and everything after that point runs once for each of them. A condition that fails only ends its own branch.
The four kinds of subcommand
- Modifiers
align,anchored,as,at,facing,in,on,positioned,rotatedandsummonset the executor, position, rotation, dimension and anchor.onandsummonwere added in Java 1.19.4.- Conditions
iforunless, followed by biome, block, blocks, data, dimension, entity, function, items, loaded, predicate or score. At the very end of a chain a condition just reports whether it passed. The newer tests are biome (1.19.3), dimension and loaded (1.19.4), function (1.20.2) and items (1.20.5).storestore resultsaves the return value andstore successsaves 1 or 0, into a block, a bossbar, an entity, a score or a storage. After a fork each branch stores its own value, and a later branch overwrites an earlier one at the same spot.run- The command to carry out. It can appear once, at the end.
A pattern worth copying
execute as @a at @s if block ~ ~-1 ~ minecraft:magma_block run effect give @s minecraft:fire_resistance 5
This gives five seconds of fire resistance to every player standing on magma. as picks each player, at moves to where they stand, the condition checks the block under their feet and run only fires for the players who pass. Put it in a tick function and the effect never wears off while they stay on the block. Functions and schedules are with the logic commands. /execute needs permission level 2.