自定义作物
intermediate
scripting
不喜欢胡萝卜?没关系,你可以打造属于自己的(更优秀的)作物!
制作作物并不像想象中困难,只需通过特定事件序列的编码练习与规划。本教程将引导你创建独特的作物方块及其种子与食物物品。
已知问题:
- 自定义作物无法被流动的熔岩破坏
- 生长速度不受光照等级影响(参见反馈帖)
作物模型
观察游戏中的胡萝卜和马铃薯等作物,你会发现它们由4个平面构成,每个平面距离边缘4像素(如下图)。这些可见面朝内排列,以避免被周围方块遮挡产生阴影。
需特别注意:与传统方块不同,每个平面都下沉了1像素。若忘记下移平面,作物会显示在耕地模型上方1像素处(耕地模型高度较矮)。通过下移1像素,作物就能完美贴合耕地表面。以下是作物模型模板:

初始方块JSON
首先,我们要像原版作物那样设置8个生长阶段,因此方块需要包含8个值的状态。以下代码还包含了作物在每个排列中都生效的基础组件:
BP/blocks/custom_crop.json
json
{
"format_version": "1.21.70",
"minecraft:block": {
"description": {
"identifier": "wiki:custom_crop",
"menu_category": {
"category": "none" // 从创造模式物品栏隐藏——应通过种子放置
},
"states": {
"wiki:growth": {
"values": { "min": 0, "max": 7 }
}
}
},
"components": {
"minecraft:collision_box": false,
"minecraft:geometry": "geometry.crop", // 使用上一步提供的模型
"minecraft:light_dampening": 0,
// 当作物未放置在耕地上时破坏它
"minecraft:placement_filter": {
"conditions": [
{
"allowed_faces": ["up"],
"block_filter": ["minecraft:farmland"]
}
]
},
// 当水流接触时破坏作物
"minecraft:liquid_detection": {
"detection_rules": [
{
"liquid_type": "water",
"on_liquid_touches": "broken"
}
]
}
}
}
}自定义生长组件
以下事件实现了三个关键功能:
onRandomTick事件用于随机间隔增加wiki:growth状态值onPlayerInteract事件实现骨粉支持:生存模式随机增加生长值,创造模式直接成熟
BP/scripts/custom_crop.js
js
import { EquipmentSlot, GameMode, world } from "@minecraft/server";
/**
* @param {number} min 最小整数
* @param {number} max 最大整数
* @returns {number} 介于min和max之间的随机整数(含边界值)
*/
const randomInt = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
const maxGrowth = 7;
/** @type {import("@minecraft/server").BlockCustomComponent} */
const BlockCustomCropGrowthComponent = {
onRandomTick({ block }) {
const growthChance = 1 / 3;
if (Math.random() > growthChance) return;
const growth = block.permutation.getState("wiki:growth");
block.setPermutation(block.permutation.withState("wiki:growth", growth + 1));
},
onPlayerInteract({ block, dimension, player }) {
if (!player) return;
const equippable = player.getComponent("minecraft:equippable");
if (!equippable) return;
const mainhand = equippable.getEquipmentSlot(EquipmentSlot.Mainhand);
if (!mainhand.hasItem() || mainhand.typeId !== "minecraft:bone_meal") return;
if (player.getGameMode() === GameMode.creative) {
// 创造模式直接成熟
block.setPermutation(block.permutation.withState("wiki:growth", 7));
} else {
let growth = block.permutation.getState("wiki:growth");
// 生存模式随机增加生长阶段
growth += randomInt(1, maxGrowth - growth);
block.setPermutation(block.permutation.withState("wiki:growth", growth));
// 减少骨粉数量
if (mainhand.amount > 1) mainhand.amount--;
else mainhand.setItem(undefined);
}
// 播放效果
const effectLocation = block.center();
dimension.playSound("item.bone_meal.use", effectLocation);
dimension.spawnParticle("minecraft:crop_growth_emitter", effectLocation);
},
};
world.beforeEvents.worldInitialize.subscribe(({ blockComponentRegistry }) => {
blockComponentRegistry.registerCustomComponent(
"wiki:custom_crop_growth",
BlockCustomCropGrowthComponent
);
});生长状态排列
当方块处于特定状态值时会发生什么?以下排列根据 wiki:growth 值设置不同的选择框、战利品表和纹理。例如当 wiki:growth 为7时,纹理变为 custom_crop_3 并可掉落食物。
minecraft:block
json
"permutations": [
{
"condition": "q.block_state('wiki:growth') < 7",
"components": {
// 未成熟作物的战利品表(原版作物幼年期只掉落种子)
"minecraft:loot": "loot_tables/blocks/custom_crop_young.json",
// 随机生长和骨粉交互的触发器(仅在未成熟时激活)
"minecraft:custom_components": ["wiki:custom_crop_growth"]
}
},
{
"condition": "q.block_state('wiki:growth') >= 0",
"components": {
"minecraft:material_instances": {
"*": {
"texture": "wiki:custom_crop_0",
"render_method": "alpha_test_single_sided",
"ambient_occlusion": false,
"face_dimming": false
}
}
}
},
// 后续各生长阶段的选择框和纹理配置...
{
"condition": "q.block_state('wiki:growth') == 7",
"components": {
"minecraft:material_instances": {
"*": {
"texture": "wiki:custom_crop_3",
"render_method": "alpha_test_single_sided",
"ambient_occlusion": false,
"face_dimming": false
}
},
"minecraft:selection_box": {
"origin": [-8, 0, -8],
"size": [16, 12.8, 16]
},
// 成熟时掉落不同战利品
"minecraft:loot": "loot_tables/blocks/custom_crop_mature.json"
}
}
]你可以根据需要添加更多生长阶段,但记得同步修改状态的 max 值和脚本中的 maxGrowth 值。
完整方块JSON
以下是完整的 wiki:custom_crop 文件参考:
自定义作物方块JSON
BP/blocks/custom_crop.json
json
{
"format_version": "1.21.70",
"minecraft:block": {
"description": {
"identifier": "wiki:custom_crop",
"menu_category": {
"category": "none"
},
"states": {
"wiki:growth": {
"values": { "min": 0, "max": 7 }
}
}
},
"components": {
"minecraft:flammable": true,
"minecraft:collision_box": false,
"minecraft:geometry": "geometry.crop",
"minecraft:light_dampening": 0,
"minecraft:placement_filter": {
"conditions": [
{
"allowed_faces": ["up"],
"block_filter": ["minecraft:farmland"]
}
]
}
},
"permutations": [
// 各生长阶段的具体配置...
]
}
}作物战利品
以下是自定义作物可用的战利品表示例:
幼年期战利品表
BP/loot_tables/blocks/custom_crop_young.json
json
{
"pools": [
{
"rolls": 1,
"entries": [
{
"type": "item",
"name": "wiki:custom_seeds"
}
]
}
]
}成熟期战利品表
BP/loot_tables/blocks/custom_crop_mature.json
json
{
"pools": [
{
"rolls": 1,
"entries": [
{
"type": "item",
"name": "wiki:custom_food",
"functions": [
{
"function": "set_count",
"count": { "min": 2, "max": 5 }
}
]
}
]
},
{
"rolls": 1,
"entries": [
{
"type": "item",
"name": "wiki:custom_seeds",
"functions": [
{
"function": "set_count",
"count": { "min": 0, "max": 3 }
}
]
}
]
}
]
}自定义种子
手持作物方块看起来不协调,所以我们用种子来种植!以下是放置作物的自定义物品JSON:
BP/items/custom_seeds.json
json
{
"format_version": "1.21.70",
"minecraft:item": {
"description": {
"identifier": "wiki:custom_seeds", // 确保与作物ID不同
"menu_category": {
"category": "nature",
"group": "minecraft:itemGroup.name.seed"
}
},
"components": {
"minecraft:icon": "wiki:custom_seeds",
"minecraft:block_placer": {
"block": "wiki:custom_crop" // 该物品放置的方块
}
}
}
}自定义食物
作物不能只掉落种子!用以下模板创建自定义食物:
BP/items/custom_food.json
json
{
"format_version": "1.21.70",
"minecraft:item": {
"description": {
"identifier": "wiki:custom_food", // 确保与作物和种子ID不同
"menu_category": {
"category": "nature",
"group": "minecraft:itemGroup.name.crop"
}
},
"components": {
"minecraft:icon": "wiki:custom_food",
"minecraft:food": {
"nutrition": 4,
"saturation_modifier": 0.6
},
"minecraft:use_animation": "eat",
"minecraft:use_modifiers": {
"use_duration": 1.6,
"movement_modifier": 0.33
}
}
}
}最终成果
你的资源包现在应包含以下文件:
- 📝custom_crop.json
- 📝custom_food.json
- 📝custom_seeds.json
- 📝custom_crop_mature.json
- 📝custom_crop_young.json
通过本教程,你现在已掌握创建自定义作物及其种子、食物物品的知识与技能。
下载示例包
如需进一步帮助或获取完整模板文件,请点击下方下载按钮。祝你设计愉快!
下载MCADDON ```贡献者
编辑 自定义作物本页面上的文本和图像内容根据 知识共享署名 4.0 国际许可协议
本页中的代码示例根据 MIT 许可证




