From 99571cafba7c3d474738394b28e640b4e09554c0 Mon Sep 17 00:00:00 2001 From: KnightMiner Date: Mon, 3 Aug 2020 00:32:59 -0500 Subject: [PATCH] Port two data gen utilities from Inspirations * Utility for adding config to recipes * Utility for getting names and generic conditions in recipes --- .../{ => data}/AbstractRecipeBuilder.java | 2 +- .../recipe/data/ConsumerWrapperBuilder.java | 113 ++++++++++++++++++ .../recipe/data/IRecipeBuilderUtils.java | 92 ++++++++++++++ .../mantle/recipe/data/package-info.java | 7 ++ 4 files changed, 213 insertions(+), 1 deletion(-) rename src/main/java/slimeknights/mantle/recipe/{ => data}/AbstractRecipeBuilder.java (98%) create mode 100644 src/main/java/slimeknights/mantle/recipe/data/ConsumerWrapperBuilder.java create mode 100644 src/main/java/slimeknights/mantle/recipe/data/IRecipeBuilderUtils.java create mode 100644 src/main/java/slimeknights/mantle/recipe/data/package-info.java diff --git a/src/main/java/slimeknights/mantle/recipe/AbstractRecipeBuilder.java b/src/main/java/slimeknights/mantle/recipe/data/AbstractRecipeBuilder.java similarity index 98% rename from src/main/java/slimeknights/mantle/recipe/AbstractRecipeBuilder.java rename to src/main/java/slimeknights/mantle/recipe/data/AbstractRecipeBuilder.java index 6304927..fd3fa8b 100644 --- a/src/main/java/slimeknights/mantle/recipe/AbstractRecipeBuilder.java +++ b/src/main/java/slimeknights/mantle/recipe/data/AbstractRecipeBuilder.java @@ -1,4 +1,4 @@ -package slimeknights.mantle.recipe; +package slimeknights.mantle.recipe.data; import net.minecraft.advancements.Advancement; import net.minecraft.advancements.AdvancementRewards; diff --git a/src/main/java/slimeknights/mantle/recipe/data/ConsumerWrapperBuilder.java b/src/main/java/slimeknights/mantle/recipe/data/ConsumerWrapperBuilder.java new file mode 100644 index 0000000..f1dacec --- /dev/null +++ b/src/main/java/slimeknights/mantle/recipe/data/ConsumerWrapperBuilder.java @@ -0,0 +1,113 @@ +package slimeknights.mantle.recipe.data; + +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import net.minecraft.data.IFinishedRecipe; +import net.minecraft.item.crafting.IRecipeSerializer; +import net.minecraft.util.ResourceLocation; +import net.minecraftforge.common.crafting.CraftingHelper; +import net.minecraftforge.common.crafting.conditions.ICondition; + +import javax.annotation.Nullable; +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; + +/** + * Builds a recipe consumer wrapper, which adds some extra properties to wrap the result of another recipe + */ +public class ConsumerWrapperBuilder { + private final List conditions = new ArrayList<>(); + @Nullable + private final IRecipeSerializer override; + + private ConsumerWrapperBuilder(@Nullable IRecipeSerializer override) { + this.override = override; + } + + /** + * Creates a wrapper builder with the default serializer + * @return Default serializer builder + */ + public static ConsumerWrapperBuilder wrap() { + return new ConsumerWrapperBuilder(null); + } + + /** + * Creates a wrapper builder with a serializer override + * @param override Serializer override + * @return Default serializer builder + */ + public static ConsumerWrapperBuilder wrap(IRecipeSerializer override) { + return new ConsumerWrapperBuilder(override); + } + + /** + * Adds a conditional to the consumer + * @param condition Condition to add + * @return Added condition + */ + public ConsumerWrapperBuilder addCondition(ICondition condition) { + conditions.add(condition); + return this; + } + + /** + * Builds the consumer for the wrapper builder + * @param consumer Base consumer + * @return Built wrapper consumer + */ + public Consumer build(Consumer consumer) { + return (recipe) -> consumer.accept(new Wrapped(recipe, conditions, override)); + } + + private static class Wrapped implements IFinishedRecipe { + private final IFinishedRecipe original; + private final List conditions; + @Nullable + private final IRecipeSerializer override; + + private Wrapped(IFinishedRecipe original, List conditions, @Nullable IRecipeSerializer override) { + this.original = original; + this.conditions = conditions; + this.override = override; + } + + @Override + public void serialize(JsonObject json) { + // add conditions on top + JsonArray conditionsArray = new JsonArray(); + for (ICondition condition : conditions) { + conditionsArray.add(CraftingHelper.serialize(condition)); + } + json.add("conditions", conditionsArray); + // serialize the normal recipe + original.serialize(json); + } + + @Override + public ResourceLocation getID() { + return original.getID(); + } + + @Override + public IRecipeSerializer getSerializer() { + if (override != null) { + return override; + } + return original.getSerializer(); + } + + @Nullable + @Override + public JsonObject getAdvancementJson() { + return original.getAdvancementJson(); + } + + @Nullable + @Override + public ResourceLocation getAdvancementID() { + return original.getAdvancementID(); + } + } +} diff --git a/src/main/java/slimeknights/mantle/recipe/data/IRecipeBuilderUtils.java b/src/main/java/slimeknights/mantle/recipe/data/IRecipeBuilderUtils.java new file mode 100644 index 0000000..b85bd8e --- /dev/null +++ b/src/main/java/slimeknights/mantle/recipe/data/IRecipeBuilderUtils.java @@ -0,0 +1,92 @@ +package slimeknights.mantle.recipe.data; + +import net.minecraft.data.IFinishedRecipe; +import net.minecraft.util.IItemProvider; +import net.minecraft.util.ResourceLocation; +import net.minecraftforge.common.crafting.conditions.ICondition; + +import javax.annotation.Nullable; +import java.util.Objects; +import java.util.function.Consumer; + +/** + * Utilities to help in the creation of recipes + */ +@SuppressWarnings("unused") +public interface IRecipeBuilderUtils { + /** + * Gets the mod ID for this recipe builder + * @return Mod ID + */ + String getModId(); + + /** + * Gets the base recipe consumer + * @return Base recipe consumer + */ + Consumer getConsumer(); + + /** + * Gets the base condition for the condition utility + */ + @Nullable + default ICondition baseCondition() { + return null; + } + + /** + * Gets a resource location under the Inspirations mod ID + * @param name Resource path + * @return Resource location for Inspirations + */ + default ResourceLocation resource(String name) { + return new ResourceLocation(getModId(), name); + } + + /** + * Gets a resource location string for the given path + * @param name Resource path + * @return Resource location string Inspirations + */ + default String resourceName(String name) { + return String.format("%s:%s", getModId(), name); + } + + /** + * Prefixes an items resource location with the given folder + * @param item Item to fetch resource location from + * @param prefix Name to prefix location with + * @return Prefixed resource location + */ + default ResourceLocation prefix(IItemProvider item, String prefix) { + return resource(prefix + Objects.requireNonNull(item.asItem().getRegistryName()).getPath()); + } + + /** + * Wraps an items resource location with the given folder and suffix + * @param item Item to fetch resource location from + * @param prefix Name to prefix location with + * @param suffix Suffix for location + * @return Prefixed resource location + */ + default ResourceLocation wrap(IItemProvider item, String prefix, String suffix) { + return resource(prefix + Objects.requireNonNull(item.asItem().getRegistryName()).getPath() + suffix); + } + + /** + * Gets a consumer with the given condition, plus the module condition + * @param conditions Conditions to add + * @return Consumer with condition + */ + default Consumer withCondition(ICondition... conditions) { + ConsumerWrapperBuilder builder = ConsumerWrapperBuilder.wrap(); + ICondition base = baseCondition(); + if (base != null) { + builder.addCondition(base); + } + for (ICondition condition : conditions) { + builder.addCondition(condition); + } + return builder.build(Objects.requireNonNull(getConsumer())); + } +} diff --git a/src/main/java/slimeknights/mantle/recipe/data/package-info.java b/src/main/java/slimeknights/mantle/recipe/data/package-info.java new file mode 100644 index 0000000..ae6a8db --- /dev/null +++ b/src/main/java/slimeknights/mantle/recipe/data/package-info.java @@ -0,0 +1,7 @@ +@ParametersAreNonnullByDefault +@MethodsReturnNonnullByDefault +package slimeknights.mantle.recipe.data; + +import mcp.MethodsReturnNonnullByDefault; + +import javax.annotation.ParametersAreNonnullByDefault;