package net.anvilcraft.anvillib.recipe; import java.util.HashMap; import java.util.Map; import java.util.Optional; import java.util.function.Function; import java.util.function.Predicate; import net.minecraft.recipe.Recipe; import net.minecraft.recipe.RecipeType; import net.minecraft.util.Identifier; public class RecipesEvent { public Map, Map>> recipes; public Map> recipesById; public RecipesEvent( Map, Map>> recipes, Map> recipesById ) { this.recipes = recipes; this.recipesById = recipesById; } public void registerRecipe(Recipe recipe) { if (!this.recipes.containsKey(recipe.getType())) this.recipes.put(recipe.getType(), new HashMap<>()); this.recipes.get(recipe.getType()).put(recipe.getId(), recipe); this.recipesById.put(recipe.getId(), recipe); } public Optional> removeRecipeID(Identifier id) { if (this.recipesById.containsKey(id)) { return Optional.of( this.recipes.get(this.recipesById.remove(id).getType()).remove(id) ); } return Optional.empty(); } public void removeRecipesMatching(Predicate> p) { var iter = this.recipesById.entrySet().iterator(); while (iter.hasNext()) { var entry = iter.next(); if (p.test(entry.getValue())) { iter.remove(); this.recipes.get(entry.getValue().getType()).remove(entry.getKey()); } } } public void mapRecipes(IRecipeMapper mapper) { var iter = this.recipesById.entrySet().iterator(); while (iter.hasNext()) { var entry = iter.next(); if (mapper.shouldMap(entry.getValue())) { var mapped = mapper.apply(entry.getValue()); if (mapped != entry.getValue()) { iter.remove(); this.recipes.get(entry.getValue().getType()).remove(entry.getKey()); this.registerRecipe(mapped); } } } } public void mapRecipeID(Identifier id, Function, Recipe> func) { var recipe = this.recipesById.get(id); if (recipe != null) { var mapped = func.apply(recipe); if (recipe != mapped) { this.removeRecipeID(id); this.registerRecipe(mapped); } } } }