====== 配方生成 ======
在开始这一节之前,请务必确保您已经了解[[zh_cn:tutorial:datagen_setup|数据生成]]的相关知识,并已经准备好一个 ''DataGenerationEntrypoint'' 的实现类。
接下来我们开始。首先,创建一个继承了 ''FabricRecipeProvider'' 的类,并将其在您的数据生成入口点上注册。示例代码如下:
private static class MyRecipeGenerator extends FabricRecipeProvider {
private MyRecipeGenerator(FabricDataOutput generator) {
super(generator);
}
@Override
protected void generateRecipes(Consumer exporter) {
// ...
}
}
// ...
@Override
public void onInitializeDataGenerator(FabricDataGenerator fabricDataGenerator) {
// ...
fabricDataGenerator.addProvider(MyRecipeGenerator::new);
// ...
}
===== 添加一个无序合成 (Shapeless) 配方 =====
现在您已经准备好了配方生成器,您现在就可以按需调用不同的方法。
在本例中,我们将创建一个无序合成配方,其中的物品 ''EXAMPLE_ITEM'' 由 ''EXAMPLE_BLOCK'' 给出。
public static Block SIMPLE_BLOCK = Registry.register(Registry.BLOCK, new Identifier("mymod", "simple_block"), new Block(...));
public static BlockItem SIMPLE_ITEM = Registry.register(Registry.ITEM, new Identifier("mymod", "simple_item"), new Item(...));
// ...
@Override
protected void generateRecipes(Consumer exporter) {
ShapelessRecipeJsonBuilder.create(RecipeCategory.BUILDING_BLOCKS, SIMPLE_ITEM).input(SIMPLE_BLOCK).criterion(FabricRecipeProvider.hasItem(SIMPLE_ITEM),
FabricRecipeProvider.conditionsFromItem(SIMPLE_ITEM)).criterion(FabricRecipeProvider.hasItem(SIMPLE_BLOCK),
FabricRecipeProvider.conditionsFromItem(SIMPLE_BLOCK)).offerTo(exporter);
}
===== 添加一个有序合成 (Shaped) 配方 =====
在本例中,我们将创建一个有序配方,用 ''EXAMPLE_ITEM'' 和 ''EXAMPLE_BLOCK'' 合成一个石头方块。
public static Block SIMPLE_BLOCK = Registry.register(Registry.BLOCK, new Identifier("mymod", "simple_block"), new Block(...));
public static BlockItem SIMPLE_ITEM = Registry.register(Registry.ITEM, new Identifier("mymod", "simple_item"), new Item(...));
// ...
@Override
protected void generateRecipes(Consumer exporter) {
ShapedRecipeJsonBuilder.create(RecipeCategory.BUILDING_BLOCKS, Blocks.STONE_BLOCK).pattern("bbb").pattern("iii")
.input('b', SIMPLE_BLOCK)
.input('i', SIMPLE_ITEM)
.criterion(FabricRecipeProvider.hasItem(SIMPLE_ITEM),
FabricRecipeProvider.conditionsFromItem(SIMPLE_ITEM))
.criterion(FabricRecipeProvider.hasItem(SIMPLE_BLOCK),
FabricRecipeProvider.conditionsFromItem(SIMPLE_BLOCK))
.offerTo(exporter);
}
===== 添加一个使用熔炉/高炉/烟熏炉的配方 =====
使用熔炉/高炉/烟熏炉的配方处理和普通的合成配方非常相似,不同之处在于需要使用 ''CookingRecipeJsonBuilder'' 进行构建:
public static final Block EXAMPLE_ORE = Registry.register(Registry.BLOCK, new Identifier("mymod", "example_ore"), new Block(...));
public static final Block DEEPSLATE_EXAMPLE_ORE = Registry.register(Registry.BLOCK, new Identifier("mymod", "deepslate_example_ore"), new Block(...));
public static final Item EXAMPLE_ORE_ITEM = Registry.register(Registry.ITEM, new Identifier("mymod", "example_ore"), new BlockItem(DEEPSLATE_EXAMPLE_ORE, ...));
public static final Item DEEPSLATE_EXAMPLE_ORE_ITEM = Registry.register(Registry.ITEM, new Identifier("mymod", "deepslate_example_ore"), new BlockItem(EXAMPLE_ORE, ...));
public static final Item RAW_EXAMPLE = Registry.register(Registry.ITEM, new Identifier("mymod", "raw_example"), new Item(...));
public static final Item EXAMPLE_INGOT = Registry.register(Registry.ITEM, new Identifier("mymod", "example_ingot"), new Item(...));
// 此处也可以使用 List.of(...) 方法,但是看你习惯怎么写 :)
public static final List SMELTABLE_TO_EXAMPLE_INGOT = Util.make(Lists.newArrayList(), list -> {
list.add(EXAMPLE_ORE);
list.add(DEEPSLATE_EXAMPLE_ORE);
list.add(RAW_EXAMPLE);
});
@Override
protected void generateRecipes(Consumer exporter) {
RecipeProvider.offerSmelting(exporter, SMELTABLE_TO_EXAMPLE_INGOT, EXAMPLE_INGOT, 0.45F, 300, "example");
}
感谢访问加宽器 (Access Wideners) 的功劳,我们同时可以在代码中调用 Mojang 在 ''RecipeProvider'' 早已定义好的一系列实用方法,而无需反复造轮子。