手册:/execute/25

来自Minecraft基岩版开发Wiki

版本25语法中,execute分为了许多子命令,有如下3类,11条子命令,共19个重载:

修饰子命令:修改命令变量和命令的运行方式,从而以特定的执行者、执行位置、执行维度、执行坐标执行命令。修饰子命令有8条: alignanchoredasatfacing (entity)inpositioned (as)rotated (as)
条件子命令:当特定条件成立或不成立时,执行命令或通过检测。条件子命令有2条:ifunless
运行子命令:运行对应的命令。运行子命令为run
Java版中还具有存储子命令(store),但基岩版中尚未加入。

子命令之间可以互相套用(除了run)。命令必须以条件子命令或运行子命令收尾,否则会报错。对于多个子命令,游戏将从前往后处理这些子命令,例如下面两个命令的意义就是不同的:

/execute positioned ~ ~ ~ as @e run tp @s ~ ~ ~ - 将所有实体传送到执行者的位置
/execute as @e positioned ~ ~ ~ run tp @s ~ ~ ~ - 将所有实体传送到其原位

[chainedCommand: ExecuteChainedOption_0]表示下一个子命令的开始。对于条件子命令,该参数是可选的;对修饰子命令,这参数是必选的。

重载[编辑]

修饰子命令部分
  1. execute align <axes: string> <chainedCommand: ExecuteChainedOption_0>
  2. execute anchored <eyes|feet> <chainedCommand: ExecuteChainedOption_0>
  3. execute as <origin: target> <chainedCommand: ExecuteChainedOption_0>
  4. execute at <origin: target> <chainedCommand: ExecuteChainedOption_0>
  5. execute facing <position: x y z> <chainedCommand: ExecuteChainedOption_0>
  6. execute facing entity <origin: target> <eyes|feet> <chainedCommand: ExecuteChainedOption_0>
  7. execute in <dimension: Dimension> <chainedCommand: ExecuteChainedOption_0>
  8. execute positioned <position: x y z> <chainedCommand: ExecuteChainedOption_0>
  9. execute positioned as <origin: target> <chainedCommand: ExecuteChainedOption_0>
  10. execute rotated <yaw: value> <pitch: value> <chainedCommand: ExecuteChainedOption_0>
  11. execute rotated as <origin: target> <chainedCommand: ExecuteChainedOption_0>
条件子命令部分
  1. execute <subcommand: Option_If_Unless> block <position: x y z> <block: Block> <blockStates: block states> [chainedCommand: ExecuteChainedOption_0]
  2. execute <subcommand: Option_If_Unless> block <position: x y z> <block: Block> <data: int> [chainedCommand: ExecuteChainedOption_0]
  3. execute <subcommand: Option_If_Unless> block <position: x y z> <block: Block> [chainedCommand: ExecuteChainedOption_0]
  4. execute <subcommand: Option_If_Unless> blocks <begin: x y z> <end: x y z> <destination: x y z> <scan mode: BlocksScanMode> [chainedCommand: ExecuteChainedOption_0]
  5. execute <subcommand: Option_If_Unless> entity <target: target> [chainedCommand: ExecuteChainedOption_0]
  6. execute <subcommand: Option_If_Unless> score <target: target> <objective: string> <operation: compare operator> <source: target> <objective: string> [chainedCommand: ExecuteChainedOption_0]
  7. execute <subcommand: Option_If_Unless> score <target: target> <objective: string> matches <range: integer range> [chainedCommand: ExecuteChainedOption_0]
运行子命令部分
  1. execute run <command: command>

参数与示例[编辑]

align更改执行坐标为方块坐标(且向下取整)。

语法[编辑]

align <axes: string> <chainedCommand: ExecuteChainedOption_0>

参数[编辑]

axes 软枚举参数
string类型,必选,为xyz的任意不重复组合,不考虑顺序。例如xxzyxxzy等。
chainCommand 枚举参数
ExecuteChainedOption_0类型,必选,指定执行什么子命令。可以为上述的任意11种子命令类型。

示例[编辑]

execute align xyz run summon armor_stand ~ ~ ~ - 在执行者的方块位置召唤一个盔甲架(可用于召唤在方块中心而不偏移)
execute positioned 19.58 88.78 13.26 align zy run tp @s ~ ~ ~ - 将执行者传送至(19.58,88.00,13.00)。

anchored更改执行位置为执行者的眼部或脚部,默认为脚部。

语法[编辑]

anchored <eyes|feet> <chainedCommand: ExecuteChainedOption_0>

参数[编辑]

eyes|feet 枚举参数
必选,更改执行位置为执行者的眼部或脚部。
chainCommand 枚举参数
ExecuteChainedOption_0类型,必选,指定执行什么子命令。可以为上述的任意11种子命令类型。

示例[编辑]

execute as @e[type=armor_stand] anchored eyes run tp @s ~ ~ ~ - 将盔甲架传送至其眼部的位置。

as更改命令的执行者。

语法[编辑]

as <origin: target> <chainedCommand: ExecuteChainedOption_0>

参数[编辑]

origin 基本参数
target类型,必选,指定执行者。必须为有效的实体ID或假名。
chainedCommand 枚举参数
ExecuteChainedOption_0类型,必选,指定执行什么子命令。可以为上述的任意7种子命令类型。

示例[编辑]

execute as @a if block ~ ~ ~ air - 检测是否有玩家所处位置为空气
execute as @e run setblock ~ ~-1 ~ redstone_block - 在所有实体脚下放置钻石块。

at更改命令的执行位置和执行维度为规定实体的位置和维度。

语法[编辑]

at <origin: target> <chainedCommand: ExecuteChainedOption_0>

参数[编辑]

origin 基本参数
target类型,必选,指定命令的执行位置和执行维度为规定实体的位置和维度。必须为有效的实体ID或假名。
chainedCommand 枚举参数
ExecuteChainedOption_0类型,必选,指定执行什么子命令。可以为上述的任意7种子命令类型。

示例[编辑]

execute as @a at @s run tp @s ~ ~5 ~ - 将所有玩家传送至其上方5格的位置
execute at @s run playsound random.orb @a ~ ~ ~ - 以执行者为音源播放经验球音效。

facing更改命令的执行朝向为规定坐标或实体。

语法[编辑]

facing <position: x y z> <chainedCommand: ExecuteChainedOption_0>
facing entity <origin: target> <eyes|feet> <chainedCommand: ExecuteChainedOption_0>

参数[编辑]

position 基本参数
x y z类型,必选,指定命令的执行朝向为特定坐标。必须为有效的坐标。
origin 基本参数
target类型,必选,指定命令的执行朝向为特定实体。
eyes|feet 枚举参数
必选,更改执行朝向为指定实体的眼部或脚部。
chainedCommand 枚举参数
ExecuteChainedOption_0类型,必选,指定执行什么子命令。可以为上述的任意11种子命令类型。

示例[编辑]

execute as @a[tag=a] facing 0 64 0 run tp @s ^^^1 - 令带标签a的玩家朝着(0,64,0)移动1格。
execute as @a[tag=a] facing entity @e[type=zombie,c=1] feet run tp @s ^^^1 - 令带标签a的玩家朝着附近的僵尸移动1格。

in更改命令的执行维度。

语法[编辑]

in <dimension: Dimension> <chainedCommand: ExecuteChainedOption_0>

参数[编辑]

dimension 枚举参数
Dimension类型,必选,指定命令的执行维度。可以为netheroverworldthe_end中的一个。
chainedCommand 枚举参数
ExecuteChainedOption_0类型,必选,指定执行什么子命令。可以为上述的任意11种子命令类型。

示例[编辑]

execute in the_end run tp @s 0 128 0 - 传送执行者到末地的(0,128,0)。
execute in nether run setblock 0 0 0 air - 在下界的(0,0,0)放置空气(对应区域必须存在常加载区域)。

positioned更改命令的执行位置为规定实体的位置,和at的区别在于其不会修改执行维度。

语法[编辑]

positioned <position: x y z> <chainedCommand: ExecuteChainedOption_0>
positioned as <origin: target> <chainedCommand: ExecuteChainedOption_0>

参数[编辑]

position 基本参数
x y z类型,必选,指定命令的执行位置为特定坐标。
origin 基本参数
target类型,必选,指定命令的执行位置为规定实体的位置。必须为有效的实体ID或假名。
chainedCommand 枚举参数
ExecuteChainedOption_0类型,必选,指定执行什么子命令。可以为上述的任意11种子命令类型。

示例[编辑]

execute positioned ~ ~ ~ run tp @a ~ ~10 ~ - 将所有玩家传送到执行者上方10格(可在命令方块执行,因其修改执行坐标不需要用到@s)。
execute as @a positioned ~ ~ ~ run tp @s ~ ~10 ~ - 将所有玩家传送到其上方10格。
execute as @a positioned as @s run tp @s ~ ~5 ~ - 将所有玩家传送至其上方5格的位置
execute positioned as @s run playsound random.orb @a ~ ~ ~ - 以执行者为音源播放经验球音效。(命令方块不可执行)

rotated更改命令的执行朝向为规定位置,或规定实体的朝向。需要注意,rotated仅改变执行朝向,而不改变执行者的朝向以及执行位置。

语法[编辑]

rotated <yaw: value> <pitch: value> <chainedCommand: ExecuteChainedOption_0>
rotated as <origin: target> <chainedCommand: ExecuteChainedOption_0>

参数[编辑]

yaw 基本参数
value类型,必选,指定水平旋转角度。正北方为-180.0,正东为-90.0,正南为0.0,正西为90.0,正北以西为179.9,之后回到-180.0。可使用波浪号指定基于当前旋转角度的相对偏移。
pitch 基本参数
value类型,必选,指定垂直旋转角度。竖直上方为-90.0,至竖直下方90.0。可使用波浪号指定基于当前旋转角度的相对偏移。
origin 基本参数
target类型,必选,指定执行朝向为规定实体的朝向。
chainedCommand 枚举参数
ExecuteChainedOption_0类型,必选,指定执行什么子命令。可以为上述的任意11种子命令类型。

示例[编辑]

execute rotated 0 0 run tp @a ^ ^ ^1 - 向南方的水平方向将所有玩家传送1格。
execute as @a rotated @e[type=zombie,c=1] run tp @s ^ ^ ^1 - 以玩家附近的僵尸的朝向方向将该玩家传送1格。

ifunless是条件子命令,可用于对特定条件进行检测或者作为命令执行的限制条件。if意为如果...就...,如果检测成功那么就通过检测;unless意为除非...否则...,如果检测成功那么就导致检测不通过(类似非门)。


条件子命令的功能与/testfor/testforblock/testforblocks/scoreboard players test ...基本一致(事实上更强)。


现在的条件子命令支持4项检测:

(if|unless) block - 检测单个方块(即/testforblock
(if|unless) blocks - 检测一个区域(即/testforblocks
(if|unless) entity - 检测实体(即/testfor
(if|unless) score - 检测目标分值(即/scoreboard players test,事实上if score更强些)


语法如下所示。

(if|unless) block[编辑]

(if|unless) block检测规定位置是否为规定方块。

语法[编辑]

<subcommand: Option_If_Unless> block <position: x y z> <block: Block> <blockStates: block states> [chainedCommand: ExecuteChainedOption_0]
<subcommand: Option_If_Unless> block <position: x y z> <block: Block> <data: int> [chainedCommand: ExecuteChainedOption_0]
<subcommand: Option_If_Unless> block <position: x y z> <block: Block> [chainedCommand: ExecuteChainedOption_0]

参数[编辑]

subcommand 枚举参数
Option_If_Unless类型,必选,指定命令的执行条件。可以选填为ifunless,当填写为if时则后面的检测条件检测成功时通过;填写为unless时则检测成功时不通过。
position 基本参数
x y z类型,必选,指定待检测位置。若填写相对坐标,则命令的执行位置以执行者的位置为参考。必须为有效的坐标
block 枚举参数
Block类型,必选,指定待检测位置是否为特定方块。必须为有效的方块ID或方块名。
blockStatesdata 基本参数
block states类型(前者)或int类型(后者),必选。指定待检测方块的方块状态或数据值。设置为-1可以匹配该方块的任意数据值。
chainedCommand 枚举参数
ExecuteChainedOption_0类型,可选,指定执行什么子命令。可以为上述的任意11种子命令类型。

示例[编辑]

execute if block 0 -64 0 bedrock - 检测(0,-64,0)是否为基岩(等效于/testforblock 0 -64 0 bedrock)。
execute if block 560 51 8 wool ["color":"red"] - 检测(560,51,8)是否为红色羊毛(等效于/testforblock 560 51 8 wool 14)。
execute unless block 114 -27 81 redstone_block run give @a diamond - 当(114,-27,81)为红石块时,给予所有玩家一颗钻石。

(if|unless) blocks[编辑]

(if|unless) blocks检测规定区域是否和另一区域一致。

语法[编辑]

<subcommand: Option_If_Unless> blocks <begin: x y z> <end: x y z> <destination: x y z> <scan mode: BlocksScanMode> [chainedCommand: ExecuteChainedOption_0]

参数[编辑]

subcommand 枚举参数
Option_If_Unless类型,必选,指定命令的执行条件。可以选填为if或unless,当填写为if时则后面的检测条件检测成功时通过;填写为unless时则检测成功时不通过。
beginend 基本参数
x y z类型,必选,指代源区域的两个对角方块位置。必须为有效的坐标
destination 基本参数
Block类型,必选,对照区域的西北下角位置(即xyz坐标均为最小的位置)。
scan mode 枚举参数
BlocksScanMode类型,必选(相对应的,/testforblocks的对应参数为可选)。指定扫描模式,必须为all(检测所有方块是否全部匹配,包括空气)或masked(检测目标区域的方块和源区域的非空气方块是否匹配)
chainedCommand 枚举参数
ExecuteChainedOption_0类型,可选,指定执行什么子命令。可以为上述的任意11种子命令类型。

示例[编辑]

execute if blocks 14 57 -69 14 57 -69 36 90 56 all - 检测(14,57,-69)和(36,90,56)两个方块的位置是否完全匹配(等效于/testforblocks 14 57 -69 14 57 -69 36 90 56)。
execute unless blocks 89 55 14 98 36 76 57 45 -13 all run setblock 11 14 13 wool 14 - 当(89,55,14)和(98,36,76)所围区域不完全和(57,45,-13)匹配时,在(11,14,13)放置红色羊毛。

(if|unless) entity[编辑]

(if|unless) entity检测是否存在规定实体。

语法[编辑]

<subcommand: Option_If_Unless> entity <target: target> [chainedCommand: ExecuteChainedOption_0]

参数[编辑]

subcommand 枚举参数
Option_If_Unless类型,必选,指定命令的执行条件。可以选填为if或unless,当填写为if时则后面的检测条件检测成功时通过;填写为unless时则检测成功时不通过。
target 基本参数
target类型,必选,指定待检测实体。必须为有效的实体ID。
block 枚举参数
Block类型,必选,指定待检测位置是否为特定方块。必须为有效的方块ID或方块名。
chainedCommand 枚举参数
ExecuteChainedOption_0类型,可选,指定执行什么子命令。可以为上述的任意11种子命令类型。

示例[编辑]

execute if entity @e[type=zombie] - 检测是否有僵尸(等效于/testfor @e[type=zombie])。
execute unless entity @a[scores={level=27}] - 当不存在记分板level上为27分的玩家时,执行成功。
execute if entity @e[type=snowball] run setblock 15 15 20 redstone_block - 当世界中存在雪球时,在(15,15,20)放置红石块。

(if|unless) score[编辑]

(if|unless) score检测目标分数是否满足特定条件。其中score ...进行两者分数的比较;而score ... matches ...则检测单个目标的分数是否符合特定条件。

语法[编辑]

<subcommand: Option_If_Unless> score <target: target> <objective: string> <operation: compare operator> <source: target> <objective: string> [chainedCommand: ExecuteChainedOption_0]
<subcommand: Option_If_Unless> score <target: target> <objective: string> matches <range: integer range> [chainedCommand: ExecuteChainedOption_0]

参数[编辑]

对于if score ...
subcommand 枚举参数
Option_If_Unless类型,必选,指定命令的执行条件。可以选填为if或unless,当填写为if时则后面的检测条件检测成功时通过;填写为unless时则检测成功时不通过。
target 基本参数
target类型,必选,指定待检测实体。必须为有效的实体ID或假名。
objective(前者) 基本参数
string类型,必选,指定追踪待检测实体分数的记分项。
operation 枚举参数
compare operator类型,必选,待检测实体和源实体进行分数比较时,该参数将指定比较指标。只能为以下5种:
= - 当待检测实体和源实体分值一致时,检测通过
> - 当待检测实体大于源实体分值时,检测通过
>= - 当待检测实体大于等于源实体分值时,检测通过
< - 当待检测实体小于源实体分值时,检测通过
<= - 当待检测实体小于等于源实体分值时,检测通过
source 基本参数
target类型,必选,指定源实体。必须为有效的实体ID或假名。
objective(后者) 基本参数
string类型,必选,指定追踪源实体分数的记分项。
chainedCommand 枚举参数
ExecuteChainedOption_0类型,可选,指定执行什么子命令。可以为上述的任意11种子命令类型。


对于if score ... matches ...:
target 基本参数
target类型,必选,指定待检测实体。必须为有效的实体ID或假名。
objective 基本参数
string类型,必选,指定追踪待检测实体分数的记分项。
range 基本参数
integer range类型,必选,检查待检测实体的分数是否满足特定范围。以数字..数字的形式指定范围,支持"!"反选。
chainedCommand 枚举参数
ExecuteChainedOption_0类型,可选,指定执行什么子命令。可以为上述的任意7种子命令类型。

示例[编辑]

execute if score @s board >= @r board - 当自己在board上的分数大于等于另一位随机玩家在board上的分数时,检测通过。
execute as @e[type=zombie,c=1] unless score @s a = @e[type=villager,c=1] a run kill @s - 当附近的一只僵尸在a上的分数不等于附近的一只村民在a上的分数时,杀死该僵尸。
execute if score @s board matches 1..5 - 当自己在board上的分数在[1,5]分上时,检测通过。
execute if score @e[type=zombie,c=1] matches !6..10 - 当附近的一只僵尸在a上的分数不在[6,10]分上时,检测通过。
execute unless score @a[tag=gaming] a matches ..2 run setblock 12 34 56 air - 当带有gaming标签的玩家的分数不小于等于2分时(即大于2分时),在(12,34,56)放置空气。(事实上这个检测部分也和execute if score @a[tag=gaming] matches 3..以及execute if score @a[tag=gaming] matches !..2等效)

run执行命令。

语法[编辑]

run <command: command>

参数[编辑]

command 基本参数
command类型,必选,指定要执行的命令。

示例[编辑]

/execute as @e[type=zombie] at @s run setblock ~ ~-1 ~ diamond_block - 在僵尸脚下放置钻石块。
/execute as @r at @s run summon lightning_bolt - 雷劈一名随机玩家。
/execute as @a at @s if block ~ ~-1 ~ concrete 14 run kill @s - 当玩家踩到红色混凝土时,杀死该玩家。