This repository has been archived by the owner on Oct 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'documentation' of https://github.com/constellation-mc/c…
…ommander into documentation
- Loading branch information
Showing
8 changed files
with
254 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# 表达式 | ||
|
||
命令官模组凭借动态实体数据,实现了运算表达式的功能。 | ||
|
||
算术使用 [EvalEx](https://ezylang.github.io/EvalEx/) 来实现运算功能。掌握它并非必要,但多有好处。 | ||
|
||
## 额外功能 | ||
|
||
本模组在 EvalEx 的基础上,添加了一些额外功能。 | ||
|
||
- `random` 能在指定范围内生成随机数。接受 2 个值(最小,最大)。 | ||
- `clamp` 将其他两个值约束在范围内。接受 3 个值(值,最小,最大)。 | ||
- `lerp` 平滑地将初值过渡到末值。`初值 + 平滑系数 * (末值 - 初值)`。接受 3 个值(平滑系数,初值,末值)。 | ||
|
||
## 读取情境数据 | ||
|
||
因为算术以游戏的战利品功能为情境,你可以通过一种叫"提取"的特殊句法来读取源数据。 | ||
``` | ||
标识符.字段 | ||
标识符.方法 | ||
标识符.方法.字段.方法 | ||
``` | ||
注意,你的用到的字段与情境**必须**存在,不然表达式是无效的。 | ||
|
||
示例: | ||
``` | ||
minecraft:this_entity.getX | ||
this_entity.getHealth | ||
minecraft:this_entity.isInWaterOrRain | ||
this_entity.blockPosition.getX | ||
origin.x | ||
origin.reverse.y | ||
minecraft:level.getDayTime | ||
``` | ||
|
||
表达式使用的是 Mojang 的映射(首次载入时下载),这意味着几乎所有公共的字段和 get 类型的方法都可以在表达式里使用。如果本模组无法设置映射,你可能需要依赖于平台的名称(比如 Fabric 的中间映射)。 | ||
|
||
## 使用表达式 | ||
|
||
通过使用 `cmd:arithmetica` 命令,你可以快速上手。记得用 `"` 括住表达式来满足格式要求。 | ||
|
||
其他使用情境见[命令宏](Commands#command-macros),`commander:arithmetica` 的数字提供器以及 `commander:expression` 接口。 | ||
|
||
使用接口的例子: | ||
|
||
```json | ||
{ | ||
"condition": "commander:expression", | ||
"value": "level.isDay" | ||
} | ||
``` | ||
|
||
在条件下使用接口的例子: | ||
|
||
```json | ||
{ | ||
"condition": "minecraft:value_check", | ||
"value": { | ||
"type": "commander:arithmetica", | ||
"value": "round(random(5, 15))" | ||
}, | ||
"range": { | ||
"min": 12.3, | ||
"max": 32 | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# 命令 | ||
|
||
大多情况下,你应该使用 `/` 类型的命令,但有时,我们会需要一些额外的发挥空间。 | ||
|
||
## 创建命令 | ||
|
||
创建新的命令很简单,你要做的是:实现 `Command` 接口,通过创建一个[编码器](https://forge.gemwire.uk/wiki/Codecs)来序列化或反序列化命令,然后用 `CommandTypes.register()` 来注册编码器。 | ||
|
||
让我们创建一个能够将字符串转化为标准输出的命令。 | ||
|
||
```java | ||
public record DummyCommand(String text) implements Command { | ||
|
||
@Override | ||
public boolean execute(EventContext context) { | ||
System.out.println(text()); | ||
return true; //执行成功则返回 | ||
} | ||
|
||
@Override | ||
public CommandType type() { | ||
return null; | ||
} | ||
} | ||
``` | ||
|
||
接下来,我们再为这个命令[编码器](https://forge.gemwire.uk/wiki/Codecs)创建一个编码器。 | ||
|
||
```java | ||
public static final Codec<DummyCommand> CODEC = Codec.STRING.fieldOf("text").xmap(DummyCommand::new, DummyCommand::text).codec(); | ||
``` | ||
|
||
再然后,我们需要注册这一命令来获取 `CommandType`。 | ||
|
||
```java | ||
public static final CommandType DUMMY = CommandType.register(new Identifier("modid", "print"), DummyCommand.CODEC); | ||
``` | ||
|
||
最后,在 `type()` 返回这个类型。 | ||
|
||
```java | ||
@Override | ||
public CommandType type() { | ||
return MyModInit.DUMMY; | ||
} | ||
``` | ||
|
||
## 事件情境 | ||
|
||
你可以通过事件情境,检索与事件类型一起传递的 `LootContext`。 | ||
|
||
```java | ||
context.lootContext().getWorld(); //返回服务端世界 | ||
|
||
context.lootContext().get(LootContextParameters.TOOL); //如果不存在,返回参数或空值。 | ||
|
||
context.lootContext().requireParameter(LootContextParameters.TOOL); //如果不存在,返回参数或抛出异常。 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# 事件 | ||
|
||
本模组引入了全新的数据包事件系统,使数据包能够像模组一样监听事件。 | ||
|
||
最好的实践办法就是找到一个 Fabric 的事件,让本模组对它进行监听。 | ||
|
||
## 创建事件类型 | ||
|
||
为了实现模组的事件支持,你需要先注册一个事件类型,以便本模组进行分发。你可以通过 `EventType.Builder` 类来构建并注册事件。 | ||
|
||
```java | ||
public static final EventType CUSTOM_EVENT = EventType.builder().build(new Identifier("modid", "custom_event")); | ||
``` | ||
|
||
如果你的事件需要返回类型,你可以通过 `cancelTerm()` 来指定取消条件编解码器。 | ||
|
||
```java | ||
EventType.builder() | ||
.cancelTerm(Codec.INT) | ||
.build(new Identifier("modid", "custom_event")); | ||
``` | ||
|
||
通过使用 `extension()`,事件可以接受额外的参数。在指定扩展后,你可以返回自定义类型。 | ||
|
||
```java | ||
EventType.builder() | ||
.extension(Codec.STRING, subscriptions -> { | ||
//处理数据 | ||
return /*返回监听者*/; | ||
}) | ||
.build(new Identifier("modid", "custom_event")); | ||
``` | ||
|
||
默认的返回类型是 `List<Command.Conditioned>`。 | ||
|
||
## 调用事件 | ||
|
||
如果你没有指定扩展,或选择返回 `List<Command.Conditioned>`,你可以使用附带的 `EventExecutors` 辅助单元。 | ||
|
||
为了使用这一辅助单元,你需要传递:事件类型、执行的世界,以及战利品情境提供者。 | ||
|
||
```java | ||
CustomEvent.EVENT.register((world, entity) -> runVoid(CUSTOM_EVENT, world, () -> makeContext(world, entity, entity.getPos()))); | ||
``` | ||
|
||
```java | ||
private static LootContext makeContext(ServerWorld world, Entity entity, Vec3d origin) { | ||
LootContextParameterSet.Builder builder = new LootContextParameterSet.Builder(world); | ||
builder.add(THIS_ENTITY, entity).add(ORIGIN, origin); | ||
return new LootContext.Builder(builder.build(LootContextTypes.COMMAND)).build(null /*在 1.20.4,.empty()可选*/); | ||
} | ||
``` | ||
|
||
如果你指定了扩展,或者使用了不受支持的返回类型,就需要编写自定义的解析逻辑。 | ||
|
||
为了编写这一逻辑,你需要创建 `EventContext`。`EventContext` 用于把执行参数传递给命令。 | ||
|
||
```java | ||
EventContext context = EventContext.builder(type) | ||
.addParameter(EventKey.LOOT_CONTEXT, /*战利品情境的实例*/) | ||
.build(); | ||
for (Command.Conditioned subscriber : subscribers) subscriber.execute(context); | ||
``` | ||
你可以通过调用 `getReturnValue(def)` 来获取返回的值,默认值可以为空值。返回的类型是 generic。 | ||
|
||
```java | ||
boolean val = context.getReturnValue(def); | ||
if (val != def) return val; | ||
``` |
Oops, something went wrong.