前言
作者IDE使用的是IntelliJ IDEA(感觉挺好用的)
新建项目
新建项目,然后去Github下个Fabric的实例文件
- 如果你的IDE没有gradle集成,在命令行中执行./gradlew genSources命令对IntelliJ IDEA:
./gradlew idea.
- 对Eclipse:
./gradlew eclipse.
- 对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);
}
}
新建物品类方法:
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();
}
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"
}
]
}
]
}
草
这就离谱
test