精选条目

手册:/execute

来自Minecraft基岩版开发Wiki
命令 > /execute
execute
用法旗标
普通命令(0)
可见性旗标
可见(0)
同步旗标
同步命令(0)
执行旗标
允许执行(0)
类型旗标
无类型(0)
作弊旗标
作弊命令(0)
游戏内描述

代表一个或多个实体执行命令。

/execute命令可以更改执行者和执行位置,然后执行命令。

版本[编辑]

本地玩家将默认使用最新版本的命令语法。附加包将通过清单文件中的最低引擎版本来判断该附加包中的/execute命令使用的命令版本。最低引擎版本是一种语义版本,由语义版本转化为命令版本的映射如下表所示。

当前命令版本枚举
键名 对应的引擎版本 描述
Invalid -1 N/A 无效
Initial 1 N/A 初始版本
TpRotationClamping 2 N/A /tp的旋转角进行钳制
NewBedrockCmdSystem 3 N/A 引入新的基岩版命令系统
ExecuteUsesVec3 4 N/A /execute使用Vec3
CloneFixes 5 N/A /clone问题修复
UpdateAquatic 6 N/A 海洋更新
EntitySelectorUsesVec3 7 N/A @e选择器使用Vec3
ContainersDontDropItemsAnymore 8 1.8.0 容器不再掉落物品
FiltersObeyDimensions 9 1.9.0 过滤器遵循所在维度
ExecuteAndBlockCommandAndSelfSelectorFixes 10 1.13.0 /execute、方块相关命令和@s选择器问题修复
InstantEffectsUseTicks 11 1.15.0 瞬间伤害瞬间治疗持续时间改用刻数
DontRegisterBrokenFunctionCommands 12 1.16.0 不再注册对应函数有问题的命令
ClearSpawnPointCommand 13 1.16.100 /clearspawnpoint命令
CloneAndTeleportRotationFixes 14 1.16.210
TeleportDimensionFixes 15 1.16.220
CloneUpdateBlockAndTimeFixes 16 1.17.30
CloneIntersectFix 17 1.17.40
FunctionExecuteOrderAndChestSlotFix 18 1.18.10
NonTickingAreasNoLongerConsideredLoaded 19 1.18.20
SpreadplayersHazardAndResolvePlayerByNameFix 20 1.19.0
NewExecuteCommandSyntaxExperimentAndChestLootTableFixAndTeleportFacingVerticalUnclampedAndLocateBiomeAndFeatureMerged 21 1.19.10 新版/execute进入实验性玩法
WaterloggingAddedToStructureCommand 22 1.19.20
SelectorDistanceFilteredAndRelativeRotationFix 23 1.19.30
NewSummonCommandAddedRotationOptionsAndBubbleColumnCloneFix 24 1.19.40
NewExecuteCommandReleaseEnchantCommandLevelFixAndHasItemDataFixAndCommandDeferral 25 1.19.50 新版/execute走出实验性玩法
ExecuteIfScoreFixes 26 1.19.60
ReplaceItemAndLootReplaceBlockCommandsDoNotPlaceItemsIntoCauldronsFix 27 1.19.70
ChangesToCommandOriginRotation 28 1.19.70 移除方块相关命令的与数据值有关的重载
RemoveAuxValueParameterFromBlockCommands 29 1.19.70
VolumeSelectorFixes 30 1.19.70
EnableSummonRotation 31 1.19.70
SummonCommandDefaultRotation 32 1.19.70
PositionalDimensionFiltering 33 1.19.80
CommandSelectorHasItemFilterNoLongerCallsSameItemFunction AgentSweepingBlockTest 34 1.20.0
35 1.20.10
36 1.20.30
37 1.20.60
38 1.20.70

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

修饰子命令:修改命令变量和命令的运行方式,从而以特定的执行者、执行位置、执行维度、执行坐标执行命令。修饰子命令有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> [chainedCommand: ExecuteChainedOption_0]
  3. execute <subcommand: Option_If_Unless> blocks <begin: x y z> <end: x y z> <destination: x y z> <scan mode: BlocksScanMode> [chainedCommand: ExecuteChainedOption_0]
  4. execute <subcommand: Option_If_Unless> entity <target: target> [chainedCommand: ExecuteChainedOption_0]
  5. execute <subcommand: Option_If_Unless> score <target: target> <objective: string> <operation: compare operator> <source: target> <objective: string> [chainedCommand: ExecuteChainedOption_0]
  6. 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> [chainedCommand: ExecuteChainedOption_0]

参数[编辑]

subcommand 枚举参数
Option_If_Unless类型,必选,指定命令的执行条件。可以选填为ifunless,当填写为if时则后面的检测条件检测成功时通过;填写为unless时则检测成功时不通过。
position 基本参数
x y z类型,必选,指定待检测位置。若填写相对坐标,则命令的执行位置以执行者的位置为参考。必须为有效的坐标
block 枚举参数
Block类型,必选,指定待检测位置是否为特定方块。必须为有效的方块ID或方块名。
blockStates 基本参数
block states类型,必选。指定待检测方块的方块状态。
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 - 当玩家踩到红色混凝土时,杀死该玩家。

版本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 - 当玩家踩到红色混凝土时,杀死该玩家。

初始版本语法中,该命令只有两个重载。

重载[编辑]

  1. execute <origin: target> <position: x y z> <command: command>
  2. execute <origin: target> <position: x y z> detect <detectPos: x y z> <block: Block> <data: int> <command: command>

参数[编辑]

重载1更改命令的执行者和执行位置,然后直接执行命令。

origin 基本参数
target类型,必选,指定命令要以何实体为执行者。必须为玩家名或目标选择器
position 基本参数
x y z类型,必选,指定命令要以何位置为执行位置。若填写相对坐标,则命令的执行位置以origin: target的位置为参考。必须为坐标
command 基本参数
command类型,必选,指定执行什么命令。特殊的,这命令可以为/execute。只能执行权限等级小于等于1的命令。

重载2更改命令的执行者和执行位置,然后检测特定位置是否为特定方块(类似于/testforblock),检测通过后执行命令。

origin 基本参数
target类型,必选,指定命令要以何实体为执行者。必须为玩家名或目标选择器
position 基本参数
x y z类型,必选,指定命令要以何位置为执行位置。若填写相对坐标,则命令的执行位置以origin: target的位置为参考。必须为坐标
detectPos 基本参数
x y z类型,必选,指定待检测位置。若填写相对坐标,则命令的执行位置以origin: target的位置为参考。必须为坐标
block 枚举参数
Block类型,必选,指定待检测位置是否为特定方块。必须为有效的方块ID或方块名。
data 基本参数
int类型,必选,指定待检测位置的方块数据值。设置为-1可以匹配该方块的任意数据值。
command 基本参数
command类型,必选,指定执行什么命令。特殊的,这命令可以为/execute。只能执行权限等级小于等于1的命令。

效果[编辑]

命令触发条件执行结果
任意参数未正确指定失败
origin无法解析为一个实体或多个实体(若为玩家名则必须在线)
(对于重载2)检测结果不一致
命令执行失败
执行成功命令以特定的执行者和执行位置执行

输出[编辑]

命令执行结果成功次数增量
任意失败0
成功< >

示例[编辑]

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

响应[编辑]

WebSocket[编辑]

  • :根对象
    •  header:报文消息的头。
      •  messagePurpose:该条报文消息的发送目的,对于命令响应恒为commandResponse
      •  requestId:造成该响应的请求报文的UUID。
      •  version:该报文的版本。
    •  body:报文消息的内容。
      •  statusCode:该响应的状态码。
      •  statusMessage:该响应的状态消息,对于命令响应为该命令的执行反馈文本。

Manual:/execute/response

示例[编辑]

脚本API[编辑]

  • :根对象
    •  statusCode:该响应的状态码。
    •  statusMessage:该响应的状态消息,对于命令响应为该命令的执行反馈文本。

Manual:/execute/response

示例[编辑]

历史[编辑]

携带版(Alpha)
0.16.0build 1加入了/execute命令。
此命令的功能相当于其在Java版1.10的功能,但在/execute中实体目标是可选的。
基岩版
1.19.101.19.10.20/execute加入了7条子命令:asatpositioned (as)ifunlessrun
1.19.10.20新版子命令属于实验性玩法,并需要命令版本21。
1.19.10.20旧版语法只有未开启实验性玩法或命令版本小于或等于20时启用。
1.19.401.19.40.20加入了rotated (as)子命令,属于实验性玩法。
1.19.40.21加入了facing (entity)align子命令,属于实验性玩法。
1.19.40.23加入了inanchored子命令,属于实验性玩法。
1.19.501.19.50.23新版子命令走出实验性玩法,命令版本提升至25。
1.19.50.23弃用旧版语法,只有在命令版本小于或等于24时启用。
基岩版(即将到来)
1.19.701.19.70.21当命令版本大于或等于28时,不再具有(if|unless) block <data: int>重载。

画廊[编辑]

Template:Commands