/execute 命令
图片处理:未转载源站游戏图像;具体物品、实体、效果或界面说明以中文文字保留。
来源:Commands/execute,英文修订 ID:3848540。
执行另一条命令,同时允许更改执行者、执行位置与角度、添加前置条件,以及存储结果。Java 版所需权限等级为 2;基岩版为 1,需要启用作弊。
本文描述 Java 版 1.13(17w45a)和基岩版 1.19.50(beta 1.19.50.23)以来的现行命令。早期版本见 /execute(旧版)。
原文更新提示:26.3-snap1 的 slots 条件子命令仍待补充,本页正文尚未记载其完整语法。
用法
原文统计 /execute 有 Java 版 14 种、基岩版 11 种子命令(也称指令)。多个子命令可以串联;Java 版分 4 类,基岩版分 3 类:
- 修饰子命令:修改执行上下文变量,使后续子命令使用指定执行者、位置、维度、旋转与锚点。
- 条件子命令:检测条件,输出结果或限制后续执行。
- 存储子命令:仅 Java 版,可把输出存到记分板、实体/方块实体/命令存储数据,或修改 Boss 栏当前值/最大值。
run:执行另一条命令。
可把所需子命令串接起来。除 run 外,子命令顺序可自由安排,也可重复。run 只能使用一次,且必须在链末尾。只有 run 或条件子命令可结束整条链,否则命令无法解析。
子命令与分叉
游戏从左向右依次处理子命令。因此以下命令不同:
# 所有实体沿各自前方向前移动一格
execute as @e at @s run tp ^ ^ ^1
# 所有实体传送到原执行者前方一格
execute at @s as @e run tp ^ ^ ^1
有些子命令会把执行分叉成多条分支,导致后续子命令多次执行。例如 as 选中多个实体时,对每个实体各执行一次后续子命令。不发生分叉时,只有一条主分支。
条件不通过、as 选中零个实体等情况可能使分支中途终止。基岩版终止时显示红色消息。所有分支均终止时,称整个 /execute 已终止。
两版分叉方式不同,另见 MC-125067 与 MCPE-165278:
- Java 版采用广度优先,按子命令逐一处理,先处理其他子命令,再处理
run;因此run不能影响其他子命令。 - 基岩版采用深度优先,分叉后把全部后续子命令视为整体,对每个分支执行一次。
例如世界中有盔甲架 A、B,玩家执行:
execute as @e[type=armor_stand] as @e[type=armor_stand] run summon armor_stand
两版中嵌套 ... run execute ... 不改变整体效果。例如以下两条等价:
execute as @e[type=armor_stand] as @e[type=armor_stand] run summon armor_stand
execute as @e[type=armor_stand] run execute as @e[type=armor_stand] run execute run execute run summon armor_stand
Java 版可通过 /function 实现深度优先。世界中有 A、B 两个盔甲架,玩家执行:
execute as @e[type=armor_stand] run function test
函数文件包含:
execute as @e[type=armor_stand] run summon armor_stand
其行为与基岩版直接执行前面的双 as 命令相同。基岩版则没有实现广度优先的方法。
输出值
详见命令输出。
存储的值
仅 Java 版。一条分支完整执行、而非中途终止后,输出 success 和 result:
- 它们属于各执行分支,不属于整个
/execute。 - 来自最后一个条件子命令,或
run中的命令。 - 均为整数;
success总为0或1,非整数result向下取整。 - 分支最后一个子命令失败时,两值均为
0。 - 可由
store存储。
run 中执行 /function 时,在某些条件下无法取得这两个值,详见 /function。
若 store 后发生分叉,每条分支完整执行后分别存储输出。分支写向同一位置时,后执行的分支直接覆盖先前值,不会累加。因此整条命令结束后,那里保存的是最后执行分支的输出。
成功次数
与大多数命令一样,/execute 本身也有成功次数,无论是否中途终止:
- 它属于整个
/execute,而非单条分支。 - 来自最后一个条件子命令或
run。 - 各分支的成功次数相加,结果为不小于
0的整数。 - 由命令方块执行时,成功次数传给命令方块,可由连锁条件命令方块检查,或由红石比较器读取。
语法
每种子命令有专用语法,完整语法树如下。-> execute 表示必须继续另一个子命令,-> [execute] 表示可选继续;基岩版 chainedCommand: ExecuteChainedOption_0 表示后续子命令的起点。
/execute
Java 版:
/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]
... stopwatch <id> <range> -> [execute]
... run <command>
基岩版:
/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>
参数格式约定
以下各节的实体参数,在 Java 版通常为 entity,可用玩家名称、目标选择器或 UUID;基岩版通常为 CommandSelector<Actor>,可用名称或目标选择器。以名称指定的玩家必须在线。能选择多个实体与必须选中单个实体的区别由各节说明。
vec3/CommandPositionFloat 是三个位置数值,可用波浪号相对坐标或插入符局部坐标。block_pos/CommandPosition 表示方块位置。resource_location 是资源位置;nbt_path 使用 NBT 路径格式。
修饰子命令
align
沿指定轴把执行位置对齐到所在方块的整数坐标,即向下取整,各轴改变量小于一格。
Java 版:
align <axes> -> execute
基岩版:
align <axes: string> <chainedCommand: ExecuteChainedOption_0>
<axes>: swizzle/axes: string:由 x、y、z 中一个或多个不重复字母组成,可任意排序。参数错误时无法解析。
例如执行位置 (-1.8,2.3,5.9) 使用 execute align xz 后为 (-2,2.3,5)。位置 (2.4,-1.1,3.8) 执行下例后,把最近玩家出生点设为 (2,-2,3):
execute align yxz run spawnpoint @p ~ ~ ~
anchored
把执行锚点设为眼部或脚部,默认为脚部。positioned <pos> 会把锚点重置为脚部。此命令改变局部坐标的中心,也改变 /rotate、/teleport、/execute 中 facing 计算角度所用的起点。基岩版相关问题见 MCPE-162681、MCPE-165051。
Java 版:
anchored <anchor> -> execute
基岩版:
anchored <eyes|feet> <chainedCommand: ExecuteChainedOption_0>
<anchor>: entity_anchor/eyes|feet:必须为 eyes 或 feet。参数错误时无法解析。/tp 会把目标脚部放到给定位置,因此第一例把执行者脚部传到眼部原位置,第二例放到眼部左侧 5 格:
execute anchored eyes run tp ^ ^ ^
execute anchored eyes run tp ^5 ^ ^
as
把执行者改为目标实体,不改变执行位置、旋转、维度和锚点,也就是改变 @s 的含义。
Java 版:
as <targets> -> execute
基岩版:
as <origin: target> <chainedCommand: ExecuteChainedOption_0>
<targets>: entity/origin: target(CommandSelector<Actor>):成为新执行者的目标。选中多个实体时分叉;没有选中实体时终止当前分支。参数错误时无法解析。
杀死所有绵羊:
execute as @e[type=sheep] run kill @s
使已加载区块内所有村民无敌(Java 版):
execute as @e[type=villager] run data merge entity @s {Invulnerable:1}
at
把执行位置、旋转、维度设为目标实体的值,不改变执行者。
Java 版:
at <targets> -> execute
基岩版:
at <origin: target> <chainedCommand: ExecuteChainedOption_0>
<targets>: entity/origin: target(CommandSelector<Actor>):要匹配位置、旋转和维度的实体。参数错误时无法解析;选中多个实体时分叉,没有选中实体时终止当前分支。
把所有绵羊上移一格:
execute as @e[type=sheep] at @s run tp ~ ~1 ~
下例杀死运行命令的玩家,因为 at 没有改变执行者:
execute at @e[type=sheep] run kill @s
facing
改变执行旋转,使其从当前锚点(眼部或脚部)朝向指定点。
Java 版:
facing <pos> -> execute
facing entity <targets> <anchor> -> execute
基岩版:
facing <position: x y z> <chainedCommand: ExecuteChainedOption_0>
facing entity <origin: target> <eyes|feet> <chainedCommand: ExecuteChainedOption_0>
<pos>: vec3/position: x y z(CommandPositionFloat)是要朝向的坐标。entity 形式的 <targets>/origin: target 是要朝向的实体,<anchor>: entity_anchor/eyes|feet 指定目标眼部 eyes 或脚部 feet。
执行旋转更新为朝向指定位置或目标。参数错误时无法解析;选中多个目标时分叉,没有选中实体时终止当前分支。
让执行者向左转:
execute facing ^1 ^ ^ run tp @s ~ ~ ~ ~ ~
所有实体向 (0,64,0) 移动一格,不改变其自身旋转:
execute as @e at @s facing 0 64 0 run tp @s ^ ^ ^1
同样移动,并改变实体旋转:
execute as @e at @s facing 0 64 0 run tp ^ ^ ^1 ~ ~
所有非玩家实体向各自最近玩家移动一格,不改变自身旋转:
execute as @e[type=!player] at @s facing entity @p feet run tp @s ^ ^ ^1
in
设置执行维度与执行位置。遵循维度坐标缩放:从主世界到下界,执行位置 X/Z 除以 8;反向则乘以 8。相对坐标、局部坐标与自定义维度同样适用。
Java 版:
in <dimension> -> execute
基岩版:
in <dimension: Dimension> <chainedCommand: ExecuteChainedOption_0>
<dimension>: dimension/dimension: Dimension:新执行维度 ID。Java 版使用已有维度资源位置;基岩版使用维度枚举。参数错误时无法解析。
从主世界查找末地城:
# Java 版
execute in minecraft:the_end run locate structure minecraft:end_city
# 基岩版
execute in the_end run locate structure end_city
主世界 (16,64,16) 的玩家执行下例后,传送至下界 (16,64,16):
# Java 版
execute in minecraft:the_nether positioned as @s run tp ~ ~ ~
# 基岩版
execute in nether positioned as @s run tp ~ ~ ~
同一玩家执行下例,因维度缩放而到下界 (2,64,2):
# Java 版
execute in minecraft:the_nether run tp ~ ~ ~
# 基岩版
execute in nether run tp ~ ~ ~
主世界 (80,64,80) 的玩家执行下例后,到下界 (10,64,15):
# Java 版
execute in minecraft:the_nether run tp ~ ~ ~5
# 基岩版
execute in nether run tp ~ ~ ~5
on
仅 Java 版。按与当前执行者实体的关系选择新执行者,不改变位置、旋转、维度或锚点。
Java 版:
on <relation> -> execute
<relation> 可为:
attacker:前 5 秒最后伤害执行者的实体。minecraft:no_anger标签的伤害不记录攻击者;交互实体不会在 5 秒后遗忘攻击者,某些生物停止敌对时会遗忘。controller:骑乘且控制当前实体的实体,详见骑乘控制。leasher:用拴绳牵引当前实体的实体。origin:导致当前实体生成的实体,例如箭的射手、激活 TNT 的实体。owner:当前可驯服动物的主人。passengers:直接骑乘当前实体的所有实体,不包含乘客的乘客。target:当前实体意图攻击的目标;交互实体可选择最后与其交互的实体。vehicle:当前实体骑乘的实体。
更新执行者会改变 @s。passengers 选中多个实体时分叉,其他关系最多选一个。当前执行者不是实体、关系不适用或没有匹配实体时,终止当前分支。
对牵着猫的实体造成一颗心(2 点)伤害:
execute as @e[type=cat] on leasher run damage @s 2 generic
给所有坐船的玩家一根钓鱼竿:
execute as @e[type=boat] on passengers run give @s fishing_rod
positioned
设置执行位置,不改变执行旋转或维度。可匹配实体位置;Java 版还可设为指定高度图所存 Y 高度上方一格。
Java 版:
positioned <pos> -> execute
positioned as <targets> -> execute
positioned over <heightmap> -> execute
基岩版:
positioned <position: x y z> <chainedCommand: ExecuteChainedOption_0>
positioned as <origin: target> <chainedCommand: ExecuteChainedOption_0>
直接位置形式的 <pos>: vec3/position: x y z(CommandPositionFloat)是新位置。as 的 <targets>: entity/origin: target 是要匹配位置的实体。仅 Java 版 over 的 <heightmap>: heightmap 必须为 world_surface、motion_blocking、motion_blocking_no_leaves、ocean_floor 之一。
直接 positioned <pos> 还会把锚点重置为脚部。参数错误时无法解析;选择多个实体时分叉;没有选中实体,或 over 使用的当前执行位置未加载时,终止当前分支。
查找 (0,64,0) 附近的村庄:
# Java 版
execute positioned 0 64 0 run locate structure #village
# 基岩版
execute positioned 0 64 0 run locate structure village
rotated
设置执行旋转,可匹配实体的旋转。
Java 版:
rotated <rot> -> execute
rotated as <targets> -> execute
基岩版:
rotated <yaw: value> <pitch: value> <chainedCommand: ExecuteChainedOption_0>
rotated as <origin: target> <chainedCommand: ExecuteChainedOption_0>
直接指定时,Java 版 <rot>: rotation 由 <yaw> <pitch> 组成;基岩版 yaw: value、pitch: value 均为 RelativeFloat,表示旋转角,可用波浪号相对角度。as 的目标参数指定要匹配旋转的实体。
参数错误时无法解析;选中多个实体时分叉;没有选中有效实体时终止当前分支。
让每只绵羊沿各自最近玩家的朝向移动一格,以下两条均可:
execute as @e[type=sheep] at @s rotated as @p run tp @s ^ ^ ^1
execute as @e[type=sheep] positioned as @s rotated as @p run tp @s ^ ^ ^1
summon
仅 Java 版。在执行位置召唤新实体,并把执行者改为它。
Java 版:
summon <entity> -> execute
<entity>: resource:将被召唤并成为执行者的实体类型,必须为 minecraft:entity_type 注册表中的资源位置。参数错误时无法解析。执行位置 X/Z 超出 [-30000000,30000000),或 Y 超出 [-20000000,20000000) 时,终止当前分支。指定 minecraft:player 或 minecraft:fishing_bobber 时终止整个 /execute。
召唤带 Test 标签的绵羊:
execute summon sheep run tag @s add Test
召唤方块展示实体并缩放:
execute summon block_display run data merge entity @s {transformation:{scale:[4.0,4.0,4.0]},block_state:{Name:"minecraft:dirt"}}
条件子命令
if 与 unless 限制命令只在指定条件下执行。通常 unless 是 if 的否定,即“如果不……”,两者参数结构完全相同。
通用结果规则:
- 参数错误时无法解析。
- 某些非法情形,例如检测世界之外的方块,会让
if和unless都终止或失败,而不是互为否定。 - 不在链末尾时,只有检测通过才继续,否则终止当前分支。Java 版的多分支执行中,相当于按上下文筛选分支。
- 在链末尾时,检测是否满足条件并输出;Java 版的输出值可由
store保存。下文“失败条件”均表示:不在末尾时终止当前分支,在末尾时命令失败。
原文列出 Java 版 12 类、基岩版 4 类条件。两版都有 block、blocks、entity、score;biome、data、dimension、function、items、loaded、predicate、stopwatch 仅 Java 版具有。
(if|unless) biome
仅 Java 版。检测指定位置的生物群系。
(if|unless) biome <pos> <biome> -> [execute]
<pos>: block_pos:检测位置。<biome>: resource_or_tag:要匹配的生物群系,必须为 minecraft:worldgen/biome 注册表的资源位置,或 # 前缀的生物群系标签。位置未加载、在世界之外或检测未通过时,按通用规则终止/失败。在链末尾成功时,success 与 result 均为 1。
(if|unless) block
比较指定位置的方块与给定方块 ID,Java 版还支持方块标签。
Java 版:
(if|unless) block <pos> <block> -> [execute]
基岩版:
<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]
<pos>: block_pos/position: x y z(CommandPosition):目标位置。<block>: block_predicate/block: Block:要匹配的方块。Java 版可使用方块 ID、# 前缀标签及状态/NBT 条件;基岩版为方块 ID,可另附 blockStates: block states(BlockStateCommandParam)以检测状态,格式为方括号中的键值对。
位置未加载、在世界之外或检测未通过时,按通用规则终止/失败。Java 版在链末尾成功时,success 与 result 均为 1。
(if|unless) blocks
比较两片大小相同的区域。
Java 版:
(if|unless) blocks <start> <end> <destination> (all|masked) -> [execute]
基岩版:
<subcommand: Option_If_Unless> blocks <begin: x y z> <end: x y z> <destination: x y z> <scan mode: BlocksScanMode> [chainedCommand: ExecuteChainedOption_0]
<start>/begin: x y z 与 <end>/end: x y z 是源区域任意两个对角顶点,类型为 block_pos/CommandPosition。<destination>/destination: x y z 是目标区域西北下角,目标区域大小与源区域相同。all|masked/scan mode: BlocksScanMode 指定比较全部方块,还是忽略源区域空气;必须为 all 或 masked。
任一上述位置未加载或在世界之外、源区域体积超过 32768(相当于 8 个区块段),或检测未通过时,按通用规则终止/失败。Java 版在链末尾成功时,success=1;if 的 result 为匹配方块数量,即 all 模式的总方块数或 masked 模式源区域非空气方块数;unless 的 result=1。
(if|unless) data
仅 Java 版。检查目标方块、实体或命令存储在指定路径是否存在数据标签。
(if|unless) data block <pos> <path> -> [execute]
(if|unless) data entity <target> <path> -> [execute]
(if|unless) data storage <source> <path> -> [execute]
block 形式的 <pos>: block_pos 是检测位置,<path>: nbt_path 是要检测的 NBT 路径。entity 形式的 <target>: entity 为单个目标,<path> 为其数据路径。storage 形式的 <source>: resource_location 为命令存储资源位置,<path> 为存储内路径。
方块位置未加载、在世界之外、该处不是方块实体、实体目标没有解析为有效实体,或检测未通过时,按通用规则终止/失败。以名称指定的玩家必须在线。链末尾成功时,success=1;if 的 result 为匹配数据标签数,unless 的 result=1。
(if|unless) dimension
仅 Java 版。检测执行维度是否匹配。
(if|unless) dimension <dimension> -> [execute]
<dimension>: dimension 为要检测的已有维度资源位置。检测未通过时按通用规则终止/失败;链末尾成功时 success 与 result 均为 1。
(if|unless) entity
检查是否存在一个或多个实体。
Java 版:
(if|unless) entity <entities> -> [execute]
基岩版:
<subcommand: Option_If_Unless> entity <target: target> [chainedCommand: ExecuteChainedOption_0]
<entities>: entity/target: target(CommandSelector<Actor>)是要检测的目标,可选多个。检测未通过时按通用规则终止/失败。Java 版链末尾成功时,success=1;if 的 result 为匹配实体数,unless 的 result=1。
(if|unless) function
仅 Java 版。运行函数或函数标签,检测它是否非 Void(有返回值)且返回值非零。不改变执行上下文;与其他条件子命令不同,被检测函数可以修改世界。此子命令不能放在链末尾,见 MC-267799。
(if|unless) function <function> -> execute
<function>: function:要检测的函数资源位置,或 # 前缀的函数标签。参数错误时无法解析。函数/标签不存在,或标签不含函数时,终止整个 /execute。
只给定一个函数时,若它不是宏、不是 Void,且返回值非零,则 if 继续,unless 终止当前分支。
给定多个函数时,按照标签文件定义的顺序逐个检查并调用:
- 遇到宏函数:结束本子命令;
if终止当前分支,unless继续。 - 遇到 Void 函数:检查并调用下一个;如果已经没有下一个,则
if终止当前分支,unless继续。 - 函数返回失败,或返回成功但值为
0:结束本子命令;if终止当前分支,unless继续。 - 函数返回成功且值非零:结束本子命令;
if继续,unless终止当前分支。
(if|unless) items
仅 Java 版。检测指定物品栏槽位是否含匹配物品。
(if|unless) items block <sourcePos> <slots> <item_predicate>
(if|unless) items entity <source> <slots> <item_predicate>
<sourcePos>: block_pos:方块实体位置。<source>: entity:目标实体。<slots>: slot_source:要检测的槽位,可使用槽位范围。<item_predicate>: item_predicate:要匹配的物品或物品标签,也可使用物品谓词条件。
位置未加载、在世界之外、该处不是方块实体、实体来源没有解析为有效实体,或检测未通过时,按通用规则终止/失败。以名称指定的玩家必须在线。链末尾成功时,success=1;if 的 result 为匹配物品数,unless 的 result=1。
(if|unless) loaded
仅 Java 版。检测指定位置的区块是否完全加载,达到实体运算(Entity Ticking)等级。
(if|unless) loaded <pos> -> [execute]
<pos>: block_pos:检测位置。位置在世界之外或检测未通过时,按通用规则终止/失败。链末尾成功时,success 与 result 均为 1。
(if|unless) predicate
仅 Java 版。检测谓词是否成功。
(if|unless) predicate <predicate> -> [execute]
<predicate> 可以是资源位置,也可以是内联定义;内联结构与谓词相同,使用 SNBT 格式。原文说明:命令在执行期间解析为未注册内容,或发送到客户端解析为客户端内容。谓词不存在或检测未通过时,按通用规则终止/失败。链末尾成功时,success 与 result 均为 1。
(if|unless) score
检测一个分数与另一个分数是否满足指定关系,或分数是否位于指定范围。
Java 版:
(if|unless) score <target> <targetObjective> (<|<=|=|>=|>) <source> <sourceObjective> -> [execute]
(if|unless) score <target> <targetObjective> matches <range> -> [execute]
基岩版:
<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]
<target>: score_holder/target: target:单个分数持有者;Java 版可为名称、目标选择器、UUID 等记分对象。基岩版可用目标选择器,也可用引号内的唯一 ID 指定计分项中的非玩家实体。<targetObjective>: objective/objective: string 是要读取目标分数的记分板计分项。
双分数比较中,运算符必须为 <、<=、=、>=、> 之一;基岩版参数类型为 CommandCompareOperator。<source>/source: target 也是单个分数持有者,格式同目标;<sourceObjective>/第二个 objective: string 指定来源计分项。
matches 中 <range>: int_range/range: integer range(CommandIntegerRange)是整数范围:单个整数匹配该值,min..max 包含两端,min.. 或 ..max 表示只有下限或上限。
Java 版目标或来源为 *,或检测未通过时,按通用规则终止/失败。Java 版链末尾成功时,success 与 result 均为 1。
(if|unless) stopwatch
仅 Java 版。比较指定秒表已流逝的时间与浮点数范围,单位为秒。
(if|unless) stopwatch <id> <range> -> [execute]
<id>: resource_location 是目标秒表。<range>: float_range 是秒数范围,可为单个浮点数、min..max、min.. 或 ..max。检测未通过时按通用规则终止/失败;链末尾成功时 success 与 result 均为 1。
存储子命令
仅 Java 版。把最后一个子命令的 result 或 success 存到某处。store 首先与链中其他子命令一起处理,记录存储位置;最后的条件子命令或 run 执行完毕后,才把输出写入该位置。命令输出始终为整数,非整数会向下取整。
共有五种模式:方块 NBT、Boss 栏、实体 NBT、记分板分数、命令存储 NBT。各模式参数错误时均无法解析。
store (result|success) block
把最终命令的输出存到方块实体的数据标签中。类型可为 byte、short、int、long、float、double。若输出是小数,先取整,再乘 <scale>。
store (result|success) block <targetPos> <path> <type> <scale> -> execute
<targetPos>: block_pos 为方块位置;<path>: nbt_path 为接收值的标签路径;<type> 必须为上述六种类型之一;<scale>: double 为存储前应用的倍率,可为负数。
store (result|success) bossbar
把最终命令的输出存为 Boss 栏的当前值或最大值。
store (result|success) bossbar <id> (value|max) -> execute
<id>: resource_location 为目标 Boss 栏 ID。value 覆盖当前值,max 覆盖最大值。
store (result|success) entity
把最终命令输出存到实体数据标签,类型为 byte、short、int、long、float、double。若为小数,先取整再乘 <scale>。与 /data 一样,/execute store 不能修改玩家 NBT。
store (result|success) entity <target> <path> <type> <scale> -> execute
<target>: entity 为单个接收数据的实体;<path>: nbt_path 为目标标签路径;<type> 为上述六种类型之一;<scale>: double 为存储前的倍率,可为负数。
store (result|success) score
用最终命令的输出覆盖 <targets> 在 <objective> 中的分数。
store (result|success) score <targets> <objective> -> execute
<targets>: score_holder 指定一个或多个分数持有者,其分数将被覆盖;<objective>: objective 是记分板计分项。
store (result|success) storage
把输出写入命令存储 <target> 中的 <path>。类型为 byte、short、int、long、float、double。小数先取整,再乘 <scale>。存储尚不存在时会创建。
store (result|success) storage <target> <path> <type> <scale> -> execute
<target>: resource_location 为目标命令存储资源位置;<path>: nbt_path 为接收值的路径;<type> 为上述六种类型之一;<scale>: double 为存储前倍率,可为负数。
run 子命令
唯一参数是要执行的命令,其执行上下文可能已由前面的子命令修改。
Java 版:
run --><command>
基岩版:
run <command: command>
command/command: command(unique_ptr<Command>)可以是任意命令。执行该命令;若它失败,run 也失败。
Java 版技术细节:run 把命令节点重置到 Brigadier 命令分派器的根节点。以聊天文本建模时,玩家输入 / 开头消息便启动命令分派器;命令节点是光标正在编辑的词/条目,可以是命令或参数;根节点位于当前命令第一个词之前。
示例
以下命令沿用源文,部分 NBT 字段来自旧版本,不应直接当作所有现行版本通用语法。代码内输出字符串为保留原例而未改写,中文含义在说明中给出。
把快捷栏第一槽位物品带有效率附魔的所有玩家传送至 (0,64,0)(Java 版):
/execute as @a if data entity @s Inventory[{Slot:0b}].tag.Enchantments[{id:"minecraft:efficiency"}] run tp @s 0 64 0
在所有玩家眼前 3 格创建烟雾粒子(Java 版):
/execute as @a at @s anchored eyes run particle smoke ^ ^ ^3
给距执行玩家 5 格内的猪装上鞍,移除 5 格外猪的鞍(Java 版):
/execute as @e[type=pig] at @s store success entity @s Saddle byte 1 if entity @p[distance=..5]
玩家位于水方块内时,让其在聊天中说“我的脚湿透了!”:
/execute as @a at @s if block ~ ~ ~ water run say "My feet are soaked!"
若 test 分数未设置,例如执行过 scoreboard players reset @s test,让玩家说“分数已重置”:
/execute as @a unless score @s test = @s test run say "Score is reset"
把距最近玩家 3 格内的所有骷髅抛向天空(Java 版):
/execute at @p as @e[type=skeleton,distance=..3] run data merge entity @s {Motion:[0.0,2.0,0.0]}
杀死所有没有头部装备的僵尸(Java 版):
/execute as @e[type=zombie] unless data entity @s ArmorItems[3].id run kill @s
把每位玩家的 nearbyRedSheep 分数设为其 20 格内红色绵羊的数量:
/execute as @a at @s store result score @s nearbyRedSheep if entity @e[type=sheep,nbt={Color:14},distance=..20]
参考资料
- MC-125067,原文状态“不予修复”。
- MCPE-165278,原文状态“未解决”。
- MCPE-162681。
- MCPE-165051。
- MC-267799,原文状态“无效”。
外部链接
- Microsoft Learn:
/execute命令,2024-07-17。
本文为 Minecraft Wiki 对应英文条目的中文翻译与改编,已按要求省略变更历史。原作者与贡献者见来源页及其历史;除另有说明外,按 CC BY-NC-SA 3.0 非商业、署名、相同方式共享。