Minecraft 模组开发笔记
本文最后更新于 1049 天前,其中的信息可能已经有所发展或是发生改变。

前言

注意
此文使用的模组api为Fabric

作者IDE使用的是IntelliJ IDEA(感觉挺好用的)

新建项目

新建项目,然后去Github下个Fabric的实例文件

注意新建项目时选择 Gradle

在IDE中打开build.gradle,等待导入(下载文件时建议挂一个梯子),然后在IDE中执行genSources任务。

  1. 如果你的IDE没有gradle集成,在命令行中执行./gradlew genSources命令对IntelliJ IDEA: ./gradlew idea.
  2. 对Eclipse: ./gradlew eclipse.
  3. 对VSCode:详见这里.

然后就可以快乐的写模组了

新建物品和物品类

新建物品方法:

public class ExampleMod implements ModInitializer {
    public static final Item FABRIC_ITEM = new Item(new FabricItemSettings().group(
            ItemGroup.MISC));
    @Override
    public void onInitialize() {
        Registry.register(Registry.ITEM,
                new Identifier("tutorial", "fabric_item"), FABRIC_ITEM);
    }
}
注意
物品json路径: …/resources/assets/tutorial/models/item/fabric_item.json

物品贴图路径: …/resources/assets/tutorial/textures/item/fabric_item.png

物品json模板:

{
  “parent” : “item / generated” ,
  “textures” : {
    “layer0” : “tutorial:item / fabric_item”
  }
}

物品示例材质:

新建物品类方法:

public class ExampleMod implements ModInitializer
{
    public static final ItemGroup ITEM_GROUP = FabricItemGroupBuilder.build(
            new Identifier("tutorial", "general"),
            () -> new ItemStack(Blocks.COBBLESTONE));
    public static final ItemGroup OTHER_GROUP = FabricItemGroupBuilder.create(
            new Identifier("tutorial", "other"))
            .icon(() -> new ItemStack(Items.BOWL))
            .build();
}
注意
请记住,传递给Identifier构造函数的参数只允许小写和下划线来命名。

namespace&path这两个参数可以包含小写字母,数字,下划线,句点或短划线[a-z0-9_.-]。

第二个参数the path也可以包含斜杠[a-z0-9/._-]。请避免使用其他符号,否则将会抛出InvalidIdentifierException! (是我踩过的坑)

把物品添加进物品类:

public static final Item YOUR_ITEM = new Item(new Item.Settings().itemGroup(ExampleMod.ITEM_GROUP));

总结:

public static final ItemGroup OTHER_GROUP = FabricItemGroupBuilder.create(
        new Identifier("genshin", "other"))
        .icon(() -> new ItemStack(Items.BOWL))
        .build();
public static final Item FABRIC_ITEM = new Item(new Item.Settings().group(OTHER_GROUP));
@Override
public void onInitialize() {
    Registry.register(Registry.ITEM, new Identifier("fabric_test", "fabric_item"), FABRIC_ITEM);
}

方块和方块状态

方块创建和注册

创建方块

public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.of(Material.METAL).build());

注册方块:

public class ExampleMod implements ModInitializer
{
    public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.of(Material.METAL).build());
    @Override
    public void onInitialize()
    {
        Registry.register(Registry.BLOCK, new Identifier("tutorial", "example_block"), EXAMPLE_BLOCK);
    }
}

注册一个方块物品:

public class ExampleMod implements ModInitializer
{
    public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.of(Material.METAL).build());
    @Override
    public void onInitialize()
    {
     Registry.register(Registry.BLOCK, new Identifier("tutorial", "example_block"), EXAMPLE_BLOCK);
     Registry.register(Registry.ITEM, new Identifier("tutorial", "example_block"), new BlockItem(EXAMPLE_BLOCK, new Item.Settings().group(ItemGroup.MISC)));
    }
}

给方块一个模型

注意

可能你已经注意到了,没有模型的方块在游戏中是个紫黑棋盘格子形式。给方块加模型比给物品加模型稍难点,需要三个文件:Blockstate(方块状态)文件,block model(方块模型)文件,如果这个方块有方块物品,还要有个item model(物品模型)文件,如果你不用原版材质的话,还需要Texture(译注:材质图片,其实正经的应该叫纹理)文件,文件应该放在这些位置:

Blockstate: src/main/resources/assets/tutorial/blockstates/example_block.json
Block Model: src/main/resources/assets/tutorial/models/block/example_block.json
Item Model: src/main/resources/assets/tutorial/models/item/example_block.json
Block Texture: src/main/resources/assets/tutorial/textures/block/example_block.png

方块状态文件决定了方块在不同状态下(blockstate)的模型,因为我们的方块只有一个状态,文件就会像这样简单:

{
    "variants": {
    "": { "model": "tutorial:block/example_block" }
}
}

方块模型文件定义了形状和材质,在这里使用cube_all,让方块的所有面简单地使用同一个材质:

{
    "parent": "block/cube_all",
        "textures": {
    "all": "tutorial:block/example_block"
}
}

大多数情况下,方块和手里拿着的方块看上去一样,这样从方块模型文件派生出一个物品模型文件就可以了:

{
    "parent": "tutorial:block/example_block"
}

(为你的方块画一个texture.png) 现在打开MC,方块已经有材质了!

添加战利品表

破坏方块的时候,方块必须有个战利品表来指定掉落物,假设你为方块注册了一个物品,而且和方块有一样的注册名,那么这个文件 (src/main/resources/data/wikitut/loot_tables/blocks/example_block.json) 会指定产出掉落物

"type": "minecraft:block",
        "pools": [
    {
        "rolls": 1,
            "entries": [
        {
            "type": "minecraft:item",
                "name": "tutorial:example_block"
        }
      ],
        "conditions": [
        {
            "condition": "minecraft:survives_explosion"
        }
      ]
    }
  ]
}

版权信息: 本文采用 知识共享许可协议 CC BY-NC-SA 4.0 许可协议。转载和引用时请注意遵守协议、注明出处!
出处: https://loliurl.club/35.html
作者: heartalborada

评论

  1. Liu
    Windows Edge 90.0.818.66
    3 年前
    2021-5-22 18:33:29

    • 博主
      Liu
      iPhone Safari 14.0
      3 年前
      2021-5-22 23:29:26

      这就离谱

    • 博主
      Liu
      Windows Edge 91.0.864.37
      3 年前
      2021-6-05 18:59:45

      test

发送评论 编辑评论


|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
Christmas
Character
Paimon's Paintings
下一篇