From c0c5b60c15c3c989699538359cae75bbe4fd25d3 Mon Sep 17 00:00:00 2001 From: Zorbatron <46525467+Zorbatron@users.noreply.github.com> Date: Sun, 10 Nov 2024 15:18:32 -0500 Subject: [PATCH] Let recipe builders skip map build actions (#2650) --- .../gregtech/api/recipes/RecipeBuilder.java | 68 ++++++++++++++++++- .../java/gregtech/api/recipes/RecipeMap.java | 4 +- 2 files changed, 68 insertions(+), 4 deletions(-) diff --git a/src/main/java/gregtech/api/recipes/RecipeBuilder.java b/src/main/java/gregtech/api/recipes/RecipeBuilder.java index c267b5f5bb5..d12ec1afb90 100644 --- a/src/main/java/gregtech/api/recipes/RecipeBuilder.java +++ b/src/main/java/gregtech/api/recipes/RecipeBuilder.java @@ -35,6 +35,7 @@ import net.minecraft.block.Block; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; +import net.minecraft.util.ResourceLocation; import net.minecraftforge.fluids.Fluid; import net.minecraftforge.fluids.FluidStack; import net.minecraftforge.fml.common.Optional; @@ -44,6 +45,7 @@ import com.cleanroommc.groovyscript.helper.ingredient.OreDictIngredient; import crafttweaker.CraftTweakerAPI; import it.unimi.dsi.fastutil.ints.IntList; +import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; import org.apache.commons.lang3.builder.ToStringBuilder; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.MustBeInvokedByOverriders; @@ -53,6 +55,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.Collections; import java.util.List; import java.util.Map; @@ -86,6 +89,9 @@ public class RecipeBuilder> { protected RecipePropertyStorage recipePropertyStorage = RecipePropertyStorage.EMPTY; protected boolean recipePropertyStorageErrored = false; + protected boolean ignoreAllBuildActions = false; + protected Map> ignoredBuildActions; + protected RecipeBuilder() { this.inputs = new ArrayList<>(); this.outputs = new ArrayList<>(); @@ -126,6 +132,10 @@ protected RecipeBuilder(RecipeBuilder recipeBuilder) { this.hidden = recipeBuilder.hidden; this.category = recipeBuilder.category; this.recipePropertyStorage = recipeBuilder.recipePropertyStorage.copy(); + this.ignoreAllBuildActions = recipeBuilder.ignoreAllBuildActions; + if (recipeBuilder.ignoredBuildActions != null) { + this.ignoredBuildActions = new Object2ObjectOpenHashMap<>(recipeBuilder.ignoredBuildActions); + } } public R cleanroom(@Nullable CleanroomType cleanroom) { @@ -887,6 +897,31 @@ public R copy() { return (R) new RecipeBuilder<>(this); } + /** + * Only use if you absolutely don't want the recipe to be run through any build actions. + * Instead, you should blacklist specific actions with {@link #ignoreBuildAction(ResourceLocation)} + */ + public R ignoreAllBuildActions() { + this.ignoreAllBuildActions = true; + return (R) this; + } + + public R ignoreBuildAction(ResourceLocation buildActionName) { + if (ignoredBuildActions == null) { + ignoredBuildActions = new Object2ObjectOpenHashMap<>(); + } else if (!recipeMap.getBuildActions().containsKey(buildActionName)) { + GTLog.logger.error("Recipe map {} does not contain build action {}!", recipeMap, buildActionName, + new Throwable()); + return (R) this; + } else if (ignoredBuildActions.containsKey(buildActionName)) { + return (R) this; + } + + ignoredBuildActions.put(buildActionName, recipeMap.getBuildActions().get(buildActionName)); + + return (R) this; + } + public ValidationResult build() { EnumValidationResult result = recipePropertyStorageErrored ? EnumValidationResult.INVALID : validate(); return ValidationResult.newResult(result, new Recipe(inputs, outputs, @@ -985,8 +1020,14 @@ protected R invalidateOnBuildAction() { */ @MustBeInvokedByOverriders public void buildAndRegister() { - for (RecipeBuildAction action : recipeMap.getBuildActions()) { - action.accept((R) this); + if (!ignoreAllBuildActions) { + for (Map.Entry> buildAction : recipeMap.getBuildActions() + .entrySet()) { + if (ignoredBuildActions != null && ignoredBuildActions.containsKey(buildAction.getKey())) { + continue; + } + buildAction.getValue().accept((R) this); + } } ValidationResult validationResult = build(); recipeMap.addRecipe(validationResult); @@ -1047,6 +1088,27 @@ public int getDuration() { return this.recipePropertyStorage.get(CleanroomProperty.getInstance(), null); } + public boolean ignoresAllBuildActions() { + return ignoreAllBuildActions; + } + + /** + * Get all ignored build actions for the recipe map. + * + * @return A map of ignored build actions. + */ + public @NotNull Map> getIgnoredBuildActions() { + if (ignoreAllBuildActions) { + return recipeMap.getBuildActions(); + } + + if (ignoredBuildActions == null) { + return Collections.emptyMap(); + } + + return ignoredBuildActions; + } + @Override public String toString() { return new ToStringBuilder(this) @@ -1064,6 +1126,8 @@ public String toString() { .append("dimensions", getDimensionIDs().toString()) .append("dimensions_blocked", getBlockedDimensionIDs().toString()) .append("recipeStatus", recipeStatus) + .append("ignoresBuildActions", ignoresAllBuildActions()) + .append("ignoredBuildActions", getIgnoredBuildActions()) .toString(); } } diff --git a/src/main/java/gregtech/api/recipes/RecipeMap.java b/src/main/java/gregtech/api/recipes/RecipeMap.java index df5ae2d7d2e..a2c2ef7b1f4 100644 --- a/src/main/java/gregtech/api/recipes/RecipeMap.java +++ b/src/main/java/gregtech/api/recipes/RecipeMap.java @@ -347,8 +347,8 @@ protected void onRecipeBuild(@NotNull Map * @return the build actions for this RecipeMap's default RecipeBuilder */ @ApiStatus.Internal - protected @UnmodifiableView @NotNull Collection<@NotNull RecipeBuildAction> getBuildActions() { - return this.recipeBuildActions.values(); + protected @UnmodifiableView @NotNull Map> getBuildActions() { + return this.recipeBuildActions; } public RecipeMap allowEmptyOutput() {