
⚠️1.19.50之后彻底改版,旧版execute @a ~ ~ ~ 命令写法已废弃,必须用子命令+run结尾格式,命令末尾必须写run,否则无效。
基础总格式
plaintext
/execute <修饰子命令> <条件子命令> run <要执行的命令>
- execute后面可以堆叠任意多个修饰、条件子命令
- 必须以 run 结尾,run后面放真正想运行的指令
- 子命令从左往右依次生效,顺序会影响结果
三类子命令
1.修饰子命令:改变执行者、执行位置、视角、维度等执行环境
2.条件子命令 if / unless:判断条件,条件不满足直接不执行后面run
3.run:唯一运行子命令,放在整条命令最后,执行目标指令
一、修饰子命令(8个)
1. as 【改变执行者,不改变位置】
语法:as <选择器>
把命令的执行者换成选中实体,执行坐标不变。@s代表刚刚as选中的实体。
示例:
mcfunction
#让所有玩家自己说hello,执行者是每个玩家
execute as @a run say hello
#让所有僵尸给自己加速度效果
execute as @e[type=zombie] run effect @s speed 5 1
2. at【改变执行位置,不改变执行者】
语法:at <选择器>
执行点移动到目标实体位置,但执行者不变。
示例:
mcfunction
#在每一个玩家头顶2格生成萤石,执行者仍然是命令方块
execute at @a run setblock ~ ~2 ~ glowstone
#在所有村民头顶生成爱心粒子
execute at @e[type=villager] run particle heart ~ ~2 ~
✨最常用组合:as + at @s
完全模拟旧版execute,既换执行者,又换到该实体的位置
mcfunction
execute as @e[type=creeper] at @s run tp @s ~ ~1 ~
含义:以每只苦力怕作为执行者,在苦力怕自身位置执行传送。
3. positioned【设置执行坐标】
两种写法:
1.positioned x y z:固定绝对/相对坐标
2.positioned as <选择器>:位置跟随实体,不修改执行者
mcfunction
#固定在(0,64,0)位置执行
execute positioned 0 64 0 run say 我出生点
#以玩家位置作为执行坐标
execute positioned as @p run summon tnt ~ ~ ~
4. anchored【锚点:眼睛/脚】
语法:anchored eyes / anchored feet
修改相对坐标^的基准点,眼睛/脚底。
mcfunction
#从玩家眼睛位置向前召唤火球
execute as @p at @s anchored eyes run summon fireball ^ ^ ^3
5. facing【视角朝向】
- facing x y z:看向某个坐标
- facing entity <选择器> eyes|feet:看向实体的眼睛或脚
mcfunction
#让玩家朝向最近的僵尸眼睛
execute as @p at @s facing entity @e[type=zombie,c=1] eyes run tp @s ~ ~ ~
6. rotated【强制设置视角角度】
语法:rotated <yaw> <pitch>
yaw水平旋转,pitch俯仰角度。
7. rotated as【跟随实体视角】
语法:rotated as <选择器>
继承目标实体的视角方向。
8. in【切换维度执行】
语法:in overworld / in nether / in the_end
在指定维度运行后续命令,不改变实体位置,只是切换执行环境。
mcfunction
#在下界维度生成粒子
execute in nether positioned 0 64 0 run particle portal ~ ~ ~
9. align【方块对齐,坐标向下取整】
语法:align xyz,坐标轴写x/y/z任意组合
把执行坐标对齐方块整数坐标,消除小数偏移。
mcfunction
execute as @p at @s align xyz run summon armor_stand ~ ~ ~
二、条件子命令 if / unless
- if xxx:条件成立,才继续执行run
- unless xxx:条件不成立,才继续执行run
多个if同时写,是并且关系,全部满足才执行。
1. if block 检测方块
语法:
if block x y z <方块名>
mcfunction
#如果玩家脚下是草方块,给玩家苹果
execute as @a at @s if block ~ ~-1 ~ grass run give @s apple
unless版本:脚下不是草方块才执行
mcfunction
execute as @a at @s unless block ~ ~-1 ~ grass run give @s apple
2. if entity 实体存在检测
语法:if entity <选择器>,只要能选中至少1个实体,条件成立
mcfunction
#如果附近有僵尸,给自己提示
execute as @p if entity @e[type=zombie,r=10] run say 附近有僵尸!
3. if score 记分板判断
两种用法:
1.记分板对比另外一个记分板
if score <目标1> <计分板A> <运算符> <目标2> <计分板B>
运算符:< = >
2.分数匹配数值范围 matches
if score @s 计分板名 matches 1..10(分数1到10)
if score @s 计分板名 matches 5(等于5)
if score @s 计分板名 matches 20..(≥20)
示例:
mcfunction
#当玩家count分数等于5时执行
execute as @a if score @s count matches 5 run say 分数到5!
4. if blocks 区域方块比对
if blocks 起点 终点 参照坐标 all|masked
对比两个长方体区域方块是否相同,类似testforblocks。
三、常用完整示例(直接复制测试)
1.所有玩家脚下是沙子就传送上天
mcfunction
execute as @a at @s if block ~ ~-1 ~ sand run tp @s ~ 100 ~
2.所有僵尸,距离小于6格,脚下是石头就着火
mcfunction
execute as @e[type=zombie] at @s if entity @p[r=6] if block ~ ~-1 ~ stone run effect @s fire_resistance 1 0
3.在所有盔甲架位置生成粒子
mcfunction
execute at @e[type=armor_stand] run particle flame ~ ~1 ~ 0 0 0 0.1 10
4.如果计分板kill≥10,给玩家钻石
mcfunction
execute as @a if score @s kill matches 10.. run give @s diamond
四、重要注意事项
1.run必须写! 忘记run整条命令无效。
2.as只改执行者;at只改位置;想要两者一起,就写as xxx at @s。
3.子命令顺序有影响!先as,再at,再positioned,再if条件。
4.旧版写法(execute @a ~ ~ ~ say hi)新版本不再支持,全部改成新版子命令格式。
5.多个if是同时全部满足才执行;想要“或者”逻辑,需要写多条execute。
6.@s指代上一步as选中的实体。
五、新旧语法对照
旧版废弃写法:
plaintext
execute @e[type=creeper] ~ ~ ~ tp @s ~ ~1 ~
新版等效写法:
plaintext
execute as @e[type=creeper] at @s run tp @s ~ ~1 ~
旧版带detect检测方块:
plaintext
execute @a ~ ~ ~ detect ~ ~-1 ~ grass 0 give @s apple
新版等效写法:
plaintext
execute as @a at @s if block ~ ~-1 ~ grass run give @s apple