Skip to content

Commit 932be0f

Browse files
committed
add debug logging to recipe removals
1 parent 7f557a9 commit 932be0f

File tree

2 files changed

+157
-13
lines changed

2 files changed

+157
-13
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package gregtech.api.recipes;
2+
3+
import gregtech.api.util.GTLog;
4+
import gregtech.common.ConfigHolder;
5+
import net.minecraft.item.ItemStack;
6+
import net.minecraftforge.fluids.FluidStack;
7+
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
11+
@SuppressWarnings("unused")
12+
public class GTRecipeHandler {
13+
14+
/**
15+
* Removes all Recipes matching given inputs and fluid inputs from a given RecipeMap.
16+
* An example of how to use it:
17+
*
18+
* <cr>
19+
* removeRecipesByInputs(RecipeMaps.CHEMICAL_RECIPES,
20+
* new ItemStack[]{
21+
* OreDictUnifier.get(OrePrefix.dust, Materials.SodiumHydroxide, 3)
22+
* },
23+
* new FluidStack[]{
24+
* Materials.HypochlorousAcid.getFluid(1000),
25+
* Materials.AllylChloride.getFluid(1000)
26+
* });
27+
* </cr>
28+
*
29+
* This method also has varargs parameter methods for when there is only ItemStack or FluidStack inputs.
30+
*
31+
* @param map The RecipeMap to search over.
32+
* @param itemInputs The ItemStack[] containing all Recipe item inputs.
33+
* @param fluidInputs The FluidStack[] containing all Recipe fluid inputs.
34+
*
35+
* @return true if a recipe was removed, false otherwise.
36+
*/
37+
public static <R extends RecipeBuilder<R>> boolean removeRecipesByInputs(RecipeMap<R> map, ItemStack[] itemInputs, FluidStack[] fluidInputs) {
38+
39+
List<String> fluidNames = new ArrayList<>();
40+
List<String> itemNames = new ArrayList<>();
41+
42+
List<ItemStack> itemIn = new ArrayList<>();
43+
for (ItemStack s : itemInputs) {
44+
itemIn.add(s);
45+
if(ConfigHolder.debug) {
46+
itemNames.add(String.format("%s x %d", s.getDisplayName(), s.getCount()));
47+
}
48+
}
49+
50+
List<FluidStack> fluidIn = new ArrayList<>();
51+
for (FluidStack s : fluidInputs) {
52+
fluidIn.add(s);
53+
if(ConfigHolder.debug) {
54+
fluidNames.add(String.format("%s x %d", s.getFluid().getName(), s.amount));
55+
}
56+
}
57+
58+
boolean wasRemoved = map.removeRecipe(map.findRecipe(Long.MAX_VALUE, itemIn, fluidIn, Integer.MAX_VALUE, MatchingMode.DEFAULT));
59+
if (ConfigHolder.debug) {
60+
if (wasRemoved)
61+
GTLog.logger.info("Removed Recipe for inputs: Items: {} Fluids: {}", itemNames, fluidNames);
62+
else GTLog.logger.info("Failed to Remove Recipe for inputs: Items: {} Fluids: {}", itemNames, fluidNames);
63+
}
64+
return wasRemoved;
65+
}
66+
67+
public static <R extends RecipeBuilder<R>> boolean removeRecipesByInputs(RecipeMap<R> map, ItemStack... itemInputs) {
68+
return removeRecipesByInputs(map, itemInputs, new FluidStack[0]);
69+
}
70+
71+
public static <R extends RecipeBuilder<R>> boolean removeRecipesByInputs(RecipeMap<R> map, FluidStack... fluidInputs) {
72+
return removeRecipesByInputs(map, new ItemStack[0], fluidInputs);
73+
}
74+
75+
/**
76+
* Removes all Recipes from a given RecipeMap. This method cannot fail at recipe removal, but if called at
77+
* the wrong time during recipe registration, it may be an incomplete or overly-complete recipe removal.
78+
* An example of how to use it:
79+
*
80+
* <cr>
81+
* removeAllRecipes(RecipeMaps.BREWING_RECIPES);
82+
* </cr>
83+
*
84+
* @param map The RecipeMap to clear all recipes from.
85+
*/
86+
public static <R extends RecipeBuilder<R>> void removeAllRecipes(RecipeMap<R> map) {
87+
88+
List<Recipe> recipes = new ArrayList<>(map.getRecipeList());
89+
90+
for (Recipe r : recipes)
91+
map.removeRecipe(r);
92+
93+
if(ConfigHolder.debug)
94+
GTLog.logger.info("Removed all recipes for Recipe Map: {}", map.unlocalizedName);
95+
}
96+
}

src/main/java/gregtech/api/recipes/ModHandler.java

Lines changed: 61 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import gregtech.api.util.GTLog;
1515
import gregtech.api.util.ShapedOreEnergyTransferRecipe;
1616
import gregtech.api.util.world.DummyWorld;
17+
import gregtech.common.ConfigHolder;
1718
import net.minecraft.block.Block;
1819
import net.minecraft.creativetab.CreativeTabs;
1920
import net.minecraft.inventory.InventoryCrafting;
@@ -478,28 +479,32 @@ public static boolean removeFurnaceSmelting(ItemStack input) {
478479
RecipeMap.setFoundInvalidRecipe(true);
479480
return false;
480481
}
482+
483+
boolean wasRemoved = false;
481484
for (ItemStack stack : FurnaceRecipes.instance().getSmeltingList().keySet()) {
482485
if (ItemStack.areItemStacksEqual(input, stack)) {
483486
FurnaceRecipes.instance().getSmeltingList().remove(stack);
484-
return true;
487+
wasRemoved = true;
485488
}
486489
}
487-
return false;
488-
}
489-
490-
public static int removeRecipes(Item output) {
491-
return removeRecipes(recipe -> {
492-
ItemStack recipeOutput = recipe.getRecipeOutput();
493-
return !recipeOutput.isEmpty() && recipeOutput.getItem() == output;
494-
});
490+
if (ConfigHolder.debug) {
491+
if (wasRemoved)
492+
GTLog.logger.info("Removed Smelting Recipe for Input: {}", input.getDisplayName());
493+
else GTLog.logger.warn("Failed to Remove Smelting Recipe for Input: {}", input.getDisplayName());
494+
}
495+
496+
return wasRemoved;
495497
}
496498

497499
public static int removeRecipes(ItemStack output) {
498-
return removeRecipes(recipe -> ItemStack.areItemStacksEqual(recipe.getRecipeOutput(), output));
499-
}
500+
int recipesRemoved = removeRecipes(recipe -> ItemStack.areItemStacksEqual(recipe.getRecipeOutput(), output));
500501

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

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

528+
/**
529+
* Removes a Crafting Table Recipe with the given name.
530+
*
531+
* @param location The ResourceLocation of the Recipe.
532+
* Can also accept a String.
533+
*/
523534
public static void removeRecipeByName(ResourceLocation location) {
535+
if (ConfigHolder.debug) {
536+
String recipeName = location.toString();
537+
if (ForgeRegistries.RECIPES.containsKey(location))
538+
GTLog.logger.info("Removed Recipe with Name: {}", recipeName);
539+
else GTLog.logger.warn("Failed to Remove Recipe with Name: {}", recipeName);
540+
}
524541
ForgeRegistries.RECIPES.register(new DummyRecipe().setRegistryName(location));
525542
}
526543

544+
public static void removeRecipeByName(String recipeName) {
545+
removeRecipeByName(new ResourceLocation(recipeName));
546+
}
547+
548+
/**
549+
* Removes Crafting Table Recipes with a range of names, being {@link GTValues} voltage names.
550+
* An example of how to use it:
551+
*
552+
* <cr>
553+
* removeTieredRecipeByName("gregtech:transformer_", EV, UV);
554+
* </cr>
555+
*
556+
* This will remove recipes with names:
557+
*
558+
* <cr>
559+
* gregtech:transformer_ev
560+
* gregtech:transformer_iv
561+
* gregtech:transformer_luv
562+
* gregtech:transformer_zpm
563+
* gregtech:transformer_uv
564+
* </cr>
565+
*
566+
* @param recipeName The base name of the Recipes to remove.
567+
* @param startTier The starting tier index, inclusive.
568+
* @param endTier The ending tier index, inclusive.
569+
*/
570+
public static void removeTieredRecipeByName(String recipeName, int startTier, int endTier) {
571+
for (int i = startTier; i <= endTier; i++)
572+
removeRecipeByName(String.format("%s%s", recipeName, GTValues.VN[i].toLowerCase()));
573+
}
574+
527575
///////////////////////////////////////////////////
528576
// Get Recipe Output Helpers //
529577
///////////////////////////////////////////////////

0 commit comments

Comments
 (0)