手册:实例/附加包/方块/农作物

来自Minecraft基岩版开发Wiki

引言[编辑]

我们可以通过方块置换来实现类似于原版农作物的方块,本实例将帮助你制作一个属于自己的自定义农作物。 由于原版农作物的工作机制较为复杂,本实例只提供一个由纯数据驱动内容实现的,仅受游戏随机刻影响的,最基本的自定义农作物。

前置知识[编辑]

你在阅读本实例前应该掌握如下知识:

  • 附加包的构建
  • 自定义物品
  • 自定义方块
  • 对方块置换的基本知识

教程[编辑]

添加种子[编辑]

首先在行为包的items文件夹下新建一个JSON文件,命名为custom_seed.json,并插入以下内容:

{
    "format_version":"1.20.50",
    "minecraft:item":{
        "description":{
            "identifier":"wiki:custom_seed",
            // 物品分组,这里将其归类至“种子”分组
            "menu_category":{
				"category": "nature",
                "group":"itemGroup.name.seed"
            },
        },
        "components":{
            // 该物品显示名称,填本地化字符串
            "minecraft:display_name":{
                "value":"item.wiki:custom_seed.name"
            },
            // 该物品纹理,填物品短名称
            "minecraft:icon":{
                "texture":"wiki.custom_seed"
            },
            // 重中之重,添加这个组件可以使物品像原版的种子一样播种作物
            "minecraft:block_placer":{
                 // 播种方块的ID
                 "block": "wiki:custom_crop",
                 // 可以在何种方块上播种,这里填farmland(耕地)
                 "use_on": ["farmland"]
                }
        }
    }
}

然后为这个物品添加纹理与名称,由于篇幅原因,这里不再赘述。

添加作物方块[编辑]

行为包部分[编辑]

在行为包的blocks文件夹下新建一个JSON文件,命名为custom_crop.json,并插入以下内容:

{
    "format_version": "1.20.50",
    "minecraft:block": {
        "description": {
            //方块ID需与种子物品的"block_placer"组件中填写的ID保持一致
            "identifier": "wiki:custom_crop",
            "states": {
                //定义农作物的总生长阶段数
                "custom_crop:growth": [0,1,2,3,4,5,6,7]
            }
        },
        //设置每个生长阶段对应的方块置换,主要为纹理与选择箱的修改
        "permutations": [
            {
                //"query.block_state(<block_state_name>)"是用于查询方块状态的molang,此处用于查询自定义农作物的当前生长阶段
                "condition": "query.block_state('custom_crop:growth') == 0",
                "components": {
                    //将方块纹理调为当前阶段对应的纹理
                    "minecraft:material_instances": {
                        "*": {
                            //在terrain_texture.json中定义的方块纹理字段
                            "texture": "custom_crop_stage0",
                            //取消环境光
                            "ambient_occlusion": false,
                            "face_dimming": false,
                            "render_method": "alpha_test"
                        }
                    },
                    //调整方块选择箱,该实例对应原版小麦
                    "minecraft:selection_box": {
                        "origin": [-8,0,-8],
                        "size": [16,3,16]
                    }
                }
            },
            {
                "condition": "query.block_state('custom_crop:growth') == 1",
                "components": {
                    "minecraft:material_instances": {
                        "*": {
                            "texture": "custom_crop_stage1",
                            "ambient_occlusion": false,
                            "face_dimming": false,
                            "render_method": "alpha_test"
                        }
                    },
                    "minecraft:selection_box": {
                        "origin": [-8,0,-8],
                        "size": [16,4,16]
                    }
                }
            },
            {
                "condition": "query.block_state('custom_crop:growth') == 2",
                "components": {
                    "minecraft:material_instances": {
                        "*": {
                            "texture": "custom_crop_stage2",
                            "ambient_occlusion": false,
                            "face_dimming": false,
                            "render_method": "alpha_test"
                        }
                    },
                    "minecraft:selection_box": {
                        "origin": [-8,0,-8],
                        "size": [16,7,16]
                    }
                }
            },
            {
                "condition": "query.block_state('custom_crop:growth') == 3",
                "components": {
                    "minecraft:material_instances": {
                        "*": {
                            "texture": "custom_crop_stage3",
                            "ambient_occlusion": false,
                            "face_dimming": false,
                            "render_method": "alpha_test"
                        }
                    },
                    "minecraft:selection_box": {
                        "origin": [-8,0,-8],
                        "size": [16,9,16]
                    }
                }
            },
            {
                "condition": "query.block_state('custom_crop:growth') == 4",
                "components": {
                    "minecraft:material_instances": {
                        "*": {
                            "texture": "custom_crop_stage4",
                            "ambient_occlusion": false,
                            "face_dimming": false,
                            "render_method": "alpha_test"
                        }
                    },
                    "minecraft:selection_box": {
                        "origin": [-8,0,-8],
                        "size": [16,12,16]
                    }
                }
            },
            {
                "condition": "query.block_state('custom_crop:growth') == 5",
                "components": {
                    "minecraft:material_instances": {
                        "*": {
                            "texture": "custom_crop_stage5",
                            "ambient_occlusion": false,
                            "face_dimming": false,
                            "render_method": "alpha_test"
                        }
                    },
                    "minecraft:selection_box": {
                        "origin": [-8,0,-8],
                        "size": [16,14,16]
                    }
                }
            },
            {
                "condition": "query.block_state('custom_crop:growth') == 6",
                "components": {
                    "minecraft:material_instances": {
                        "*": {
                            "texture": "custom_crop_stage6",
                            "ambient_occlusion": false,
                            "face_dimming": false,
                            "render_method": "alpha_test"
                        }
                    },
                    "minecraft:selection_box": {
                        "origin": [-8,0,-8],
                        "size": [16,15,16]
                    }
                }
            },
            {
                "condition": "query.block_state('custom_crop:growth') == 7",
                "components": {
                    //设置成熟之后农作物的战利品表
                    "minecraft:loot": "loot_tables/blocks/custom_crop_riped.json",
                    "minecraft:material_instances": {
                        "*": {
                            "texture": "custom_crop_stage7",
                            "ambient_occlusion": false,
                            "face_dimming": false,
                            "render_method": "alpha_test"
                        }
                    },
                    "minecraft:selection_box": {
                        "origin": [-8,0,-8],
                        "size": [16,16,16]
                    }
                }
            }
        ],
        "components": {
            //设置未成熟时农作物的战利品表
            "minecraft:loot": "loot_tables/blocks/custom_crop.json",
            //使该方块可被瞬间破坏
            "minecraft:destructible_by_mining": {
                "seconds_to_destroy": 0.0
            },
            //使该方块不阻挡光线
            "minecraft:light_dampening": 0,
            //设置农作物模型,此处模型需手动装载,详情见下文
            "minecraft:geometry": "geometry.bush_crop",
            "minecraft:destructible_by_explosion": {
                "explosion_resistance": 0.0
            },
            //取消该方块的碰撞箱
            "minecraft:collision_box": false,
            //设置该方块的骨粉催熟事件触发器
            "minecraft:on_interact": {
                "event": "wiki:on_interact",
                //条件限制,若手持物品非骨粉或状态等于7则不触发事件
                "condition": "query.is_item_name_any('slot.weapon.mainhand','minecraft:bone_meal' && query.block_state('custom_crop:growth')<7)"
            },
            //设置该方块只能放置在耕地上方
            "minecraft:placement_filter": {
                "conditions": [
                    {
                        "block_filter": ["farmland"],
                        "allowed_faces": ["up"]
                    }
                ]
            },
            //设置该方块的随机刻生长事件触发器
            "minecraft:random_ticking": {
                "on_tick": {
                    "event": "grow_stage",
                    "target": "self"
                }
            },
            "tag:crop": {}
        },
        "events": {
            //设置该方块的随机刻生长事件响应
            "grow_stage": {
                //若未成熟,则使生长阶段+1
                "condition": "query.block_state('custom_crop:growth')<7",
                "set_block_state": {
                    "custom_crop:growth": "(query.block_state('custom_crop:growth')<7) ? query.block_state('custom_crop:growth') + 1 : 7"
                    }
            },
            //设置该方块的骨粉催熟事件响应
            "wiki:on_interact": {
                "condition": "query.block_state('custom_crop:growth')<7"
                "sequence": [
                    {
                        //有12/17的概率使生长阶段+3,若超过最大阶段则设为最大阶段
                        "randomize": [
                            {
                                "weight": 5
                            },
                            {
                                "weight": 12,
                                "sequence": [
                                    {
                                        "set_block_state": {
                                            "custom_crop:growth": "(query.block_state('custom_crop:growth')<4) ? query.block_state('custom_crop:growth') + 3 : 7"
                                        }
                                    }
                                ]
                            }
                        ]
                    },
                    {
                        //消耗一个骨粉物品
                        "decrement_stack": {}
                    },
                    {
                        //在方块上播放催熟粒子
                        "run_command": {
                            "command": [
                                "particle minecraft:crop_growth_emitter ~~~"
                            ]
                        }
                    }
                ]
            }
        }
    }
}

至此,该作物方块的行为部分就写好了。

资源包部分[编辑]

虽然方块纹理已经在行为包部分进行了配置,但是其声音仍需在blocks.json中单独设置,其中农作物对应的字段是"grass"

上文中的农作物模型游戏并未内置,所以我们需要在models/blocks文件夹下新建一个bush_crop.geo.json,具体内容如下:

{
    //该文件对应原版小麦模型
	"format_version": "1.12.0",
	"minecraft:geometry": [
		{
			"description": {
				"identifier": "geometry.bush_crop",
				"texture_width": 16,
				"texture_height": 16,
				"visible_bounds_width": 2,
				"visible_bounds_height": 2.5,
				"visible_bounds_offset": [0, 0.75, 0]
			},
			"bones": [
				{
					"name": "bone",
					"pivot": [8, -1, -8],
					"cubes": [
						{
							"origin": [4, -1, -8],
							"size": [0, 15, 16],
							"uv": {
								"west": {"uv": [16, 0], "uv_size": [-16, 16]}
							}
						},
						{
							"origin": [-4, -1, -8],
							"size": [0, 15, 16],
							"uv": {
								"east": {"uv": [16, 0], "uv_size": [-16, 16]}
							}
						},
						{
							"origin": [-8, -1, -4],
							"size": [16, 15, 0],
							"uv": {
								"north": {"uv": [16, 0], "uv_size": [-16, 16]}
							}
						},
						{
							"origin": [-8, -1, 4],
							"size": [16, 15, 0],
							"uv": {
								"south": {"uv": [16, 0], "uv_size": [-16, 16]}
							}
						}
					]
				}
			]
		}
	]
}

至此,该方块的模型和声音也配置完毕了,只需要再配置名称即可。

结语[编辑]

如果你看到了这里,那么恭喜你,你已经学会了如何自定义一个基础的农作物了!然而,农作物的实现方式远远不止这一种,你可以对其中的任意一个环节进行修改,来做出更酷的效果或者更复杂的生长机制,甚至可以配合脚本API编写更深刻的逻辑。