From 40605f5fa96b679cdad4914dc76dcd5f3a8706a4 Mon Sep 17 00:00:00 2001 From: Zixxl Date: Fri, 3 Jul 2015 17:09:37 +0200 Subject: [PATCH] Continuing work - Fixed bugs in BaseMap-Addition/Removal - Reworked handlers for TE Transposer & Smelter - Added recipe logging for TE Transposer & Smelter --- src/main/java/modtweaker2/Commands.java | 6 + .../java/modtweaker2/helpers/InputHelper.java | 17 +- .../Commands/AppliedEnergisticsLogger.java | 4 +- .../mods/botania/commands/BotaniaLogger.java | 4 +- .../tconstruct/commands/TConstructLogger.java | 4 +- .../mods/thermalexpansion/ThermalHelper.java | 11 +- .../commands/ThermalExpansionLogger.java | 81 +++++ .../thermalexpansion/handlers/Smelter.java | 136 ++++---- .../thermalexpansion/handlers/Transposer.java | 312 ++++++++++-------- .../modtweaker2/utils/BaseListAddition.java | 16 +- .../modtweaker2/utils/BaseMapAddition.java | 38 ++- .../utils/BaseMapModification.java | 2 +- .../modtweaker2/utils/BaseMapRemoval.java | 14 +- 13 files changed, 380 insertions(+), 265 deletions(-) create mode 100644 src/main/java/modtweaker2/mods/thermalexpansion/commands/ThermalExpansionLogger.java diff --git a/src/main/java/modtweaker2/Commands.java b/src/main/java/modtweaker2/Commands.java index 9d10d0e..a412b14 100644 --- a/src/main/java/modtweaker2/Commands.java +++ b/src/main/java/modtweaker2/Commands.java @@ -19,6 +19,7 @@ import modtweaker2.mods.tconstruct.MaterialLogger; import modtweaker2.mods.tconstruct.commands.TConstructLogger; import modtweaker2.mods.thaumcraft.commands.AspectLogger; import modtweaker2.mods.thaumcraft.research.commands.ResearchLogger; +import modtweaker2.mods.thermalexpansion.commands.ThermalExpansionLogger; import modtweaker2.utils.TweakerPlugin; public class Commands { @@ -66,9 +67,14 @@ public class Commands { MineTweakerAPI.server.addMineTweakerCommand("chiselGroups", new String[] { "/minetweaker chiselGroups", " Outputs a list of chisel groups" }, new ChiselGroupLogger()); MineTweakerAPI.server.addMineTweakerCommand("chiselVariations", new String[] { "/minetweaker chiselVariations", "/minetweaker chiselVariations [GROUP]", " Outputs a list of chisel variations" }, new ChiselVariationLogger()); } + if (TweakerPlugin.isLoaded("aura")) { MineTweakerAPI.server.addMineTweakerCommand("auras", new String[] { "/minetweaker auras", "Outputs a list of Aura Types" }, new AuraLogger()); } + + if (TweakerPlugin.isLoaded("ThermalExpansion")) { + MineTweakerAPI.server.addMineTweakerCommand("texpansion", new String[] {"/minetweaker texpansion [FILTER]", " Outputs a list of all Thermal Expansion recipes."}, new ThermalExpansionLogger()); + } } } diff --git a/src/main/java/modtweaker2/helpers/InputHelper.java b/src/main/java/modtweaker2/helpers/InputHelper.java index d4a0e56..81d39cb 100644 --- a/src/main/java/modtweaker2/helpers/InputHelper.java +++ b/src/main/java/modtweaker2/helpers/InputHelper.java @@ -88,14 +88,19 @@ public class InputHelper { } } - public static String getArrayDescription(List objects) { + public static String getArrayDescription(List objects) { StringBuilder sb = new StringBuilder(); - sb.append('['); - for(Object object : objects) { - sb.append(InputHelper.getStackDescription(object)).append(", "); + + if(objects.isEmpty()) { + sb.append("[]"); + } else { + sb.append('['); + for(Object object : objects) { + sb.append(InputHelper.getStackDescription(object)).append(", "); + } + sb.setLength(sb.length() - 2); + sb.append(']'); } - sb.setLength(sb.length() - 2); - sb.append(']'); return sb.toString(); } diff --git a/src/main/java/modtweaker2/mods/appeng/Commands/AppliedEnergisticsLogger.java b/src/main/java/modtweaker2/mods/appeng/Commands/AppliedEnergisticsLogger.java index d166987..da2f6e1 100644 --- a/src/main/java/modtweaker2/mods/appeng/Commands/AppliedEnergisticsLogger.java +++ b/src/main/java/modtweaker2/mods/appeng/Commands/AppliedEnergisticsLogger.java @@ -28,7 +28,9 @@ public class AppliedEnergisticsLogger implements ICommandFunction { List args = StringHelper.toLowerCase(Arrays.asList(arguments)); if(!validArguments.containsAll(args)) { - player.sendChat(MineTweakerImplementationAPI.platform.getMessage("Invalid arguments for command. Valid arguments: " + StringHelper.join(validArguments, ", "))); + if(player != null) { + player.sendChat(MineTweakerImplementationAPI.platform.getMessage("Invalid arguments for command. Valid arguments: " + StringHelper.join(validArguments, ", "))); + } } else { if(args.isEmpty() || args.contains("grinder")) { for(IGrinderEntry recipe : AEApi.instance().registries().grinder().getRecipes()) { diff --git a/src/main/java/modtweaker2/mods/botania/commands/BotaniaLogger.java b/src/main/java/modtweaker2/mods/botania/commands/BotaniaLogger.java index 8efdf50..bc04dab 100644 --- a/src/main/java/modtweaker2/mods/botania/commands/BotaniaLogger.java +++ b/src/main/java/modtweaker2/mods/botania/commands/BotaniaLogger.java @@ -32,7 +32,9 @@ public class BotaniaLogger implements ICommandFunction { List args = StringHelper.toLowerCase(Arrays.asList(arguments)); if(!validArguments.containsAll(args)) { - player.sendChat(MineTweakerImplementationAPI.platform.getMessage("Invalid arguments for command. Valid arguments: " + StringHelper.join(validArguments, ", "))); + if(player != null) { + player.sendChat(MineTweakerImplementationAPI.platform.getMessage("Invalid arguments for command. Valid arguments: " + StringHelper.join(validArguments, ", "))); + } } else { if(args.isEmpty() || args.contains("apothecary")) { for(RecipePetals recipe : BotaniaAPI.petalRecipes) { diff --git a/src/main/java/modtweaker2/mods/tconstruct/commands/TConstructLogger.java b/src/main/java/modtweaker2/mods/tconstruct/commands/TConstructLogger.java index 57d505e..5080a06 100644 --- a/src/main/java/modtweaker2/mods/tconstruct/commands/TConstructLogger.java +++ b/src/main/java/modtweaker2/mods/tconstruct/commands/TConstructLogger.java @@ -35,7 +35,9 @@ public class TConstructLogger implements ICommandFunction { List args = StringHelper.toLowerCase(Arrays.asList(arguments)); if(!validArguments.containsAll(args)) { - player.sendChat(MineTweakerImplementationAPI.platform.getMessage("Invalid arguments for command. Valid arguments: " + StringHelper.join(validArguments, ", "))); + if(player != null) { + player.sendChat(MineTweakerImplementationAPI.platform.getMessage("Invalid arguments for command. Valid arguments: " + StringHelper.join(validArguments, ", "))); + } } else { if(args.isEmpty() || args.contains("casting")) { for(CastingRecipe recipe : TConstructHelper.basinCasting) { diff --git a/src/main/java/modtweaker2/mods/thermalexpansion/ThermalHelper.java b/src/main/java/modtweaker2/mods/thermalexpansion/ThermalHelper.java index 2afd17b..fe36bf3 100644 --- a/src/main/java/modtweaker2/mods/thermalexpansion/ThermalHelper.java +++ b/src/main/java/modtweaker2/mods/thermalexpansion/ThermalHelper.java @@ -11,15 +11,16 @@ import java.util.Set; import net.minecraft.item.ItemStack; import net.minecraftforge.fluids.FluidStack; import cofh.lib.inventory.ComparableItemStackSafe; +import cofh.thermalexpansion.util.crafting.SmelterManager.ComparableItemStackSmelter; import cofh.thermalexpansion.util.crafting.SmelterManager.RecipeSmelter; import cofh.thermalexpansion.util.crafting.TransposerManager.RecipeTransposer; public class ThermalHelper { - private static Map smelter; - public static Set smelterValid; + private static Map, RecipeSmelter> smelter; + public static Set smelterValid; private static Map crucible; - private static Map transposerFill; + private static Map, RecipeTransposer> transposerFill; private static Map transposerEmpty; public static Set transposerValid; @@ -34,7 +35,7 @@ public class ThermalHelper { } /** Need to perform this reflection on the fly as the map is ALWAYS changing, thanks to the way that te handles stuff */ - public static Map getSmelterMap() { + public static Map, RecipeSmelter> getSmelterMap() { try { smelter = getStaticObject(Class.forName("cofh.thermalexpansion.util.crafting.SmelterManager"), "recipeMap"); smelterValid = getStaticObject(Class.forName("cofh.thermalexpansion.util.crafting.SmelterManager"), "validationSet"); @@ -43,7 +44,7 @@ public class ThermalHelper { return smelter; } - public static Map getFillMap() { + public static Map, RecipeTransposer> getFillMap() { try { transposerFill = getStaticObject(Class.forName("cofh.thermalexpansion.util.crafting.TransposerManager"), "recipeMapFill"); transposerValid = getStaticObject(Class.forName("cofh.thermalexpansion.util.crafting.TransposerManager"), "validationSet"); diff --git a/src/main/java/modtweaker2/mods/thermalexpansion/commands/ThermalExpansionLogger.java b/src/main/java/modtweaker2/mods/thermalexpansion/commands/ThermalExpansionLogger.java new file mode 100644 index 0000000..1993081 --- /dev/null +++ b/src/main/java/modtweaker2/mods/thermalexpansion/commands/ThermalExpansionLogger.java @@ -0,0 +1,81 @@ +package modtweaker2.mods.thermalexpansion.commands; + +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; + +import minetweaker.MineTweakerAPI; +import minetweaker.MineTweakerImplementationAPI; +import minetweaker.api.player.IPlayer; +import minetweaker.api.server.ICommandFunction; +import modtweaker2.helpers.InputHelper; +import modtweaker2.helpers.StringHelper; +import modtweaker2.mods.thermalexpansion.ThermalHelper; +import cofh.thermalexpansion.util.crafting.SmelterManager.RecipeSmelter; +import cofh.thermalexpansion.util.crafting.TransposerManager.RecipeTransposer; + +public class ThermalExpansionLogger implements ICommandFunction { + private static final List validArguments = new LinkedList(); + + static { + validArguments.add("crucible"); + validArguments.add("furnace"); + validArguments.add("pulverizer"); + validArguments.add("sawmill"); + validArguments.add("transposer"); + } + + @Override + public void execute(String[] arguments, IPlayer player) { + List args = StringHelper.toLowerCase(Arrays.asList(arguments)); + + if(!validArguments.containsAll(args)) { + if(player != null) { + player.sendChat(MineTweakerImplementationAPI.platform.getMessage("Invalid arguments for command. Valid arguments: " + StringHelper.join(validArguments, ", "))); + } + } else { + if(args.isEmpty() || args.contains("smelter")) { + for(RecipeSmelter recipe : ThermalHelper.getSmelterMap().values()) { + if(recipe.getSecondaryOutput() != null) { + MineTweakerAPI.logCommand(String.format("mods.thermalexpansion.Smelter.addRecipe(%d, %s, %s, %s, %s, %d);", + recipe.getEnergy(), + InputHelper.getStackDescription(recipe.getPrimaryInput()), + InputHelper.getStackDescription(recipe.getSecondaryInput()), + InputHelper.getStackDescription(recipe.getPrimaryOutput()), + InputHelper.getStackDescription(recipe.getSecondaryOutput()), + recipe.getSecondaryOutputChance())); + } else { + MineTweakerAPI.logCommand(String.format("mods.thermalexpansion.Smelter.addRecipe(%d, %s, %s, %s);", + recipe.getEnergy(), + InputHelper.getStackDescription(recipe.getPrimaryInput()), + InputHelper.getStackDescription(recipe.getSecondaryInput()), + InputHelper.getStackDescription(recipe.getPrimaryOutput()))); + } + } + } + + if(args.isEmpty() || args.contains("transposer")) { + for(RecipeTransposer recipe : ThermalHelper.getFillMap().values()) { + MineTweakerAPI.logCommand(String.format("mods.thermalexpansion.Transposer.addFillRecipe(%d, %s, %s, %s);", + recipe.getEnergy(), + InputHelper.getStackDescription(recipe.getInput()), + InputHelper.getStackDescription(recipe.getOutput()), + InputHelper.getStackDescription(recipe.getFluid()))); + } + + for(RecipeTransposer recipe : ThermalHelper.getExtractMap().values()) { + MineTweakerAPI.logCommand(String.format("mods.thermalexpansion.Transposer.addExtractRecipe(%d, %s, %s, %s, %d);", + recipe.getEnergy(), + InputHelper.getStackDescription(recipe.getInput()), + InputHelper.getStackDescription(recipe.getOutput()), + InputHelper.getStackDescription(recipe.getFluid()), + recipe.getChance())); + } + } + + if (player != null) { + player.sendChat(MineTweakerImplementationAPI.platform.getMessage("List generated; see minetweaker.log in your minecraft dir")); + } + } + } +} diff --git a/src/main/java/modtweaker2/mods/thermalexpansion/handlers/Smelter.java b/src/main/java/modtweaker2/mods/thermalexpansion/handlers/Smelter.java index 44e25c9..7d8bfbf 100644 --- a/src/main/java/modtweaker2/mods/thermalexpansion/handlers/Smelter.java +++ b/src/main/java/modtweaker2/mods/thermalexpansion/handlers/Smelter.java @@ -1,38 +1,34 @@ package modtweaker2.mods.thermalexpansion.handlers; +import static modtweaker2.helpers.InputHelper.toIItemStack; import static modtweaker2.helpers.InputHelper.toStack; +import static modtweaker2.helpers.StackHelper.matches; import java.util.Arrays; +import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; +import modtweaker2.helpers.InputHelper; +import modtweaker2.helpers.LogHelper; import modtweaker2.mods.thermalexpansion.ThermalHelper; -import modtweaker2.utils.BaseMultiModification; +import modtweaker2.utils.BaseMapAddition; +import modtweaker2.utils.BaseMapRemoval; import net.minecraft.item.ItemStack; import stanhebben.zenscript.annotations.ZenClass; import stanhebben.zenscript.annotations.ZenMethod; -import cofh.lib.inventory.ComparableItemStackSafe; +import cofh.thermalexpansion.util.crafting.SmelterManager; +import cofh.thermalexpansion.util.crafting.SmelterManager.ComparableItemStackSmelter; import cofh.thermalexpansion.util.crafting.SmelterManager.RecipeSmelter; @ZenClass("mods.thermalexpansion.Smelter") public class Smelter { - private static boolean removeValidated(ComparableItemStackSafe stack) { - for (Map.Entry entry : ThermalHelper.getSmelterMap().entrySet()) { - if (entry != null && entry.getValue() != null) { - RecipeSmelter recipe = (RecipeSmelter) entry.getValue(); - if (recipe != null) { - if (stack.equals(new ComparableItemStackSafe(recipe.getPrimaryInput()))) { - return false; - } else if (stack.equals(new ComparableItemStackSafe(recipe.getSecondaryInput()))) { - return false; - } - } - } - } - return ThermalHelper.smelterValid.remove(stack); - } + + public static final String name = "Thermal Expansion Smelter"; @ZenMethod public static void addRecipe(int energy, IItemStack input, IItemStack input2, IItemStack output) { @@ -45,94 +41,86 @@ public class Smelter { ItemStack in2 = toStack(input2); ItemStack out1 = toStack(output); ItemStack out2 = toStack(output2); - RecipeSmelter recipe = (RecipeSmelter) ThermalHelper.getTERecipe(ThermalHelper.smelterRecipe, in1, in2, out1, out2, chance, energy); - MineTweakerAPI.apply(new Add(in1, in2, recipe)); + List key = Arrays.asList(new ComparableItemStackSmelter[] { new ComparableItemStackSmelter(in1), new ComparableItemStackSmelter(in2) }); + RecipeSmelter value = (RecipeSmelter) ThermalHelper.getTERecipe(ThermalHelper.smelterRecipe, in1, in2, out1, out2, chance, energy); + + Map, RecipeSmelter> recipes = new HashMap, RecipeSmelter>(); + + recipes.put(key, value); + + MineTweakerAPI.apply(new Add(recipes)); } - private static class Add extends BaseMultiModification { - private final ComparableItemStackSafe input1; - private final ComparableItemStackSafe input2; - private final List key; - private final RecipeSmelter recipe; - - public Add(ItemStack input1, ItemStack input2, RecipeSmelter recipe) { - super("Induction Smelter"); - this.input1 = new ComparableItemStackSafe(input1); - this.input2 = new ComparableItemStackSafe(input2); - this.key = (Arrays.asList(new ComparableItemStackSafe[] { this.input1, this.input2 })); - this.recipe = recipe; + private static class Add extends BaseMapAddition, RecipeSmelter> { + public Add(Map, RecipeSmelter> recipes) { + super(Smelter.name, ThermalHelper.getSmelterMap(), recipes); } @Override public void apply() { - ThermalHelper.getSmelterMap().put(key, recipe); - ThermalHelper.smelterValid.add(input1); - ThermalHelper.smelterValid.add(input2); - } - - @Override - public boolean canUndo() { - return ThermalHelper.getSmelterMap() != null; + map = ThermalHelper.getSmelterMap(); + super.apply(); + SmelterManager.refreshRecipes(); } @Override public void undo() { - ThermalHelper.getSmelterMap().remove(key); - removeValidated(input1); - removeValidated(input2); + map = ThermalHelper.getSmelterMap(); + super.undo(); + SmelterManager.refreshRecipes(); } @Override - public String getRecipeInfo() { - return ((RecipeSmelter) recipe).getPrimaryOutput().getDisplayName(); + public String getRecipeInfo(Entry, RecipeSmelter> recipe) { + return InputHelper.getStackDescription(recipe.getValue().getPrimaryOutput()); } } // /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @ZenMethod - public static void removeRecipe(IItemStack input, IItemStack input2) { - - MineTweakerAPI.apply(new Remove(toStack(input), toStack(input2))); + public static void removeRecipe(IIngredient input1, IIngredient input2) { + Map, RecipeSmelter> recipes = new HashMap, RecipeSmelter>(); + + for(Entry, RecipeSmelter> entry : ThermalHelper.getSmelterMap().entrySet()) { + RecipeSmelter recipe = entry.getValue(); + + if(recipe != null && matches(input1, toIItemStack(recipe.getPrimaryInput())) && matches(input2, toIItemStack(recipe.getSecondaryInput()))) { + recipes.put(entry.getKey(), entry.getValue()); + } + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new Remove(recipes)); + } else { + LogHelper.logWarning(String.format("No %s Recipes found for %s and %s. Command ignored!", Smelter.name, input1.toString(), input2.toString())); + } } - private static class Remove extends BaseMultiModification { - private final ComparableItemStackSafe input1; - private final ComparableItemStackSafe input2; - private final List key; - private RecipeSmelter recipe; + private static class Remove extends BaseMapRemoval, RecipeSmelter> { - public Remove(ItemStack input1, ItemStack input2) { - super("Induction Smelter"); - this.input1 = new ComparableItemStackSafe(input1); - this.input2 = new ComparableItemStackSafe(input2); - this.key = (Arrays.asList(new ComparableItemStackSafe[] { this.input1, this.input2 })); + public Remove(Map, RecipeSmelter> recipes) { + super(Smelter.name, ThermalHelper.getSmelterMap(), recipes); } @Override public void apply() { - recipe = ThermalHelper.getSmelterMap().get(key); - ThermalHelper.getSmelterMap().remove(key); - removeValidated(input1); - removeValidated(input2); + map = ThermalHelper.getSmelterMap(); + super.apply(); + SmelterManager.refreshRecipes(); } - - @Override - public boolean canUndo() { - return ThermalHelper.getSmelterMap() != null; - } - + @Override public void undo() { - ThermalHelper.getSmelterMap().put(key, recipe); - ThermalHelper.smelterValid.add(input1); - ThermalHelper.smelterValid.add(input2); + map = ThermalHelper.getSmelterMap(); + super.undo(); + SmelterManager.refreshRecipes(); } - @Override - public String getRecipeInfo() { - return input1.toItemStack().getDisplayName() + " + " + input2.toItemStack().getDisplayName(); - } + @Override + public String getRecipeInfo(Entry, RecipeSmelter> recipe) { + return InputHelper.getStackDescription(recipe.getValue().getPrimaryOutput()); + } } } diff --git a/src/main/java/modtweaker2/mods/thermalexpansion/handlers/Transposer.java b/src/main/java/modtweaker2/mods/thermalexpansion/handlers/Transposer.java index 9251116..0196f52 100644 --- a/src/main/java/modtweaker2/mods/thermalexpansion/handlers/Transposer.java +++ b/src/main/java/modtweaker2/mods/thermalexpansion/handlers/Transposer.java @@ -1,182 +1,208 @@ package modtweaker2.mods.thermalexpansion.handlers; import static modtweaker2.helpers.InputHelper.toFluid; +import static modtweaker2.helpers.InputHelper.toIItemStack; +import static modtweaker2.helpers.InputHelper.toILiquidStack; import static modtweaker2.helpers.InputHelper.toStack; +import static modtweaker2.helpers.StackHelper.matches; import java.util.Arrays; +import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; import minetweaker.api.liquid.ILiquidStack; +import modtweaker2.helpers.InputHelper; +import modtweaker2.helpers.LogHelper; import modtweaker2.mods.thermalexpansion.ThermalHelper; -import modtweaker2.utils.BaseUndoable; +import modtweaker2.utils.BaseMapAddition; +import modtweaker2.utils.BaseMapRemoval; import net.minecraft.item.ItemStack; import net.minecraftforge.fluids.FluidStack; import stanhebben.zenscript.annotations.ZenClass; import stanhebben.zenscript.annotations.ZenMethod; import cofh.lib.inventory.ComparableItemStackSafe; +import cofh.thermalexpansion.util.crafting.TransposerManager; import cofh.thermalexpansion.util.crafting.TransposerManager.RecipeTransposer; @ZenClass("mods.thermalexpansion.Transposer") public class Transposer { - private static boolean removeValidated(ComparableItemStackSafe stack) { - // Check if there is a recipe that requires this in the validation set - // for filling - for (Map.Entry entry : ThermalHelper.getFillMap().entrySet()) { - RecipeTransposer recipe = (RecipeTransposer) entry.getValue(); - if (stack.equals(new ComparableItemStackSafe(recipe.getInput()))) { - return false; - } - } - - // Check if there is a recipe that requires this in the validation set - // for extracting - for (Map.Entry entry : ThermalHelper.getExtractMap().entrySet()) { - RecipeTransposer recipe = (RecipeTransposer) entry.getValue(); - if (stack.equals(new ComparableItemStackSafe(recipe.getInput()))) { - return false; - } - } - - return ThermalHelper.transposerValid.remove(stack); - } - + + public static final String nameFill = "Thermal Expansion Transposer (Fill)"; + public static final String nameExtract = "Thermal Expansion Transposer (Extract)"; + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + @ZenMethod public static void addFillRecipe(int energy, IItemStack input, IItemStack output, ILiquidStack liquid) { - addRecipe(energy, input, output, liquid, 100, true); + Map, RecipeTransposer> recipes = new HashMap, RecipeTransposer>(); + ItemStack in = toStack(input); + ItemStack out = toStack(output); + FluidStack fluid = toFluid(liquid); + List key = Arrays.asList(new Integer[] { Integer.valueOf(new ComparableItemStackSafe(in).hashCode()), Integer.valueOf(fluid.fluidID) }); + RecipeTransposer value = (RecipeTransposer) ThermalHelper.getTERecipe(ThermalHelper.transposerRecipe, in, out, fluid, energy, 100); + + recipes.put(key, value); + + MineTweakerAPI.apply(new AddFill(recipes)); } @ZenMethod public static void addExtractRecipe(int energy, IItemStack input, IItemStack output, ILiquidStack liquid, int chance) { - addRecipe(energy, input, output, liquid, chance, false); + Map recipes = new HashMap(); + ItemStack in = toStack(input); + ItemStack out = toStack(output); + FluidStack fluid = toFluid(liquid); + ComparableItemStackSafe key = new ComparableItemStackSafe(in); + RecipeTransposer value = (RecipeTransposer) ThermalHelper.getTERecipe(ThermalHelper.transposerRecipe, in, out, fluid, energy, 100); + + recipes.put(key, value); + + MineTweakerAPI.apply(new AddExtract(recipes)); + } + + private static class AddFill extends BaseMapAddition, RecipeTransposer> { + + protected AddFill(Map, RecipeTransposer> recipes) { + super(Transposer.nameFill, ThermalHelper.getFillMap()); + recipes.putAll(recipes); + } + + @Override + public void apply() { + map = ThermalHelper.getFillMap(); + super.apply(); + TransposerManager.refreshRecipes(); + } + + @Override + public void undo() { + map = ThermalHelper.getFillMap(); + super.undo(); + TransposerManager.refreshRecipes(); + } + + @Override + protected String getRecipeInfo(Entry, RecipeTransposer> recipe) { + return InputHelper.getStackDescription(recipe.getValue().getOutput()); + } + } + + private static class AddExtract extends BaseMapAddition { + + protected AddExtract(Map recipes) { + super(Transposer.nameFill, ThermalHelper.getExtractMap()); + recipes.putAll(recipes); + } + + @Override + public void apply() { + map = ThermalHelper.getExtractMap(); + super.apply(); + TransposerManager.refreshRecipes(); + } + + @Override + public void undo() { + map = ThermalHelper.getExtractMap(); + super.undo(); + TransposerManager.refreshRecipes(); + } + + @Override + protected String getRecipeInfo(Entry recipe) { + return InputHelper.getStackDescription(recipe.getValue().getInput()); + } + } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + @ZenMethod + public static void removeFillRecipe(IIngredient input, IIngredient liquid) { + Map, RecipeTransposer> recipes = new HashMap, RecipeTransposer>(); + for(Entry, RecipeTransposer> recipe : ThermalHelper.getFillMap().entrySet()) { + if(recipe != null && recipe.getValue() != null && matches(input, toIItemStack(recipe.getValue().getInput())) && matches(liquid, toILiquidStack(recipe.getValue().getFluid()))) { + recipes.put(recipe.getKey(), recipe.getValue()); + } + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new RemoveFill(recipes)); + } else { + LogHelper.logWarning(String.format("No %s Recipe found for %s and %s. Command ignored!", nameFill, input.toString(), liquid.toString())); + } } @ZenMethod - public static void addRecipe(int energy, IItemStack input, IItemStack output, ILiquidStack liquid, int chance, boolean isFillRecipe) { - ItemStack in = toStack(input); - ItemStack out = toStack(output); - FluidStack fluid = toFluid(liquid); - RecipeTransposer recipe = (RecipeTransposer) ThermalHelper.getTERecipe(ThermalHelper.transposerRecipe, in, out, fluid, energy, chance); - - MineTweakerAPI.apply(new Add(in, fluid, recipe, isFillRecipe)); + public static void removeExtractRecipe(IIngredient input) { + Map recipes = new HashMap(); + for(Entry recipe : ThermalHelper.getExtractMap().entrySet()) { + if(recipe != null && recipe.getValue() != null && matches(input, toIItemStack(recipe.getValue().getInput()))) { + recipes.put(recipe.getKey(), recipe.getValue()); + } + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new RemoveExtract(recipes)); + } else { + LogHelper.logWarning(String.format("No %s Recipe found for %s. Command ignored!", nameFill, input.toString())); + } + } - private static class Add extends BaseUndoable { - private List key; - private boolean isFillRecipe; - private final ComparableItemStackSafe input; - private final RecipeTransposer recipe; + private static class RemoveFill extends BaseMapRemoval, RecipeTransposer> { - public Add(ItemStack input, FluidStack fluid, RecipeTransposer recipe, boolean isFillRecipe) { - super("Transposer"); - this.isFillRecipe = isFillRecipe; - this.input = new ComparableItemStackSafe(input); + protected RemoveFill(Map, RecipeTransposer> recipes) { + super(nameFill, ThermalHelper.getFillMap(), recipes); + } + + @Override + public void apply() { + map = ThermalHelper.getFillMap(); + super.apply(); + TransposerManager.refreshRecipes(); + } + + @Override + public void undo() { + map = ThermalHelper.getFillMap(); + super.undo(); + TransposerManager.refreshRecipes(); + } - this.recipe = recipe; - if (this.isFillRecipe) { - this.key = Arrays.asList(new Integer[] { Integer.valueOf(new ComparableItemStackSafe(input).hashCode()), Integer.valueOf(fluid.fluidID) }); - } - } - - @Override - public void apply() { - if (isFillRecipe) { - ThermalHelper.getFillMap().put(key, recipe); - } else { - ThermalHelper.getExtractMap().put(input, recipe); - } - - ThermalHelper.transposerValid.add(input); - } - - @Override - public boolean canUndo() { - return input != null; - } - - @Override - public void undo() { - if (isFillRecipe) { - ThermalHelper.getFillMap().remove(key); - } else { - ThermalHelper.getExtractMap().remove(input); - } - - removeValidated(input); - } - - @Override - public String getRecipeInfo() { - return ((RecipeTransposer) recipe).getOutput().getDisplayName(); - } + @Override + protected String getRecipeInfo(Entry, RecipeTransposer> recipe) { + return InputHelper.getStackDescription(recipe.getValue().getOutput()); + } } + + private static class RemoveExtract extends BaseMapRemoval { - // /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - @ZenMethod - public static void removeFillRecipe(IItemStack input, ILiquidStack liquid) { - removeRecipe(input, liquid, true); - } + protected RemoveExtract(Map recipes) { + super(nameExtract, ThermalHelper.getExtractMap(), recipes); + } + + @Override + public void apply() { + map = ThermalHelper.getExtractMap(); + super.apply(); + TransposerManager.refreshRecipes(); + } + + @Override + public void undo() { + map = ThermalHelper.getExtractMap(); + super.undo(); + TransposerManager.refreshRecipes(); + } - @ZenMethod - public static void removeExtractRecipe(IItemStack input) { - removeRecipe(input, null, false); - } - - @ZenMethod - public static void removeRecipe(IItemStack input, ILiquidStack liquid, boolean isFillRecipe) { - - MineTweakerAPI.apply(new Remove(toStack(input), toFluid(liquid), isFillRecipe)); - } - - private static class Remove extends BaseUndoable { - private final boolean isFillRecipe; - private final ComparableItemStackSafe input; - private List key; - private RecipeTransposer recipe; - - public Remove(ItemStack input, FluidStack fluid, boolean isFillRecipe) { - super("Transposer"); - this.isFillRecipe = isFillRecipe; - this.input = new ComparableItemStackSafe(input); - if (this.isFillRecipe) { - this.key = Arrays.asList(new Integer[] { Integer.valueOf(new ComparableItemStackSafe(input).hashCode()), Integer.valueOf(fluid.fluidID) }); - } - } - - @Override - public void apply() { - if (isFillRecipe) { - recipe = ThermalHelper.getFillMap().get(key); - ThermalHelper.getFillMap().remove(key); - } else { - recipe = ThermalHelper.getExtractMap().get(input); - ThermalHelper.getExtractMap().remove(input); - } - - removeValidated(input); - } - - @Override - public boolean canUndo() { - return input != null; - } - - @Override - public void undo() { - if (isFillRecipe) { - ThermalHelper.getFillMap().put(key, recipe); - } else { - ThermalHelper.getExtractMap().put(input, recipe); - } - - ThermalHelper.transposerValid.add(input); - } - - @Override - public String getRecipeInfo() { - return input.toItemStack().getDisplayName(); - } - } + @Override + protected String getRecipeInfo(Entry recipe) { + return InputHelper.getStackDescription(recipe.getValue().getInput()); + } + } } diff --git a/src/main/java/modtweaker2/utils/BaseListAddition.java b/src/main/java/modtweaker2/utils/BaseListAddition.java index db66588..9e16460 100644 --- a/src/main/java/modtweaker2/utils/BaseListAddition.java +++ b/src/main/java/modtweaker2/utils/BaseListAddition.java @@ -25,13 +25,13 @@ public abstract class BaseListAddition extends BaseListModification { for(T recipe : recipes) { if(recipe != null) { - if(this.list.add(recipe)) { + if(list.add(recipe)) { successful.add(recipe); } else { - LogHelper.logError(String.format("Error adding %s Recipe for %s", this.name, this.getRecipeInfo(recipe))); + LogHelper.logError(String.format("Error adding %s Recipe for %s", name, getRecipeInfo(recipe))); } } else { - LogHelper.logError(String.format("Error adding %s Recipe: null object", this.name)); + LogHelper.logError(String.format("Error adding %s Recipe: null object", name)); } } } @@ -44,22 +44,22 @@ public abstract class BaseListAddition extends BaseListModification { for(T recipe : successful) { if(recipe != null) { - if(!this.list.remove(recipe)) { - LogHelper.logError(String.format("Error removing %s Recipe for %s", this.name, this.getRecipeInfo(recipe))); + if(!list.remove(recipe)) { + LogHelper.logError(String.format("Error removing %s Recipe for %s", name, this.getRecipeInfo(recipe))); } } else { - LogHelper.logError(String.format("Error removing %s Recipe: null object", this.name)); + LogHelper.logError(String.format("Error removing %s Recipe: null object", name)); } } } @Override public String describe() { - return String.format("[ModTweaker2] Adding %d %s Recipe(s) for %s", this.recipes.size(), this.name, this.getRecipeInfo()); + return String.format("[ModTweaker2] Adding %d %s Recipe(s) for %s", recipes.size(), name, getRecipeInfo()); } @Override public String describeUndo() { - return String.format("[ModTweaker2] Removing %d %s Recipe(s) for %s", this.recipes.size(), this.name, this.getRecipeInfo()); + return String.format("[ModTweaker2] Removing %d %s Recipe(s) for %s", recipes.size(), name, getRecipeInfo()); } } \ No newline at end of file diff --git a/src/main/java/modtweaker2/utils/BaseMapAddition.java b/src/main/java/modtweaker2/utils/BaseMapAddition.java index 354993a..424ae6f 100644 --- a/src/main/java/modtweaker2/utils/BaseMapAddition.java +++ b/src/main/java/modtweaker2/utils/BaseMapAddition.java @@ -18,7 +18,7 @@ public abstract class BaseMapAddition extends BaseMapModification { protected BaseMapAddition(String name, Map map, Map recipes) { this(name, map); - recipes.putAll(recipes); + this.recipes.putAll(recipes); } @Override @@ -27,37 +27,39 @@ public abstract class BaseMapAddition extends BaseMapModification { return; for(Entry entry : recipes.entrySet()) { - V value = map.put(entry.getKey(), entry.getValue()); - if(value != null) { + K key = entry.getKey(); + V value = entry.getValue(); + V oldValue = map.put(key, value); + + if(oldValue != null) { LogHelper.logWarning(String.format("Overwritten %s Recipe for %s", name, getRecipeInfo( new AbstractMap.SimpleEntry(entry.getKey(), value)))); - overwritten.put(entry.getKey(), value); + overwritten.put(key, oldValue); } - successful.put(entry.getKey(), entry.getValue()); + + successful.put(key, value); } } @Override public void undo() { - if(successful.isEmpty() || overwritten.isEmpty()) + if(successful.isEmpty() && overwritten.isEmpty()) return; - - for(K key : recipes.keySet()) { - map.remove(key); - } - + for(Entry entry : successful.entrySet()) { - V value = map.remove(entry.getKey()); - + K key = entry.getKey(); + V value = map.remove(key); + if(value == null) { LogHelper.logError(String.format("Error removing %s Recipe: null object", name)); } - } for(Entry entry : overwritten.entrySet()) { - V value = map.put(entry.getKey(), entry.getValue()); + K key = entry.getKey(); + V value = entry.getValue(); + V oldValue = map.put(key, value); - if(value != null) { + if(oldValue != null) { LogHelper.logWarning(String.format("Overwritten %s Recipe which should not exist for %s", name, getRecipeInfo(new AbstractMap.SimpleEntry(entry.getKey(), value)))); } } @@ -65,11 +67,11 @@ public abstract class BaseMapAddition extends BaseMapModification { @Override public String describe() { - return String.format("Adding %d %s Recipe(s) for %d", recipes.size(), name, getRecipeInfo()); + return String.format("Adding %d %s Recipe(s) for %s", recipes.size(), name, getRecipeInfo()); } @Override public String describeUndo() { - return String.format("Removing %d %s Recipe(s) for %d", recipes.size(), name, getRecipeInfo()); + return String.format("Removing %d %s Recipe(s) for %s", recipes.size(), name, getRecipeInfo()); } } diff --git a/src/main/java/modtweaker2/utils/BaseMapModification.java b/src/main/java/modtweaker2/utils/BaseMapModification.java index 89d5a7a..156b371 100644 --- a/src/main/java/modtweaker2/utils/BaseMapModification.java +++ b/src/main/java/modtweaker2/utils/BaseMapModification.java @@ -5,7 +5,7 @@ import java.util.Map; import java.util.Map.Entry; public abstract class BaseMapModification extends BaseUndoable { - protected final Map map; + protected Map map; protected final HashMap recipes; protected final HashMap successful; diff --git a/src/main/java/modtweaker2/utils/BaseMapRemoval.java b/src/main/java/modtweaker2/utils/BaseMapRemoval.java index 7385483..dcdc9ce 100644 --- a/src/main/java/modtweaker2/utils/BaseMapRemoval.java +++ b/src/main/java/modtweaker2/utils/BaseMapRemoval.java @@ -25,10 +25,10 @@ public abstract class BaseMapRemoval extends BaseMapModification { return; for(K key : recipes.keySet()) { - V value = map.remove(key); + V oldValue = map.remove(key); - if(value != null) { - successful.put(key, value); + if(oldValue != null) { + successful.put(key, oldValue); } else { LogHelper.logError(String.format("Error removing %s Recipe : null object", name)); } @@ -42,8 +42,8 @@ public abstract class BaseMapRemoval extends BaseMapModification { for(Entry entry : successful.entrySet()) { if(entry != null) { - V value = map.put(entry.getKey(), entry.getValue()); - if(value != null) { + V oldValue = map.put(entry.getKey(), entry.getValue()); + if(oldValue != null) { LogHelper.logWarning(String.format("Overwritten %s Recipe for %s while restoring.", name, getRecipeInfo(entry))); } } @@ -52,11 +52,11 @@ public abstract class BaseMapRemoval extends BaseMapModification { @Override public String describe() { - return String.format("[ModTweaker2] Removing %d %s Recipe(s) for %d", this.recipes.size(), this.name); + return String.format("[ModTweaker2] Removing %d %s Recipe(s) for %s", recipes.size(), name, getRecipeInfo()); } @Override public String describeUndo() { - return String.format("[ModTweaker2] Restoring %d %s Recipe(s) for %d", this.recipes.size(), this.name); + return String.format("[ModTweaker2] Restoring %d %s Recipe(s) for %s", recipes.size(), name, getRecipeInfo()); } }