zh_cn:tutorial:datagen_model
This is an old revision of the document!
模型生成
你可以通过 FabricModelProvider
类轻松地生成方块(包括动态方块属性)和物品模型。
开始之前,我们创建一个类来继承(extends) FabricModelProvider
,然后像这样在数据根入口(datagen entrypoint)注册它:
private static class MyModelGenerator extends FabricModelProvider { private MyModelGenerator(FabricDataGenerator generator) { super(generator); } @Override public void generateBlockStateModels(BlockStateModelGenerator blockStateModelGenerator) { // ... } @Override public void generateItemModels(ItemModelGenerator itemModelGenerator) { // ... } } // ... @Override public void onInitializeDataGenerator(FabricDataGenerator fabricDataGenerator) { // ... fabricDataGenerator.addProvider(MyModelGenerator::new); // ... }
添加方块模型
BlockStateModelGenerator
类包含了很多创建方块模型的方法。在本例中,我们将会创建一个每个面都相同的方块。同时作为 minecraft:block/cube_all
出现在 JSON 中。
public static Block SIMPLE_BLOCK = Registry.register(Registry.BLOCK, new Identifier("mymod", "simple_block"), new Block(...)); public static BlockItem SIMPLE_BLOCK_ITEM = Registry.register(Registry.ITEM, ..., new BlockItem(SIMPLE_BLOCK, ...)); // ... @Override public void generateBlockStateModels(BlockStateModelGenerator blockStateModelGenerator) { blockStateModelGenerator.registerSimpleCubeAll(SIMPLE_BLOCK); }
因为 SIMPLE_BLOCK
的 BlockItem
已经存在并且已经注册,所以一个项目模型也会自动生成,作为块模型的父级。这可以在 generateItemModels
方法中重写。
严格验证
默认情况下,如果运行没有为所有属于已处理 mods 的块生成块状态,数据生成将报错。Fabric API 允许禁用该功能,为此,请编辑你的 build.gradle
文件,删除 -Dfabric-api.datagen.strict-validation
,VM 参数将会来自 loom{}
块。
添加物品模型
ItemModelGenerator
包含了多种创建物品模型的方法。
在这个例子中,我们重写上一个例子中的 SIMPLE_BLOCK
方块模型生成的项目模型。我们将从 SIMPLE_BLOCK_ITEM
生成一个项目模型。
public static Block SIMPLE_BLOCK = Registry.register(Registry.BLOCK, new Identifier("mymod", "simple_block"), new Block(...)); public static BlockItem SIMPLE_BLOCK_ITEM = Registry.register(Registry.ITEM, ..., new BlockItem(SIMPLE_BLOCK, ...)); // ... @Override public void generateItemModels(ItemModelGenerator itemModelGenerator) { itemModelGenerator.register(SIMPLE_BLOCK_ITEM, Models.GENERATED); }
正在施工中
定向块的数据生成
快速警告:这将非常复杂!!!
在本例中,我们将为 MACHINE_BLOCK
生成定向块。
首先,添加方块自身并且注册它。
// 在 Tutorial 类里 (或者你自己的 mod 的初始化类中) public static final Block MACHINE_BLOCK = new Block(FabricBlockSettings.copy(Blocks.BLAST_FURNACE)); @Override public void onInitialize() { Registry.register(Registry.BLOCK, new Identifier("tutorial", "machine"), MACHINE_BLOCK); }
现在我们成功地注册好好了方块,让我们进入重点!
private static class MyModelGenerator extends FabricModelProvider { private MyModelGenerator(FabricDataGenerator generator) { super(generator); } @Override public void generateBlockStateModels(BlockStateModelGenerator blockStateModelGenerator) { // ... blockStateModelGenerator.blockStateCollector.accept(MultipartBlockStateSupplier.create(Tutorial.MACHINE_BLOCK) .with(When.create().set(Properties.HORIZONTAL_FACING, Direction.NORTH), BlockStateVariant.create().put(VariantSettings.X, VariantSettings.Rotation.X))); } @Override public void generateItemModels(ItemModelGenerator itemModelGenerator) { // ... } }
zh_cn/tutorial/datagen_model.1676647256.txt.gz · Last modified: 2023/02/17 15:20 by hanatomizu