Skip to content

Commit

Permalink
Merge remote-tracking branch 'Taskeren/recipemap-callback' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Dream-Master committed Feb 13, 2025
2 parents e24b520 + 7d9439d commit c045b91
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/main/java/gregtech/api/recipe/RecipeMapBackend.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -75,6 +76,22 @@ public class RecipeMapBackend {
*/
protected final RecipeMapBackendProperties properties;

/**
* Called when a recipe is added.
*
* @see #watchRecipeAdded(Consumer)
*/
@Nullable
protected Consumer<GTRecipe> recipeAddedCallback;

/**
* Called when a {@link GTRecipeBuilder} is added.
*
* @see #watchRecipeBuilderAdded(Consumer)
*/
@Nullable
protected Consumer<GTRecipeBuilder> recipeBuilderAddedCallback;

public RecipeMapBackend(RecipeMapBackendPropertiesBuilder propertiesBuilder) {
this.properties = propertiesBuilder.build();
GregTechAPI.itemStackMultiMaps.add(itemIndex);
Expand Down Expand Up @@ -137,6 +154,9 @@ public GTRecipe compileRecipe(GTRecipe recipe) {
}
recipesByCategory.computeIfAbsent(recipe.getRecipeCategory(), v -> new ArrayList<>())
.add(recipe);
if (recipeAddedCallback != null) {
recipeAddedCallback.accept(recipe);
}
for (FluidStack fluid : recipe.mFluidInputs) {
if (fluid == null) continue;
fluidIndex.put(
Expand Down Expand Up @@ -171,6 +191,9 @@ protected GTRecipe addToItemMap(GTRecipe recipe) {
* Builds recipe from supplied recipe builder and adds it.
*/
protected Collection<GTRecipe> doAdd(GTRecipeBuilder builder) {
if (recipeBuilderAddedCallback != null) {
recipeBuilderAddedCallback.accept(builder);
}
Iterable<? extends GTRecipe> recipes = properties.recipeEmitter.apply(builder);
Collection<GTRecipe> ret = new ArrayList<>();
for (GTRecipe recipe : recipes) {
Expand Down Expand Up @@ -306,6 +329,26 @@ public boolean containsInput(Fluid fluid) {
return fluidIndex.containsKey(fluid.getName());
}

/**
* Calls the given callback when a recipe is added.
*
* @param callback the callback
*/
public void watchRecipeAdded(Consumer<GTRecipe> callback) {
Consumer<GTRecipe> current = this.recipeAddedCallback;
this.recipeAddedCallback = current != null ? current.andThen(callback) : callback;
}

/**
* Calls the given callback when a {@link GTRecipeBuilder} is added.
*
* @param callback the callback
*/
public void watchRecipeBuilderAdded(Consumer<GTRecipeBuilder> callback) {
Consumer<GTRecipeBuilder> current = this.recipeBuilderAddedCallback;
this.recipeBuilderAddedCallback = current != null ? current.andThen(callback) : callback;
}

// region find recipe

/**
Expand Down

0 comments on commit c045b91

Please sign in to comment.