完整版BedrockWiki镜像!
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
---
|
||||
title: New Execute
|
||||
category: Commands
|
||||
title: 全新execute命令
|
||||
category: 命令
|
||||
tags:
|
||||
- easy
|
||||
- 简单
|
||||
mentions:
|
||||
- JaylyDev
|
||||
- Sprunkles137
|
||||
@@ -12,236 +12,243 @@ mentions:
|
||||
- zheaEvyline
|
||||
---
|
||||
|
||||
## Introduction
|
||||
# 全新execute命令
|
||||
|
||||
With the release of 1.19.50, the `/execute` command was given a syntax overhaul. While the syntax is now more verbose and longer to write, it allows much finer control over the contextual components of commands and adds support for conditions to commands, superseding the use of commands like `/testfor`, `/testforblock`, and `/testforblocks`.
|
||||
<!--@include: @/wiki/bedrock-wiki-mirror.md-->
|
||||
|
||||
Before we dive into the syntax and how to write it, we need to understand how the old `/execute` command worked, and what changed and why. This will make explaining the concepts found in the syntax easier.
|
||||
## 引言
|
||||
|
||||
## Understanding Execution Context
|
||||
随着1.19.50版本的发布,`/execute`命令迎来了语法革新。虽然新语法需要书写更长的指令,但它能更精细地控制命令的上下文组件,并为命令添加了条件支持,从而取代了`/testfor`、`/testforblock`和`/testforblocks`等命令。
|
||||
|
||||
For both beginners to commands and those well versed in how old `/execute` behaved, it may be a good idea to review the concept of a command's **execution context**.
|
||||
在深入解析语法细节之前,我们需要理解旧版`/execute`的工作原理及其演变逻辑,这将有助于更好地理解新语法中的概念。
|
||||
|
||||
In short, these are the parameters that affects how a command runs. Who the command will run as, also known as its executor; where the command will run, and in which dimension; and the rotation applied to the command are all parameters that can be changed.
|
||||
## 理解执行上下文
|
||||
|
||||
Every command has this context applied to it, and this context changes depending on how the command runs. Commands fired from command blocks do not have an executor, and the position is set to that command block; commands ran from chat define the executor as the player, and it runs at the player's position.
|
||||
无论是命令新手还是熟悉旧版`/execute`的开发者,都有必要重新审视命令的**执行上下文**概念。
|
||||
|
||||
## Execute, and Why it Changed
|
||||
简而言之,这些参数决定了命令的运行方式:命令的执行者(即由谁执行)、执行位置、所在维度以及执行时的视角旋转角度等。
|
||||
|
||||
The `/execute` command executes a command on behalf of one or more entities. The old syntax used to be this:
|
||||
每个命令都携带这样的上下文,且上下文会根据命令的运行方式动态变化。从命令方块触发的命令没有明确执行者,位置固定在命令方块处;通过聊天框执行的命令则以玩家为执行者,并在玩家当前位置运行。
|
||||
|
||||
## Execute命令的演变逻辑
|
||||
|
||||
`/execute`命令允许通过一个或多个实体代理执行命令。旧版语法结构如下:
|
||||
|
||||
```
|
||||
/execute <target> <position> <command>
|
||||
/execute <target> <position> detect <position> <block> <data value> <command>
|
||||
/execute <目标> <位置> <命令>
|
||||
/execute <目标> <位置> detect <位置> <方块> <数据值> <命令>
|
||||
```
|
||||
|
||||
You specified a target to execute as, then the command's context would change to run as that target, and at that target. Any position changes were then always relative to that target.
|
||||
旧语法指定目标后,命令的上下文将切换至该目标的位置执行。所有位置偏移都默认相对于该目标。
|
||||
|
||||
While this is useful in most cases, it also forces the fact that a command's target and its position are always tied together (unless you were to manually insert world coordinates in place of `<position>`). It is also not very malleable in regards to making conditional statements, as you have to execute as an entity every time.
|
||||
虽然这在多数情况下很实用,但也强制绑定了命令目标与执行位置(除非手动输入世界坐标)。同时,在构建条件语句时缺乏灵活性,因为每次都需要通过实体执行。
|
||||
|
||||
Back in the Summer of 2017 during the Update Aquatic's development, the developers of Minecraft: Java Edition were getting feedback from the community on how they can improve the `/execute` command's syntax, and the basic concept that was conceived is this: `/execute` takes an unlimited number of **subcommands** that manipulate certain aspects of the command in the order you specify, then a "run" subcommand is placed at the end to fire a command.
|
||||
在2017年夏季水域更新的开发阶段,Minecraft Java版开发团队收集了社区反馈后,提出了全新设计理念:`/execute`可以串联无限个**子命令**来按序调整命令的不同上下文维度,最后通过"run"子命令触发实际命令。
|
||||
|
||||
This allows for much greater control for what `/execute` can do to a command, and allows splitting up the executor and the command's position.
|
||||
这种设计大幅提升了`/execute`的上下文控制能力,实现了执行者与执行位置的分离。
|
||||
|
||||
## New Syntax
|
||||
## 新语法解析
|
||||
|
||||
Now, let us review the new `/execute` syntax. They are as follows:
|
||||
现在让我们系统梳理新版`/execute`语法结构:
|
||||
|
||||
### `/execute as`
|
||||
|
||||
Changes the executor of the command, or what the target selector `@s` will select.
|
||||
更改命令执行者,即影响目标选择器`@s`的指向。
|
||||
|
||||
```
|
||||
/execute as <origin: target> -> execute
|
||||
/execute as <origin: 目标> -> execute
|
||||
```
|
||||
|
||||
This does not change the position, rotation, or dimension context of the command.
|
||||
此命令不会改变执行位置、视角或维度上下文。
|
||||
|
||||
If multiple targets are specified then a command is ran once for each target, where `@s` selects each entity in turn.
|
||||
若指定多个目标,则依次以每个目标为`@s`执行命令。
|
||||
|
||||
### `/execute at`
|
||||
|
||||
Changes where the command runs, setting the command's position, rotation, and dimension context to the entity.
|
||||
更改命令执行位置,将命令的坐标、视角和维度同步至目标实体。
|
||||
|
||||
```
|
||||
/execute at <origin: target> -> execute
|
||||
/execute at <origin: 目标> -> execute
|
||||
```
|
||||
|
||||
This does not change the executor of the command, so `@s` will remain as whoever was targeted last.
|
||||
此命令不会改变执行者身份,故`@s`仍指向最近一次指定的目标。
|
||||
|
||||
If multiple targets are specified then a command is ran once for each target, setting the position, rotation, and dimension context to each target.
|
||||
若指定多个目标,则依次在目标位置执行命令。
|
||||
|
||||
### `/execute in`
|
||||
|
||||
Sets the dimension in which the command should run.
|
||||
设置命令执行的维度环境。
|
||||
|
||||
```
|
||||
/execute in <dimension: string> -> execute
|
||||
/execute in <dimension: 字符串> -> execute
|
||||
```
|
||||
|
||||
Currently accepted values are `overworld`, `nether`, and `the_end`.
|
||||
当前可用维度标识符为`overworld`、`nether`和`the_end`。
|
||||
|
||||
This change in dimension will respect that dimension's scale; going from the Overworld to The Nether will apply a scale of x0.125 to the position, and vice versa will apply a x8 scale to the position.
|
||||
维度切换时会自动换算坐标比例(如主世界至下界应用x0.125缩放,反之则x8放大)。
|
||||
|
||||
### `/execute positioned`
|
||||
|
||||
Directly sets the position context of the command.
|
||||
直接设定命令执行坐标。
|
||||
|
||||
```
|
||||
/execute positioned <position: x y z> -> execute
|
||||
```
|
||||
|
||||
Sets the position of the command to specific values. [Relative and local coordinates](/commands/relative-coordinates) are based around the current position of the command.
|
||||
将命令位置设为指定坐标。[相对坐标与局部坐标](/wiki/commands/relative-coordinates)基于当前命令位置计算。
|
||||
|
||||
```
|
||||
/execute positioned as <origin: target> -> execute
|
||||
/execute positioned as <origin: 目标> -> execute
|
||||
```
|
||||
|
||||
Sets the position of the command to a target's location. This is similar to how `/execute at` works, but it only sets the command's position and not its rotation or dimension.
|
||||
将命令位置同步至目标坐标。功能类似`/execute at`,但仅改变位置不改变视角与维度。
|
||||
|
||||
If multiple targets are specified then a command is ran once for each target, setting the position context to the target's position.
|
||||
若指定多个目标,则依次在目标位置执行命令。
|
||||
|
||||
### `/execute align`
|
||||
|
||||
Aligns the current position of the command to the block grid.
|
||||
将当前命令位置对齐至方块网格。
|
||||
|
||||
```
|
||||
/execute align <axes: swizzle> -> execute
|
||||
/execute align <axes: 轴组合> -> execute
|
||||
```
|
||||
|
||||
Aligning a position will floor it. This subcommand accepts any non-repeating combination of the letters "x", "y", and "z", and will floor the position along each axis specified.
|
||||
对齐操作将坐标向下取整。本子命令接受由"x"、"y"、"z"组成的非重复组合,对指定轴进行取整。
|
||||
|
||||
### `/execute anchored`
|
||||
|
||||
Sets the anchor of the command to the executor's feet or eyes. Changing the anchor will affect the position where local coordinates will start at.
|
||||
设置命令的锚点位置(脚部或眼部),影响局部坐标基准。
|
||||
|
||||
```
|
||||
/execute anchored (eyes|feet) -> execute
|
||||
```
|
||||
|
||||
The default anchor when executing at a target is their feet.
|
||||
默认锚点为脚部。
|
||||
|
||||
When the anchor is set to `eyes`, the command's local position is offset by some amount corresponding to the "eye height" of the current executor.
|
||||
当锚点设为`eyes`时,命令的局部坐标会根据执行者的"眼高"进行偏移。
|
||||
|
||||
This offset should only apply to local coordinates, but it currently affects relative coordinates due to a bug: [MCPE-162681](https://bugs.mojang.com/browse/MCPE-162681).
|
||||
注意:由于漏洞[MCPE-162681](https://bugs.mojang.com/browse/MCPE-162681),当前该设置会影响相对坐标。
|
||||
|
||||
### `/execute rotated`
|
||||
|
||||
Directly sets the rotation context of the command.
|
||||
直接设定命令执行视角。
|
||||
|
||||
```
|
||||
/execute rotated <yaw: value> <pitch: value> -> execute
|
||||
/execute rotated <yaw: 值> <pitch: 值> -> execute
|
||||
```
|
||||
|
||||
Sets the rotation of the command to specific values. Relative and local coordinates are based around the current rotation of the command. This defaults to 0 for both pitch and yaw, unless the rotation was changed prior.
|
||||
设置具体视角参数。相对坐标与局部坐标基于当前视角计算,默认值为0(若未变更过视角)。
|
||||
|
||||
```
|
||||
/execute rotated as <origin: target> -> execute
|
||||
/execute rotated as <origin: 目标> -> execute
|
||||
```
|
||||
|
||||
Sets the rotation of the command to a target's rotation.
|
||||
同步目标实体的视角参数。
|
||||
|
||||
If multiple targets are specified then a command is ran once for each target, setting the rotation context to the target's rotation.
|
||||
若指定多个目标,则依次应用目标视角执行命令。
|
||||
|
||||
### `/execute facing`
|
||||
|
||||
Sets the rotation of the command to face some position. This rotation is calculated based on the current position of the command.
|
||||
设置命令视角朝向特定坐标或实体部位。
|
||||
|
||||
```
|
||||
/execute facing <position: x y z> -> execute
|
||||
```
|
||||
|
||||
Sets the rotation to face a block position. Relative and local coordinates are based around the current position of the command.
|
||||
使命令视角朝向指定方块坐标,基于当前命令位置计算。
|
||||
|
||||
```
|
||||
/execute facing entity <origin: target> (eyes|feet) -> execute
|
||||
/execute facing entity <origin: 目标> (eyes|feet) -> execute
|
||||
```
|
||||
|
||||
Sets the rotation to face a target's position. Setting the anchor to `feet` will aim the rotation to face where they are currently standing, while setting the anchor to `eyes` will aim the command up at the "eye position" of that target (see [`/execute anchored`](/commands/new-execute#execute-anchored)).
|
||||
使命令视角朝向目标实体部位。选择`feet`锚点将瞄准脚部位置,`eyes`则瞄准眼部(参考[`/execute anchored`](/wiki/commands/new-execute#execute-anchored))。
|
||||
|
||||
If multiple targets are specified then a command is ran once for each target, setting the rotation context to face that target.
|
||||
若指定多个目标,则依次应用目标朝向执行命令。
|
||||
|
||||
### `/execute (if|unless)`
|
||||
|
||||
Prevents running a command based on a condition. If the condition is true then the command will continue, or stop otherwise.
|
||||
|
||||
`/execute unless` acts as the opposite, testing if the condition is false in order to continue.
|
||||
根据条件判断是否执行命令。`if`在条件为真时继续,`unless`则相反。
|
||||
|
||||
```
|
||||
/execute if entity <target: target> -> execute
|
||||
/execute if entity <target: 目标> -> execute
|
||||
```
|
||||
|
||||
Acts like `/testfor`. Returns true if the targets exist.
|
||||
类似`/testfor`,检测目标是否存在。
|
||||
|
||||
```
|
||||
/execute if block <position: x y z> <block: string> -> execute
|
||||
/execute if block <position: x y z> <block: 字符串> -> execute
|
||||
```
|
||||
|
||||
Acts like `/testforblock`. Returns true if the block at the specified location exists.
|
||||
类似`/testforblock`,检测指定位置方块。
|
||||
|
||||
A data value or block state may additionally be specified, otherwise it ignores block states (acts as if it were set to `-1`).
|
||||
可附加数据值或方块状态参数,否则忽略状态(等效于`-1`)。
|
||||
|
||||
```
|
||||
/execute if blocks <begin: x y z> <end: x y z> <destination: x y z> (all|masked) -> execute
|
||||
```
|
||||
|
||||
Acts like `/testforblocks`. It constructs a volume between the beginning and end positions, and returns true if the volume at the destination matches the original volume.
|
||||
类似`/testforblocks`,比对源区域与目标区域的方块布局。
|
||||
|
||||
The parameter `all` tests that all blocks must match, while `masked` will ignore air blocks.
|
||||
`all`参数要求完全匹配,`masked`忽略空气方块。
|
||||
|
||||
```
|
||||
/execute if score <target: target> <objective: string> matches <range: integer range> -> execute
|
||||
/execute if score <target: 目标> <objective: 字符串> matches <range: 整数范围> -> execute
|
||||
```
|
||||
|
||||
Tests if a specified score is a certain value. This uses the integer range syntax.
|
||||
检测指定分数是否符合数值范围(使用整数区间语法)。
|
||||
|
||||
```
|
||||
/execute if score <target: target> <objective: string> (=|<|<=|>|>=) <source: target> <objective: string> -> execute
|
||||
/execute if score <target: 目标> <objective: 字符串> (=|<|<=|>|>=) <source: 目标> <objective: 字符串> -> execute
|
||||
```
|
||||
|
||||
Tests if a specified score matches some logical comparison to another score. Operators are equals (`=`), greater than (`>`), greater than or equal to (`>=`), less than (`<`), and less than or equal to (`<=`).
|
||||
比对两个分数的逻辑关系,支持等于(`=`)、大于(`>`)、大于等于(`>=`)、小于(`<`)、小于等于(`<=`)。
|
||||
|
||||
### `/execute run`
|
||||
|
||||
```
|
||||
/execute run <command: command>
|
||||
/execute run <command: 命令>
|
||||
```
|
||||
|
||||
Runs a command using all of the currently applied context modifications. This subcommand always goes last in one `/execute` command.
|
||||
应用所有上下文修改后执行指定命令。本子命令必须作为`/execute`链的末尾。
|
||||
|
||||
This subcommand is not always required however; an `/execute` command ending with an `if` or `unless` subcommand is valid too, and will return the success of the test it performed.
|
||||
注意:当`/execute`链以`if`或`unless`结尾时,可不带`run`直接返回检测结果。
|
||||
|
||||
## Examples and Upgrading Old Commands
|
||||
## 实例解析与旧指令升级
|
||||
|
||||
Since subcommands can be chained limitlessly, there really is a nearly infinite combination of arguments for an `/execute` command and they cannot all be listed. Instead, listed here are some common examples of commands.
|
||||
由于子命令可无限串联,实际组合方式近乎无限。以下列举典型应用场景:
|
||||
|
||||
The old functionality of `/execute` can be replicated with `as <target> at @s`. If you need a positional offset relative to the entity, add `positioned`. If you want to detect if a block is present, add `if block`. Here are some equivalents:
|
||||
旧版`/execute`功能可通过`as <目标> at @s`复现。如需相对位置偏移,添加`positioned`;如需方块检测,添加`if block`。示例如下:
|
||||
|
||||
```
|
||||
# Teleport with an offset
|
||||
::: code-group
|
||||
```mcfunction [旧版偏移传送]
|
||||
/execute @p ~ ~1.62 ~ teleport @s ^ ^ ^3
|
||||
|
||||
```
|
||||
```mcfunction [新版等效]
|
||||
/execute as @p at @s positioned ~ ~1.62 ~ run teleport @s ^ ^ ^3
|
||||
```
|
||||
:::
|
||||
|
||||
```
|
||||
# Chaining multiple '/execute's
|
||||
::: code-group
|
||||
```mcfunction [旧版链式检测]
|
||||
/execute @e[type=sheep] ~ ~ ~ execute @e[type=item,r=5] ~ ~ ~ detect ~ ~-1 ~ stone 0 kill @s
|
||||
|
||||
```
|
||||
```mcfunction [新版等效]
|
||||
/execute at @e[type=sheep] as @e[type=item,r=5] at @s if block ~ ~-1 ~ stone 0 run kill @s
|
||||
```
|
||||
:::
|
||||
|
||||
(Note that we do not use `as @e[type=sheep] at @s` because we do not need to execute as the sheep; only the position here is required.)
|
||||
(注意此处使用`at @e[type=sheep]`而非`as`,因为只需位置信息)
|
||||
|
||||
Now for some examples of things that were not possible to do in one command, or were more difficult to perform before the new syntax was introduced.
|
||||
新版语法还支持许多旧版难以实现的功能:
|
||||
|
||||
```
|
||||
# Testing a fake player's score
|
||||
/execute if score game_settings var matches 3.. run say [Game] Difficulty set to Hard.
|
||||
::: code-group
|
||||
```mcfunction [检测虚拟玩家分数]
|
||||
/execute if score game_settings var matches 3.. run say [游戏] 难度已设为困难。
|
||||
|
||||
# Comparing if two scores are equal
|
||||
/execute as @a if score @s x = @s y run say My X is equal to my Y.
|
||||
```mcfunction [分数比对]
|
||||
/execute as @a if score @s x = @s y run say 我的X值等于Y值。
|
||||
|
||||
# Test for an entity without targeting it
|
||||
```mcfunction [非选中实体检测]
|
||||
/execute as @a at @s if entity @e[type=armor_stand,r=10] run gamemode survival @s
|
||||
```
|
||||
:::
|
||||
Reference in New Issue
Block a user