前言
通过fabric编写模组,创建一个自定义物品腐烂的苹果
新建项目
选择Minecraft生成器, 选择对应的minecraft版本,frabic版本及模组名称等

创建物品类
在main
中创建一个类RottenApple
来实现自定义物品
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| public class RottenApple{ public static final Logger LOGGER = LoggerFactory.getLogger("MyMod");
public static final FoodComponent ROTTEN_FOOD_COMPONENT = new FoodComponent.Builder() .alwaysEdible() .statusEffect(new StatusEffectInstance(StatusEffects.POISON, 6 * 20, 0), 0.8f) .build();
static class CustomToolTip extends Item { static String tips; public CustomToolTip(Settings settings, String tip) { super(settings); tips = tip; }
@Override public void appendTooltip(ItemStack stack, TooltipContext context, List<Text> tooltip, TooltipType type) { tooltip.add(Text.translatable(tips).styled(s -> s.withColor(0x6D4C41))); } }
public static final Item RottenApple = register( new CustomToolTip(new Item.Settings().food(ROTTEN_FOOD_COMPONENT), "腐烂的"), "rotten_apple" );
public static Item register(Item item, String id) { Identifier itemID = Identifier.of("mymod", id); return Registry.register(Registries.ITEM, itemID, item); }
public static void initialize() { ItemGroupEvents.modifyEntriesEvent(ItemGroups.FOOD_AND_DRINK) .register((itemGroup) -> itemGroup.add(RottenApple));
CompostingChanceRegistry.INSTANCE.add(RottenApple, 0.3f);
LOGGER.info("[DEBUG] 物品注册完成"); }
}
|
在Mymod
中进行模组初始化
1 2 3 4 5 6 7
| public class Mymod implements ModInitializer {
@Override public void onInitialize() { RottenApple.initialize(); } }
|
在gradle
中,选择fabric
,运行runClient
,进入游戏。新建游戏后,在物品栏中可以看到我们新建的物品。

但是此时,物品名称和纹理都还没有添加
添加翻译
在main\resources\assets\mymod\lang
文件夹中,新建一个en_us.json
(如果文件夹不存在则新建)
写入翻译和名称
1 2 3
| { "item.mymod.rotten_apple": "RottenApple" }
|

添加纹理和模型
模型
在main\resources\assets\mymod\models\item
文件夹中,新建一个rotten_apple.json
(如果文件夹不存在则新建)
写入模型数据
1 2 3 4 5 6
| { "parent": "item/generated", "textures": { "layer0": "mymod:item/rotten_apple" } }
|
- parent:模型要继承的模型。大多物品继承的模型是 item/generate, 也有其他的,比如 item/handheld,用于拿在玩家手中的物品,例如工具。
- textures:为模型定义纹理的地方。 layer0 键是模型使用的纹理。
纹理
将纹理文件放在main\resources\assets\mymod\textures\item
文件夹中,并命名为rotten_apple.png
(如果文件夹不存在则新建)
需要注意的是:
- 必须是png格式的文件
- 像素需要是16×16或32×32

合成配方
通过腐肉
+ 苹果
进行无序合成
在main\resources\data\mymod\recipe
文件夹中,新建一个rotten_apple.json
(如果文件夹不存在则新建)
写入合成配方(无序合成)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| { "type": "minecraft:crafting_shapeless", "ingredients": [ { "item": "minecraft:rotten_flesh" }, { "item": "minecraft:apple" } ], "result": { "id": "mymod:rotten_apple", "count": 1 } }
|

实现效果
