From c09b02d8c87b1907b2c1a5b381e348b741108935 Mon Sep 17 00:00:00 2001 From: Sollace Date: Sat, 18 May 2024 21:10:10 +0100 Subject: [PATCH] Fix and add farmer's delight cutting table recipes for all the palm and zap wood blocks --- .../recipe/CuttingBoardRecipeJsonBuilder.java | 146 ++++++++++++++++++ .../providers/recipe/URecipeProvider.java | 43 ++++++ .../unicopia/recipes/cutting/hay_fries.json | 2 +- 3 files changed, 190 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/minelittlepony/unicopia/datagen/providers/recipe/CuttingBoardRecipeJsonBuilder.java diff --git a/src/main/java/com/minelittlepony/unicopia/datagen/providers/recipe/CuttingBoardRecipeJsonBuilder.java b/src/main/java/com/minelittlepony/unicopia/datagen/providers/recipe/CuttingBoardRecipeJsonBuilder.java new file mode 100644 index 00000000..6aa9924d --- /dev/null +++ b/src/main/java/com/minelittlepony/unicopia/datagen/providers/recipe/CuttingBoardRecipeJsonBuilder.java @@ -0,0 +1,146 @@ +package com.minelittlepony.unicopia.datagen.providers.recipe; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; +import java.util.function.Supplier; + +import org.spongepowered.include.com.google.common.base.Preconditions; + +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import net.minecraft.advancement.Advancement; +import net.minecraft.advancement.AdvancementRewards; +import net.minecraft.advancement.CriterionMerger; +import net.minecraft.advancement.criterion.CriterionConditions; +import net.minecraft.advancement.criterion.RecipeUnlockedCriterion; +import net.minecraft.data.server.recipe.CraftingRecipeJsonBuilder; +import net.minecraft.data.server.recipe.RecipeJsonProvider; +import net.minecraft.item.Item; +import net.minecraft.item.ItemConvertible; +import net.minecraft.recipe.Ingredient; +import net.minecraft.recipe.RecipeSerializer; +import net.minecraft.registry.Registries; +import net.minecraft.registry.tag.TagKey; +import net.minecraft.util.Identifier; + +public class CuttingBoardRecipeJsonBuilder { + private final Advancement.Builder advancementBuilder = Advancement.Builder.createUntelemetered(); + + private final ItemConvertible output; + private final TagKey tool; + + private final List> results = new ArrayList<>(); + private final List ingredients = new ArrayList<>(); + + public static CuttingBoardRecipeJsonBuilder create(ItemConvertible output, TagKey tool) { + return new CuttingBoardRecipeJsonBuilder(output, tool); + } + + protected CuttingBoardRecipeJsonBuilder(ItemConvertible output, TagKey tool) { + this.output = output; + this.tool = tool; + result(output); + } + + public CuttingBoardRecipeJsonBuilder input(ItemConvertible input) { + ingredients.add(Ingredient.ofItems(input)); + return this; + } + + public CuttingBoardRecipeJsonBuilder result(ItemConvertible result) { + results.add(() -> Registries.ITEM.getId(result.asItem())); + return this; + } + + public CuttingBoardRecipeJsonBuilder result(Identifier result) { + results.add(() -> result); + return this; + } + + public CuttingBoardRecipeJsonBuilder criterion(String name, CriterionConditions condition) { + advancementBuilder.criterion(name, condition); + return this; + } + + public void offerTo(Consumer exporter, Identifier id) { + id = id.withPrefixedPath("cutting/"); + Preconditions.checkState(!advancementBuilder.getCriteria().isEmpty(), "No way of obtaining recipe " + id); + advancementBuilder + .parent(CraftingRecipeJsonBuilder.ROOT) + .criterion("has_the_recipe", RecipeUnlockedCriterion.create(id)) + .rewards(AdvancementRewards.Builder.recipe(id)) + .criteriaMerger(CriterionMerger.OR); + exporter.accept(new JsonProvider(id, id.withPrefixedPath("recipes/"))); + } + + public void offerTo(Consumer exporter) { + offerTo(exporter, Registries.ITEM.getId(output.asItem())); + } + + public void offerTo(Consumer exporter, String recipePath) { + Identifier recipeId = new Identifier(recipePath); + if (recipeId.equals(Registries.ITEM.getId(output.asItem()))) { + throw new IllegalStateException("Recipe " + recipePath + " should remove its 'save' argument as it is equal to default one"); + } + offerTo(exporter, recipeId); + } + + private class JsonProvider implements RecipeJsonProvider { + private final Identifier recipeId; + private final Identifier advancementId; + public JsonProvider(Identifier recipeId, Identifier advancementId) { + this.recipeId = recipeId; + this.advancementId = advancementId; + } + + @Override + public JsonObject toJson() { + JsonObject jsonObject = new JsonObject(); + jsonObject.addProperty("type", "farmersdelight:cutting"); + serialize(jsonObject); + return jsonObject; + } + + @Override + public void serialize(JsonObject json) { + JsonArray ingredientsJson = new JsonArray(); + for (var ingredient : ingredients) { + ingredientsJson.add(ingredient.toJson()); + } + json.add("ingredients", ingredientsJson); + JsonObject toolJson = new JsonObject(); + toolJson.addProperty("type", "farmersdelight:tool"); + toolJson.addProperty("tag", tool.id().toString()); + json.add("tool", toolJson); + JsonArray resultJson = new JsonArray(); + for (var result : results) { + JsonObject o = new JsonObject(); + o.addProperty("item", result.get().toString()); + resultJson.add(o); + } + json.add("result", resultJson); + } + + @Override + public Identifier getRecipeId() { + return recipeId; + } + + @Override + public RecipeSerializer getSerializer() { + return RecipeSerializer.SHAPELESS; + } + + @Override + public JsonObject toAdvancementJson() { + return advancementBuilder.toJson(); + } + + @Override + public Identifier getAdvancementId() { + return advancementId; + } + + } +} diff --git a/src/main/java/com/minelittlepony/unicopia/datagen/providers/recipe/URecipeProvider.java b/src/main/java/com/minelittlepony/unicopia/datagen/providers/recipe/URecipeProvider.java index c6585b49..1534c7ed 100644 --- a/src/main/java/com/minelittlepony/unicopia/datagen/providers/recipe/URecipeProvider.java +++ b/src/main/java/com/minelittlepony/unicopia/datagen/providers/recipe/URecipeProvider.java @@ -2,6 +2,7 @@ package com.minelittlepony.unicopia.datagen.providers.recipe; import java.util.Arrays; import java.util.List; +import java.util.Map; import java.util.function.Consumer; import java.util.stream.Stream; @@ -23,10 +24,12 @@ import com.mojang.datafixers.util.Either; import net.fabricmc.fabric.api.datagen.v1.FabricDataOutput; import net.fabricmc.fabric.api.datagen.v1.provider.FabricRecipeProvider; +import net.fabricmc.fabric.api.resource.conditions.v1.DefaultResourceConditions; import net.fabricmc.fabric.api.tag.convention.v1.ConventionalItemTags; import net.minecraft.advancement.criterion.InventoryChangedCriterion; import net.minecraft.block.Block; import net.minecraft.block.Blocks; +import net.minecraft.data.family.BlockFamily.Variant; import net.minecraft.data.server.recipe.ComplexRecipeJsonBuilder; import net.minecraft.data.server.recipe.RecipeJsonProvider; import net.minecraft.data.server.recipe.RecipeProvider; @@ -43,6 +46,7 @@ import net.minecraft.recipe.book.RecipeCategory; import net.minecraft.registry.Registries; import net.minecraft.registry.tag.ItemTags; import net.minecraft.registry.tag.TagKey; +import net.minecraft.util.Identifier; public class URecipeProvider extends FabricRecipeProvider { private static final List WOOLS = List.of(Items.BLACK_WOOL, Items.BLUE_WOOL, Items.BROWN_WOOL, Items.CYAN_WOOL, Items.GRAY_WOOL, Items.GREEN_WOOL, Items.LIGHT_BLUE_WOOL, Items.LIGHT_GRAY_WOOL, Items.LIME_WOOL, Items.MAGENTA_WOOL, Items.ORANGE_WOOL, Items.PINK_WOOL, Items.PURPLE_WOOL, Items.RED_WOOL, Items.YELLOW_WOOL, Items.WHITE_WOOL); @@ -77,6 +81,9 @@ public class URecipeProvider extends FabricRecipeProvider { .input(ConventionalItemTags.GLASS_BLOCKS) .input(UItems.SUNGLASSES).criterion("has_broken_sunglasses", conditionsFromItem(UItems.BROKEN_SUNGLASSES)) .offerTo(exporter, convertBetween(UItems.SUNGLASSES, UItems.BROKEN_SUNGLASSES)); + + // farmers delight + offerFarmersDelightCuttingRecipes(withConditions(exporter, DefaultResourceConditions.allModsLoaded("farmersdelight"))); } private void generateVanillaRecipeExtensions(Consumer exporter) { @@ -681,6 +688,42 @@ public class URecipeProvider extends FabricRecipeProvider { PatternTemplate.SEVEN_COLOR.offerTo(exporter, UItems.RAINBOW_BED_SHEETS, UItems.RAINBOW_BED_SHEETS, Items.LIGHT_BLUE_WOOL, Items.RED_WOOL, Items.ORANGE_WOOL, Items.YELLOW_WOOL, Items.BLUE_WOOL, Items.GREEN_WOOL, Items.PURPLE_WOOL); } + private void offerFarmersDelightCuttingRecipes(Consumer exporter) { + + // unwaxing + UBlockFamilies.WAXED_ZAP.getVariants().forEach((variant, waxed) -> { + if (variant == Variant.WALL_SIGN) return; + var unwaxed = UBlockFamilies.ZAP.getVariant(variant); + CuttingBoardRecipeJsonBuilder.create(unwaxed, ItemTags.AXES) + .input(waxed).criterion(hasItem(waxed), conditionsFromItem(waxed)) + .result(Items.HONEYCOMB) + .offerTo(exporter, getItemPath(unwaxed) + "_from_waxed"); + }); + List.of(UBlockFamilies.ZAP, UBlockFamilies.PALM).forEach(family -> { + family.getVariants().forEach((variant, block) -> { + if (variant == Variant.WALL_SIGN) return; + CuttingBoardRecipeJsonBuilder.create(family.getBaseBlock(), ItemTags.AXES) + .input(block).criterion(hasItem(block), conditionsFromItem(block)) + .offerTo(exporter, getItemPath(block)); + }); + }); + CuttingBoardRecipeJsonBuilder.create(UBlocks.PALM_PLANKS, ItemTags.AXES) + .input(UBlocks.PALM_HANGING_SIGN).criterion(hasItem(UBlocks.PALM_HANGING_SIGN), conditionsFromItem(UBlocks.PALM_HANGING_SIGN)) + .offerTo(exporter); + + Map.of( + UBlocks.PALM_LOG, UBlocks.STRIPPED_PALM_LOG, + UBlocks.PALM_WOOD, UBlocks.STRIPPED_PALM_WOOD, + UBlocks.ZAP_LOG, UBlocks.STRIPPED_ZAP_LOG, + UBlocks.ZAP_WOOD, UBlocks.STRIPPED_ZAP_WOOD + ).forEach((unstripped, stripped) -> { + CuttingBoardRecipeJsonBuilder.create(stripped, ItemTags.AXES) + .input(unstripped).criterion(hasItem(unstripped), conditionsFromItem(unstripped)) + .result(new Identifier("farmersdelight:tree_bark")) + .offerTo(exporter, convertBetween(stripped, unstripped)); + }); + } + public static void offerCompactingRecipe(Consumer exporter, RecipeCategory category, ItemConvertible output, ItemConvertible input, int resultCount) { offerCompactingRecipe(exporter, category, output, input, hasItem(input), resultCount); } diff --git a/src/main/resources/data/unicopia/recipes/cutting/hay_fries.json b/src/main/resources/data/unicopia/recipes/cutting/hay_fries.json index c0694f55..abb99342 100644 --- a/src/main/resources/data/unicopia/recipes/cutting/hay_fries.json +++ b/src/main/resources/data/unicopia/recipes/cutting/hay_fries.json @@ -7,7 +7,7 @@ ], "tool": { "type": "farmersdelight:tool", - "tag": "c:tools/axes" + "tag": "minecraft:axes" }, "result": [ {