diff --git a/src/main/java/gregtech/api/recipe/RecipeMapBackend.java b/src/main/java/gregtech/api/recipe/RecipeMapBackend.java index 8c23e2bfb9a..1cfa99673a5 100644 --- a/src/main/java/gregtech/api/recipe/RecipeMapBackend.java +++ b/src/main/java/gregtech/api/recipe/RecipeMapBackend.java @@ -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; @@ -75,6 +76,22 @@ public class RecipeMapBackend { */ protected final RecipeMapBackendProperties properties; + /** + * Called when a recipe is added. + * + * @see #watchRecipeAdded(Consumer) + */ + @Nullable + protected Consumer recipeAddedCallback; + + /** + * Called when a {@link GTRecipeBuilder} is added. + * + * @see #watchRecipeBuilderAdded(Consumer) + */ + @Nullable + protected Consumer recipeBuilderAddedCallback; + public RecipeMapBackend(RecipeMapBackendPropertiesBuilder propertiesBuilder) { this.properties = propertiesBuilder.build(); GregTechAPI.itemStackMultiMaps.add(itemIndex); @@ -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( @@ -171,6 +191,9 @@ protected GTRecipe addToItemMap(GTRecipe recipe) { * Builds recipe from supplied recipe builder and adds it. */ protected Collection doAdd(GTRecipeBuilder builder) { + if (recipeBuilderAddedCallback != null) { + recipeBuilderAddedCallback.accept(builder); + } Iterable recipes = properties.recipeEmitter.apply(builder); Collection ret = new ArrayList<>(); for (GTRecipe recipe : recipes) { @@ -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 callback) { + Consumer 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 callback) { + Consumer current = this.recipeBuilderAddedCallback; + this.recipeBuilderAddedCallback = current != null ? current.andThen(callback) : callback; + } + // region find recipe /**