Skip to content

Commit

Permalink
Let recipe builders skip map build actions (GregTechCEu#2650)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zorbatron authored Nov 10, 2024
1 parent d747c98 commit c0c5b60
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 deletions.
68 changes: 66 additions & 2 deletions src/main/java/gregtech/api/recipes/RecipeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -86,6 +89,9 @@ public class RecipeBuilder<R extends RecipeBuilder<R>> {
protected RecipePropertyStorage recipePropertyStorage = RecipePropertyStorage.EMPTY;
protected boolean recipePropertyStorageErrored = false;

protected boolean ignoreAllBuildActions = false;
protected Map<ResourceLocation, RecipeBuildAction<R>> ignoredBuildActions;

protected RecipeBuilder() {
this.inputs = new ArrayList<>();
this.outputs = new ArrayList<>();
Expand Down Expand Up @@ -126,6 +132,10 @@ protected RecipeBuilder(RecipeBuilder<R> 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) {
Expand Down Expand Up @@ -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<Recipe> build() {
EnumValidationResult result = recipePropertyStorageErrored ? EnumValidationResult.INVALID : validate();
return ValidationResult.newResult(result, new Recipe(inputs, outputs,
Expand Down Expand Up @@ -985,8 +1020,14 @@ protected R invalidateOnBuildAction() {
*/
@MustBeInvokedByOverriders
public void buildAndRegister() {
for (RecipeBuildAction<R> action : recipeMap.getBuildActions()) {
action.accept((R) this);
if (!ignoreAllBuildActions) {
for (Map.Entry<ResourceLocation, RecipeBuildAction<R>> buildAction : recipeMap.getBuildActions()
.entrySet()) {
if (ignoredBuildActions != null && ignoredBuildActions.containsKey(buildAction.getKey())) {
continue;
}
buildAction.getValue().accept((R) this);
}
}
ValidationResult<Recipe> validationResult = build();
recipeMap.addRecipe(validationResult);
Expand Down Expand Up @@ -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<ResourceLocation, RecipeBuildAction<R>> getIgnoredBuildActions() {
if (ignoreAllBuildActions) {
return recipeMap.getBuildActions();
}

if (ignoredBuildActions == null) {
return Collections.emptyMap();
}

return ignoredBuildActions;
}

@Override
public String toString() {
return new ToStringBuilder(this)
Expand All @@ -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();
}
}
4 changes: 2 additions & 2 deletions src/main/java/gregtech/api/recipes/RecipeMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,8 @@ protected void onRecipeBuild(@NotNull Map<ResourceLocation, RecipeBuildAction<R>
* @return the build actions for this RecipeMap's default RecipeBuilder
*/
@ApiStatus.Internal
protected @UnmodifiableView @NotNull Collection<@NotNull RecipeBuildAction<R>> getBuildActions() {
return this.recipeBuildActions.values();
protected @UnmodifiableView @NotNull Map<ResourceLocation, RecipeBuildAction<R>> getBuildActions() {
return this.recipeBuildActions;
}

public RecipeMap<R> allowEmptyOutput() {
Expand Down

0 comments on commit c0c5b60

Please sign in to comment.