Skip to content

Commit

Permalink
allow multiple build actions per recipemap
Browse files Browse the repository at this point in the history
  • Loading branch information
TechLord22 committed Dec 10, 2023
1 parent 5d64ceb commit bfb172d
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 54 deletions.
17 changes: 17 additions & 0 deletions src/main/java/gregtech/api/recipes/RecipeBuildAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package gregtech.api.recipes;

import org.jetbrains.annotations.NotNull;

@FunctionalInterface
public interface RecipeBuildAction<R extends RecipeBuilder<R>> {

/**
* Process a RecipeBuilder to perform an action with.
* <p>
* <strong>Do not call {@link RecipeBuilder#buildAndRegister()} on the passed builder.</strong>
* It is safe to do so only on other builders, i.e. created through {@link RecipeBuilder#copy()}.
*
* @param builder the builder to utilize
*/
void accept(@NotNull R builder);
}
43 changes: 31 additions & 12 deletions src/main/java/gregtech/api/recipes/RecipeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
import gregtech.api.recipes.chance.output.ChancedOutputLogic;
import gregtech.api.recipes.chance.output.impl.ChancedFluidOutput;
import gregtech.api.recipes.chance.output.impl.ChancedItemOutput;
import gregtech.api.recipes.ingredients.*;
import gregtech.api.recipes.ingredients.GTRecipeFluidInput;
import gregtech.api.recipes.ingredients.GTRecipeInput;
import gregtech.api.recipes.ingredients.GTRecipeItemInput;
import gregtech.api.recipes.ingredients.GTRecipeOreInput;
import gregtech.api.recipes.ingredients.IntCircuitIngredient;
import gregtech.api.recipes.ingredients.nbtmatch.NBTCondition;
import gregtech.api.recipes.ingredients.nbtmatch.NBTMatcher;
import gregtech.api.recipes.recipeproperties.CleanroomProperty;
Expand Down Expand Up @@ -39,11 +43,17 @@
import com.cleanroommc.groovyscript.helper.ingredient.OreDictIngredient;
import crafttweaker.CraftTweakerAPI;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.UnmodifiableView;

import java.util.*;
import java.util.function.Consumer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
* @see Recipe
Expand All @@ -70,9 +80,9 @@ public class RecipeBuilder<R extends RecipeBuilder<R>> {
protected GTRecipeCategory category;
protected boolean isCTRecipe = false;
protected int parallel = 0;
protected Consumer<R> onBuildAction = null;
protected @Nullable @UnmodifiableView List<RecipeBuildAction<R>> onBuildActions = null;
protected EnumValidationResult recipeStatus = EnumValidationResult.VALID;
protected IRecipePropertyStorage recipePropertyStorage = null;
protected @Nullable IRecipePropertyStorage recipePropertyStorage = null;
protected boolean recipePropertyStorageErrored = false;

protected RecipeBuilder() {
Expand Down Expand Up @@ -121,8 +131,10 @@ protected RecipeBuilder(RecipeBuilder<R> recipeBuilder) {
this.EUt = recipeBuilder.EUt;
this.hidden = recipeBuilder.hidden;
this.category = recipeBuilder.category;
this.onBuildAction = recipeBuilder.onBuildAction;
this.recipePropertyStorage = recipeBuilder.recipePropertyStorage;
this.onBuildActions = recipeBuilder.onBuildActions == null ? null :
new ArrayList<>(recipeBuilder.onBuildActions);
this.recipePropertyStorage = recipeBuilder.recipePropertyStorage == null ? null :
recipeBuilder.recipePropertyStorage.copy();
if (this.recipePropertyStorage != null) {
this.recipePropertyStorage = this.recipePropertyStorage.copy();
}
Expand Down Expand Up @@ -886,19 +898,26 @@ protected static String getRequiredString(int max, int actual, @NotNull String t
return out;
}

protected R onBuild(Consumer<R> consumer) {
this.onBuildAction = consumer;
protected R onBuild(@NotNull List<@NotNull RecipeBuildAction<R>> actions) {
this.onBuildActions = actions;
return (R) this;
}

/**
* @deprecated Obsolete. Does not need calling.
*/
@ApiStatus.Obsolete
@ApiStatus.ScheduledForRemoval(inVersion = "2.9")
@Deprecated
protected R invalidateOnBuildAction() {
this.onBuildAction = null;
return (R) this;
}

public void buildAndRegister() {
if (onBuildAction != null) {
onBuildAction.accept((R) this);
if (onBuildActions != null) {
for (RecipeBuildAction<R> action : onBuildActions) {
action.accept((R) this);
}
}
ValidationResult<Recipe> validationResult = build();
recipeMap.addRecipe(validationResult);
Expand Down
40 changes: 30 additions & 10 deletions src/main/java/gregtech/api/recipes/RecipeMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.function.Consumer;
import java.util.function.DoubleSupplier;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -123,9 +121,9 @@ public class RecipeMap<R extends RecipeBuilder<R>> {

private final Map<GTRecipeCategory, List<Recipe>> recipeByCategory = new Object2ObjectOpenHashMap<>();

private Consumer<R> onRecipeBuildAction;
protected SoundEvent sound;
private RecipeMap<?> smallRecipeMap;
private @Nullable List<RecipeBuildAction<R>> recipeBuildActions;
protected @Nullable SoundEvent sound;
private @Nullable RecipeMap<?> smallRecipeMap;

/**
* Create and register new instance of RecipeMap with specified properties. All
Expand All @@ -139,7 +137,7 @@ public class RecipeMap<R extends RecipeBuilder<R>> {
* @param defaultRecipeBuilder the default RecipeBuilder for the RecipeMap
* @param isHidden if the RecipeMap should have a category in JEI
*
* @deprecated {@link #RecipeMap(String, RecipeBuilder, Function, int, int, int, int)}
* @deprecated {@link RecipeMap#RecipeMap(String, R, RecipeMapUIFunction, int, int, int, int)}
*/
@ApiStatus.ScheduledForRemoval(inVersion = "2.9")
@Deprecated
Expand Down Expand Up @@ -168,7 +166,7 @@ public RecipeMap(@NotNull String unlocalizedName,
* @param defaultRecipeBuilder the default RecipeBuilder for the RecipeMap
* @param isHidden if the RecipeMap should have a category in JEI
*
* @deprecated {@link #RecipeMap(String, RecipeBuilder, Function, int, int, int, int)}
* @deprecated {@link RecipeMap#RecipeMap(String, R, RecipeMapUIFunction, int, int, int, int)}
*/
@ApiStatus.ScheduledForRemoval(inVersion = "2.9")
@Deprecated
Expand Down Expand Up @@ -310,11 +308,33 @@ public RecipeMap<R> setChanceFunction(@NotNull ChanceBoostFunction function) {
return this;
}

public RecipeMap<R> onRecipeBuild(Consumer<R> consumer) {
onRecipeBuildAction = consumer;
/**
* Add a recipe build action to be performed upon this RecipeMap's builder's recipe registration.
*
* @param action the action to perform
* @return this
*/
public RecipeMap<R> onRecipeBuild(@NotNull RecipeBuildAction<R> action) {
if (recipeBuildActions == null) {
recipeBuildActions = new ArrayList<>();
}
recipeBuildActions.add(action);
return this;
}

/**
* Overwrite the RecipeMap's build actions with new ones.
* <p>
* <strong>Use with caution</strong>. You probably want {@link #onRecipeBuild(RecipeBuildAction)} instead.
*
* @see #onRecipeBuild(RecipeBuildAction)
*
* @param actions the actions to set
*/
public void setOnBuildActions(@NotNull List<@NotNull RecipeBuildAction<R>> actions) {
this.recipeBuildActions = actions;
}

public RecipeMap<R> allowEmptyOutput() {
this.allowEmptyOutput = true;
return this;
Expand Down Expand Up @@ -1325,7 +1345,7 @@ public String getUnlocalizedName() {
}

public R recipeBuilder() {
return recipeBuilderSample.copy().onBuild(onRecipeBuildAction);
return recipeBuilderSample.copy().onBuild(recipeBuildActions);
}

/**
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/gregtech/api/recipes/RecipeMapBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

import static gregtech.api.recipes.ui.RecipeMapUI.computeOverlayKey;

public class RecipeMapBuilder<B extends RecipeBuilder<B>> {
Expand Down Expand Up @@ -41,6 +44,8 @@ public class RecipeMapBuilder<B extends RecipeBuilder<B>> {
private SoundEvent sound;
private boolean allowEmptyOutputs;

private @Nullable List<RecipeBuildAction<B>> buildActions;

/**
* @param unlocalizedName the name of the recipemap
* @param defaultRecipeBuilder the default recipe builder of the recipemap
Expand Down Expand Up @@ -247,6 +252,20 @@ public RecipeMapBuilder(@NotNull String unlocalizedName, @NotNull B defaultRecip
return this;
}

/**
* Add a recipe build action to be performed upon this RecipeMap's builder's recipe registration.
*
* @param action the action to perform
* @return this
*/
public @NotNull RecipeMapBuilder<B> onBuild(@NotNull RecipeBuildAction<B> action) {
if (buildActions == null) {
buildActions = new ArrayList<>();
}
buildActions.add(action);
return this;
}

/**
* <strong>Do not call this twice. RecipeMapBuilders are not re-usable.</strong>
*
Expand All @@ -260,6 +279,9 @@ public RecipeMapBuilder(@NotNull String unlocalizedName, @NotNull B defaultRecip
if (allowEmptyOutputs) {
recipeMap.allowEmptyOutput();
}
if (buildActions != null) {
recipeMap.setOnBuildActions(buildActions);
}
return recipeMap;
}
}
56 changes: 24 additions & 32 deletions src/main/java/gregtech/api/recipes/RecipeMaps.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,12 @@ public final class RecipeMaps {
.fluidOutputs(1)
.progressBar(GuiTextures.PROGRESS_BAR_ARC_FURNACE)
.sound(GTSoundEvents.ARC)
.build()
.onRecipeBuild(recipeBuilder -> {
recipeBuilder.invalidateOnBuildAction();
.onBuild(recipeBuilder -> {
if (recipeBuilder.getFluidInputs().isEmpty()) {
recipeBuilder.fluidInputs(Materials.Oxygen.getFluid(recipeBuilder.getDuration()));
}
});
})
.build();

/**
* Example:
Expand All @@ -152,9 +151,7 @@ public final class RecipeMaps {
.itemSlotOverlay(GuiTextures.CIRCUIT_OVERLAY, false)
.progressBar(GuiTextures.PROGRESS_BAR_CIRCUIT)
.sound(GTSoundEvents.ASSEMBLER)
.build()
.onRecipeBuild(recipeBuilder -> {
recipeBuilder.invalidateOnBuildAction();
.onBuild(recipeBuilder -> {
var fluidInputs = recipeBuilder.getFluidInputs();
if (fluidInputs.size() == 1 && fluidInputs.get(0).getInputFluidStack().getFluid() ==
Materials.SolderingAlloy.getFluid()) {
Expand All @@ -173,7 +170,8 @@ public final class RecipeMaps {
OreDictUnifier.registerOre(outputStack, info);
}
}
});
})
.build();

/**
* Example:
Expand Down Expand Up @@ -437,21 +435,18 @@ public final class RecipeMaps {
.fluidSlotOverlay(GuiTextures.VIAL_OVERLAY_2, true)
.progressBar(GuiTextures.PROGRESS_BAR_ARROW_MULTIPLE)
.sound(GTValues.FOOLS.get() ? GTSoundEvents.SCIENCE : GTSoundEvents.CHEMICAL_REACTOR)
.build()
.onRecipeBuild(recipeBuilder -> {
recipeBuilder.invalidateOnBuildAction();
RecipeMaps.LARGE_CHEMICAL_RECIPES.recipeBuilder()
.inputs(recipeBuilder.getInputs().toArray(new GTRecipeInput[0]))
.fluidInputs(recipeBuilder.getFluidInputs())
.outputs(recipeBuilder.getOutputs())
.chancedOutputs(recipeBuilder.getChancedOutputs())
.fluidOutputs(recipeBuilder.getFluidOutputs())
.chancedFluidOutputs(recipeBuilder.getChancedFluidOutputs())
.cleanroom(recipeBuilder.getCleanroom())
.duration(recipeBuilder.getDuration())
.EUt(recipeBuilder.getEUt())
.buildAndRegister();
});
.onBuild(recipeBuilder -> RecipeMaps.LARGE_CHEMICAL_RECIPES.recipeBuilder()
.inputs(recipeBuilder.getInputs().toArray(new GTRecipeInput[0]))
.fluidInputs(recipeBuilder.getFluidInputs())
.outputs(recipeBuilder.getOutputs())
.chancedOutputs(recipeBuilder.getChancedOutputs())
.fluidOutputs(recipeBuilder.getFluidOutputs())
.chancedFluidOutputs(recipeBuilder.getChancedFluidOutputs())
.cleanroom(recipeBuilder.getCleanroom())
.duration(recipeBuilder.getDuration())
.EUt(recipeBuilder.getEUt())
.buildAndRegister())
.build();

/**
* Example:
Expand Down Expand Up @@ -489,9 +484,7 @@ public final class RecipeMaps {
.itemSlotOverlay(GuiTextures.CIRCUIT_OVERLAY, false)
.progressBar(GuiTextures.PROGRESS_BAR_CIRCUIT_ASSEMBLER)
.sound(GTSoundEvents.ASSEMBLER)
.build()
.onRecipeBuild(recipeBuilder -> {
recipeBuilder.invalidateOnBuildAction();
.onBuild(recipeBuilder -> {
if (recipeBuilder.getFluidInputs().isEmpty()) {
recipeBuilder.copy()
.fluidInputs(Materials.SolderingAlloy.getFluid(Math.max(1,
Expand All @@ -504,7 +497,8 @@ public final class RecipeMaps {
recipeBuilder.fluidInputs(Materials.Tin.getFluid(Math.max(1, GTValues.L *
recipeBuilder.getSolderMultiplier())));
}
});
})
.build();

/**
* Example:
Expand Down Expand Up @@ -612,11 +606,8 @@ public final class RecipeMaps {
.itemSlotOverlay(GuiTextures.DUST_OVERLAY, true, true)
.progressBar(GuiTextures.PROGRESS_BAR_SLICE)
.sound(GTSoundEvents.CUT)
.build()
.onRecipeBuild(recipeBuilder -> {
recipeBuilder.invalidateOnBuildAction();
.onBuild(recipeBuilder -> {
if (recipeBuilder.getFluidInputs().isEmpty()) {

int duration = recipeBuilder.getDuration();
int eut = recipeBuilder.getEUt();
recipeBuilder
Expand All @@ -642,7 +633,8 @@ public final class RecipeMaps {
.duration(Math.max(1, duration));

}
});
})
.build();

/**
* Examples:
Expand Down

0 comments on commit bfb172d

Please sign in to comment.