Skip to content

Commit

Permalink
add debug logging to recipe removals
Browse files Browse the repository at this point in the history
  • Loading branch information
serenibyss committed Aug 9, 2021
1 parent 7f557a9 commit 932be0f
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 13 deletions.
96 changes: 96 additions & 0 deletions src/main/java/gregtech/api/recipes/GTRecipeHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package gregtech.api.recipes;

import gregtech.api.util.GTLog;
import gregtech.common.ConfigHolder;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.FluidStack;

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

@SuppressWarnings("unused")
public class GTRecipeHandler {

/**
* Removes all Recipes matching given inputs and fluid inputs from a given RecipeMap.
* An example of how to use it:
*
* <cr>
* removeRecipesByInputs(RecipeMaps.CHEMICAL_RECIPES,
* new ItemStack[]{
* OreDictUnifier.get(OrePrefix.dust, Materials.SodiumHydroxide, 3)
* },
* new FluidStack[]{
* Materials.HypochlorousAcid.getFluid(1000),
* Materials.AllylChloride.getFluid(1000)
* });
* </cr>
*
* This method also has varargs parameter methods for when there is only ItemStack or FluidStack inputs.
*
* @param map The RecipeMap to search over.
* @param itemInputs The ItemStack[] containing all Recipe item inputs.
* @param fluidInputs The FluidStack[] containing all Recipe fluid inputs.
*
* @return true if a recipe was removed, false otherwise.
*/
public static <R extends RecipeBuilder<R>> boolean removeRecipesByInputs(RecipeMap<R> map, ItemStack[] itemInputs, FluidStack[] fluidInputs) {

List<String> fluidNames = new ArrayList<>();
List<String> itemNames = new ArrayList<>();

List<ItemStack> itemIn = new ArrayList<>();
for (ItemStack s : itemInputs) {
itemIn.add(s);
if(ConfigHolder.debug) {
itemNames.add(String.format("%s x %d", s.getDisplayName(), s.getCount()));
}
}

List<FluidStack> fluidIn = new ArrayList<>();
for (FluidStack s : fluidInputs) {
fluidIn.add(s);
if(ConfigHolder.debug) {
fluidNames.add(String.format("%s x %d", s.getFluid().getName(), s.amount));
}
}

boolean wasRemoved = map.removeRecipe(map.findRecipe(Long.MAX_VALUE, itemIn, fluidIn, Integer.MAX_VALUE, MatchingMode.DEFAULT));
if (ConfigHolder.debug) {
if (wasRemoved)
GTLog.logger.info("Removed Recipe for inputs: Items: {} Fluids: {}", itemNames, fluidNames);
else GTLog.logger.info("Failed to Remove Recipe for inputs: Items: {} Fluids: {}", itemNames, fluidNames);
}
return wasRemoved;
}

public static <R extends RecipeBuilder<R>> boolean removeRecipesByInputs(RecipeMap<R> map, ItemStack... itemInputs) {
return removeRecipesByInputs(map, itemInputs, new FluidStack[0]);
}

public static <R extends RecipeBuilder<R>> boolean removeRecipesByInputs(RecipeMap<R> map, FluidStack... fluidInputs) {
return removeRecipesByInputs(map, new ItemStack[0], fluidInputs);
}

/**
* Removes all Recipes from a given RecipeMap. This method cannot fail at recipe removal, but if called at
* the wrong time during recipe registration, it may be an incomplete or overly-complete recipe removal.
* An example of how to use it:
*
* <cr>
* removeAllRecipes(RecipeMaps.BREWING_RECIPES);
* </cr>
*
* @param map The RecipeMap to clear all recipes from.
*/
public static <R extends RecipeBuilder<R>> void removeAllRecipes(RecipeMap<R> map) {

List<Recipe> recipes = new ArrayList<>(map.getRecipeList());

for (Recipe r : recipes)
map.removeRecipe(r);

if(ConfigHolder.debug)
GTLog.logger.info("Removed all recipes for Recipe Map: {}", map.unlocalizedName);
}
}
74 changes: 61 additions & 13 deletions src/main/java/gregtech/api/recipes/ModHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import gregtech.api.util.GTLog;
import gregtech.api.util.ShapedOreEnergyTransferRecipe;
import gregtech.api.util.world.DummyWorld;
import gregtech.common.ConfigHolder;
import net.minecraft.block.Block;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.inventory.InventoryCrafting;
Expand Down Expand Up @@ -478,28 +479,32 @@ public static boolean removeFurnaceSmelting(ItemStack input) {
RecipeMap.setFoundInvalidRecipe(true);
return false;
}

boolean wasRemoved = false;
for (ItemStack stack : FurnaceRecipes.instance().getSmeltingList().keySet()) {
if (ItemStack.areItemStacksEqual(input, stack)) {
FurnaceRecipes.instance().getSmeltingList().remove(stack);
return true;
wasRemoved = true;
}
}
return false;
}

public static int removeRecipes(Item output) {
return removeRecipes(recipe -> {
ItemStack recipeOutput = recipe.getRecipeOutput();
return !recipeOutput.isEmpty() && recipeOutput.getItem() == output;
});
if (ConfigHolder.debug) {
if (wasRemoved)
GTLog.logger.info("Removed Smelting Recipe for Input: {}", input.getDisplayName());
else GTLog.logger.warn("Failed to Remove Smelting Recipe for Input: {}", input.getDisplayName());
}

return wasRemoved;
}

public static int removeRecipes(ItemStack output) {
return removeRecipes(recipe -> ItemStack.areItemStacksEqual(recipe.getRecipeOutput(), output));
}
int recipesRemoved = removeRecipes(recipe -> ItemStack.areItemStacksEqual(recipe.getRecipeOutput(), output));

public static <R extends IRecipe> int removeRecipes(Class<R> recipeClass) {
return removeRecipes(recipeClass::isInstance);
if (ConfigHolder.debug) {
if (recipesRemoved != 0)
GTLog.logger.info("Removed {} Recipe(s) with Output: {}", recipesRemoved, output.getDisplayName());
else GTLog.logger.warn("Failed to Remove Recipe with Output: {}", output.getDisplayName());
}
return recipesRemoved;
}

public static int removeRecipes(Predicate<IRecipe> predicate) {
Expand All @@ -520,10 +525,53 @@ public static int removeRecipes(Predicate<IRecipe> predicate) {
return recipesRemoved;
}

/**
* Removes a Crafting Table Recipe with the given name.
*
* @param location The ResourceLocation of the Recipe.
* Can also accept a String.
*/
public static void removeRecipeByName(ResourceLocation location) {
if (ConfigHolder.debug) {
String recipeName = location.toString();
if (ForgeRegistries.RECIPES.containsKey(location))
GTLog.logger.info("Removed Recipe with Name: {}", recipeName);
else GTLog.logger.warn("Failed to Remove Recipe with Name: {}", recipeName);
}
ForgeRegistries.RECIPES.register(new DummyRecipe().setRegistryName(location));
}

public static void removeRecipeByName(String recipeName) {
removeRecipeByName(new ResourceLocation(recipeName));
}

/**
* Removes Crafting Table Recipes with a range of names, being {@link GTValues} voltage names.
* An example of how to use it:
*
* <cr>
* removeTieredRecipeByName("gregtech:transformer_", EV, UV);
* </cr>
*
* This will remove recipes with names:
*
* <cr>
* gregtech:transformer_ev
* gregtech:transformer_iv
* gregtech:transformer_luv
* gregtech:transformer_zpm
* gregtech:transformer_uv
* </cr>
*
* @param recipeName The base name of the Recipes to remove.
* @param startTier The starting tier index, inclusive.
* @param endTier The ending tier index, inclusive.
*/
public static void removeTieredRecipeByName(String recipeName, int startTier, int endTier) {
for (int i = startTier; i <= endTier; i++)
removeRecipeByName(String.format("%s%s", recipeName, GTValues.VN[i].toLowerCase()));
}

///////////////////////////////////////////////////
// Get Recipe Output Helpers //
///////////////////////////////////////////////////
Expand Down

0 comments on commit 932be0f

Please sign in to comment.