完整版BedrockWiki镜像!
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
---
|
||||
title: Run Commands with Equipped Items
|
||||
category: Tutorials
|
||||
title: 通过装备物品运行命令
|
||||
category: 巧思案例
|
||||
tags:
|
||||
- experimental
|
||||
- intermediate
|
||||
- 实验性
|
||||
- 中级
|
||||
mentions:
|
||||
- Chikorita-Lover
|
||||
- MedicalJewel105
|
||||
@@ -11,23 +11,26 @@ mentions:
|
||||
- TheItsNameless
|
||||
---
|
||||
|
||||
## Introduction
|
||||
# 通过装备物品运行命令
|
||||
|
||||
A common concept for add-ons is implementing new armor sets with unique effects, just like the turtle shell and netherite armor. While items have a knockback resistance component, they don't have a component for inflicting mob effects, emitting particles, etc. under certain conditions. However, using server animations, Molang and item tags, this can easily be done!
|
||||
<!--@include: @/wiki/bedrock-wiki-mirror.md-->
|
||||
|
||||
Keep in mind that this requires modifying the player behavior, which is a common theme for many add-ons; thus, your add-on may not be compatible with others if you wish to do this.
|
||||
## 简介
|
||||
|
||||
> However some people found a way not to use player.json. They replace it with dummy entity-rider. Try experimenting yourself!
|
||||
附加组件开发中常见的需求是为新盔甲套装添加独特效果,就像海龟壳和下界合金盔甲那样。虽然物品本身具有击退抗性组件,但它们并没有在特定条件下施加生物效果、生成粒子等功能的原生组件。不过通过服务器动画、Molang查询和物品标签,我们可以轻松实现这些效果!
|
||||
|
||||
The use of Holiday Creator Features is also required to add item tags and easily equip our item in armor or off-hand slots.
|
||||
请注意,此方法需要修改玩家行为文件,这是许多附加组件的常见操作。因此如果使用此方法,你的附加包可能会与其他修改玩家行为的附加包产生兼容性问题。
|
||||
|
||||
## Server Animation
|
||||
> 有开发者发现可以通过实体骑乘机制替代直接修改玩家行为文件的方法。建议自行实验探索!
|
||||
|
||||
The first step will be to create a server animation, which is a file that runs commands or events at certain keyframes. While client animations are in the resource pack, server animations are in the behavior pack. You can read a bit more [here](/entities/timers#animation-based-timers). We can start by using the following as a template:
|
||||
本教程需要使用假日创作者功能来添加物品标签,并方便地在盔甲栏或副手槽位装备物品。
|
||||
|
||||
<CodeHeader>BP/animations/player.json</CodeHeader>
|
||||
## 服务器动画
|
||||
|
||||
```json
|
||||
第一步是创建服务器动画文件,这种文件可以在特定关键帧执行命令或触发事件。客户端动画存放在资源包中,而服务器动画则位于行为包内。更多信息可参阅[基于动画的计时器](/wiki/entities/timers#animation-based-timers)。以下模板可供参考:
|
||||
|
||||
::: code-group
|
||||
```json [BP/animations/player.json]
|
||||
{
|
||||
"format_version": "1.10.0",
|
||||
"animations": {
|
||||
@@ -41,58 +44,54 @@ The first step will be to create a server animation, which is a file that runs c
|
||||
}
|
||||
}
|
||||
```
|
||||
:::
|
||||
|
||||
Let's go over what's in this template and what everything does:
|
||||
模板参数解析:
|
||||
|
||||
- `animation.player.emerald_armor` is our animation's identifier; you can change this to something else, such as `animation.player.phantom_armor`.
|
||||
- `timeline` runs commands and events at given keyframes.
|
||||
- `animation_length` is how long the animation lasts; we'll use 0.05 seconds, as that's the length of an in-game tick.
|
||||
- `loop` is quite straight-forward; setting it to true makes the animation loop.
|
||||
- `animation.player.emerald_armor` 是动画标识符,可自定义如`animation.player.phantom_armor`
|
||||
- `timeline` 用于在指定时间点执行命令/事件
|
||||
- `animation_length` 设置动画时长(0.05秒即1游戏刻)
|
||||
- `loop` 控制是否循环播放
|
||||
|
||||
We can add commands to the `0.0` array in our timeline to execute, such as an `/effect` command, like such:
|
||||
在时间轴中添加命令示例:
|
||||
|
||||
<CodeHeader>BP/animations/player.json#timeline</CodeHeader>
|
||||
|
||||
```json
|
||||
::: code-group
|
||||
```json [BP/animations/player.json#timeline]
|
||||
{
|
||||
"0.0": [
|
||||
"/effect @s speed 1 0"
|
||||
]
|
||||
}
|
||||
```
|
||||
:::
|
||||
|
||||
We're not limited to `/effect`, of course. If you want to use some other command, such as `/function` or `/particle`, go right ahead!
|
||||
除了`/effect`,也可以使用`/function`、`/particle`等其他命令。完成服务器动画配置后,接下来需要设置物品行为。
|
||||
|
||||
After this, we're finished in our server animation, and we'll head into the behavior file for our item for a quick addition.
|
||||
## 物品行为
|
||||
|
||||
## Item Behavior
|
||||
为了检测装备状态,我们需要使用Molang查询配合物品标签。以下情况可跳过本节:
|
||||
|
||||
To actually check if our item is equipped, we can use a Molang query that checks for item tags.
|
||||
- 检测原版物品(如通过`minecraft:iron_tier`标签检测铁质盔甲)
|
||||
- 使用`q.is_item_name_any`通过物品ID检测
|
||||
|
||||
You can skip this section if:
|
||||
添加标签组件示例:
|
||||
|
||||
- You want check for a vanilla item instead, such as an iron armor piece through the `minecraft:iron_tier` tag
|
||||
- You want to check for the item via `q.is_item_name_any`, which checks for an item identifier in any slot
|
||||
|
||||
In our item's behavior, we'll have to add a tag to `components`. For example, if we wanted to add the `example:emerald_tier` tag, we would add the `tag:example:emerald_tier` component:
|
||||
|
||||
<CodeHeader>BP/items/my_item.json#components</CodeHeader>
|
||||
|
||||
```json
|
||||
::: code-group
|
||||
```json [BP/items/my_item.json#components]
|
||||
"tag:example:emerald_tier": {}
|
||||
```
|
||||
:::
|
||||
|
||||
That's it, now your item has whatever tag you assigned it! You can add more tags if you want, but this is all we need for what we're doing.
|
||||
至此物品已拥有指定标签,可根据需要添加多个标签。
|
||||
|
||||
## Player Behavior
|
||||
## 玩家行为
|
||||
|
||||
Finally, we need to modify the player's behavior to run the server animation. We'll be working entirely within `description`.
|
||||
最后需要修改玩家行为文件来触发动画。主要操作在`description`部分完成。
|
||||
|
||||
First, we need to set a short name for our animation. If you have any experience with client animations, this process will be quite similar. Add `animations` to `description`, and set a short name, like such:
|
||||
首先为动画设置简称:
|
||||
|
||||
<CodeHeader>BP/entities/player.json#description</CodeHeader>
|
||||
|
||||
```json
|
||||
::: code-group
|
||||
```json [BP/entities/player.json#description]
|
||||
{
|
||||
"identifier": "minecraft:player",
|
||||
"is_spawnable": false,
|
||||
@@ -103,31 +102,31 @@ First, we need to set a short name for our animation. If you have any experience
|
||||
}
|
||||
}
|
||||
```
|
||||
:::
|
||||
|
||||
Now with a short name set, we can run our animation.
|
||||
接着在`scripts`中添加Molang条件检测:
|
||||
|
||||
Add `scripts` to `description`, and set a Molang query to run. To check for the item, we can use one of the following:
|
||||
检测方式选择:
|
||||
|
||||
- `q.is_item_name_any`, to check for a given item identifier in any slot. This example will check for `example:totem_of_retreat` in either hand:
|
||||
- `q.is_item_name_any` 检测指定ID物品(示例检测双手是否持有图腾):
|
||||
```
|
||||
q.is_item_name_any('slot.weapon.mainhand',0,'example:totem_of_retreat') || q.is_item_name_any('slot.weapon.offhand',0,'example:totem_of_retreat')
|
||||
```
|
||||
|
||||
- `q.equipped_item_any_tag`, to check for at least one of any given tag in a given slot. This example will allow an emerald- or phantom- tier armor piece to be used:
|
||||
- `q.equipped_item_any_tag` 检测单标签存在(示例检测头部护甲标签):
|
||||
```
|
||||
q.equipped_item_any_tag('slot.armor.head','example:emerald_tier','example:phantom_tier')
|
||||
```
|
||||
|
||||
- `q.equipped_item_all_tags`, to check for all given tags in a given slot. This example will only allow an armor piece that's both emerald- and ancient- tier:
|
||||
- `q.equipped_item_all_tags` 检测多标签共存:
|
||||
```
|
||||
q.equipped_item_all_tags('slot.armor.head','example:ancient_tier','example:emerald_tier')
|
||||
```
|
||||
|
||||
Let's take a look at an example using `q.equipped_item_any_tag`:
|
||||
应用示例:
|
||||
|
||||
<CodeHeader>BP/entities/player.json#description</CodeHeader>
|
||||
|
||||
```json
|
||||
::: code-group
|
||||
```json [BP/entities/player.json#description]
|
||||
{
|
||||
"identifier": "minecraft:player",
|
||||
"is_spawnable": false,
|
||||
@@ -145,54 +144,52 @@ Let's take a look at an example using `q.equipped_item_any_tag`:
|
||||
}
|
||||
}
|
||||
```
|
||||
:::
|
||||
|
||||
This example will run a server animation with the `emerald_armor` short name if an emerald-tier item is equipped in the helmet slot. You can change the Molang field to match your item tag, use a different query, or add additional queries.
|
||||
此配置会在玩家头部装备翡翠标签物品时触发动画。更多装备槽位标识符请参考[Minecraft Wiki](https://minecraft.wiki/w/Slot#Bedrock_Edition)。
|
||||
|
||||
You can view a list of additional slot identifiers at the [Minecraft Wiki](https://minecraft.wiki/w/Slot#Bedrock_Edition).
|
||||
## 总结
|
||||
|
||||
## Conclusion
|
||||
完成服务器动画、玩家行为和物品标签的配置后,装备特定物品即可执行自定义命令!此技术突破了物品组件的限制,为物品定制提供了更多可能性。如需扩展功能,请参考以下附加内容。
|
||||
|
||||
With the server animation, player behavior, and item tag all set up, your equipped item can now run commands! This technique allows for greater item customization than being restricted to item components. If you want to add more to the effect or add-on, check the next section; otherwise, congratulations, you're finished!
|
||||
## 扩展应用
|
||||
|
||||
## Additions
|
||||
### 多件套装检测
|
||||
|
||||
### Multiple Required Items
|
||||
检测全套装备示例:
|
||||
|
||||
If you want to run a command when multiple of the armor set's pieces are equipped, we can expand our Molang from before:
|
||||
|
||||
<CodeHeader>BP/entities/player.json#scripts</CodeHeader>
|
||||
|
||||
```json
|
||||
::: code-group
|
||||
```json [BP/entities/player.json#scripts]
|
||||
"animate": [
|
||||
{
|
||||
"emerald_armor": "q.equipped_item_any_tag('slot.armor.head','example:emerald_tier') && q.equipped_item_any_tag('slot.armor.chest','example:emerald_tier') && q.equipped_item_any_tag('slot.armor.legs','example:emerald_tier') && q.equipped_item_any_tag('slot.armor.feet','example:emerald_tier')"
|
||||
}
|
||||
]
|
||||
```
|
||||
:::
|
||||
|
||||
This example will check for emerald-tier armor in all four armor slots, and run the animation if they're all equipped.
|
||||
当四件护甲都具备指定标签时触发效果。
|
||||
|
||||
### Further Conditions
|
||||
### 复合条件判断
|
||||
|
||||
The turtle shell doesn't always inflict Water Breathing, but instead only for 10 seconds when a player first enters water. If we want our emerald armor to only run our animation when we have lower health, we can add another query to our Molang:
|
||||
仿照海龟壳的水下呼吸机制,添加血量条件:
|
||||
|
||||
<CodeHeader>BP/entities/player.json#scripts</CodeHeader>
|
||||
|
||||
```json
|
||||
::: code-group
|
||||
```json [BP/entities/player.json#scripts]
|
||||
"animate": [
|
||||
{
|
||||
"emerald_armor": "q.equipped_item_any_tag('slot.armor.head','example:emerald_tier') && q.health <= 5"
|
||||
}
|
||||
]
|
||||
```
|
||||
:::
|
||||
|
||||
This example will run the animation with 2.5 hearts or less remaining, allowing players to make a quick getaway when they're in danger.
|
||||
当玩家生命值≤2.5心时触发逃生效果。
|
||||
|
||||
We can also apply this to requiring multiple armor pieces, with even longer Molang:
|
||||
复合条件示例:
|
||||
|
||||
<CodeHeader>BP/entities/player.json#scripts</CodeHeader>
|
||||
|
||||
```json
|
||||
::: code-group
|
||||
```json [BP/entities/player.json#scripts]
|
||||
{
|
||||
"animate": [
|
||||
{
|
||||
@@ -201,16 +198,16 @@ We can also apply this to requiring multiple armor pieces, with even longer Mola
|
||||
]
|
||||
}
|
||||
```
|
||||
:::
|
||||
|
||||
You can view a list of documented Molang queries at [bedrock.dev](https://bedrock.dev/docs/stable/Molang#List%20of%20Entity%20Queries).
|
||||
完整Molang查询列表详见[bedrock.dev](https://bedrock.dev/docs/stable/Molang#List%20of%20Entity%20Queries)。
|
||||
|
||||
### Multiple Items with Effects
|
||||
### 多物品系统
|
||||
|
||||
If you want to add more items with unique effects, fret not; this is easily done. You can either create a new server animation file, or add on to the file from before, like such:
|
||||
添加新物品效果时,可扩展动画文件:
|
||||
|
||||
<CodeHeader>BP/animations/player.json</CodeHeader>
|
||||
|
||||
```json
|
||||
::: code-group
|
||||
```json [BP/animations/player.json]
|
||||
{
|
||||
"format_version": "1.10.0",
|
||||
"animations": {
|
||||
@@ -231,12 +228,12 @@ If you want to add more items with unique effects, fret not; this is easily done
|
||||
}
|
||||
}
|
||||
```
|
||||
:::
|
||||
|
||||
In our player behavior, you'll have to add on to `animations` and `scripts` as well.
|
||||
同步更新玩家行为文件:
|
||||
|
||||
<CodeHeader>BP/entities/player.json#description</CodeHeader>
|
||||
|
||||
```json
|
||||
::: code-group
|
||||
```json [BP/entities/player.json#description]
|
||||
{
|
||||
"identifier": "minecraft:player",
|
||||
"is_spawnable": false,
|
||||
@@ -258,3 +255,4 @@ In our player behavior, you'll have to add on to `animations` and `scripts` as w
|
||||
}
|
||||
}
|
||||
```
|
||||
:::
|
||||
Reference in New Issue
Block a user