Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let recipe builders skip map build actions #2650

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 62 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.HashMap;
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 @@ -887,6 +893,32 @@ 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)) {
GTLog.logger.error("Builder already ignores {}!", buildActionName, new Throwable());
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 +1017,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 +1085,26 @@ public int getDuration() {
return this.recipePropertyStorage.get(CleanroomProperty.getInstance(), null);
}

public boolean ignoresAllBuildActions() {
return ignoreAllBuildActions;
}

/**
* Get all ignored build actions for the recipe map.
* Will return an empty map if none are ignored.
*/
public @NotNull Map<ResourceLocation, RecipeBuildAction<R>> getIgnoredBuildActions() {
if (ignoreAllBuildActions) {
return recipeMap.getBuildActions();
}

if (ignoredBuildActions == null) {
return new HashMap<>();
}

return ignoredBuildActions;
}

@Override
public String toString() {
return new ToStringBuilder(this)
Expand All @@ -1064,6 +1122,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