附加包教程:9.物品(四)
📖 前言
上期,我们学习了我已知的所有物品事件方法。也就是说,我们已经完成了物品相关的知识储备。
这一期,我们要进行一次实战演练!
🎯 演练目标
根据演练目标,使用对应的组件和事件,完成一个自定义武器。
::: info 目标
- 命名空间 ID:
test_pack:test_sword
- 伤害为 26
- 创造模式下 不能破坏方块
- 附魔闪光(即使没有附魔)
- 附魔能力为 70
- 耐久为 5000
- 耐久损耗概率 50%
- 潜行攻击实体:范围 5 格伤害 + 掉 5 点耐久
- 潜行打方块:1/4 几率清除所有状态,添加力量 5 和急迫 5(持续 30 秒)+ 掉 5 点耐久
- 可副手持有
- 不会刷新掉
:::
🧠 思路分析
让我们按照顺序,逐步实现以上所有功能。
🏗️ 基础设置
既然是一把剑,那么在 components
中先添加标签:
"tag:minecraft:sword"
添加基础组件
"minecraft:damage": 26,
"minecraft:can_destroy_in_creative": false,
"minecraft:glint": true
添加附魔能力
"minecraft:enchantable": {
"value": 70,
"slot": "sword"
}
添加耐久系统
"minecraft:durability": {
"damage_chance": 0.5,
"max_durability": 5000
}
⚙️ 通用组件
"minecraft:should_despawn": false,
"minecraft:allow_off_hand": true
🧱 特殊破坏功能
::: tip 小技巧
作为一把剑,应该快速破坏蜘蛛网和竹子。这部分我们还要加上破坏时触发的事件。
:::
添加相关组件和事件
"minecraft:hand_equipped": true,
"minecraft:max_stack_size": 1,
"minecraft:digger": {
"use_efficiency": false,
"destroy_speeds": [
{
"block": "minecraft:web",
"speed": 30,
"on_dig": {
"event": "damage"
}
},
{
"block": "minecraft:bamboo",
"speed": 30,
"on_dig": {
"event": "damage"
}
}
]
}
"damage": {
"damage": {
"type": "durability",
"amount": 1,
"target": "self"
}
}
🛠️ 创造栏分类
"minecraft:creative_category": {
"parent": "itemGroup.name.sword"
}
🧨 潜行攻击逻辑(重点)
::: danger 注意minecraft:weapon
组件在 1.20.40.21
版本中已被移除,若你使用的是此版本或更高版本,请使用脚本实现类似逻辑。
:::
添加组件
"minecraft:weapon": {
"on_hit_block": "destroy",
"on_hurt_entity": "hurt",
"on_not_hurt_entity": "damage"
}
事件定义(点击查看)
📜 事件结构:使用 sequence 和 randomize
"hurt": {
"sequence": [
{
"damage": {
"type": "durability",
"amount": 5,
"target": "self"
}
},
{
"condition": "q.is_sneaking",
"run_command": {
"command": "damage @e[r=5,rm=0.1] 26 entity_attack entity @p"
}
}
]
},
"destroy": {
"randomize": [
{
"weight": 3,
"sequence": [
{
"run_command": {
"command": "effect @p clear"
}
},
{
"run_command": {
"command": [
"effect @p strength 30 4",
"effect @p haste 30 4"
]
}
},
{
"damage": {
"type": "durability",
"amount": 5,
"target": "self"
}
}
]
},
{
"weight": 1
}
]
}
✅ 总结
这一期我们打造了一把拥有多个特殊功能的自定义剑,学习了大量组件的组合使用和事件系统的写法。在下期教程中,我们将继续深入学习更多复杂的组件使用方法。