Flandre923
268 words
1 minutes
04 合成表和数据生成

04 合成表和数据生成#



public class ModRecipe extends RecipeProvider {

    public ModRecipe(PackOutput output, CompletableFuture<HolderLookup.Provider> registries) {
        super(output, registries);
    }

    @Override
    protected void buildRecipes(RecipeOutput recipeOutput) { // 有序合成
        ShapedRecipeBuilder.shaped(RecipeCategory.MISC, ModItems.BREAD_SWORD.get())
                .pattern("B")
                .pattern("B")
                .pattern("S")
                .define('B',Items.BREAD)
                .define('S',Items.STICK)
                .unlockedBy("has_ruby",has(Items.BREAD))
                .save(recipeOutput);
		// 野炊
        cookRecipes(recipeOutput, "campfire_cooking", RecipeSerializer.CAMPFIRE_COOKING_RECIPE, CampfireCookingRecipe::new, 100);
	// 无序合成
        ShapelessRecipeBuilder.shapeless(RecipeCategory.MISC,ModItems.STONE_BALL.get())
                .requires(Items.SNOWBALL)
                .requires(Items.COBBLESTONE)
                .unlockedBy("has_snowball",has(Items.SNOWBALL))
                .save(recipeOutput);

        ShapelessRecipeBuilder.shapeless(RecipeCategory.MISC,ModItems.TNT_BALL.get())
                .requires(Items.SNOWBALL)
                .requires(Items.TNT)
                .unlockedBy("has_snowball",has(Items.TNT))
                .save(recipeOutput);

    }

    protected static <T extends AbstractCookingRecipe> void cookRecipes(
            RecipeOutput recipeOutput, String cookingMethod, RecipeSerializer<T> cookingSerializer, AbstractCookingRecipe.Factory<T> recipeFactory, int cookingTime
    ) {
        simpleCookingRecipe(recipeOutput, cookingMethod, cookingSerializer, recipeFactory, cookingTime, ModItems.BREAD_SWORD, ModItems.BREAD_SWORD_HOT, 0.35F);
        simpleCookingRecipe(recipeOutput, cookingMethod, cookingSerializer, recipeFactory, cookingTime, ModItems.BREAD_SWORD_HOT, ModItems.BREAD_SWORD_VERY_HOT, 0.35F);
    }

    protected static <T extends AbstractCookingRecipe> void simpleCookingRecipe(
            RecipeOutput recipeOutput,
            String cookingMethod,
            RecipeSerializer<T> cookingSerializer,
            AbstractCookingRecipe.Factory<T> recipeFactory,
            int cookingTime,
            ItemLike material,
            ItemLike result,
            float experience
    ) {
        SimpleCookingRecipeBuilder.generic(Ingredient.of(material), RecipeCategory.FOOD, result, experience, cookingTime, cookingSerializer, recipeFactory)
                .unlockedBy(getHasName(material), has(material))
                .save(recipeOutput, ResourceLocation.fromNamespaceAndPath(NeoMafishMod.MODID,getItemName(result) + "_from_" + cookingMethod));
    }
}


// 用于注册数据生成器的类,该类通过EventBusSubscriber注解自动注册到MOD总线上
@EventBusSubscriber(bus = EventBusSubscriber.Bus.MOD, modid = NeoMafishMod.MODID)
public class ModDataGenerator {
    // 订阅GatherDataEvent事件,当数据收集事件触发时执行该方法
    @SubscribeEvent
    public static void register(GatherDataEvent event) {
        DataGenerator generator = event.getGenerator();
        PackOutput output = generator.getPackOutput();
        CompletableFuture<HolderLookup.Provider> lookupProvider = event.getLookupProvider();
        ExistingFileHelper existingFileHelper = event.getExistingFileHelper();
        generator.addProvider(event.includeServer(),new ModRecipe(output,lookupProvider));
    }
}

如何生成json

image无报错情况下就可以生成。

如何查看

image

然后启动游戏进入游戏查看即可。

04 合成表和数据生成
https://fuwari.vercel.app/posts/minecraft1_21_0/04_合成表和数据生成/
Author
Flandre923
Published at
2024-08-24