快乐小方
本帖最后由 快乐小方 于 2022-6-10 14:29 编辑

目录
  • 从零开始制作一个矿石作物 Addon
    • 你将学到…
    • 一切开始前…
    • 准备就绪
      • 开始的开始
      • 清单文件 manifest.json
        • header
        • modules
        • dependencies
      • 图标文件 pack_icon.png
    • 第一个物品
      • 添加物品
      • 定义纹理与物品名称
        • 定义纹理
        • 物品名称
      • 物品组件
        • minecraft:creative_category
        • minecraft:display_name
        • minecraft:icon
        • minecraft:max_stack_size
    • 第一个方块
      • 添加方块
      • 定义纹理
      • 方块组件
        • minecraft:collision_box
        • minecraft:geometry
        • minecraft:placement_filter
    • 种子、作物
      • 种子
      • 作物
        • 不同生长状态
        • 生长
    • 收获
      • 战利品表
      • 收获事件
    • 合成种子
    • 向世界推广你的 Addon

    • 参考与引用
    • 致谢

从零开始制作一个矿石作物 Addon

本篇教程并不是完整的附加包开发教程,仅包括了与完成“矿石作物”这个目标相关的内容,学习更多内容请访问官方文档

本文中提到的一些内容在你阅读的时候可能已经过时,请以官方文档为准

本文若有错误,欢迎在评论区指出,不胜感激

你将学到...

  • 自定义物品
  • 自定义方块
  • 自定义合成配方
  • 自定义战利品表
  • 本地化
  • 一点点 Molang

一切开始前...

  • 准备需要使用到的纹理(图标、种子的纹理、作物方块不同生长阶段的纹理等)
  • 准备一个文本编辑器
  • 安装 Blockbench,你可以在这里下载
  • 别忘了最重要的 —— Minecraft 基岩版

准备就绪

开始的开始

你可以按照下面的目录树创建这些在后续需要用到的文件夹

BP是行为包(Behavior Pack)的缩写,\BP 目录则是我们的行为包部分

RP是资源包(Resource Pack)的缩写,\RP 目录则是我们的资源包部分

  1. HelloWorld
  2.   ├─BP
  3.   │  ├─blocks
  4.   │  ├─items
  5.   │  ├─loot_tables
  6.   │  └─recipes
  7.   └─RP
  8.      ├─models
  9.      │  └─entity
  10.      ├─texts
  11.      └─textures
  12.         ├─blocks
  13.         └─items
复制代码

清单文件 manifest.json

首先我们需要在 \BP\RP 目录下分别创建一个名叫 manifest.json 文件

  1. HelloWorld
  2.   ├─BP
  3.   │  ├─...
  4.   │  └─manifest.json
  5.   └─RP
  6.      ├─...
  7.      └─manifest.json
复制代码

manifest.json 的基本构成

名称描述
format_version告诉 Minecraft 需要用什么版本来解析这个 Json
header清单的头部信息
modules此附加包所拥有的模块
dependencies此附加包的依赖项

header

header 所需要包含的头部信息:

名称数据类型描述
name字符串此附加包的名称
version数组 [a, b, c]此附加包的版本号
min_engine_version数组 [a, b, c]此附加包所兼容的 Minecraft 的最低版本
uuid字符串此附加包的标识

我们直接把包名设置为 Ore Crops 好了,后面加上 BP、RP 加以区分(事实上不这么做也可以)

描述中我们可以写上作者信息之类的东西

版本号设置成 0.0.1 <sup>[注意:若要将附加包上传至基岩版市场,a 值需要大于  0]</sup>

uuid 将作为区分附加包的标识,我们需要设置一个独一无二的,你可以在这个网站在线生成 uuid

modules

modules 所需要包含的内容:

名称数据类型描述
type字符串模块的类型
description字符串模块的描述
version数组 [a, b, c]模块的版本
uuid字符串模块的标识

我们需要分别为 \BP\manifest.json\RP\manifest.json 添加行为包模块(data)与资源包模块(resources

dependencies

dependencies所需要包含的内容:

名称数据类型描述
uuid字符串所依赖的附加包的 uuid
version数组 [a, b, c]所依赖的附加包的版本

为了将行为包与资源包关联起来,我们需要将他们添加到对方的 dependencies

现在,我们的清单文件应该是这样的:

\BP\manifest.json :

  1. {
  2.     "format_version":2,
  3.     "header":{
  4.         "name":"Ore Crops (BP)",
  5.         "description":"Ore Crops behavior pack",
  6.         "uuid":"c5960d44-cfa8-457d-83e4-ccc56fbc22e4",
  7.         "version":[0,0,1],
  8.         "min_engine_version":[1,18,10]
  9.     },
  10.     "modules":[
  11.         {
  12.             "type":"data",
  13.             "description":"Ore Crops behavior pack",
  14.             "uuid":"713b3a86-1a48-c1b2-26f8-4416db5b876b",
  15.             "version":[0,0,1]
  16.         }
  17.     ],
  18.     "dependencies":[
  19.         {
  20.             "uuid":"330199b2-ea33-da59-8cb9-c412055d23e9",
  21.             "version":[0,0,1]
  22.         }
  23.     ]
  24. }
复制代码

\BP\manifest.json :

  1. {
  2.     "format_version":2,
  3.     "header":{
  4.         "name":"Ore Crops (RP)",
  5.         "description":"Ore Crops resource pack",
  6.         "uuid":"330199b2-ea33-da59-8cb9-c412055d23e9",
  7.         "version":[0,0,1],
  8.         "min_engine_version":[1,18,10]
  9.     },
  10.     "modules":[
  11.         {
  12.             "type":"resources",
  13.             "description":"Ore Crops resource pack",
  14.             "uuid":"0e262f01-38dc-b893-0902-cb0140885a43",
  15.             "version":[0,0,1]
  16.         }
  17.     ],
  18.     "dependencies":[
  19.         {
  20.             "uuid":"c5960d44-cfa8-457d-83e4-ccc56fbc22e4",
  21.             "version":[0,0,1]
  22.         }
  23.     ]
  24. }
复制代码

图标文件 pack_icon.png

别忘了设置一个图标!

\BP\RP 目录下放置 pack_icon.png 即可

完成了这些,我们可以正式开始开发工作了 ——

第一个物品

添加物品

首先你需要在 \BP\items 目录下创建一个 .json 文件,文件名随意,这里使用 coal_seed.json 作为“煤炭种子”的物品 json 文件。

物品 json 文件的基本格式如下

  1. {
  2.     "format_version":"1.18.0",  // 游戏版本号
  3.     "minecraft:item":{
  4.         "description":{
  5.             // 物品的基本信息
  6.         },
  7.         "components":{
  8.             // 物品的组件
  9.         },
  10.         "events":{
  11.             // 物品的事件
  12.         }
  13.     }
  14. }
复制代码

description 的基本结构如下:

名称数据类型描述
identifier字符串物品的命名空间与物品名构成的标识
category字符串物品在创造模式物品栏的分类
is_experimental布尔值物品是否为实验性物品

首先是 identifier,命名空间一般为 addon 的名称,这里“煤炭种子”可以设置为 orecrops:coal_seed

category 的可选值有:equipment(装备), items(物品), natural(自然), block(建筑), commands(不可通过命令获取)。这里我们选择 natural

is_experimental将决定物品是否需开启实验性玩法才可获得,默认值为 false

现在,你的 \BP\items\coal_seed.json 应该是这样子的:

  1. {
  2.     "format_version":"1.18.0",
  3.     "minecraft:item":{
  4.         "description":{
  5.             "identifier":"orecrops:coal_seed",
  6.             "category":"natural"
  7.         }
  8.     }
  9. }
复制代码

至此,你已经成功添加了你的第一个物品!

但是它还缺少纹理、名字等等一些必要属性,下面我们为它定义一个纹理

定义纹理与物品名称

定义纹理

转到 \RP\textures\items 目录,在这里放置你的物品纹理

回到 \RP\textures 目录,在这里新建一个名为 item_texture.json 的文件,在里面输入下面的内容来定义物品纹理

  1. {
  2.     "resource_pack_name":"Ore Crops resource pack",
  3.     "texture_name":"atlas.items",
  4.     "texture_data":{
  5.         "coal_seed":{
  6.             "textures":"textures/items/fang/coal_seed"
  7.         }
  8.     }
  9. }
复制代码

名称描述
resource_pack_name资源包的名称,可填vanilla或其他名称<sup>[存疑]</sup>。
texture_name请填为 atlas.item <sup>[存疑]</sup>
texture_data纹理资源 "纹理名称":{"textures":"纹理路径"}

物品名称

我们在 \RP\texts 目录下新建一个 zh_CN.lang, 在里面输入:

  1. item.orecrops:coal_seed.name=煤炭种子
复制代码

我们在 zh_CN.lang 中输入的这一行“等式”定义了在简体中文语言下 item.orecrops:coal_seed.name 键所对应的文本,以这种方式设置物品名称将方便我们后续进行本地化,与本地化相关的内容我们会在最后学习

到这里你可能会很疑惑,为什么我的物品还是没有纹理?

我们成功添加了一个物品并定义了他的纹理,下面我们将使用组件赋予它名字、纹理与一些作为物品该有的基本属性

物品组件

我们将使用到下表中的组件,更多组件请查阅文档

组件名称描述
minecraft:creative_category注册物品到创造模式物品栏的指定分类、分组
minecraft:display_name设置物品显示的名称(可使用本地化键名)
minecraft:icon设置物品显示的纹理
minecraft:max_stack_size设置物品的最大堆叠数量

minecraft:creative_category

  1. "minecraft:creative_category": {
  2.     "parent": "分组"
  3. }
复制代码

分组大全

我们使用这个组件将“煤炭种子”归入“种子”分组

minecraft:display_name

  1. "minecraft:display_name": {
  2.     "name":"名称或本地化键名"
  3. }
复制代码

在上一节我们定义了“煤炭种子”的本地化键名(item.orecrops:coal_seed.name),直接填进去即可

minecraft:icon

  1. "minecraft:icon": {
  2.     "texture":"纹理名称"
  3. }
复制代码

在上一节我们定义了“煤炭种子”的纹理名称(coal_seed),直接填进去即可

minecraft:max_stack_size

  1. "minecraft:max_stack_size": 堆叠上限
复制代码

堆叠上限是一个大于等于 1 小于等于 64 的整数型值

我们设置为 64

现在,我们的 coal_seed.json 应该是这样的:

  1. {
  2.     "format_version":"1.18.0",
  3.     "minecraft:item":{
  4.         "description":{
  5.             "identifier":"orecrops:coal_seed",
  6.             "category":"nature"
  7.         },
  8.         "components":{
  9.             "minecraft:display_name":{
  10.                 "value":"item.orecrops:coal_seed.name"
  11.             },
  12.             "minecraft:creative_category":{
  13.                 "parent":"itemGroup.name.seed"
  14.             },
  15.             "minecraft:max_stack_size":64,
  16.             "minecraft:icon":{
  17.                 "texture":"coal_seed"
  18.             }
  19.         }
  20.     }
  21. }
复制代码

现在,你的第一个物品已经有了名字、纹理,下面我们来完成你的第一个方块

第一个方块

添加方块

\BP\blocks 目录创建一个名为 coal_crops.json 的文件作为“煤炭作物”的作物方块

方块 json 文件的基本格式如下

  1. {
  2.     "format_version": "1.18.0",  // 游戏版本
  3.     "minecraft:block": {
  4.         "description": {
  5.             // 描述
  6.         },
  7.         "components": {
  8.             // 组件
  9.         },
  10.         "events": {
  11.             // 事件
  12.         },
  13.         "permutations": [
  14.             //置换
  15.         ]
  16.     }
  17. }
复制代码

方块的描述部分与物品稍有不同:

名称数据类型描述
identifier字符串方块的命名空间与物品名构成的标识
properties对象方块的属性
is_experimental布尔值方块是否为实验性方块

identifier 我们设置为 orecrops:coal_crop

properties 在下一章将具体学习

is_experimental 与物品相同,默认值为 false

现在,你的\BP\blocks\coal_crop.json 应该是这样的:

  1. {
  2.     "format_version":"1.18.0",
  3.     "minecraft:block":{
  4.         "description":{
  5.             "identifier":"orecrops:coal_crop"
  6.         }
  7.     }
  8. }
复制代码

定义纹理

将作物的不同生长状态的纹理放进 \RP\textures\blocks 中,本教程中我为作物设置了 8 个不同生长阶段

回到 \RP\textures 目录,新建 terrain_texture.json,在里面输入:

  1. {
  2.     "resource_pack_name":"Ore Crops resource pack",
  3.     "texture_name":"atlas.terrain",
  4.     "num_mip_levels":4,
  5.     "padding":8,
  6.     "texture_data":{
  7.         "coal_crop_0":{
  8.             "textures":"textures/blocks/fang/crop_stage_0"
  9.         },
  10.         "coal_crop_1":{
  11.             "textures":"textures/blocks/fang/crop_stage_1"
  12.         },
  13.         "coal_crop_2":{
  14.             "textures":"textures/blocks/fang/crop_stage_2"
  15.         },
  16.         "coal_crop_3":{
  17.             "textures":"textures/blocks/fang/crop_stage_3"
  18.         },
  19.         "coal_crop_4":{
  20.             "textures":"textures/blocks/fang/crop_stage_4"
  21.         },
  22.         "coal_crop_5":{
  23.             "textures":"textures/blocks/fang/coal_stage_5"
  24.         },
  25.         "coal_crop_6":{
  26.             "textures":"textures/blocks/fang/coal_stage_6"
  27.         },
  28.         "coal_crop_7":{
  29.             "textures":"textures/blocks/fang/coal_stage_7"
  30.         }
  31.     }
  32. }
复制代码

我们还需要在资源包中为方块指定纹理,回到 \RP 目录

创建 blocks.json,为我们的作物方块指定纹理和音效

  1. {
  2.     "format_version":"1.18.0",
  3.     "orecrops:coal_crop":{
  4.         "textures":"coal_crop_0",
  5.         "sound":"grass"
  6.     }
  7. }
复制代码

textures 的值也可以是一个对象,用来设置方块不同面的纹理,例如这样:

  1. "textures":{
  2.     "down" : "log_end",
  3.     "side" : "log_side",
  4.     "up" : "log_end"
  5. }
复制代码

现在他是一个有纹理的方块了,但是看起来一点都不像一个作物该有的样子

打开 Blockbench,为他制作一个模型(更多 Blockbench 的用法,请查阅此教程



选择新建 Bedrock Model



设置好后点击 Confirm



制作出想要的模型(新建两个 cube,将 Size 的第三个值设置成 0 就成薄片状了)



将导出的模型放在 \RP\models\entity 中,之后我们就可以通过组件为方块设置模型和必要属性了

方块组件

我们将使用到下表中的组件,更多组件请查阅文档

名称描述
minecraft:collision_box设置方块的碰撞箱
minecraft:geometry设置方块的模型
minecraft:placement_filter设置方块被放置时的条件

minecraft:collision_box

  1. // 对象写法
  2. "minecraft:collision_box":{
  3.     "origin": 锚点,
  4.     "size": 尺寸
  5. }

  6. // 布尔值写法
  7. "minecraft:collision_box": 布尔值
复制代码

以上是 minecraft:collision_box 组件的不同写法,当布尔值写法的数据值设置为 false 时则禁用碰撞箱,也就是说下面两种写法是等价的

  1. // 对象写法
  2. "minecraft:collision_box":{
  3.     "origin": [0, 0, 0],
  4.     "size": [0, 0, 0]
  5. }

  6. // 布尔值写法
  7. "minecraft:collision_box": false
复制代码

在原版中的作物方块实体是可以自由穿过的,这里我们可以直接将 minecraft:collision_box 的数据值设置为 false 禁用碰撞箱

minecraft:geometry

  1. "minecraft:geometry": "几何模型"
复制代码

还记得你在上一节设置的 Model Identifier 吗?将它填进去就好

minecraft:placement_filter

  1. "minecraft:placement_filter": {
  2.   "conditions": [   // 条件可以设置多个
  3.     {
  4.       "block_filter": 方块数组,
  5.       "allowed_faces": 面数组
  6.     }
  7.   ]
  8. }
复制代码

我们的作物方块只可以放置在耕地方块的上面block_filter 设置为 ["farmland"]allowed_faces 设置为 ["up"]

另:allowed_faces 可选的值有 up(顶面)、down(底面)、north(北面)、south(南面)、east(东面)、west(西面)、side(侧面)、all(所有面)

现在我们的 coal_crops.json 应该是这样的:

  1. {
  2.     "format_version":"1.18.0",
  3.     "minecraft:block":{
  4.         "description":{
  5.             "identifier":"orecrops:coal_crop"
  6.         },
  7.         "components":{
  8.             "minecraft:collision_box":false,
  9.             "minecraft:geometry":"geometry.crop",
  10.             "minecraft:placement_filter":{
  11.                 "conditions":[
  12.                     {
  13.                         "block_filter":["farmland"],
  14.                         "allowed_faces":["up"]
  15.                     }
  16.                 ]
  17.             }
  18.         }
  19.     }
  20. }
复制代码

种子、作物

种子

我们将使用 minecraft:block_placer 组件让“煤炭种子”拥有放置方块的能力,在放置方块的同时将使用一个物品

  1. "minecraft:block_placer":{
  2.     "block": "方块的标识",
  3.     "use_on": 可以在何种方块上使用(数组)
  4. }
复制代码

写好后应该长这样:

  1. "minecraft:block_placer":{
  2.     "block": "orecrops:coal_crop",
  3.     "use_on": ["farmland"]
  4. }
复制代码

大功告成!物品这一部分我们已经彻底完成了 ——

  1. {
  2.     "format_version":"1.18.0",
  3.     "minecraft:item":{
  4.         "description":{
  5.             "identifier":"orecrops:coal_seed",
  6.             "category":"nature"
  7.         },
  8.         "components":{
  9.             "minecraft:display_name":{
  10.                 "value":"item.orecrops:coal_seed.name"
  11.             },
  12.             "minecraft:creative_category":{
  13.                 "parent":"itemGroup.name.seed"
  14.             },
  15.             "minecraft:max_stack_size":64,
  16.             "minecraft:icon":{
  17.                 "texture":"coal_seed"
  18.             },
  19.             "minecraft:block_placer":{
  20.                 "block":"orecrops:coal_crop",
  21.                 "use_on":["farmland"]
  22.             }
  23.         }
  24.     }
  25. }
复制代码

作物

不同生长状态

为了实现不同生长状态,我们首先需要在方块的描述中为方块定义一个属性(properties),它们类似“变量”,一个方块可以拥有多个属性,属性的值可以是数字、布尔值或字符串

我们先为作物方块定义一个 orecrops:growth 属性,来储存它的不同生长阶段

  1. "properties":{
  2.     "orecrops:growth":[0, 1, 2, 3, 4, 5, 6, 7]
  3. }
复制代码

为了让作物方块在不同生长阶段拥有不同的纹理,我们需要使用置换(permutations

在置换部分我们要接触到 Molang 了!

下面是 permutations 的格式,你可以设置不同条件对应不同欲被替换的组件

  1. {
  2.     "condition":"条件",
  3.     "components": 欲被替换的组件
  4. }
复制代码

条件部分我们可以使用 Molang,在这里我们通常使用 Molang 的逻辑运算符对值进行判断

运算符描述示例
!逻辑非运算符。使一个表达式的结果从 true 变为 false ,从 false 变为 true!query.can_fly
&&逻辑与运算符。只有当两个表达式都为true 时,整个表达式的结果才为truequery.can_walk == 1 && query.can_climb == 1
||逻辑或运算符。只要其中任意一个表达式为 true 整个表达式都为 true`query.can_walk == 1
<小于运算符。比较左右两边表达式数值的大小,若左边小于右边即为 true,反之则为 falsequery.total_particle_count < 5
<=小于等于运算符。用于比较左右两边表达式数值的大小,若左边小于或等于右边即为 true,反之则为 falsequery.trade_tier <= 2
>=大于等于运算符。用于比较左右两边表达式数值的大小,若左边大于或等于右边即为 true,反之则为 falsequery.health  >= 5
>大于运算符。用于比较左右两边表达式数值的大小,若左边大于右边即为 true,反之则为 falsequery.day > 7
==等于运算符。若左边与右边表达式的结果相等时即为 true,反之则为 falsequery.block_face == query.cardinal_player_facing
!=不等于运算符。若左边与右边表达式的结果不相等时即为 true,反之则为 falsequery.body_x_rotation != query.body_y_rotation

我们需要判断此前设置的 orecrops:growth 的值是否为特定值,所以选用等于运算符 ==

若要获取 orecrops:growth 还需要用到查询函数 query.block_property(),这个函数需要传入一个参数,参数则是要查询的属性名称

我们的煤炭作物生长第一阶段的“条件”是:query.block_property('orecrops:growth') == 0

我们共需要设置 8 个这样的置换,来做到 8 个生长状态:

  1. "permutations": [
  2.     {
  3.         "condition":"query.block_property('orecrops:growth') == 0",
  4.         "components":{
  5.             "minecraft:material_instances":{
  6.                 "*":{
  7.                     "texture":"crop_0",
  8.                     "render_method":"alpha_test"
  9.                 }
  10.             }
  11.         }
  12.     },
  13.     {
  14.         "condition":"query.block_property('orecrops:growth') == 1",
  15.         "components":{
  16.             "minecraft:material_instances":{
  17.                 "*":{
  18.                     "texture":"coal_crop_1",
  19.                     "render_method":"alpha_test"
  20.                 }
  21.             }
  22.         }
  23.     },
  24.     {
  25.         "condition":"query.block_property('orecrops:growth') == 2",
  26.         "components":{
  27.             "minecraft:material_instances":{
  28.                 "*":{
  29.                     "texture":"coal_crop_2",
  30.                     "render_method":"alpha_test"
  31.                 }
  32.             }
  33.         }
  34.     },
  35.     {
  36.         "condition":"query.block_property('orecrops:growth') == 3",
  37.         "components":{
  38.             "minecraft:material_instances":{
  39.                 "*":{
  40.                     "texture":"coal_crop_3",
  41.                     "render_method":"alpha_test"
  42.                 }
  43.             }
  44.         }
  45.     },
  46.     {
  47.         "condition":"query.block_property('orecrops:growth') == 4",
  48.         "components":{
  49.             "minecraft:material_instances":{
  50.                 "*":{
  51.                     "texture":"coal_crop_4",
  52.                     "render_method":"alpha_test"
  53.                 }
  54.             }
  55.         }
  56.     },
  57.     {
  58.         "condition":"query.block_property('orecrops:growth') == 5",
  59.         "components":{
  60.             "minecraft:material_instances":{
  61.                 "*":{
  62.                     "texture":"coal_crop_5",
  63.                     "render_method":"alpha_test"
  64.                 }
  65.             }
  66.         }
  67.     },
  68.     {
  69.         "condition":"query.block_property('orecrops:growth') == 6",
  70.         "components":{
  71.             "minecraft:material_instances":{
  72.                 "*":{
  73.                     "texture":"coal_crop_6",
  74.                     "render_method":"alpha_test"
  75.                 }
  76.             }
  77.         }
  78.     },
  79.     {
  80.         "condition":"query.block_property('orecrops:growth') == 7",
  81.         "components":{
  82.             "minecraft:material_instances":{
  83.                 "*":{
  84.                     "texture":"coal_crop_7",
  85.                     "render_method":"alpha_test"
  86.                 }
  87.             }
  88.         }
  89.     }
  90. ]
复制代码

生长

我们在上一节已经完整了作物的不同生长阶段,下面我们使用触发器组件与自定义事件完成作物的自然生长与骨粉催熟

我们先来设置事件,先来一个生长事件:

  1. "grow_event":{  // 自定义事件名
  2.     "randomize":[    // 随机执行一个事件组
  3.         {
  4.             "weight":1   // 权重,如果随机到这里什么都不会发生
  5.         },
  6.         {
  7.             "weight":1,  // 权重
  8.             "set_block_property":{   // 设置方块的属性的值
  9.                 "orecrops:growth":"(query.block_property('orecrops:growth')<7) ? query.block_property('orecrops:growth') + 1 : 7"  // 在下方详细解释
  10.             }
  11.         }
  12.     ]
  13. }
复制代码

set_block_property 中我们使用了 Molang 让 orecrops:growth 增加

这里我们用到了 Molang 的三元条件运算符 ?::当问号左边的表达式为真,则将问号与冒号中间表达式的值作为整个表达式的取值,反之则将冒号后面表达式的值作为整个表达式的取值。

翻译成人话就是:若 orecrops:growth 小于 7orecrops:growth + 1,否则设置 orecrops:growth7

下面我们来完成骨粉催熟事件

  1. "usebonemeal_event": {  // 自定义事件名
  2.     "sequence":[  // 执行下面的所有事件
  3.         {
  4.             "decrement_stack":{  // 减少一个手持的物品
  5.                 "ignore_game_mode":false  // 是否忽略游戏模式
  6.             }
  7.         },
  8.         {
  9.             "run_command":{  // 执行命令
  10.                 "command":[  // 命令(数组)
  11.                     "particle minecraft:crop_growth_emitter ~ ~ ~"  // 在方块处生成 crop_growth_emitter 粒子
  12.                 ]
  13.             }
  14.         },
  15.         {
  16.             "trigger":{  
  17.                 "event":"grow_event"  // 执行自定义事件“grow_event”
  18.             }
  19.         }
  20.     ]
  21. }
复制代码

完成事件后我们还需要使用触发器组件来触发这些事件

  1. "minecraft:on_interact": {  // 右键触发器组件
  2.     "condition":"query.get_equipped_item_name('main_hand') == 'bone_meal'",  // 触发条件是拿着骨粉右键
  3.     "event":"usebonemeal_event"  // 触发拿着骨粉右键将触发 usebonemeal_event 事件
  4. },
  5. "minecraft:random_ticking": {  // 随机刻组件
  6.     "on_tick":{  // 触发器
  7.         "event":"grow_event"  // 触发 grow_event 事件
  8.     }
  9. }
复制代码

现在我们的 coal_crop.json 是这个样子的:

  1. {
  2.     "format_version":"1.18.0",
  3.     "minecraft:block":{
  4.         "description":{
  5.             "identifier":"orecrops:coal_crop",
  6.             "properties":{
  7.                 "orecrops:growth":[0, 1, 2, 3, 4, 5, 6, 7]
  8.             }
  9.         },
  10.         "components":{
  11.             "minecraft:geometry":"geometry.crop",
  12.             "minecraft:collision_box":false,
  13.             "minecraft:placement_filter":{
  14.                 "conditions":[
  15.                     {
  16.                         "block_filter":["farmland"],
  17.                         "allowed_faces":["up"]
  18.                     }
  19.                 ]
  20.             },
  21.             "minecraft:on_interact":{
  22.                 "condition":"query.get_equipped_item_name('main_hand') == 'bone_meal'",
  23.                 "event":"usebonemeal_event"
  24.             },
  25.             "minecraft:random_ticking":{
  26.                 "on_tick":{
  27.                     "event":"grow_event"
  28.                 }
  29.             }
  30.         },
  31.         "events":{
  32.             "grow_event":{
  33.                 "randomize":[
  34.                     {
  35.                         "weight":1
  36.                     },
  37.                     {
  38.                         "weight":1,
  39.                         "set_block_property":{
  40.                             "orecrops:growth":"(query.block_property('orecrops:growth')<7) ? query.block_property('orecrops:growth') + 1 : 7"
  41.                         }
  42.                     }
  43.                 ]
  44.             },
  45.             "usebonemeal_event":{
  46.                 "sequence":[
  47.                     {
  48.                         "decrement_stack":{
  49.                             "ignore_game_mode":false
  50.                         }
  51.                     },
  52.                     {
  53.                         "run_command":{
  54.                             "command":[
  55.                                 "particle minecraft:crop_growth_emitter ~ ~ ~"
  56.                             ]
  57.                         }
  58.                     },
  59.                     {
  60.                         "trigger":{
  61.                             "event":"grow_event"
  62.                         }
  63.                     }
  64.                 ]
  65.             }
  66.         },
  67.         "permutations":[
  68.             {
  69.                 "condition":"query.block_property('orecrops:growth') == 0",
  70.                 "components":{
  71.                     "minecraft:material_instances":{
  72.                         "*":{
  73.                             "texture":"coal_crop_0",
  74.                             "render_method":"alpha_test"
  75.                         }
  76.                     }
  77.                 }
  78.             },
  79.             {
  80.                 "condition":"query.block_property('orecrops:growth') == 1",
  81.                 "components":{
  82.                     "minecraft:material_instances":{
  83.                         "*":{
  84.                             "texture":"coal_crop_1",
  85.                             "render_method":"alpha_test"
  86.                         }
  87.                     }
  88.                 }
  89.             },
  90.             {
  91.                 "condition":"query.block_property('orecrops:growth') == 2",
  92.                 "components":{
  93.                     "minecraft:material_instances":{
  94.                         "*":{
  95.                             "texture":"coal_crop_2",
  96.                             "render_method":"alpha_test"
  97.                         }
  98.                     }
  99.                 }
  100.             },
  101.             {
  102.                 "condition":"query.block_property('orecrops:growth') == 3",
  103.                 "components":{
  104.                     "minecraft:material_instances":{
  105.                         "*":{
  106.                             "texture":"coal_crop_3",
  107.                             "render_method":"alpha_test"
  108.                         }
  109.                     }
  110.                 }
  111.             },
  112.             {
  113.                 "condition":"query.block_property('orecrops:growth') == 4",
  114.                 "components":{
  115.                     "minecraft:material_instances":{
  116.                         "*":{
  117.                             "texture":"coal_crop_4",
  118.                             "render_method":"alpha_test"
  119.                         }
  120.                     }
  121.                 }
  122.             },
  123.             {
  124.                 "condition":"query.block_property('orecrops:growth') == 5",
  125.                 "components":{
  126.                     "minecraft:material_instances":{
  127.                         "*":{
  128.                             "texture":"coal_crop_5",
  129.                             "render_method":"alpha_test"
  130.                         }
  131.                     }
  132.                 }
  133.             },
  134.             {
  135.                 "condition":"query.block_property('orecrops:growth') == 6",
  136.                 "components":{
  137.                     "minecraft:material_instances":{
  138.                         "*":{
  139.                             "texture":"coal_crop_6",
  140.                             "render_method":"alpha_test"
  141.                         }
  142.                     }
  143.                 }
  144.             },
  145.             {
  146.                 "condition":"query.block_property('orecrops:growth') == 7",
  147.                 "components":{
  148.                     "minecraft:material_instances":{
  149.                         "*":{
  150.                             "texture":"coal_crop_7",
  151.                             "render_method":"alpha_test"
  152.                         }
  153.                     }
  154.                 }
  155.             }
  156.         ]
  157.     }
  158. }
复制代码

现在,一个可生长的作物已经初步成型了,等等——破坏掉它没有掉落物?!

收获

战利品表

我们需要使用战利品表自定义作物的掉落物

\BP\loot_tables 目录新建 coal_seed.json 文件作为物品还未成熟时的掉落物的战利品表

  1. {
  2.     "pools":[  // 随机池列表
  3.         {
  4.             "rolls":1,  // 此随机池中的抽取次数
  5.             "entries":[  
  6.                 {
  7.                     "weight":1,  // 权重
  8.                     "type":"item",  // 战利品的类型
  9.                     "name":"orecrops:coal_seed", // 战利品的标识
  10.                     "functions":[  // 函数列表
  11.                         {
  12.                             "function":"set_count",  // 函数名(设置战利品的数量)
  13.                             "count":1  // 数量为 1
  14.                         }
  15.                     ]
  16.                 }
  17.             ]
  18.         }
  19.     ]
  20. }
复制代码

\BP\loot_tables 目录新建 coal_crop.json 文件作为物品已经成熟时的掉落物的战利品表

  1. {
  2.     "pools":[
  3.         {
  4.             "rolls":1,
  5.             "entries":[
  6.                 {
  7.                     "weight":1,
  8.                     "type":"item",
  9.                     "name":"orecrops:coal_seed",
  10.                     "functions":[
  11.                         {
  12.                             "function":"set_count",
  13.                             "count":{
  14.                                 "min":0,  // 随机 0~2 个战利品
  15.                                 "max":2
  16.                             }
  17.                         }
  18.                     ]
  19.                 }
  20.             ]
  21.         },
  22.         {
  23.             "rolls":1,
  24.             "entries":[
  25.                 {
  26.                     "weight":1,
  27.                     "type":"item",
  28.                     "name":"minecraft:coal",
  29.                     "functions":[
  30.                         {
  31.                             "function":"set_count",
  32.                             "count":{
  33.                                 "min":1,  // 随机 1~3 个战利品
  34.                                 "max":3
  35.                             }
  36.                         }
  37.                     ]
  38.                 }
  39.             ]
  40.         }
  41.     ]
  42. }
复制代码

回到方块的 json 文件,使用 minecraft:loot 组件设置战利品表

我们为未成熟的作物方块设置 coal_seed.json 战利品

为成熟的作物方块设置 coal_crop.json 战利品

收获事件

为了在作物成熟的时候右键即可收获,且一株作物可以多次结矿物,我们可以新增一个自定义事件 harvest_event

  1. "harvest_event":{
  2.     "spawn_loot":{  // 生成战利品表
  3.         "table":"loot_tables/coal_crop.json"
  4.     },
  5.     "set_block_property":{  // 设置 orecrops:growth 为 5
  6.         "orecrops:growth":"5"
  7.     }
  8. }
复制代码

我们还需要在 orecrops:growth7 时的置换中置换 minecraft:on_interact 事件和 minecraft:loot 事件

  1. {
  2.     "condition":"query.block_property('orecrops:growth') == 7",
  3.     "components":{
  4.         "minecraft:material_instances":{
  5.             "*":{
  6.                 "texture":"coal_crop_7",
  7.                 "render_method":"alpha_test"
  8.             }
  9.         },
  10.         "minecraft:loot":"loot_tables/coal_crop.json",
  11.         "minecraft:on_interact":{
  12.             "event":"harvest_event"
  13.         }
  14.     }
  15. }
复制代码

最终,我们的 coal_crop.json 是这样的:

  1. {
  2.     "format_version":"1.18.0",
  3.     "minecraft:block":{
  4.         "description":{
  5.             "identifier":"orecrops:coal_crop",
  6.             "properties":{
  7.                 "orecrops:growth":[0, 1, 2, 3, 4, 5, 6, 7]
  8.             }
  9.         },
  10.         "components":{
  11.             "minecraft:loot":"loot_tables/coal_seed.json",
  12.             "minecraft:geometry":"geometry.crop",
  13.             "minecraft:collision_box":false,
  14.             "minecraft:placement_filter":{
  15.                 "conditions":[
  16.                     {
  17.                         "block_filter":["farmland"],
  18.                         "allowed_faces":["up"]
  19.                     }
  20.                 ]
  21.             },
  22.             "minecraft:on_interact":{
  23.                 "condition":"query.get_equipped_item_name('main_hand') == 'bone_meal'",
  24.                 "event":"usebonemeal_event"
  25.             },
  26.             "minecraft:random_ticking":{
  27.                 "on_tick":{
  28.                     "event":"grow_event"
  29.                 }
  30.             }
  31.         },
  32.         "events":{
  33.             "grow_event":{
  34.                 "randomize":[
  35.                     {
  36.                         "weight":1
  37.                     },
  38.                     {
  39.                         "weight":1,
  40.                         "set_block_property":{
  41.                             "orecrops:growth":"(query.block_property('orecrops:growth')<7) ? query.block_property('orecrops:growth') + 1 : 7"
  42.                         }
  43.                     }
  44.                 ]
  45.             },
  46.             "harvest_event":{
  47.                 "spawn_loot":{
  48.                     "table":"loot_tables/coal_crop.json"
  49.                 },
  50.                 "set_block_property":{
  51.                     "orecrops:growth":"5"
  52.                 }
  53.             },
  54.             "usebonemeal_event":{
  55.                 "sequence":[
  56.                     {
  57.                         "decrement_stack":{
  58.                             "ignore_game_mode":false
  59.                         }
  60.                     },
  61.                     {
  62.                         "run_command":{
  63.                             "command":[
  64.                                 "particle minecraft:crop_growth_emitter ~ ~ ~"
  65.                             ]
  66.                         }
  67.                     },
  68.                     {
  69.                         "trigger":{
  70.                             "event":"grow_event"
  71.                         }
  72.                     }
  73.                 ]
  74.             }
  75.         },
  76.         "permutations":[
  77.             {
  78.                 "condition":"query.block_property('orecrops:growth') == 0",
  79.                 "components":{
  80.                     "minecraft:material_instances":{
  81.                         "*":{
  82.                             "texture":"coal_crop_0",
  83.                             "render_method":"alpha_test"
  84.                         }
  85.                     },
  86.                     "minecraft:loot":"loot_tables/coal_seed.json"
  87.                 }
  88.             },
  89.             {
  90.                 "condition":"query.block_property('orecrops:growth') == 1",
  91.                 "components":{
  92.                     "minecraft:material_instances":{
  93.                         "*":{
  94.                             "texture":"coal_crop_1",
  95.                             "render_method":"alpha_test"
  96.                         }
  97.                     },
  98.                     "minecraft:loot":"loot_tables/coal_seed.json"
  99.                 }
  100.             },
  101.             {
  102.                 "condition":"query.block_property('orecrops:growth') == 2",
  103.                 "components":{
  104.                     "minecraft:material_instances":{
  105.                         "*":{
  106.                             "texture":"coal_crop_2",
  107.                             "render_method":"alpha_test"
  108.                         }
  109.                     },
  110.                     "minecraft:loot":"loot_tables/coal_seed.json"
  111.                 }
  112.             },
  113.             {
  114.                 "condition":"query.block_property('orecrops:growth') == 3",
  115.                 "components":{
  116.                     "minecraft:material_instances":{
  117.                         "*":{
  118.                             "texture":"coal_crop_3",
  119.                             "render_method":"alpha_test"
  120.                         }
  121.                     },
  122.                     "minecraft:loot":"loot_tables/coal_seed.json"
  123.                 }
  124.             },
  125.             {
  126.                 "condition":"query.block_property('orecrops:growth') == 4",
  127.                 "components":{
  128.                     "minecraft:material_instances":{
  129.                         "*":{
  130.                             "texture":"coal_crop_4",
  131.                             "render_method":"alpha_test"
  132.                         }
  133.                     },
  134.                     "minecraft:loot":"loot_tables/coal_seed.json"
  135.                 }
  136.             },
  137.             {
  138.                 "condition":"query.block_property('orecrops:growth') == 5",
  139.                 "components":{
  140.                     "minecraft:material_instances":{
  141.                         "*":{
  142.                             "texture":"coal_crop_5",
  143.                             "render_method":"alpha_test"
  144.                         }
  145.                     },
  146.                     "minecraft:loot":"loot_tables/coal_seed.json"
  147.                 }
  148.             },
  149.             {
  150.                 "condition":"query.block_property('orecrops:growth') == 6",
  151.                 "components":{
  152.                     "minecraft:material_instances":{
  153.                         "*":{
  154.                             "texture":"coal_crop_6",
  155.                             "render_method":"alpha_test"
  156.                         }
  157.                     },
  158.                     "minecraft:loot":"loot_tables/coal_seed.json"
  159.                 }
  160.             },
  161.             {
  162.                 "condition":"query.block_property('orecrops:growth') == 7",
  163.                 "components":{
  164.                     "minecraft:material_instances":{
  165.                         "*":{
  166.                             "texture":"coal_crop_7",
  167.                             "render_method":"alpha_test"
  168.                         }
  169.                     },
  170.                     "minecraft:loot":"loot_tables/coal_crop.json",
  171.                     "minecraft:on_interact":{
  172.                         "event":"harvest_event"
  173.                     }
  174.                 }
  175.             }
  176.         ]
  177.     }
  178. }
复制代码

至此,种子与作物方块已经全部完成了,下面一章我们将学习使用自定义合成配方

合成种子

有序合成配方的基本结构

  1. {
  2.     "format_version":"1.12",
  3.     "minecraft:recipe_shaped":{
  4.         "description":{
  5.             // 描述
  6.         },
  7.         "tags":[
  8.             // 类别
  9.         ],
  10.         "pattern":[
  11.             // 样式
  12.         ],
  13.         "key":{
  14.             // 材料
  15.         },
  16.         "result":{
  17.             // 成品
  18.         }
  19.     }
  20. }
复制代码

pattern:有序合成的物品摆放样式

key:使用字符代表物品,用在 pattern

  1. // 写法1
  2. "字符":"物品的标识"

  3. //写法2
  4. "字符":{
  5.     "item": "物品的标识",
  6.     "data": "物品的特殊值(支持 Molang)"
  7. }
复制代码

\BP\recipes 目录新建一个 coal_seed.json

看看下面 coal_seed.json 的例子就懂了

  1. {
  2.     "format_version":"1.18.0",
  3.     "minecraft:recipe_shaped": {
  4.         "description":{
  5.             "identifier": "orecrops:coal_seed"
  6.         },
  7.         "tags": ["crafting_table"],
  8.         "pattern": [
  9.             "OOO",
  10.             "OSO",
  11.             "OOO"
  12.         ],
  13.         "key": {
  14.             "O": {
  15.                 "item": "minecraft:coal"
  16.             },
  17.             "S": {
  18.                 "item": "minecraft:wheat_seeds"
  19.             }
  20.         },
  21.         "result":{
  22.             "item": "orecrops:coal_seed"
  23.         }
  24.     }
  25. }
复制代码

向世界推广你的 Addon

为了适配不同语言的游戏,我们在很早就做好了本地化的准备。现在,如果你想增加一门语言的支持,只需要在 \RP\texts 目录新建一个 语言代号.lang,例如 en_US.lang

在这些语言文件中增加两行:

  1. pack.description=资源包的描述
  2. pack.name=矿石作物
复制代码

修改你的包名和描述为 pack.namepack.description

再新建一个文件 languages.json,输入:

  1. [
  2.     "zh_CN",
  3.     "en_US"
  4. ]
复制代码

这样就可以做到预读取语言文件,在不同语言下附加包的名称与描述都会变为与对应语言文件中对应的文本



参考与引用
本文在撰写过程中参考或引用了来自以下网站的文本

致谢
感谢以下组织、个人、网站、软件在教程编写过程中对本人提供的帮助

https://afdian.net/leaflet?slug=Mr_Fang
来自群组: Uncode Studio

liujinsong48
稍微有亿些复杂qwq

第一页 上一页 下一页 最后一页