diff --git a/build.gradle b/build.gradle index d3e565b..2315f2c 100644 --- a/build.gradle +++ b/build.gradle @@ -17,7 +17,7 @@ buildscript { apply plugin: 'forge' -version = "0.8.3" +version = "0.9.0" group= "modtweaker" // http://maven.apache.org/guides/mini/guide-naming-conventions.html archivesBaseName = "ModTweaker~2" diff --git a/libs/Mekanism-1.7.10-8.1.1.225.jar b/libs/Mekanism-1.7.10-8.1.5.246.jar similarity index 93% rename from libs/Mekanism-1.7.10-8.1.1.225.jar rename to libs/Mekanism-1.7.10-8.1.5.246.jar index 04b423f..850c63f 100644 Binary files a/libs/Mekanism-1.7.10-8.1.1.225.jar and b/libs/Mekanism-1.7.10-8.1.5.246.jar differ diff --git a/src/main/java/modtweaker2/ClientEvents.java b/src/main/java/modtweaker2/ClientEvents.java index 233b51d..abcfb4f 100644 --- a/src/main/java/modtweaker2/ClientEvents.java +++ b/src/main/java/modtweaker2/ClientEvents.java @@ -4,11 +4,8 @@ import static modtweaker2.helpers.LogHelper.print; import minetweaker.api.item.IItemStack; import minetweaker.api.minecraft.MineTweakerMC; import minetweaker.api.player.IPlayer; -import modtweaker2.utils.TweakerPlugin; import net.minecraft.client.Minecraft; import net.minecraft.client.settings.GameSettings; -import net.minecraft.item.ItemStack; -import net.minecraft.util.EnumChatFormatting; import net.minecraftforge.event.entity.player.ItemTooltipEvent; import cpw.mods.fml.common.eventhandler.SubscribeEvent; diff --git a/src/main/java/modtweaker2/Commands.java b/src/main/java/modtweaker2/Commands.java index 0d64b67..b352c73 100644 --- a/src/main/java/modtweaker2/Commands.java +++ b/src/main/java/modtweaker2/Commands.java @@ -4,18 +4,26 @@ import minetweaker.MineTweakerAPI; import minetweaker.api.player.IPlayer; import minetweaker.api.server.ICommandFunction; import modtweaker2.commands.EntityMappingLogger; -import modtweaker2.mods.auracascade.aura.AuraLogger; +import modtweaker2.mods.appeng.commands.AppliedEnergisticsLogger; +import modtweaker2.mods.auracascade.commands.AuraLogger; import modtweaker2.mods.botania.commands.BotaniaBrewLogger; +import modtweaker2.mods.botania.commands.BotaniaLogger; import modtweaker2.mods.botania.commands.BotaniaOrechidLogger; import modtweaker2.mods.botania.lexicon.commands.LexiconCategoryLogger; import modtweaker2.mods.botania.lexicon.commands.LexiconKnowledgeTypesLogger; import modtweaker2.mods.botania.lexicon.commands.LexiconPageLogger; import modtweaker2.mods.chisel.commands.ChiselGroupLogger; import modtweaker2.mods.chisel.commands.ChiselVariationLogger; -import modtweaker2.mods.mekanism.gas.GasLogger; +import modtweaker2.mods.exnihilo.commands.ExNihiloLogger; +import modtweaker2.mods.factorization.commands.FactorizationLogger; +import modtweaker2.mods.mekanism.commands.GasLogger; +import modtweaker2.mods.mekanism.commands.MekanismLogger; +import modtweaker2.mods.railcraft.commands.RailcraftLogger; 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 { @@ -31,9 +39,14 @@ public class Commands { }); MineTweakerAPI.server.addMineTweakerCommand("entities", new String[] { "/minetweaker entities", " Outputs a list of entities class mapping keys and the entity IDs" }, new EntityMappingLogger()); + + if (TweakerPlugin.isLoaded("appliedenergistics2-core")) { + MineTweakerAPI.server.addMineTweakerCommand("ae2", new String[] {"/minetweaker ae2 [FILTER]", " Outputs a list of all Applied Energistics 2 recipes."}, new AppliedEnergisticsLogger()); + } if (TweakerPlugin.isLoaded("Mekanism")) { MineTweakerAPI.server.addMineTweakerCommand("gases", new String[] { "/minetweaker gases", " Outputs a list of all gas names in the game to the minetweaker log" }, new GasLogger()); + MineTweakerAPI.server.addMineTweakerCommand("mekanism", new String[] { "/minetweaker mekanism [FILTER]", " Outputs a list of all Mekanism recipes." }, new MekanismLogger()); } if (TweakerPlugin.isLoaded("Thaumcraft")) { @@ -43,6 +56,7 @@ public class Commands { if (TweakerPlugin.isLoaded("TConstruct")) { MineTweakerAPI.server.addMineTweakerCommand("materials", new String[] { "/minetweaker materials", " Outputs a list of all Tinker's Construct material names in the game to the minetweaker log" }, new MaterialLogger()); + MineTweakerAPI.server.addMineTweakerCommand("tconstruct", new String[]{ "/minetweaker tconstruct [FILTER]", " Outputs a list of all Tinkers Construct recipes."}, new TConstructLogger()); } if (TweakerPlugin.isLoaded("Botania")) { @@ -51,17 +65,34 @@ public class Commands { MineTweakerAPI.server.addMineTweakerCommand("botaniaBrews", new String[] { "/minetweaker botaniaBrews", " Outputs a list of keys for botania brews" }, new BotaniaBrewLogger()); MineTweakerAPI.server.addMineTweakerCommand("lexiconKnowledgeTypes", new String[] { "/minetweaker lexiconKnowledgeTypes", " Outputs a list of keys for lexicon knowledge types" }, new LexiconKnowledgeTypesLogger()); MineTweakerAPI.server.addMineTweakerCommand("botaniaOrchid", new String[] { "/minetweaker botaniaOrechid", " Outputs a list of keys for botania orechid weights" }, new BotaniaOrechidLogger()); + MineTweakerAPI.server.addMineTweakerCommand("botania", new String[] { "/minetweaker botania [FILTER]", " Outputs a list of all Botania recipes." }, new BotaniaLogger()); } if (TweakerPlugin.isLoaded("chisel")) { 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()); + } + if (TweakerPlugin.isLoaded("factorization")) { + MineTweakerAPI.server.addMineTweakerCommand("factorization", new String[] {"/minetweaker factorization [FILTER]", " Outputs a list of all Factorization recipes."}, new FactorizationLogger()); + } + + if (TweakerPlugin.isLoaded("Railcraft")) { + MineTweakerAPI.server.addMineTweakerCommand("railcraft", new String[] {"/minetweaker railcraft [FILTER]", " Outputs a list of all Railcraft recipes."}, new RailcraftLogger()); + } + + if (TweakerPlugin.isLoaded("exnihilo")) { + MineTweakerAPI.server.addMineTweakerCommand("exnihilo", new String[] {"/minetweaker exnihilo [FILTER]", " Outputs a list of all ExNihilo recipes."}, new ExNihiloLogger()); + } + } } public static void registerMineTweakerCommand(String commandName, String[] usages, ICommandFunction commandFunction) { diff --git a/src/main/java/modtweaker2/ModProps.java b/src/main/java/modtweaker2/ModProps.java index 26c0041..3beb52a 100644 --- a/src/main/java/modtweaker2/ModProps.java +++ b/src/main/java/modtweaker2/ModProps.java @@ -4,6 +4,6 @@ public class ModProps { public static final String NAME = "Mod Tweaker 2", name = NAME; public static final String MODID = "modtweaker2", modid = MODID; - public static final String VERSION = "0.8.3", version = VERSION; + public static final String VERSION = "0.9.0", version = VERSION; public static final String DEPENDENCIES = "required-after:MineTweaker3", dependencies = DEPENDENCIES; } diff --git a/src/main/java/modtweaker2/ModTweaker2.java b/src/main/java/modtweaker2/ModTweaker2.java index 27cc611..cdce8f3 100644 --- a/src/main/java/modtweaker2/ModTweaker2.java +++ b/src/main/java/modtweaker2/ModTweaker2.java @@ -2,10 +2,8 @@ package modtweaker2; import java.io.File; -import minetweaker.MineTweakerAPI; import minetweaker.MineTweakerImplementationAPI; import minetweaker.MineTweakerImplementationAPI.ReloadEvent; -import minetweaker.mc1710.MineTweakerMod; import minetweaker.runtime.providers.ScriptProviderDirectory; import minetweaker.util.IEventHandler; import modtweaker2.mods.appeng.AppliedEnergistics; diff --git a/src/main/java/modtweaker2/commands/EntityMappingLogger.java b/src/main/java/modtweaker2/commands/EntityMappingLogger.java index 2a9739e..309fb63 100644 --- a/src/main/java/modtweaker2/commands/EntityMappingLogger.java +++ b/src/main/java/modtweaker2/commands/EntityMappingLogger.java @@ -14,8 +14,11 @@ public class EntityMappingLogger implements ICommandFunction { @Override public void execute(String[] arguments, IPlayer player) { - Set keys = EntityList.stringToIDMapping.keySet(); + @SuppressWarnings("unchecked") + Set keys = EntityList.stringToIDMapping.keySet(); + System.out.println("Mob Keys: " + keys.size()); + for (Integer key : keys) { ModTweaker2.logger.info("Mob Key " + EntityList.getStringFromID(key) + " : " + key); MineTweakerAPI.logCommand("Mob Key " + EntityList.getStringFromID(key) + " : " + key); diff --git a/src/main/java/modtweaker2/helpers/ForgeHelper.java b/src/main/java/modtweaker2/helpers/ForgeHelper.java index fc39853..b196bea 100644 --- a/src/main/java/modtweaker2/helpers/ForgeHelper.java +++ b/src/main/java/modtweaker2/helpers/ForgeHelper.java @@ -1,11 +1,13 @@ package modtweaker2.helpers; -import java.lang.reflect.Constructor; +import static modtweaker2.helpers.ReflectionHelper.getConstructor; +import static modtweaker2.helpers.ReflectionHelper.getFinalObject; +import static modtweaker2.helpers.ReflectionHelper.getInstance; +import static modtweaker2.helpers.ReflectionHelper.getStaticObject; + import java.util.HashMap; import java.util.List; import java.util.Map; -import static modtweaker2.helpers.ReflectionHelper.*; - import net.minecraft.item.ItemStack; import net.minecraft.util.StatCollector; @@ -16,7 +18,9 @@ import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.relauncher.Side; public class ForgeHelper { + @SuppressWarnings("rawtypes") public static Map translate = null; + @SuppressWarnings("rawtypes") public static List seeds = null; public static HashMap loot = null; @@ -25,23 +29,25 @@ public class ForgeHelper { seeds = getStaticObject(ForgeHooks.class, "seedList"); loot = getStaticObject(ChestGenHooks.class, "chestInfo"); translate = getFinalObject(getStaticObject(StatCollector.class, "localizedName", "field_74839_a"), "languageList", "field_74816_c"); - } catch (Exception e) {} + } catch (Exception e) { } } - private ForgeHelper() {} + private ForgeHelper() { + } public static Object getSeedEntry(ItemStack stack, int weight) { - try { - Class clazz = Class.forName("net.minecraftforge.common.ForgeHooks$SeedEntry"); - Constructor constructor = clazz.getDeclaredConstructor(ItemStack.class, int.class); - constructor.setAccessible(true); - return constructor.newInstance(stack, weight); - } catch (Exception e) { + Object seedEntry = getInstance(getConstructor("net.minecraftforge.common.ForgeHooks$SeedEntry", + ItemStack.class, int.class), stack, weight); + + if (seedEntry == null) { throw new NullPointerException("Failed to instantiate SeedEntry"); } + + return seedEntry; } public static boolean isLangActive(String lang) { - return FMLCommonHandler.instance().getSide() == Side.SERVER ? null : FMLClientHandler.instance().getCurrentLanguage().equals(lang); + return FMLCommonHandler.instance().getSide() == Side.SERVER ? null + : FMLClientHandler.instance().getCurrentLanguage().equals(lang); } } diff --git a/src/main/java/modtweaker2/helpers/InputHelper.java b/src/main/java/modtweaker2/helpers/InputHelper.java index 4f2b552..6912c3f 100644 --- a/src/main/java/modtweaker2/helpers/InputHelper.java +++ b/src/main/java/modtweaker2/helpers/InputHelper.java @@ -1,13 +1,15 @@ package modtweaker2.helpers; +import java.lang.reflect.Array; import java.util.ArrayList; -import minetweaker.MineTweakerAPI; import minetweaker.api.entity.IEntity; import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; import minetweaker.api.liquid.ILiquidStack; import minetweaker.api.oredict.IOreDictEntry; +import minetweaker.mc1710.item.MCItemStack; +import minetweaker.mc1710.liquid.MCLiquidStack; import net.minecraft.entity.Entity; import net.minecraft.item.ItemBlock; import net.minecraft.item.ItemStack; @@ -18,12 +20,26 @@ import net.minecraftforge.fluids.FluidStack; public class InputHelper { public static boolean isABlock(IItemStack block) { if (!(isABlock(toStack(block)))) { - MineTweakerAPI.getLogger().logError("Item must be a block, or you must specify a block to render as when adding a TConstruct Melting recipe"); + LogHelper.logError("Item must be a block, or you must specify a block to render as when adding a TConstruct Melting recipe"); return false; } else return true; } - + + public static T[][] getMultiDimensionalArray(Class clazz, T[] array, int height, int width) { + + @SuppressWarnings("unchecked") + T[][] multiDim = (T[][])Array.newInstance(clazz, new int[] {height, width}); + + for(int y = 0; y < height; y++) { + for(int x = 0; x < width; x++) { + multiDim[y][x] = array[x + (y * width)]; + } + } + + return multiDim; + } + public static IItemStack[] toStacks(IIngredient[] iIngredient) { ArrayList stacks = new ArrayList(); for (IIngredient ing : iIngredient) { @@ -52,12 +68,39 @@ public class InputHelper { else { Object internal = iStack.getInternal(); if (internal == null || !(internal instanceof ItemStack)) { - MineTweakerAPI.getLogger().logError("Not a valid item stack: " + iStack); + LogHelper.logError("Not a valid item stack: " + iStack); } return (ItemStack) internal; } } + + public static IIngredient toIngredient(ItemStack stack) { + return toIItemStack(stack); + } + + public static IIngredient toIngredient(FluidStack stack) { + if(stack == null) + return null; + + return new MCLiquidStack(stack); + } + + public static IItemStack toIItemStack(ItemStack stack) { + if(stack == null) { + return null; + } + + return new MCItemStack(stack); + } + + public static ILiquidStack toILiquidStack(FluidStack stack) { + if(stack == null) { + return null; + } + + return new MCLiquidStack(stack); + } public static ItemStack[] toStacks(IItemStack[] iStack) { if (iStack == null) @@ -101,7 +144,8 @@ public class InputHelper { } } - public static Object[] toShapedObjects(IIngredient[][] ingredients) { + @SuppressWarnings({ "rawtypes", "unchecked" }) + public static Object[] toShapedObjects(IIngredient[][] ingredients) { if (ingredients == null) return null; else { diff --git a/src/main/java/modtweaker2/helpers/LogHelper.java b/src/main/java/modtweaker2/helpers/LogHelper.java index f3adf57..6d4ad8d 100644 --- a/src/main/java/modtweaker2/helpers/LogHelper.java +++ b/src/main/java/modtweaker2/helpers/LogHelper.java @@ -1,8 +1,22 @@ package modtweaker2.helpers; +import java.util.Arrays; +import java.util.List; + +import mekanism.api.gas.GasStack; import minetweaker.MineTweakerAPI; import minetweaker.MineTweakerImplementationAPI; import minetweaker.api.player.IPlayer; +import minetweaker.mc1710.data.NBTConverter; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.item.crafting.IRecipe; +import net.minecraft.item.crafting.ShapedRecipes; +import net.minecraft.item.crafting.ShapelessRecipes; +import net.minecraftforge.fluids.FluidStack; +import net.minecraftforge.oredict.OreDictionary; +import net.minecraftforge.oredict.ShapedOreRecipe; +import net.minecraftforge.oredict.ShapelessOreRecipe; public class LogHelper { public static void logPrinted(IPlayer player) { @@ -21,4 +35,157 @@ public class LogHelper { System.out.println(string); MineTweakerAPI.logCommand(string); } + + public static void logError(String message) { + MineTweakerAPI.logError("[ModTweaker2] " + message); + } + + public static void logError(String message, Throwable exception) { + MineTweakerAPI.logError("[ModTweaker2] " + message, exception); + } + + public static void logWarning(String message) { + MineTweakerAPI.logWarning("[ModTweaker2] " + message); + } + + public static void logInfo(String message) { + MineTweakerAPI.logInfo("[ModTweaker2] " + message); + } + + /** + * Returns a string representation of the item which can also be used in scripts + */ + public static String getStackDescription(Object object) { + if(object instanceof ItemStack) { + return getStackDescription((ItemStack)object); + } else if (object instanceof FluidStack) { + return getStackDescription((FluidStack)object); + } else if (object instanceof GasStack) { + return getStackDescription((GasStack)object); + } else if (object instanceof String) { + // Check if string specifies an oredict entry + List ores = OreDictionary.getOres((String)object); + + if(!ores.isEmpty()) { + return ""; + } else { + return "\"" + (String)object + "\""; + } + } else if (object != null) { + return "\"" + object.toString() + "\""; + } else { + return "null"; + } + } + + public static String getStackDescription(ItemStack stack) { + StringBuilder sb = new StringBuilder(); + + // Creates a name like or + sb.append('<'); + sb.append(Item.itemRegistry.getNameForObject(stack.getItem())); + if(stack.getItemDamage() == 32767) { + sb.append(":*"); + } else if (stack.getItemDamage() > 0) { + sb.append(":").append(stack.getItemDamage()); + } + sb.append('>'); + + // Do we have a tag? (e.g. .withTag({}) ) + if(stack.getTagCompound() != null) + { + sb.append(".withTag("); + sb.append(NBTConverter.from(stack.getTagCompound(), true)); + sb.append(')'); + } + + // Do we have a stack size > 1? + if(stack.stackSize > 1) { + sb.append(" * ").append(stack.stackSize); + } + + return sb.toString(); + } + + public static String getStackDescription(FluidStack stack) { + StringBuilder sb = new StringBuilder(); + + sb.append("'); + + if(stack.amount > 1) { + sb.append(" * ").append(stack.amount); + } + + return sb.toString(); + } + + public static String getStackDescription(GasStack stack) { + StringBuilder sb = new StringBuilder(); + + sb.append("'); + + if(stack.amount > 1) { + sb.append(" * ").append(stack.amount); + } + + return sb.toString(); + } + + public static String getListDescription(List objects) { + StringBuilder sb = new StringBuilder(); + + if(objects.isEmpty()) { + sb.append("[]"); + } else { + sb.append('['); + for(Object object : objects) { + if(object instanceof List) { + sb.append(getListDescription((List)object)).append(", "); + } else if(object instanceof Object[]) { + sb.append(getListDescription(Arrays.asList((Object[])object))).append(", "); + } else { + sb.append(getStackDescription(object)).append(", "); + } + } + sb.setLength(sb.length() - 2); + sb.append(']'); + } + + return sb.toString(); + } + + public static String getCraftingDescription(IRecipe recipe) { + if(recipe instanceof ShapelessOreRecipe) + return LogHelper.getCraftingDescription((ShapelessOreRecipe)recipe); + else if(recipe instanceof ShapedOreRecipe) + return LogHelper.getCraftingDescription((ShapedOreRecipe)recipe); + else if(recipe instanceof ShapelessRecipes) + return LogHelper.getCraftingDescription((ShapelessRecipes)recipe); + else if(recipe instanceof ShapedRecipes) + return LogHelper.getCraftingDescription((ShapedRecipes)recipe); + + return recipe.toString(); + } + + public static String getCraftingDescription(ShapelessOreRecipe recipe) { + return getListDescription(recipe.getInput()); + } + + public static String getCraftingDescription(ShapelessRecipes recipe) { + return getListDescription(recipe.recipeItems); + } + + public static String getCraftingDescription(ShapedOreRecipe recipe) { + int height = ReflectionHelper.getObject(recipe, "width"); + int width = ReflectionHelper.getObject(recipe, "height"); + + Object[][] recipes = InputHelper.getMultiDimensionalArray(Object.class, recipe.getInput(), height, width); + + return getListDescription(Arrays.asList(recipes)); + } + + public static String getCraftingDescription(ShapedRecipes recipe) { + ItemStack[][] recipes = InputHelper.getMultiDimensionalArray(ItemStack.class, recipe.recipeItems, recipe.recipeHeight, recipe.recipeWidth); + return getListDescription(Arrays.asList(recipes)); + } } diff --git a/src/main/java/modtweaker2/helpers/ReflectionHelper.java b/src/main/java/modtweaker2/helpers/ReflectionHelper.java index 281dd86..d2a0a0b 100644 --- a/src/main/java/modtweaker2/helpers/ReflectionHelper.java +++ b/src/main/java/modtweaker2/helpers/ReflectionHelper.java @@ -5,67 +5,156 @@ import java.lang.reflect.Field; import java.lang.reflect.Modifier; public class ReflectionHelper { - public static Constructor getConstructor(String string, Class... types) { + + /** + * Creates a new instance with the provided constructor and arguments. + * @param constructor The constructor of the class for which a new instance should be created. + * @param arguments Array of objects to be passed as arguments to the constructor call. + * @return A new object created by calling the constructor. + */ + public static T getInstance(Constructor constructor, Object... arguments) { try { - Class clazz = Class.forName(string); - Constructor constructor = clazz.getDeclaredConstructor(types); + return constructor.newInstance(arguments); + } catch (Exception e) { LogHelper.logError("Exception creating instance of " + constructor.getClass().getName(), e); } + + return null; + } + + /** + * Obtains the constructor for the named class identified by the parameter types. + * @param className The fully qualified name of the class. + * @param argumentTypes Parameter types to identify the constructor. + * @return Constructor object representing the declared constructor for the parameter types. + */ + public static Constructor getConstructor(final String className, final Class... argumentTypes) { + if(className == null || argumentTypes == null) { + LogHelper.logError("The provided class name or arguments can't be null."); + return null; + } + + try { + Class clazz = Class.forName(className); + Constructor constructor = clazz.getDeclaredConstructor(argumentTypes); constructor.setAccessible(true); return constructor; - } catch (Exception ex) {} + } catch (Exception ex) { LogHelper.logError("Exception getting constructor of " + className, ex); } + + return null; + } + + /** + * Obtains the constructor for the class identified by the parameter types. + * @param clazz The Class for which the constructor should be obtained. + * @param argumentTypes Parameter types to identify the constructor. + * @return Constructor object representing the declared constructor for the parameter types. + */ + public static Constructor getConstructor(Class clazz, Class... types) { + try { + Constructor constructor = clazz.getDeclaredConstructor(types); + constructor.setAccessible(true); + return constructor; + } catch (Exception ex) { LogHelper.logError("Exception getting constructore of " + clazz.getName(), ex); } return null; } - public static T getObject(Object o, String... fieldName) { - Class cls = o.getClass(); - for (String field : fieldName) { + /** + * Returns the value of a private field for an object instance. + * @param object An object instance from which the field value is to be extracted. + * @param fieldNames A list of field names for which the value should be extracted. + * The functions returns value of the first field found. + * @return The value of the provided field name. + */ + @SuppressWarnings("unchecked") + public static T getObject(Object object, String... fieldNames) { + Class cls = object.getClass(); + for (String field : fieldNames) { try { Field result = cls.getDeclaredField(field); result.setAccessible(true); - return (T) result.get(o); + return (T) result.get(object); } catch (Exception ex) {} } + LogHelper.logError("Could not retrieve any object for the provided field names."); + return null; } - public static T getFinalObject(Object o, String... fieldName) { - Class cls = o.getClass(); - for (String field : fieldName) { + /** + * Returns the value of a private final field for an object instance and removes the final modifier. + * @param object An object instance from which the field value is to be extracted. + * @param fieldNames A list of field names for which the value should be extracted. + * The functions returns value of the first field found. + * @return The value of the provided field name. + */ + @SuppressWarnings("unchecked") + public static T getFinalObject(Object object, String... fieldNames) { + Class cls = object.getClass(); + for (String field : fieldNames) { try { Field result = cls.getDeclaredField(field); result.setAccessible(true); Field modifiersField = Field.class.getDeclaredField("modifiers"); modifiersField.setAccessible(true); modifiersField.setInt(result, result.getModifiers() & ~Modifier.FINAL); - return (T) result.get(o); + return (T) result.get(object); } catch (Exception ex) {} } - + + LogHelper.logError("Could not retrieve any final object for the provided field names."); + return null; } - public static T getStaticObject(Class cls, String... fieldName) { - for (String field : fieldName) { + /** + * Returns the value of a private static field for a class. + * @param clazz The class for which the field value is to be extracted. + * @param fieldNames A list of field names for which the value should be extracted. + * @return The value of the provided field name. + */ + @SuppressWarnings("unchecked") + public static T getStaticObject(Class clazz, String... fieldNames) { + for (String field : fieldNames) { try { - Field result = cls.getDeclaredField(field); + Field result = clazz.getDeclaredField(field); result.setAccessible(true); return (T) result.get(null); } catch (Exception e) {} } + + LogHelper.logError("Could not retrieve any static object for the provided field names."); return null; } + + /** + * Returns the value of a private static field for a class. + * @param className The fully qualified name of the class. + * @param fieldNames A list of field names for which the value should be extracted. + * @return The value of the provided field name. + */ + public static T getStaticObject(String className, String... fieldNames) { + try { + Class clazz = Class.forName(className); + return getStaticObject(clazz, fieldNames); + } catch (ClassNotFoundException e) { } + + LogHelper.logError("Could not retrieve any static object for the provided field names."); + + return null; + } - public static void setPrivateValue(Class cls, String field, int var) { + public static void setPrivateValue(Class cls, String field, int var) { try { Field f = cls.getDeclaredField(field); f.setAccessible(true); f.setInt(null, var); } catch (Exception e) {} } - - public static void setPrivateValue(Class cls, Object o, String field, Object var) { + + @SuppressWarnings({ "rawtypes", "unchecked" }) + public static void setPrivateValue( Class cls, Object o, String field, Object var) { cpw.mods.fml.relauncher.ReflectionHelper.setPrivateValue(cls, o, var, field); } } diff --git a/src/main/java/modtweaker2/helpers/StackHelper.java b/src/main/java/modtweaker2/helpers/StackHelper.java index 77529ed..6b98700 100644 --- a/src/main/java/modtweaker2/helpers/StackHelper.java +++ b/src/main/java/modtweaker2/helpers/StackHelper.java @@ -1,19 +1,129 @@ package modtweaker2.helpers; -//import modtweaker2.mods.botania.Botania; +import static modtweaker2.helpers.InputHelper.toStack; +import mekanism.api.gas.GasStack; +import minetweaker.api.item.IIngredient; +import minetweaker.api.item.IItemStack; +import minetweaker.api.item.IngredientAny; +import minetweaker.api.liquid.ILiquidStack; import modtweaker2.mods.botania.Botania; +import modtweaker2.mods.mekanism.MekanismHelper; +import modtweaker2.mods.mekanism.gas.IGasStack; import modtweaker2.utils.TweakerPlugin; import net.minecraft.item.ItemStack; +import net.minecraftforge.fluids.FluidStack; public class StackHelper { - // Stack is the stack that is part of a recipe, stack2 is the one input - // trying to match - public static boolean areEqual(ItemStack stack, ItemStack stack2) { - if (stack == null || stack2 == null) + /** + * Compares two ItemStack instances, trying to match the referenced item. + */ + public static boolean areEqual(ItemStack stack1, ItemStack stack2) { + if (stack1 == null || stack2 == null) { return false; - else if (TweakerPlugin.isLoaded("Botania") && Botania.isSubtile(stack) && Botania.isSubtile(stack2)) { - return Botania.subtileMatches(stack, stack2); - } else - return stack.isItemEqual(stack2); + } else if (TweakerPlugin.isLoaded("Botania") && Botania.isSubtile(stack1) && Botania.isSubtile(stack2)) { + return Botania.subtileMatches(stack1, stack2); + } else { + return stack1.isItemEqual(stack2); + } } + + public static boolean areEqual(FluidStack stack1, FluidStack stack2) { + if(stack1 == null || stack2 == null) { + return false; + } + + return stack1.isFluidEqual(stack2); + } + + /** + * Compares two ItemStack instances if they reference the same object or + * are both null. If false, the method areEqual() is called to check if the + * two instances reference the same item. + */ + public static boolean areEqualOrNull(ItemStack stack1, ItemStack stack2) { + // Check if they reference the same object or are are both null + if(stack1 == stack2) { + return true; + } + + return(areEqual(stack1, stack2)); + } + + public static boolean areEqualOrNull(FluidStack stack1, FluidStack stack2) { + // Check if they reference the same object or are are both null + if(stack1 == stack2) { + return true; + } + + return(areEqual(stack1, stack2)); + } + + /** + * Adds extra check to IIngredient matches() for Botania special flowers + */ + public static boolean matches(IIngredient ingredient, IItemStack itemStack) { + if(ingredient == null) { + return false; + } + + if(!ingredient.matches(itemStack)) { + return false; + } + + // Check for Botania special flowers + if(ingredient.getItems() != null && Botania.isSubtile(toStack(itemStack))) { + for(IItemStack item : ingredient.getItems()) { + if(areEqual(toStack(item), toStack(itemStack))) { + return true; + } + } + } else { + return true; + } + + return false; + } + + /** + * This function compares an ingredient with a fluid. MCLiquidStack.matches() function + * is currently broken in MineTweaker3, thus this function is only a workaround. + */ + public static boolean matches(IIngredient ingredient, ILiquidStack liquidStack) { + if(ingredient == null) { + return false; + } + + // Do we have a wildcard (<*>) ? + if(ingredient.matches(liquidStack)) { + return true; + } + + // Does ingredient reference liquids? + if(ingredient.getLiquids() != null) { + for (ILiquidStack liquid : ingredient.getLiquids()) { + if(InputHelper.toFluid(liquid).isFluidEqual(InputHelper.toFluid(liquidStack))) { + return true; + } + } + } + + return false; + } + + public static boolean matches(IIngredient ingredient, IGasStack gasStack) { + if(ingredient == null) { + return false; + } + + if(ingredient == IngredientAny.INSTANCE) { + return true; + } + + if(ingredient instanceof IGasStack) { + return MekanismHelper.toGas((IGasStack)ingredient).isGasEqual(MekanismHelper.toGas(gasStack)); + } + + return false; + } + } diff --git a/src/main/java/modtweaker2/helpers/StringHelper.java b/src/main/java/modtweaker2/helpers/StringHelper.java new file mode 100644 index 0000000..9953281 --- /dev/null +++ b/src/main/java/modtweaker2/helpers/StringHelper.java @@ -0,0 +1,34 @@ +package modtweaker2.helpers; + +import java.util.List; +import java.util.ListIterator; + +public class StringHelper { + public static List toLowerCase(List stringList) { + ListIterator iterator = stringList.listIterator(); + + while(iterator.hasNext()) { + iterator.set(iterator.next().toLowerCase()); + } + + return stringList; + } + + public static String join(List list, String conjunction) { + StringBuilder sb = new StringBuilder(); + + if(conjunction == null) { + conjunction = ", "; + } + + if(list != null && !list.isEmpty()) { + for(String string : list) { + sb.append(string).append(conjunction); + } + + sb.setLength(sb.length() - conjunction.length()); + } + + return sb.toString(); + } +} diff --git a/src/main/java/modtweaker2/mods/appeng/AppliedEnergistics.java b/src/main/java/modtweaker2/mods/appeng/AppliedEnergistics.java index abeb0a1..42ba6af 100644 --- a/src/main/java/modtweaker2/mods/appeng/AppliedEnergistics.java +++ b/src/main/java/modtweaker2/mods/appeng/AppliedEnergistics.java @@ -9,7 +9,5 @@ public class AppliedEnergistics { public AppliedEnergistics() { MineTweakerAPI.registerClass(Inscriber.class); MineTweakerAPI.registerClass(Grind.class); - } - } diff --git a/src/main/java/modtweaker2/mods/appeng/AppliedEnergisticsHelper.java b/src/main/java/modtweaker2/mods/appeng/AppliedEnergisticsHelper.java new file mode 100644 index 0000000..c1579d1 --- /dev/null +++ b/src/main/java/modtweaker2/mods/appeng/AppliedEnergisticsHelper.java @@ -0,0 +1,77 @@ +package modtweaker2.mods.appeng; + +import static modtweaker2.helpers.StackHelper.areEqualOrNull; +import net.minecraft.item.ItemStack; +import appeng.api.features.IGrinderEntry; +import appeng.api.features.IInscriberRecipe; + +public class AppliedEnergisticsHelper { + /** + * Compares two IInscriberRecipe objects, if they are the same or have the same inputs + */ + public static boolean equals(IInscriberRecipe r1, IInscriberRecipe r2) { + if(r1 == r2) { + return true; + } + + if (r1 == null || r2 == null) { + return false; + } + + if(!areEqualOrNull(r1.getTopOptional().orNull(), r2.getTopOptional().orNull())) { + return false; + } + + if(!areEqualOrNull(r1.getBottomOptional().orNull(), r2.getBottomOptional().orNull())) { + return false; + } + + if(r1.getInputs() == null && r2.getInputs() != null || r1.getInputs() != null && r2.getInputs() == null) { + return false; + } + + if(r1.getInputs() != null && r2.getInputs() != null) { + if(r1.getInputs().size() != r2.getInputs().size()) { + return false; + } + + // Check if every item in recipe 1 is in recipe 2 + for(ItemStack i1 : r1.getInputs()) { + boolean found = false; + + for(ItemStack i2 : r2.getInputs()) { + if(areEqualOrNull(i1, i2)) { + found = true; + } + } + + if(!found) { + return false; + } + } + } + + return true; + } + + /** + * Compares two IGrinderEntry objects, if they are the same or have the same inputs + */ + public static boolean equals(IGrinderEntry r1, IGrinderEntry r2) { + if(r1 == r2) { + return true; + } + + if (r1 == null || r2 == null) { + return false; + } + + if(!areEqualOrNull(r1.getInput(), r2.getInput())) { + return false; + } + + return true; + } + + +} diff --git a/src/main/java/modtweaker2/mods/appeng/commands/AppliedEnergisticsLogger.java b/src/main/java/modtweaker2/mods/appeng/commands/AppliedEnergisticsLogger.java new file mode 100644 index 0000000..1fe753a --- /dev/null +++ b/src/main/java/modtweaker2/mods/appeng/commands/AppliedEnergisticsLogger.java @@ -0,0 +1,64 @@ +package modtweaker2.mods.appeng.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.LogHelper; +import modtweaker2.helpers.StringHelper; +import appeng.api.AEApi; +import appeng.api.features.IGrinderEntry; +import appeng.api.features.IInscriberRecipe; + +public class AppliedEnergisticsLogger implements ICommandFunction { + + private static final List validArguments = new LinkedList(); + + static { + validArguments.add("grinder"); + validArguments.add("inscriber"); + } + + @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("grinder")) { + for(IGrinderEntry recipe : AEApi.instance().registries().grinder().getRecipes()) { + MineTweakerAPI.logCommand(String.format("mods.appeng.Grinder.addRecipe(%s, %s, %d, %s, %s, %s, %s);", + LogHelper.getStackDescription(recipe.getInput()), + LogHelper.getStackDescription(recipe.getOutput()), + recipe.getEnergyCost(), + LogHelper.getStackDescription(recipe.getOptionalOutput()), + recipe.getOptionalChance(), + LogHelper.getStackDescription(recipe.getSecondOptionalOutput()), + recipe.getSecondOptionalChance())); + } + } + + if(args.isEmpty() || args.contains("inscriber")) { + for(IInscriberRecipe recipe : AEApi.instance().registries().inscriber().getRecipes()) { + MineTweakerAPI.logCommand(String.format("mods.appeng.Inscriber.addRecipe(%s, %s, %s, %s, \"%s\");", + LogHelper.getListDescription(recipe.getInputs()), + LogHelper.getStackDescription(recipe.getTopOptional().orNull()), + LogHelper.getStackDescription(recipe.getBottomOptional().orNull()), + LogHelper.getStackDescription(recipe.getOutput()), + recipe.getProcessType().toString())); + } + } + + 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/appeng/handlers/Grind.java b/src/main/java/modtweaker2/mods/appeng/handlers/Grind.java index c4cdd5e..6625690 100644 --- a/src/main/java/modtweaker2/mods/appeng/handlers/Grind.java +++ b/src/main/java/modtweaker2/mods/appeng/handlers/Grind.java @@ -1,14 +1,20 @@ package modtweaker2.mods.appeng.handlers; -import java.util.ArrayList; -import java.util.List; +import static modtweaker2.helpers.InputHelper.toIItemStack; +import static modtweaker2.helpers.InputHelper.toStack; +import static modtweaker2.helpers.StackHelper.matches; + +import java.util.LinkedList; -import net.minecraft.item.ItemStack; import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; import modtweaker2.helpers.InputHelper; +import modtweaker2.helpers.LogHelper; +import modtweaker2.mods.appeng.AppliedEnergisticsHelper; import modtweaker2.utils.BaseListAddition; import modtweaker2.utils.BaseListRemoval; +import stanhebben.zenscript.annotations.Optional; import stanhebben.zenscript.annotations.ZenClass; import stanhebben.zenscript.annotations.ZenMethod; import appeng.api.AEApi; @@ -17,49 +23,93 @@ import appeng.core.features.registries.entries.AppEngGrinderRecipe; @ZenClass("mods.appeng.Grinder") public class Grind { + + protected static final String name = "Applied Energistics 2 Grinder"; + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + @ZenMethod + public static void addRecipe(IItemStack input, IItemStack output, int energy, @Optional IItemStack output2, @Optional float chance2, @Optional IItemStack output3, @Optional float chance3) { + if(input == null || output == null) { + LogHelper.logError(String.format("Required parameters missing for %s Recipe.", name)); + return; + } + + // Create recipe + IGrinderEntry recipe; + + if(output2 != null && output3 != null) + recipe = new AppEngGrinderRecipe(InputHelper.toStack(input), InputHelper.toStack(output), InputHelper.toStack(output2), InputHelper.toStack(output3), chance2, chance3, energy); + else if(output2 != null) + recipe = new AppEngGrinderRecipe(InputHelper.toStack(input), InputHelper.toStack(output), InputHelper.toStack(output2), chance2, energy); + else + recipe = new AppEngGrinderRecipe(InputHelper.toStack(input), InputHelper.toStack(output), energy); + + // Check if the recipe is already present, we don't want to add duplicates + for(IGrinderEntry r : AEApi.instance().registries().grinder().getRecipes()) { + if(r != null && AppliedEnergisticsHelper.equals(r, recipe)) { + LogHelper.logWarning(String.format("Duplicate %s Recipe found for %s. Command ignored!", name, LogHelper.getStackDescription(toStack(input)))); + return; + } + } + + MineTweakerAPI.apply(new Add(recipe)); + } + + private static class Add extends BaseListAddition { + + public Add(IGrinderEntry recipe) { + super(Grind.name, AEApi.instance().registries().grinder().getRecipes()); + recipes.add(recipe); + } + + @Override + public String getRecipeInfo(IGrinderEntry recipe) { + return LogHelper.getStackDescription(recipe.getInput()); + } + + @Override + protected boolean equals(IGrinderEntry recipe, IGrinderEntry otherRecipe) { + return AppliedEnergisticsHelper.equals(recipe, otherRecipe); + } + } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + @ZenMethod - public static void addRecipe(IItemStack input, IItemStack output, int energy) { - MineTweakerAPI.apply(new Add(new AppEngGrinderRecipe(InputHelper.toStack(input), InputHelper.toStack(output), energy))); + public static void removeRecipe(IIngredient output) { + if(output == null) { + LogHelper.logError(String.format("Required parameters missing for %s Recipe.", name)); + return; + } + + // Get list of existing recipes, matching with parameter + LinkedList result = new LinkedList(); + + for(IGrinderEntry entry : AEApi.instance().registries().grinder().getRecipes()) { + if(entry != null && entry.getOutput() != null && matches(output, toIItemStack(entry.getOutput()))) { + result.add(entry); + } + } + + // Check if we found the recipes and apply the action + if(!result.isEmpty()) { + MineTweakerAPI.apply(new Remove(result)); + } else { + LogHelper.logWarning(String.format("No %s Recipe found for %s. Command ignored!", name, output.toString())); + } } - private static class Add extends BaseListAddition { - - public Add(AppEngGrinderRecipe recipe) { - super(AEApi.instance().registries().grinder().getRecipes(), recipe); + private static class Remove extends BaseListRemoval { + public Remove(LinkedList recipes) { + super(Grind.name, AEApi.instance().registries().grinder().getRecipes(), recipes); } @Override - public void apply() { - AEApi.instance().registries().grinder().getRecipes().add((AppEngGrinderRecipe) recipe); + public String getRecipeInfo(IGrinderEntry recipe) { + return LogHelper.getStackDescription(recipe.getInput()); } } - @ZenMethod - public static void removeRecipe(IItemStack output) { - MineTweakerAPI.apply(new Remove(InputHelper.toStack(output))); - } - - private static class Remove extends BaseListRemoval { - public Remove(ItemStack stack) { - super("Applied Energistics 2 Grinder", AEApi.instance().registries().grinder().getRecipes(), stack); - } - - @Override - public void apply() { - for (IGrinderEntry r : AEApi.instance().registries().grinder().getRecipes()) { - if (r.getOutput().isItemEqual(stack)) { - recipes.add(r); - } - } - super.apply(); - } - - @Override - public String getRecipeInfo() { - return stack.getDisplayName(); - } - - } - } \ No newline at end of file diff --git a/src/main/java/modtweaker2/mods/appeng/handlers/Inscriber.java b/src/main/java/modtweaker2/mods/appeng/handlers/Inscriber.java index 6f1893d..694aee8 100644 --- a/src/main/java/modtweaker2/mods/appeng/handlers/Inscriber.java +++ b/src/main/java/modtweaker2/mods/appeng/handlers/Inscriber.java @@ -1,77 +1,160 @@ package modtweaker2.mods.appeng.handlers; +import static modtweaker2.helpers.InputHelper.toIItemStack; import static modtweaker2.helpers.InputHelper.toStack; import static modtweaker2.helpers.InputHelper.toStacks; +import static modtweaker2.helpers.StackHelper.matches; -import java.util.ArrayList; +import java.util.LinkedList; import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; +import modtweaker2.helpers.LogHelper; +import modtweaker2.mods.appeng.AppliedEnergisticsHelper; import modtweaker2.utils.ArrayUtils; import modtweaker2.utils.BaseListAddition; import modtweaker2.utils.BaseListRemoval; -import net.minecraft.item.ItemStack; import stanhebben.zenscript.annotations.ZenClass; import stanhebben.zenscript.annotations.ZenMethod; import appeng.api.AEApi; import appeng.api.features.IInscriberRecipe; import appeng.api.features.InscriberProcessType; import appeng.core.features.registries.entries.InscriberRecipe; -import appeng.recipes.handlers.Inscribe; @ZenClass("mods.appeng.Inscriber") public class Inscriber { + + protected static final String name = "Applied Energistics 2 Inscriber"; + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + @ZenMethod public static void addRecipe(IItemStack[] imprintable, IItemStack plateA, IItemStack plateB, IItemStack out, String type) { - MineTweakerAPI.apply(new Add(new InscriberRecipe(ArrayUtils.toArrayList(toStacks(imprintable)), toStack(out), toStack(plateA), toStack(plateB), InscriberProcessType.valueOf(type)))); - for (IItemStack stack : imprintable) { - AEApi.instance().registries().inscriber().getInputs().add(toStack(stack)); - - } - if (plateA != null) - AEApi.instance().registries().inscriber().getOptionals().add(toStack(plateA)); - - if (plateB != null) - AEApi.instance().registries().inscriber().getOptionals().add(toStack(plateB)); - + if(imprintable == null || out == null || (!type.equals("Press") || !type.equals("Inscribe"))) { + LogHelper.logError(String.format("Required parameters missing for %s Recipe.", Inscriber.name)); + return; + } + + // Create recipe + IInscriberRecipe recipe = new InscriberRecipe(ArrayUtils.toArrayList(toStacks(imprintable)), toStack(out), toStack(plateA), toStack(plateB), InscriberProcessType.valueOf(type)); + + // Check if the recipe is already present, we don't want to add duplicates + for(IInscriberRecipe r : AEApi.instance().registries().inscriber().getRecipes()) { + if(r != null && AppliedEnergisticsHelper.equals(r, recipe)) { + LogHelper.logWarning(String.format("Duplicate %s Recipe found for %s. Command ignored!", Inscriber.name, LogHelper.getStackDescription(toStack(out)))); + return; + } + } + + MineTweakerAPI.apply(new Add(recipe)); } - public static class Add extends BaseListAddition { - + public static class Add extends BaseListAddition { public Add(IInscriberRecipe recipe) { - super(recipe.toString(), AEApi.instance().registries().inscriber().getRecipes(), recipe); + super(Inscriber.name, AEApi.instance().registries().inscriber().getRecipes()); + recipes.add(recipe); } @Override public void apply() { - AEApi.instance().registries().inscriber().addRecipe((IInscriberRecipe) recipe); - } + if(recipes.isEmpty()) { + return; + } + for(IInscriberRecipe recipe : recipes) { + try { + AEApi.instance().registries().inscriber().addRecipe(recipe); + successful.add(recipe); + } catch (Exception ex) { LogHelper.logError("Error adding inscriber recipe.", ex); } + } + } + + @Override + public void undo() { + if(successful.isEmpty()) { + return; + } + + for(IInscriberRecipe recipe : successful) { + try { + AEApi.instance().registries().inscriber().removeRecipe(recipe); + } catch (Exception ex) { LogHelper.logError("Error removing inscriber recipe.", ex); } + } + } + + @Override + public String getRecipeInfo(IInscriberRecipe recipe) { + return LogHelper.getStackDescription(recipe.getOutput()); + } + + @Override + protected boolean equals(IInscriberRecipe recipe, IInscriberRecipe otherRecipe) { + return AppliedEnergisticsHelper.equals(recipe, otherRecipe); + } } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @ZenMethod - public static void removeRecipe(IItemStack output) { - MineTweakerAPI.apply(new Remove(toStack(output))); + public static void removeRecipe(IIngredient output) { + if(output == null) { + LogHelper.logError(String.format("Required parameters missing for %s Recipe.", Inscriber.name)); + return; + } + + // Get list of existing recipes, matching with parameter + LinkedList result = new LinkedList(); + + for(IInscriberRecipe entry : AEApi.instance().registries().inscriber().getRecipes()) { + if(entry != null && entry.getOutput() != null && matches(output, toIItemStack(entry.getOutput()))) { + result.add(entry); + } + } + + // Check if we found the recipes and apply the action + if(!result.isEmpty()) { + MineTweakerAPI.apply(new Remove(result)); + } else { + LogHelper.logWarning(String.format("No %s Recipe found for %s. Command ignored!", Inscriber.name, output.toString())); + } } - public static class Remove extends BaseListRemoval { - public Remove(ItemStack stack) { - super("Applied Energistics 2 Inscriber", AEApi.instance().registries().inscriber().getRecipes(), stack); - } - - @Override - public void apply() { - for (IInscriberRecipe recipe : AEApi.instance().registries().inscriber().getRecipes()) { - if (recipe != null && recipe.getOutput() != null && recipe.getOutput().isItemEqual(stack)) { - recipes.add(recipe); - } - } - super.apply(); + public static class Remove extends BaseListRemoval { + public Remove(LinkedList recipes) { + super(Inscriber.name, AEApi.instance().registries().inscriber().getRecipes(), recipes); } - @Override - public String getRecipeInfo() { - return stack.getDisplayName(); + @Override + public void apply() { + if(this.recipes.isEmpty()) { + return; + } + + for(IInscriberRecipe recipe : recipes) { + try { + AEApi.instance().registries().inscriber().removeRecipe(recipe); + successful.add(recipe); + } catch (Exception ex) { LogHelper.logError("Error removing inscriber recipe.", ex); } + } + } + + @Override + public void undo() { + if(successful.isEmpty()) { + return; + } + + for(IInscriberRecipe recipe : successful) { + try { + AEApi.instance().registries().inscriber().addRecipe(recipe); + } catch (Exception ex) { LogHelper.logError("Error restoring inscriber recipe.", ex); } + } + } + + @Override + public String getRecipeInfo(IInscriberRecipe recipe) { + return LogHelper.getStackDescription(recipe.getOutput()); } } } diff --git a/src/main/java/modtweaker2/mods/auracascade/aura/AuraBracketHandler.java b/src/main/java/modtweaker2/mods/auracascade/aura/AuraBracketHandler.java index 2b9c989..d773a5f 100644 --- a/src/main/java/modtweaker2/mods/auracascade/aura/AuraBracketHandler.java +++ b/src/main/java/modtweaker2/mods/auracascade/aura/AuraBracketHandler.java @@ -2,8 +2,6 @@ package modtweaker2.mods.auracascade.aura; import java.util.List; -import mekanism.api.gas.Gas; -import mekanism.api.gas.GasRegistry; import minetweaker.IBracketHandler; import minetweaker.MineTweakerAPI; import minetweaker.annotations.BracketHandler; @@ -56,8 +54,8 @@ public class AuraBracketHandler implements IBracketHandler { } private IZenSymbol find(IEnvironmentGlobal environment, List tokens, int startIndex, int endIndex) { - StringBuilder valueBuilder = new StringBuilder(); -// } + //StringBuilder valueBuilder = new StringBuilder(); + for (EnumAura a : EnumAura.values()) { for (int i = startIndex; i < endIndex; i++) { Token token = tokens.get(i); diff --git a/src/main/java/modtweaker2/mods/auracascade/aura/AuraLogger.java b/src/main/java/modtweaker2/mods/auracascade/commands/AuraLogger.java similarity index 88% rename from src/main/java/modtweaker2/mods/auracascade/aura/AuraLogger.java rename to src/main/java/modtweaker2/mods/auracascade/commands/AuraLogger.java index f53b9bd..24d5d1e 100644 --- a/src/main/java/modtweaker2/mods/auracascade/aura/AuraLogger.java +++ b/src/main/java/modtweaker2/mods/auracascade/commands/AuraLogger.java @@ -1,14 +1,11 @@ -package modtweaker2.mods.auracascade.aura; +package modtweaker2.mods.auracascade.commands; -import java.util.Set; - -import pixlepix.auracascade.data.EnumAura; import minetweaker.MineTweakerAPI; import minetweaker.MineTweakerImplementationAPI; import minetweaker.api.player.IPlayer; import minetweaker.api.server.ICommandFunction; import modtweaker2.ModTweaker2; -import net.minecraft.entity.EntityList; +import pixlepix.auracascade.data.EnumAura; public class AuraLogger implements ICommandFunction { diff --git a/src/main/java/modtweaker2/mods/auracascade/handlers/Pylon.java b/src/main/java/modtweaker2/mods/auracascade/handlers/Pylon.java index 33954c4..6dc2544 100644 --- a/src/main/java/modtweaker2/mods/auracascade/handlers/Pylon.java +++ b/src/main/java/modtweaker2/mods/auracascade/handlers/Pylon.java @@ -1,16 +1,19 @@ package modtweaker2.mods.auracascade.handlers; +import static modtweaker2.helpers.InputHelper.toIItemStack; import static modtweaker2.helpers.InputHelper.toStack; -import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; +import modtweaker2.helpers.LogHelper; import modtweaker2.mods.auracascade.AuraCascadeHelper; import modtweaker2.mods.auracascade.aura.IAuraStack; import modtweaker2.utils.BaseListAddition; import modtweaker2.utils.BaseListRemoval; -import net.minecraft.item.ItemStack; import pixlepix.auracascade.data.recipe.PylonRecipe; import pixlepix.auracascade.data.recipe.PylonRecipeComponent; import pixlepix.auracascade.data.recipe.PylonRecipeRegistry; @@ -19,7 +22,11 @@ import stanhebben.zenscript.annotations.ZenMethod; @ZenClass("mods.auraCascade.Pylon") public class Pylon { + + protected static final String name = "Aura Cascade Pylon"; + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + @ZenMethod public static void addRecipe(IItemStack ouput, IAuraStack stack, IItemStack input) { addRecipe(ouput, stack, input, stack, input, stack, input, stack, input); @@ -27,37 +34,53 @@ public class Pylon { @ZenMethod public static void addRecipe(IItemStack ouput, IAuraStack aura1, IItemStack input1, IAuraStack aura2, IItemStack input2, IAuraStack aura3, IItemStack input3, IAuraStack aura4, IItemStack input4) { - MineTweakerAPI.apply(new Add(new PylonRecipe(toStack(ouput), new PylonRecipeComponent(AuraCascadeHelper.toAura(aura1), toStack(input1)), new PylonRecipeComponent(AuraCascadeHelper.toAura(aura2), toStack(input2)), new PylonRecipeComponent(AuraCascadeHelper.toAura(aura3), toStack(input3)), new PylonRecipeComponent(AuraCascadeHelper.toAura(aura1), toStack(input4))))); + List recipes = new LinkedList(); + + recipes.add(new PylonRecipe(toStack(ouput), new PylonRecipeComponent(AuraCascadeHelper.toAura(aura1), toStack(input1)), new PylonRecipeComponent(AuraCascadeHelper.toAura(aura2), toStack(input2)), new PylonRecipeComponent(AuraCascadeHelper.toAura(aura3), toStack(input3)), new PylonRecipeComponent(AuraCascadeHelper.toAura(aura1), toStack(input4)))); + + MineTweakerAPI.apply(new Add(recipes)); } - private static class Add extends BaseListAddition { + private static class Add extends BaseListAddition { - public Add(PylonRecipe recipe) { - super(PylonRecipeRegistry.recipes, recipe); + public Add(List recipes) { + super(Pylon.name, PylonRecipeRegistry.recipes, recipes); } - + + @Override + protected String getRecipeInfo(PylonRecipe recipe) { + return LogHelper.getStackDescription(recipe.result); + } } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @ZenMethod - public static void removeRecipe(IItemStack ouput) { - MineTweakerAPI.apply(new Remove(toStack(ouput))); + public static void removeRecipe(IIngredient output) { + List recipes = new LinkedList(); + + for (PylonRecipe r : PylonRecipeRegistry.recipes) { + if (output.matches(toIItemStack(r.result))) { + recipes.add(r); + } + } + + // Check if we found the recipes and apply the action + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new Remove(recipes)); + } else { + LogHelper.logWarning(String.format("No %s Recipe found for %s. Command ignored!", Pylon.name, output.toString())); + } } - private static class Remove extends BaseListRemoval { - public Remove(ItemStack stack) { - super(PylonRecipeRegistry.recipes, stack); - } - - @Override - public void apply() { - for (PylonRecipe r : PylonRecipeRegistry.recipes) { - if (r.result.isItemEqual(stack)) { - recipes.add(r); - } - } - - super.apply(); + private static class Remove extends BaseListRemoval { + public Remove(List recipes) { + super(Pylon.name, PylonRecipeRegistry.recipes, recipes); } + @Override + protected String getRecipeInfo(PylonRecipe recipe) { + return LogHelper.getStackDescription(recipe.result); + } } } diff --git a/src/main/java/modtweaker2/mods/botania/commands/BotaniaLogger.java b/src/main/java/modtweaker2/mods/botania/commands/BotaniaLogger.java new file mode 100644 index 0000000..95e2d9c --- /dev/null +++ b/src/main/java/modtweaker2/mods/botania/commands/BotaniaLogger.java @@ -0,0 +1,83 @@ +package modtweaker2.mods.botania.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.LogHelper; +import modtweaker2.helpers.StringHelper; +import vazkii.botania.api.BotaniaAPI; +import vazkii.botania.api.recipe.RecipeElvenTrade; +import vazkii.botania.api.recipe.RecipeManaInfusion; +import vazkii.botania.api.recipe.RecipePetals; +import vazkii.botania.api.recipe.RecipeRuneAltar; + +public class BotaniaLogger implements ICommandFunction { + + private static final List validArguments = new LinkedList(); + + static { + validArguments.add("apothecary"); + validArguments.add("elventrade"); + validArguments.add("manainfusion"); + validArguments.add("runealtar"); + } + + @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("apothecary")) { + for(RecipePetals recipe : BotaniaAPI.petalRecipes) { + MineTweakerAPI.logCommand(String.format("mods.botania.Apothecary.addRecipe(%s, %s);", + LogHelper.getStackDescription(recipe.getOutput()), + LogHelper.getListDescription(recipe.getInputs()) // Need to resolve "petalXXX" to an item + )); + } + } + + if(args.isEmpty() || args.contains("elventrade")) { + for(RecipeElvenTrade recipe : BotaniaAPI.elvenTradeRecipes) { + MineTweakerAPI.logCommand(String.format("mods.botania.ElvenTrade.addRecipe(%s, %s);", + LogHelper.getStackDescription(recipe.getOutput()), + LogHelper.getListDescription(recipe.getInputs() + ))); + } + } + + if(args.isEmpty() || args.contains("manainfusion")) { + for(RecipeManaInfusion recipe : BotaniaAPI.manaInfusionRecipes) { + MineTweakerAPI.logCommand(String.format("mods.botania.ManaInfusion.add%s(%s, %s, %d);", + recipe.isAlchemy() ? "Alchemy" : recipe.isConjuration() ? "Conjuration" : "Infusion", + LogHelper.getStackDescription(recipe.getOutput()), + LogHelper.getStackDescription(recipe.getInput()), + recipe.getManaToConsume() + )); + } + } + + if(args.isEmpty() || args.contains("runealtar")) { + for(RecipeRuneAltar recipe : BotaniaAPI.runeAltarRecipes) { + MineTweakerAPI.logCommand(String.format("mods.botania.RuneAltar.addRecipe(%s, %s, %d);", + LogHelper.getStackDescription(recipe.getOutput()), + LogHelper.getListDescription(recipe.getInputs()), + recipe.getManaUsage() + )); + } + } + + 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/botania/handlers/Apothecary.java b/src/main/java/modtweaker2/mods/botania/handlers/Apothecary.java index 4d2433b..d61b4ae 100644 --- a/src/main/java/modtweaker2/mods/botania/handlers/Apothecary.java +++ b/src/main/java/modtweaker2/mods/botania/handlers/Apothecary.java @@ -1,14 +1,19 @@ package modtweaker2.mods.botania.handlers; +import static modtweaker2.helpers.InputHelper.toIItemStack; import static modtweaker2.helpers.InputHelper.toObjects; import static modtweaker2.helpers.InputHelper.toStack; -import static modtweaker2.helpers.StackHelper.areEqual; +import static modtweaker2.helpers.StackHelper.matches; + +import java.util.LinkedList; +import java.util.List; + import minetweaker.MineTweakerAPI; import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; +import modtweaker2.helpers.LogHelper; import modtweaker2.utils.BaseListAddition; import modtweaker2.utils.BaseListRemoval; -import net.minecraft.item.ItemStack; import stanhebben.zenscript.annotations.ZenClass; import stanhebben.zenscript.annotations.ZenMethod; import vazkii.botania.api.BotaniaAPI; @@ -17,6 +22,11 @@ import vazkii.botania.common.item.block.ItemBlockSpecialFlower; @ZenClass("mods.botania.Apothecary") public class Apothecary { + + protected static final String name = "Botania Petal"; + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + @ZenMethod public static void addRecipe(IItemStack output, IIngredient[] input) { MineTweakerAPI.apply(new Add(new RecipePetals(toStack(output), toObjects(input)))); @@ -24,50 +34,55 @@ public class Apothecary { @ZenMethod public static void addRecipe(String output, IIngredient[] input) { - MineTweakerAPI.apply(new Add(new RecipePetals(ItemBlockSpecialFlower.ofType(output), toObjects(input)))); + addRecipe(toIItemStack(ItemBlockSpecialFlower.ofType(output)), input); } - private static class Add extends BaseListAddition { + private static class Add extends BaseListAddition { public Add(RecipePetals recipe) { - super("Botania Petal Recipe", BotaniaAPI.petalRecipes, recipe); + super("Botania Petal", BotaniaAPI.petalRecipes); + recipes.add(recipe); } @Override - public String getRecipeInfo() { - return ((RecipePetals) recipe).getOutput().getDisplayName(); + public String getRecipeInfo(RecipePetals recipe) { + return LogHelper.getStackDescription(recipe.getOutput()); } } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @ZenMethod - public static void removeRecipe(IItemStack output) { - MineTweakerAPI.apply(new Remove(toStack(output))); + public static void removeRecipe(IIngredient output) { + // Get list of existing recipes, matching with parameter + LinkedList result = new LinkedList(); + + for(RecipePetals entry : BotaniaAPI.petalRecipes) { + if(entry != null && entry.getOutput() != null && matches(output, toIItemStack(entry.getOutput()))) { + result.add(entry); + } + } + + // Check if we found the recipes and apply the action + if(!result.isEmpty()) { + MineTweakerAPI.apply(new Remove(result)); + } else { + LogHelper.logWarning(String.format("No %s Recipe found for %s. Command ignored!", Apothecary.name, output.toString())); + } } @ZenMethod public static void removeRecipe(String output) { - MineTweakerAPI.apply(new Remove(ItemBlockSpecialFlower.ofType(output))); + removeRecipe(toIItemStack(ItemBlockSpecialFlower.ofType(output))); } - private static class Remove extends BaseListRemoval { - public Remove(ItemStack stack) { - super("Botania Petal recipe", BotaniaAPI.petalRecipes, stack); + private static class Remove extends BaseListRemoval { + public Remove(List recipes) { + super(Apothecary.name, BotaniaAPI.petalRecipes, recipes); } @Override - public void apply() { - for (RecipePetals r : BotaniaAPI.petalRecipes) { - if (areEqual(r.getOutput(), stack)) { - recipes.add(r); - } - } - super.apply(); - } - - @Override - public String getRecipeInfo() { - return stack.getDisplayName(); + public String getRecipeInfo(RecipePetals recipe) { + return LogHelper.getStackDescription(recipe.getOutput()); } } } diff --git a/src/main/java/modtweaker2/mods/botania/handlers/ElvenTrade.java b/src/main/java/modtweaker2/mods/botania/handlers/ElvenTrade.java index c64ea40..78cfcde 100644 --- a/src/main/java/modtweaker2/mods/botania/handlers/ElvenTrade.java +++ b/src/main/java/modtweaker2/mods/botania/handlers/ElvenTrade.java @@ -1,14 +1,19 @@ package modtweaker2.mods.botania.handlers; +import static modtweaker2.helpers.InputHelper.toIItemStack; import static modtweaker2.helpers.InputHelper.toObjects; import static modtweaker2.helpers.InputHelper.toStack; -import static modtweaker2.helpers.StackHelper.areEqual; +import static modtweaker2.helpers.StackHelper.matches; + +import java.util.LinkedList; +import java.util.List; + import minetweaker.MineTweakerAPI; import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; +import modtweaker2.helpers.LogHelper; import modtweaker2.utils.BaseListAddition; import modtweaker2.utils.BaseListRemoval; -import net.minecraft.item.ItemStack; import stanhebben.zenscript.annotations.ZenClass; import stanhebben.zenscript.annotations.ZenMethod; import vazkii.botania.api.BotaniaAPI; @@ -16,47 +21,57 @@ import vazkii.botania.api.recipe.RecipeElvenTrade; @ZenClass("mods.botania.ElvenTrade") public class ElvenTrade { + + protected static final String name = "Botania Eleven Trade"; + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + @ZenMethod public static void addRecipe(IItemStack output, IIngredient[] input) { MineTweakerAPI.apply(new Add(new RecipeElvenTrade(toStack(output), toObjects(input)))); } - private static class Add extends BaseListAddition { + private static class Add extends BaseListAddition { public Add(RecipeElvenTrade recipe) { - super("Botania Eleven Trade", BotaniaAPI.elvenTradeRecipes, recipe); + super(ElvenTrade.name, BotaniaAPI.elvenTradeRecipes); + recipes.add(recipe); } @Override - public String getRecipeInfo() { - return ((RecipeElvenTrade) recipe).getOutput().getDisplayName(); + public String getRecipeInfo(RecipeElvenTrade recipe) { + return LogHelper.getStackDescription(recipe.getOutput()); } } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @ZenMethod - public static void removeRecipe(IItemStack output) { - MineTweakerAPI.apply(new Remove(toStack(output))); + public static void removeRecipe(IIngredient output) { + // Get list of existing recipes, matching with parameter + LinkedList recipes = new LinkedList(); + + for(RecipeElvenTrade entry : BotaniaAPI.elvenTradeRecipes) { + if(entry != null && entry.getOutput() != null && matches(output, toIItemStack(entry.getOutput()))) { + recipes.add(entry); + } + } + + // Check if we found the recipes and apply the action + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new Remove(recipes)); + } else { + LogHelper.logWarning(String.format("No %s Recipe found for %s. Command ignored!", ElvenTrade.name, output.toString())); + } } - private static class Remove extends BaseListRemoval { - public Remove(ItemStack stack) { - super("Botania Elven Trade", BotaniaAPI.elvenTradeRecipes, stack); + private static class Remove extends BaseListRemoval { + public Remove(List recipes) { + super(ElvenTrade.name, BotaniaAPI.elvenTradeRecipes, recipes); } @Override - public void apply() { - for (RecipeElvenTrade r : BotaniaAPI.elvenTradeRecipes) { - if (r.getOutput() != null && areEqual(r.getOutput(), stack)) { - recipes.add(r); - } - } - super.apply(); - } - - @Override - public String getRecipeInfo() { - return stack.getDisplayName(); + public String getRecipeInfo(RecipeElvenTrade recipe) { + return LogHelper.getStackDescription(recipe.getOutput()); } } } diff --git a/src/main/java/modtweaker2/mods/botania/handlers/ManaInfusion.java b/src/main/java/modtweaker2/mods/botania/handlers/ManaInfusion.java index 7b06848..5d5b898 100644 --- a/src/main/java/modtweaker2/mods/botania/handlers/ManaInfusion.java +++ b/src/main/java/modtweaker2/mods/botania/handlers/ManaInfusion.java @@ -1,14 +1,19 @@ package modtweaker2.mods.botania.handlers; +import static modtweaker2.helpers.InputHelper.toIItemStack; import static modtweaker2.helpers.InputHelper.toObject; import static modtweaker2.helpers.InputHelper.toStack; -import static modtweaker2.helpers.StackHelper.areEqual; +import static modtweaker2.helpers.StackHelper.matches; + +import java.util.LinkedList; +import java.util.List; + import minetweaker.MineTweakerAPI; import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; +import modtweaker2.helpers.LogHelper; import modtweaker2.utils.BaseListAddition; import modtweaker2.utils.BaseListRemoval; -import net.minecraft.item.ItemStack; import stanhebben.zenscript.annotations.ZenClass; import stanhebben.zenscript.annotations.ZenMethod; import vazkii.botania.api.BotaniaAPI; @@ -17,6 +22,9 @@ import vazkii.botania.api.recipe.RecipeManaInfusion; @ZenClass("mods.botania.ManaInfusion") public class ManaInfusion { + + protected static final String name = "Botania Mana Infusion"; + @ZenMethod public static void addInfusion(IItemStack output, IIngredient input, int mana) { MineTweakerAPI.apply(new Add(new RecipeManaInfusion(toStack(output), toObject(input), mana))); @@ -36,42 +44,47 @@ public class ManaInfusion { MineTweakerAPI.apply(new Add(recipe)); } - private static class Add extends BaseListAddition { + private static class Add extends BaseListAddition { public Add(RecipeManaInfusion recipe) { - super("Botania Mana Infusion", BotaniaAPI.manaInfusionRecipes, recipe); + super(ManaInfusion.name, BotaniaAPI.manaInfusionRecipes); + recipes.add(recipe); } @Override - public String getRecipeInfo() { - return ((RecipeManaInfusion) recipe).getOutput().getDisplayName(); + public String getRecipeInfo(RecipeManaInfusion recipe) { + return LogHelper.getStackDescription(recipe.getOutput()); } } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @ZenMethod - public static void removeRecipe(IItemStack output) { - MineTweakerAPI.apply(new Remove(toStack(output))); + public static void removeRecipe(IIngredient output) { + // Get list of existing recipes, matching with parameter + List recipes = new LinkedList(); + + for (RecipeManaInfusion r : BotaniaAPI.manaInfusionRecipes) { + if (r.getOutput() != null && matches(output, toIItemStack(r.getOutput()))) { + recipes.add(r); + } + } + + // Check if we found the recipes and apply the action + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new Remove(recipes)); + } else { + LogHelper.logWarning(String.format("No %s Recipe found for %s. Command ignored!", ManaInfusion.name, output.toString())); + } } - private static class Remove extends BaseListRemoval { - public Remove(ItemStack stack) { - super("Botania Mana Infusion", BotaniaAPI.manaInfusionRecipes, stack); + private static class Remove extends BaseListRemoval { + public Remove(List recipes) { + super(ManaInfusion.name, BotaniaAPI.manaInfusionRecipes, recipes); } @Override - public void apply() { - for (RecipeManaInfusion r : BotaniaAPI.manaInfusionRecipes) { - if (r.getOutput() != null && areEqual(r.getOutput(), stack)) { - recipes.add(r); - } - } - super.apply(); - } - - @Override - public String getRecipeInfo() { - return stack.getDisplayName(); + public String getRecipeInfo(RecipeManaInfusion recipe) { + return LogHelper.getStackDescription(recipe.getOutput()); } } } diff --git a/src/main/java/modtweaker2/mods/botania/handlers/RuneAltar.java b/src/main/java/modtweaker2/mods/botania/handlers/RuneAltar.java index b09c65b..51bf2eb 100644 --- a/src/main/java/modtweaker2/mods/botania/handlers/RuneAltar.java +++ b/src/main/java/modtweaker2/mods/botania/handlers/RuneAltar.java @@ -1,14 +1,19 @@ package modtweaker2.mods.botania.handlers; +import static modtweaker2.helpers.InputHelper.toIItemStack; import static modtweaker2.helpers.InputHelper.toObjects; import static modtweaker2.helpers.InputHelper.toStack; -import static modtweaker2.helpers.StackHelper.areEqual; +import static modtweaker2.helpers.StackHelper.matches; + +import java.util.LinkedList; +import java.util.List; + import minetweaker.MineTweakerAPI; import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; +import modtweaker2.helpers.LogHelper; import modtweaker2.utils.BaseListAddition; import modtweaker2.utils.BaseListRemoval; -import net.minecraft.item.ItemStack; import stanhebben.zenscript.annotations.ZenClass; import stanhebben.zenscript.annotations.ZenMethod; import vazkii.botania.api.BotaniaAPI; @@ -16,47 +21,58 @@ import vazkii.botania.api.recipe.RecipeRuneAltar; @ZenClass("mods.botania.RuneAltar") public class RuneAltar { + + protected static final String name = "Botania Rune Altar"; + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + @ZenMethod public static void addRecipe(IItemStack output, IIngredient[] input, int mana) { MineTweakerAPI.apply(new Add(new RecipeRuneAltar(toStack(output), mana, toObjects(input)))); } - private static class Add extends BaseListAddition { + private static class Add extends BaseListAddition { public Add(RecipeRuneAltar recipe) { - super("Botania Rune Altar", BotaniaAPI.runeAltarRecipes, recipe); + super(RuneAltar.name, BotaniaAPI.runeAltarRecipes); + + recipes.add(recipe); } @Override - public String getRecipeInfo() { - return ((RecipeRuneAltar) recipe).getOutput().getDisplayName(); + public String getRecipeInfo(RecipeRuneAltar recipe) { + return LogHelper.getStackDescription(recipe.getOutput()); } } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @ZenMethod - public static void removeRecipe(IItemStack output) { - MineTweakerAPI.apply(new Remove(toStack(output))); + public static void removeRecipe(IIngredient output) { + // Get list of existing recipes, matching with parameter + List recipes = new LinkedList(); + + for (RecipeRuneAltar r : BotaniaAPI.runeAltarRecipes) { + if (r != null && r.getOutput() != null && matches(output, toIItemStack(r.getOutput()))) { + recipes.add(r); + } + } + + // Check if we found the recipes and apply the action + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new Remove(recipes)); + } else { + LogHelper.logWarning(String.format("No %s Recipe found for %s. Command ignored!", ElvenTrade.name, output.toString())); + } } - private static class Remove extends BaseListRemoval { - public Remove(ItemStack stack) { - super("Botania Rune Altar", BotaniaAPI.runeAltarRecipes, stack); + private static class Remove extends BaseListRemoval { + public Remove(List recipes) { + super(RuneAltar.name, BotaniaAPI.runeAltarRecipes, recipes); } @Override - public void apply() { - for (RecipeRuneAltar r : BotaniaAPI.runeAltarRecipes) { - if (r.getOutput() != null && areEqual(r.getOutput(), stack)) { - recipes.add(r); - } - } - super.apply(); - } - - @Override - public String getRecipeInfo() { - return stack.getDisplayName(); + public String getRecipeInfo(RecipeRuneAltar recipe) { + return LogHelper.getStackDescription(recipe.getOutput()); } } } diff --git a/src/main/java/modtweaker2/mods/exnihilo/ExNihiloHelper.java b/src/main/java/modtweaker2/mods/exnihilo/ExNihiloHelper.java new file mode 100644 index 0000000..d4eca5b --- /dev/null +++ b/src/main/java/modtweaker2/mods/exnihilo/ExNihiloHelper.java @@ -0,0 +1,13 @@ +package modtweaker2.mods.exnihilo; + +import java.util.Map; + +import modtweaker2.helpers.ReflectionHelper; +import exnihilo.registries.HeatRegistry; +import exnihilo.utils.ItemInfo; + +public class ExNihiloHelper { + public static Map getHeatMap() { + return ReflectionHelper.>getStaticObject(HeatRegistry.class, "heatmap"); + } +} diff --git a/src/main/java/modtweaker2/mods/exnihilo/commands/ExNihiloLogger.java b/src/main/java/modtweaker2/mods/exnihilo/commands/ExNihiloLogger.java new file mode 100644 index 0000000..0c27f30 --- /dev/null +++ b/src/main/java/modtweaker2/mods/exnihilo/commands/ExNihiloLogger.java @@ -0,0 +1,99 @@ +package modtweaker2.mods.exnihilo.commands; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; +import java.util.Map.Entry; + +import minetweaker.MineTweakerAPI; +import minetweaker.MineTweakerImplementationAPI; +import minetweaker.api.player.IPlayer; +import minetweaker.api.server.ICommandFunction; +import modtweaker2.helpers.LogHelper; +import modtweaker2.helpers.StringHelper; +import modtweaker2.mods.exnihilo.ExNihiloHelper; +import net.minecraft.item.ItemStack; +import net.minecraftforge.fluids.FluidStack; +import exnihilo.registries.CompostRegistry; +import exnihilo.registries.CrucibleRegistry; +import exnihilo.registries.HammerRegistry; +import exnihilo.registries.SieveRegistry; +import exnihilo.registries.helpers.Compostable; +import exnihilo.registries.helpers.Meltable; +import exnihilo.registries.helpers.SiftingResult; +import exnihilo.registries.helpers.Smashable; +import exnihilo.utils.ItemInfo; + +public class ExNihiloLogger implements ICommandFunction { + + private static final List validArguments = new LinkedList(); + + static { + validArguments.add("compost"); + validArguments.add("crucible"); + validArguments.add("hammer"); + validArguments.add("sieve"); + } + + @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("compost")) { + for(Entry recipe : CompostRegistry.entries.entrySet()) { + MineTweakerAPI.logCommand(String.format("mods.exnihilo.Composting.addRecipe(%s, %s, \"%s\");", + LogHelper.getStackDescription(recipe.getKey().getStack()), + recipe.getValue().value, + Integer.toHexString(recipe.getValue().color.toInt()).substring(2))); + } + } + + if(args.isEmpty() || args.contains("crucible")) { + for(Meltable recipe : CrucibleRegistry.entries.values()) { + MineTweakerAPI.logCommand(String.format("mods.exnihilo.Crucible.addRecipe(%s, %s);", + LogHelper.getStackDescription(new ItemStack(recipe.block, 1, recipe.meta)), + LogHelper.getStackDescription(new FluidStack(recipe.fluid, (int)recipe.fluidVolume)))); + } + + for(Entry recipe : ExNihiloHelper.getHeatMap().entrySet()) { + MineTweakerAPI.logCommand(String.format("mods.exnihilo.Crucible.addHeatSource(%s, %s);", + LogHelper.getStackDescription(recipe.getKey().getStack()), + recipe.getValue())); + } + } + + if(args.isEmpty() || args.contains("hammer")) { + for(Entry> entry : HammerRegistry.getRewards().entrySet()) { + for(Smashable recipe : entry.getValue()) { + MineTweakerAPI.logCommand(String.format("mods.exnihilo.Hammer.addRecipe(%s, %s, %s, %s);", + LogHelper.getStackDescription(entry.getKey().getStack()), + LogHelper.getStackDescription(new ItemStack(recipe.item, 1, recipe.meta)), + recipe.chance, + recipe.luckMultiplier)); + } + } + } + + if(args.isEmpty() || args.contains("sieve")) { + for(Entry> entry : SieveRegistry.getSiftables().entrySet()) { + for(SiftingResult recipe : entry.getValue()) { + MineTweakerAPI.logCommand(String.format("mods.exnihilo.Sieve.addRecipe(%s, %s, %s);", + LogHelper.getStackDescription(entry.getKey().getStack()), + LogHelper.getStackDescription(new ItemStack(recipe.item, 1, recipe.meta)), + recipe.rarity)); + } + } + } + + 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/exnihilo/handlers/Compost.java b/src/main/java/modtweaker2/mods/exnihilo/handlers/Compost.java index 5b8d225..04c4457 100644 --- a/src/main/java/modtweaker2/mods/exnihilo/handlers/Compost.java +++ b/src/main/java/modtweaker2/mods/exnihilo/handlers/Compost.java @@ -1,117 +1,92 @@ package modtweaker2.mods.exnihilo.handlers; +import static modtweaker2.helpers.InputHelper.toIItemStack; import static modtweaker2.helpers.InputHelper.toStack; -import minetweaker.IUndoableAction; +import static modtweaker2.helpers.StackHelper.matches; + +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; + import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; -import net.minecraft.item.Item; -import net.minecraft.item.ItemStack; +import modtweaker2.helpers.LogHelper; +import modtweaker2.utils.BaseMapAddition; +import modtweaker2.utils.BaseMapRemoval; import stanhebben.zenscript.annotations.Optional; import stanhebben.zenscript.annotations.ZenClass; import stanhebben.zenscript.annotations.ZenMethod; import exnihilo.registries.CompostRegistry; import exnihilo.registries.helpers.Color; +import exnihilo.registries.helpers.Compostable; +import exnihilo.utils.ItemInfo; + @ZenClass("mods.exnihilo.Composting") public class Compost { - //Adding a Ex Nihilo Composting recipe + + public static final String name = "ExNihilo Composting"; + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + @ZenMethod public static void addRecipe(IItemStack input, double value, @Optional String hex) { - hex = (hex == null || hex.equals("")) ? "35A82A" : hex; - MineTweakerAPI.apply(new Add(toStack(input).getItem(), toStack(input).getItemDamage(), Math.min(1.0F, (float) value), new Color(hex))); - } - - //Passes the list to the map list implementation, and adds the recipe - private static class Add implements IUndoableAction - { - private Item item; - private int meta; - private float value; - private Color color; - - public Add(Item item, int meta, float value, Color color) { - this.item = item; - this.meta = meta; - this.value = value; - this.color = color; + if(input == null) { + LogHelper.logError(String.format("Required parameters missing for %s Recipe.", name)); + return; + } + + hex = (hex == null || hex.equals("")) ? "35A82A" : hex; + + Map recipes = new HashMap(); + + recipes.put( + new ItemInfo(toStack(input)), + new Compostable(Math.min(1.0F, (float)value), new Color(hex))); + + MineTweakerAPI.apply(new Add(recipes)); + } + + private static class Add extends BaseMapAddition { + public Add(Map recipes) { + super(Compost.name, CompostRegistry.entries, recipes); + } + + @Override + protected String getRecipeInfo(Entry recipe) { + return LogHelper.getStackDescription(recipe.getKey().getStack()); } - - @Override - public void apply() { - CompostRegistry.register(item, meta, value, color); - } - - @Override - public boolean canUndo() { - return false; - } - - @Override - public String describe() { - return "Adding Composting Recipe using " + item.getUnlocalizedName(); - } - - @Override - public String describeUndo() { - return null; - } - - @Override - public Object getOverrideKey() { - return null; - } - - @Override - public void undo() { - - } - } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - //Removing a Ex Nihilo Composting recipe @ZenMethod - public static void removeRecipe(IItemStack input) { - MineTweakerAPI.apply(new Remove(toStack(input))); - } - - //Removes a recipe, will always remove the key, so all should be good - private static class Remove implements IUndoableAction - { - private ItemStack stack; - - public Remove(ItemStack stack) { - this.stack = stack; + public static void removeRecipe(IIngredient input) { + Map recipes = new HashMap(); + + for(Entry recipe : CompostRegistry.entries.entrySet()) { + if(matches(input, toIItemStack(recipe.getKey().getStack()))) { + recipes.put(recipe.getKey(), recipe.getValue()); + } + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new Remove(recipes)); + } else { + LogHelper.logWarning(String.format("No %s recipes found for %s. Command ignored!", Compost.name, input.toString())); + } + + } + + private static class Remove extends BaseMapRemoval { + public Remove(Map recipes) { + super(Compost.name, CompostRegistry.entries, recipes); + } + + @Override + protected String getRecipeInfo(Entry recipe) { + return LogHelper.getStackDescription(recipe.getKey().getStack()); } - - @Override - public void apply() { - CompostRegistry.unregister(stack.getItem(), stack.getItemDamage()); - } - - @Override - public boolean canUndo() { - return false; - } - - @Override - public String describe() { - return "Removing Composting Recipe using " + stack.getUnlocalizedName(); - } - - @Override - public String describeUndo() { - return null; - } - - @Override - public Object getOverrideKey() { - return null; - } - - @Override - public void undo() { - } } } diff --git a/src/main/java/modtweaker2/mods/exnihilo/handlers/Crucible.java b/src/main/java/modtweaker2/mods/exnihilo/handlers/Crucible.java index b0a3f32..dff1082 100644 --- a/src/main/java/modtweaker2/mods/exnihilo/handlers/Crucible.java +++ b/src/main/java/modtweaker2/mods/exnihilo/handlers/Crucible.java @@ -2,44 +2,74 @@ package modtweaker2.mods.exnihilo.handlers; import static modtweaker2.helpers.InputHelper.isABlock; 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 minetweaker.IUndoableAction; +import static modtweaker2.helpers.StackHelper.matches; + +import java.util.HashMap; +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.LogHelper; +import modtweaker2.mods.exnihilo.ExNihiloHelper; import modtweaker2.utils.BaseMapAddition; import modtweaker2.utils.BaseMapRemoval; import net.minecraft.block.Block; import net.minecraft.item.ItemStack; +import net.minecraftforge.fluids.FluidStack; import stanhebben.zenscript.annotations.ZenClass; import stanhebben.zenscript.annotations.ZenMethod; import exnihilo.registries.CrucibleRegistry; -import exnihilo.registries.HeatRegistry; import exnihilo.registries.helpers.Meltable; +import exnihilo.utils.ItemInfo; @ZenClass("mods.exnihilo.Crucible") public class Crucible { + public static final String nameMelting = "ExNihilo Crucible (Melting)"; + public static final String nameHeatSource = "ExNihilo Crucible (Heat source)"; + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + /************************************************ Crucible Melting ************************************************/ //Adding a Ex Nihilo Crucible recipe @ZenMethod public static void addRecipe(IItemStack input, ILiquidStack fluid) { - if (isABlock(input)) { - Block theBlock = Block.getBlockFromItem(toStack(input).getItem()); - int theMeta = toStack(input).getItemDamage(); - MineTweakerAPI.apply(new AddRecipe(new Meltable(theBlock, theMeta, 2000, toFluid(fluid).getFluid(), toFluid(fluid).amount, theBlock))); + if(input == null || fluid == null) { + LogHelper.logError(String.format("Required parameters missing for %s recipe.", nameMelting)); + return; } + + if(!isABlock(input)) { + LogHelper.logError(String.format("Input item for %s recipe must be a block.", nameMelting)); + return; + } + + Map recipes = new HashMap(); + Block theBlock = Block.getBlockFromItem(toStack(input).getItem()); + int theMeta = toStack(input).getItemDamage(); + + recipes.put( + theBlock + ":" + theMeta, + new Meltable(theBlock, theMeta, 2000, toFluid(fluid).getFluid(), toFluid(fluid).amount, theBlock)); + + MineTweakerAPI.apply(new AddRecipe(recipes)); } //Passes the list to the map list implementation, and adds the recipe - private static class AddRecipe extends BaseMapAddition { - public AddRecipe(Meltable recipe) { - super("ExNihilo Crucible", CrucibleRegistry.entries, recipe.block + ":" + recipe.meta, recipe); + private static class AddRecipe extends BaseMapAddition { + public AddRecipe(Map recipes) { + super(nameMelting, CrucibleRegistry.entries, recipes); } - + @Override - public String getRecipeInfo() { - return new ItemStack(((Meltable) recipe).block, 1, ((Meltable) recipe).meta).getDisplayName(); + protected String getRecipeInfo(Entry recipe) { + return LogHelper.getStackDescription(new ItemStack(recipe.getValue().block, 1, recipe.getValue().meta)); } } @@ -47,21 +77,32 @@ public class Crucible { //Removing a Ex Nihilo Crucible recipe @ZenMethod - public static void removeRecipe(IItemStack output) { - if (isABlock(output)) { - MineTweakerAPI.apply(new RemoveRecipe(toStack(output))); + public static void removeRecipe(IIngredient output) { + Map recipes = new HashMap(); + + for(Entry entry : CrucibleRegistry.entries.entrySet()) { + FluidStack fluid = new FluidStack(entry.getValue().fluid, (int)entry.getValue().fluidVolume); + if(matches(output, toILiquidStack(fluid))) { + recipes.put(entry.getKey(), entry.getValue()); + } + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new RemoveRecipe(recipes)); + } else { + LogHelper.logWarning(String.format("No %s recipe found for %s. Command ignored!", Crucible.nameMelting, output.toString())); } } //Removes a recipe, will always remove the key, so all should be good - private static class RemoveRecipe extends BaseMapRemoval { - public RemoveRecipe(ItemStack stack) { - super("ExNihilo Crucible", CrucibleRegistry.entries, Block.getBlockFromItem(stack.getItem()) + ":" + stack.getItemDamage(), stack); + private static class RemoveRecipe extends BaseMapRemoval { + public RemoveRecipe(Map recipes) { + super(nameMelting, CrucibleRegistry.entries, recipes); } @Override - public String getRecipeInfo() { - return ((ItemStack) stack).getDisplayName(); + protected String getRecipeInfo(Entry recipe) { + return LogHelper.getStackDescription(new ItemStack(recipe.getValue().block, 1, recipe.getValue().meta)); } } @@ -69,92 +110,67 @@ public class Crucible { //Adding a Ex Nihilo Crucible heat source @ZenMethod public static void addHeatSource(IItemStack input, double value) { - if (isABlock(input)) { - Block theBlock = Block.getBlockFromItem(toStack(input).getItem()); - int theMeta = toStack(input).getItemDamage(); - MineTweakerAPI.apply(new AddHeatSource(theBlock, theMeta, (float) value)); + if(input == null) { + LogHelper.logError(String.format("Required parameters missing for %s recipe.", nameMelting)); + return; + } + + if(!isABlock(input)) { + LogHelper.logError(String.format("Input item for %s recipe must be a block.", nameMelting)); + return; + } + + Map recipes = new HashMap(); + recipes.put(new ItemInfo(toStack(input)), (float)value); + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new AddHeatSource(recipes)); } } //Passes the list to the base map implementation, and adds the recipe - private static class AddHeatSource implements IUndoableAction + private static class AddHeatSource extends BaseMapAddition { - Block source; - int sourceMeta; - float value; - - public AddHeatSource(Block source, int sourceMeta, float value) { - this.source = source; - this.sourceMeta = sourceMeta; - this.value = value; + public AddHeatSource(Map recipes) { + super(Crucible.nameHeatSource, ExNihiloHelper.getHeatMap(), recipes); } - public void apply() { - HeatRegistry.register(source, sourceMeta, value); - } - - public boolean canUndo() { - return false; - } - - public String describe() { - return "Adding ExNihilo Heat source of " + source.getLocalizedName(); - } - - public String describeUndo() { - return null; - } - - public Object getOverrideKey() { - return null; - } - - public void undo() { + @Override + protected String getRecipeInfo(Entry recipe) { + return LogHelper.getStackDescription(recipe.getKey().getStack()); } } - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //Removing a Ex Nihilo Crucible heat source @ZenMethod - public static void removeHeatSource(IItemStack output) { - if (isABlock(output)) { - Block block = Block.getBlockFromItem(toStack(output).getItem()); - MineTweakerAPI.apply(new RemoveHeatSource(block)); + public static void removeHeatSource(IIngredient output) { + Map recipes = new HashMap(); + + for(Entry entry : ExNihiloHelper.getHeatMap().entrySet()) { + if(matches(output, toIItemStack(entry.getKey().getStack()))) { + recipes.put(entry.getKey(), entry.getValue()); + } + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new RemoveHeatSource(recipes)); + } else { + LogHelper.logWarning(String.format("No %s recipe found for %s. Command ignored!", Crucible.nameMelting, output.toString())); } } //Removes a recipe, will always remove the key, so all should be good - private static class RemoveHeatSource implements IUndoableAction + private static class RemoveHeatSource extends BaseMapAddition { - Block block; - - public RemoveHeatSource(Block block) { - this.block = block; + public RemoveHeatSource(Map recipes) { + super(Crucible.nameHeatSource, ExNihiloHelper.getHeatMap(), recipes); } - public void apply() { - HeatRegistry.unregister(block); - } - - public boolean canUndo() { - return false; - } - - public String describe() { - return "Removing ExNihilo Heat source of " + block.getLocalizedName(); - } - - public String describeUndo() { - return null; - } - - public Object getOverrideKey() { - return null; - } - - public void undo() { - } + @Override + protected String getRecipeInfo(Entry recipe) { + return LogHelper.getStackDescription(recipe.getKey().getStack()); + } } } diff --git a/src/main/java/modtweaker2/mods/exnihilo/handlers/Hammer.java b/src/main/java/modtweaker2/mods/exnihilo/handlers/Hammer.java index 6c27e89..27242ce 100644 --- a/src/main/java/modtweaker2/mods/exnihilo/handlers/Hammer.java +++ b/src/main/java/modtweaker2/mods/exnihilo/handlers/Hammer.java @@ -1,16 +1,26 @@ package modtweaker2.mods.exnihilo.handlers; import static modtweaker2.helpers.InputHelper.isABlock; +import static modtweaker2.helpers.InputHelper.toIItemStack; import static modtweaker2.helpers.InputHelper.toStack; +import static modtweaker2.helpers.StackHelper.matches; -import java.util.List; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Map.Entry; -import minetweaker.IUndoableAction; import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; +import minetweaker.api.item.IngredientAny; +import modtweaker2.helpers.LogHelper; +import modtweaker2.utils.BaseMapAddition; +import modtweaker2.utils.BaseMapRemoval; import net.minecraft.block.Block; -import net.minecraft.item.Item; import net.minecraft.item.ItemStack; +import stanhebben.zenscript.annotations.Optional; import stanhebben.zenscript.annotations.ZenClass; import stanhebben.zenscript.annotations.ZenMethod; import exnihilo.registries.HammerRegistry; @@ -19,106 +29,221 @@ import exnihilo.utils.ItemInfo; @ZenClass("mods.exnihilo.Hammer") public class Hammer { - // Adding a Ex Nihilo Hammer recipe + + public static final String name = "ExNihilo Hammer"; + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + @ZenMethod public static void addRecipe(IItemStack input, IItemStack output, double chance, double luck) { - if (isABlock(input)) { - Block theBlock = Block.getBlockFromItem(toStack(input).getItem()); - int theMeta = toStack(input).getItemDamage(); - MineTweakerAPI.apply(new Add(theBlock, theMeta, toStack(output).getItem(), toStack(output).getItemDamage(), (float) chance, (float) luck)); - } + if(input == null || output == null) { + LogHelper.logError(String.format("Required parameters missing for %s recipe.", name)); + return; + } + + addRecipe(input, new IItemStack[] { output }, new double[] { chance }, new double[] { luck }); + } + + @ZenMethod + public static void addRecipe(IItemStack input, IItemStack[] output, double[] chance, double[] luck) { + if(input == null || output == null || chance == null || luck == null) { + LogHelper.logError(String.format("Required parameters missing for %s recipe.", name)); + return; + } + + if(!isABlock(input)) { + LogHelper.logError(String.format("Input item for %s recipe must be a block.", name)); + return; + } + + if(output.length == 0 || output.length != chance.length || output.length != luck.length) { + LogHelper.logError(String.format("Number of items in arrays are different or null (%d, %d, %d)", output.length, chance.length, luck.length)); + return; + } + + Map> recipes = new HashMap>(); + ArrayList list = new ArrayList(); + ItemInfo itemInfo = new ItemInfo(toStack(input)); + recipes.put(itemInfo, list); + + for(int i = 0; i < output.length; i++) { + ItemStack out = toStack(output[i]); + list.add(new Smashable(Block.getBlockFromItem(itemInfo.getItem()), itemInfo.getMeta(), out.getItem(), out.getItemDamage(), (float)chance[i], (float)luck[i])); + } + + MineTweakerAPI.apply(new Add(recipes)); } - // Passes the list to the base list implementation, and adds the recipe - private static class Add implements IUndoableAction { - - Block block; - int blockMeta; - Item output; - int outputMeta; - float chance; - float luck; - - public Add(Block block, int blockMeta, Item output, int outputMeta, float chance, float luck) { - this.block = block; - this.blockMeta = blockMeta; - this.output = output; - this.outputMeta = outputMeta; - this.chance = chance; - this.luck = luck; + private static class Add extends BaseMapAddition> { + public Add(Map> recipes) { + super(Hammer.name, HammerRegistry.getRewards(), recipes); } public void apply() { - HammerRegistry.register(block, blockMeta, output, outputMeta, chance, luck); - } - - public boolean canUndo() { - return false; - } - - public String describe() { - return "Adding ExNihilo Hammer recipe by mining " + block.getLocalizedName() + " to get the result of " + output.getUnlocalizedName(); - } - - public String describeUndo() { - return ""; - } - - public Object getOverrideKey() { - return null; + for(Entry> entry : recipes.entrySet()) { + if(!map.containsKey(entry.getKey())) { + // no recipe for input available, add new entry + map.put(entry.getKey(), new ArrayList(entry.getValue())); + } else { + // recipes already available, add to present list + ArrayList list = map.get(entry.getKey()); + + // store old Smashable objects if present + ArrayList backup = new ArrayList(); + for(Smashable recipe : entry.getValue()) { + backup.addAll(removeFromList(list, recipe)); + } + + if(!backup.isEmpty()) { + LogHelper.logWarning(String.format("Overwritten %d %s recipe outputs for %s.", backup.size(), Hammer.name, LogHelper.getStackDescription(entry.getKey().getStack()))); + overwritten.put(entry.getKey(), backup); + } + + // add new Smashable objects + list.addAll(entry.getValue()); + } + + successful.put(entry.getKey(), entry.getValue()); + } } public void undo() { + // remove all recipes + for(Entry> entry : successful.entrySet()) { + ArrayList list = map.get(entry.getKey()); + for(Smashable recipe : entry.getValue()) { + removeFromList(list, recipe); + } + } + + // re-add overwritten recipes + for(Entry> entry : overwritten.entrySet()) { + map.get(entry.getKey()).addAll(entry.getValue()); + } + + // remove empty entries + for(Iterator>> it = map.entrySet().iterator(); it.hasNext(); ) { + Entry> entry = it.next(); + if(entry.getValue().isEmpty()) { + it.remove(); + } + } + } + + @Override + protected String getRecipeInfo(Entry> recipe) { + return LogHelper.getStackDescription(recipe.getKey().getStack()) + " (" + recipe.getValue().size() + " entries)"; } } - // /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - // Removing a Ex Nihilo Hammer recipe + @Deprecated @ZenMethod public static void removeRecipe(IItemStack inputBlock, int inputBlockMeta, IItemStack outputItem, int outputItemMeta) { - MineTweakerAPI.apply(new Remove(Block.getBlockFromItem(toStack(inputBlock).getItem()), inputBlockMeta, toStack(outputItem).getItem(), outputItemMeta)); + removeRecipe(toIItemStack(new ItemStack(toStack(inputBlock).getItem(), 1, inputBlockMeta)), toIItemStack(new ItemStack(toStack(outputItem).getItem(), 1, outputItemMeta))); + } + + @ZenMethod + public static void removeRecipe(IIngredient input, @Optional IIngredient output) { + if(input == null) { + LogHelper.logError(String.format("Required parameters missing for %s recipe.", name)); + return; + } + + if(output == null) { + output = IngredientAny.INSTANCE; + } + + Map> recipes = new HashMap>(); + + for(Entry> entry : HammerRegistry.getRewards().entrySet()) { + if(input.matches(toIItemStack(entry.getKey().getStack()))) { + ArrayList list = entry.getValue(); + ArrayList toRemove = new ArrayList(); + + for(Smashable recipe : list) { + ItemStack item = new ItemStack(recipe.item, 1, recipe.meta); + + if(matches(output, toIItemStack(item))) { + toRemove.add(recipe); + } + } + + if(!toRemove.isEmpty()) { + recipes.put(entry.getKey(), toRemove); + } + } + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new Remove(recipes)); + } else { + LogHelper.logWarning(String.format("No %s recipes found for %s and %s. Command ignored!", input.toString(), output.toString())); + } } // Removes a recipe, apply is never the same for anything, so will always // need to override it - private static class Remove implements IUndoableAction { - Block block; - int inputBlockMeta; - Item output; - int outputItemMeta; - + private static class Remove extends BaseMapRemoval> { - public Remove(Block block, int inputBlockMeta, Item output, int outputItemMeta) { - this.block = block; - this.inputBlockMeta = inputBlockMeta; - this.output = output; - this.outputItemMeta = outputItemMeta; + public Remove(Map> recipes) { + super(Hammer.name, HammerRegistry.getRewards(), recipes); } - // Loops through the registry, to find the item that matches, saves that - // recipe then removes it + @Override public void apply() { - HammerRegistry.remove(block, inputBlockMeta, output, outputItemMeta); - } - - public boolean canUndo() { - return false; - } - - public String describe() { - return "Removing ExNihilo Hammer recipe"; - } - - public String describeUndo() { - return null; - } - - public Object getOverrideKey() { - return null; + for(Entry> entry : recipes.entrySet()) { + ArrayList list = map.get(entry.getKey()); + + for(Smashable recipe : entry.getValue()) { + removeFromList(list, recipe); + } + + successful.put(entry.getKey(), entry.getValue()); + } + + // remove empty entries + for(Iterator>> it = map.entrySet().iterator(); it.hasNext(); ) { + Entry> entry = it.next(); + if(entry.getValue().isEmpty()) { + it.remove(); + } + } } + @Override public void undo() { + for(Entry> entry : successful.entrySet()) { + if(!map.containsKey(entry.getKey())) { + map.put(entry.getKey(), new ArrayList(entry.getValue())); + } else { + map.get(entry.getKey()).addAll(entry.getValue()); + } + } } + + @Override + protected String getRecipeInfo(Entry> recipe) { + return LogHelper.getStackDescription(recipe.getKey().getStack()) + " (" + recipe.getValue().size() + " entries)"; + } } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + public static ArrayList removeFromList(ArrayList list, Smashable s) { + ArrayList removed = new ArrayList(); + ItemInfo toRemove = new ItemInfo(s.item, s.meta); + for(Iterator it = list.iterator(); it.hasNext(); ) { + Smashable itS = it.next(); + ItemInfo item = new ItemInfo(itS.item, itS.meta); + + if(item.equals(toRemove)) { + removed.add(itS); + it.remove(); + } + } + + return removed; + } } diff --git a/src/main/java/modtweaker2/mods/exnihilo/handlers/Sieve.java b/src/main/java/modtweaker2/mods/exnihilo/handlers/Sieve.java index d9c8863..bcd929a 100644 --- a/src/main/java/modtweaker2/mods/exnihilo/handlers/Sieve.java +++ b/src/main/java/modtweaker2/mods/exnihilo/handlers/Sieve.java @@ -1,224 +1,266 @@ package modtweaker2.mods.exnihilo.handlers; import static modtweaker2.helpers.InputHelper.isABlock; +import static modtweaker2.helpers.InputHelper.toIItemStack; import static modtweaker2.helpers.InputHelper.toStack; +import static modtweaker2.helpers.StackHelper.matches; import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Map.Entry; -import minetweaker.IUndoableAction; import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; -import net.minecraft.block.Block; -import net.minecraft.item.Item; +import minetweaker.api.item.IngredientAny; +import modtweaker2.helpers.LogHelper; +import modtweaker2.utils.BaseMapAddition; +import modtweaker2.utils.BaseMapRemoval; +import net.minecraft.item.ItemStack; +import stanhebben.zenscript.annotations.Optional; import stanhebben.zenscript.annotations.ZenClass; import stanhebben.zenscript.annotations.ZenMethod; import exnihilo.registries.SieveRegistry; import exnihilo.registries.helpers.SiftingResult; +import exnihilo.utils.ItemInfo; @ZenClass("mods.exnihilo.Sieve") public class Sieve { + + public static final String name = "ExNihilo Sieve"; + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + // Adding a Ex Nihilo Sieve recipe @ZenMethod public static void addRecipe(IItemStack input, IItemStack output, int rarity) { - if (isABlock(input)) { - Block theBlock = Block.getBlockFromItem(toStack(input).getItem()); - int theMeta = toStack(input).getItemDamage(); - MineTweakerAPI.apply(new Add(theBlock, theMeta, toStack(output).getItem(), toStack(output).getItemDamage(), rarity)); - } + if(input == null || output == null) { + LogHelper.logError(String.format("Required parameters missing for %s recipe.", name)); + return; + } + + addRecipe(input, new IItemStack[] { output }, new int[] { rarity }); + } + + public static void addRecipe(IItemStack input, IItemStack[] output, int[] rarity) { + if(input == null || output == null || rarity == null) { + LogHelper.logError(String.format("Required parameters missing for %s recipe.", name)); + return; + } + + if(!isABlock(input)) { + LogHelper.logError(String.format("Input item for %s recipe must be a block.", name)); + return; + } + + if(output.length == 0 || output.length != rarity.length) { + LogHelper.logError(String.format("Number of items in arrays are different or null (%d, %d)", output.length, rarity.length)); + return; + } + + Map> recipes = new HashMap>(); + ArrayList list = new ArrayList(); + ItemInfo itemInfo = new ItemInfo(toStack(input)); + recipes.put(itemInfo, list); + + for(int i = 0; i < output.length; i++) { + ItemStack out = toStack(output[i]); + list.add(new SiftingResult(out.getItem(), out.getItemDamage(), rarity[i])); + } + + MineTweakerAPI.apply(new Add(recipes)); } - // Passes the list to the base list implementation, and adds the recipe - private static class Add implements IUndoableAction { - Block source; - int sourceMeta; - Item output; - int outputMeta; - int rarity; + private static class Add extends BaseMapAddition> { - public Add(Block source, int sourceMeta, Item output, int outputMeta, int rarity) { - this.source = source; - this.sourceMeta = sourceMeta; - this.output = output; - this.outputMeta = outputMeta; - this.rarity = rarity; + public Add(Map> recipes) { + super(Sieve.name, SieveRegistry.getSiftables(), recipes); } - + + @Override public void apply() { - SieveRegistry.register(source, sourceMeta, output, outputMeta, rarity); - + for(Entry> entry : recipes.entrySet()) { + if(!map.containsKey(entry.getKey())) { + // no recipe for input available, add new entry + map.put(entry.getKey(), new ArrayList(entry.getValue())); + } else { + // recipes already available, add to present list + ArrayList list = map.get(entry.getKey()); + + // store old SiftingResult objects if present + ArrayList backup = new ArrayList(); + for(SiftingResult recipe : entry.getValue()) { + backup.addAll(removeFromList(list, recipe)); + } + + if(!backup.isEmpty()) { + LogHelper.logWarning(String.format("Overwritten %d %s recipe outputs for %s.", backup.size(), Hammer.name, LogHelper.getStackDescription(entry.getKey().getStack()))); + overwritten.put(entry.getKey(), backup); + } + + // add new SiftingResult objects + list.addAll(entry.getValue()); + } + + successful.put(entry.getKey(), entry.getValue()); + } } - - public boolean canUndo() { - return source != null && output != null; - } - - public String describe() { - return "Adding ExNihilo Sieve Recipe using " + source.getLocalizedName() + " to get the result of " + output.getUnlocalizedName(); - - } - - public String describeUndo() { - return "Removing ExNihilo Sieve Recipe using " + source.getLocalizedName() + " to get the result of " + output.getUnlocalizedName(); - } - - public Object getOverrideKey() { - return null; - } - + + @Override public void undo() { - if (this.canUndo()) - SieveRegistry.unregisterReward(this.source, this.sourceMeta, this.output, this.outputMeta); + // remove all recipes + for(Entry> entry : successful.entrySet()) { + ArrayList list = map.get(entry.getKey()); + for(SiftingResult recipe : entry.getValue()) { + removeFromList(list, recipe); + } + } + + // re-add overwritten recipes + for(Entry> entry : overwritten.entrySet()) { + map.get(entry.getKey()).addAll(entry.getValue()); + } + + // remove empty entries + for(Iterator>> it = map.entrySet().iterator(); it.hasNext(); ) { + Entry> entry = it.next(); + if(entry.getValue().isEmpty()) { + it.remove(); + } + } } + + @Override + protected String getRecipeInfo(Entry> recipe) { + return LogHelper.getStackDescription(recipe.getKey().getStack()) + " (" + recipe.getValue().size() + " entries)"; + } } - // /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - // Removing a Ex Nihilo Sieve recipe + @Deprecated @ZenMethod public static void removeRecipe(IItemStack input, IItemStack output) { - if (isABlock(input)) { - Block theBlock = Block.getBlockFromItem(toStack(input).getItem()); - int theMeta = toStack(input).getItemDamage(); - MineTweakerAPI.apply(new Remove(theBlock, theMeta, toStack(output).getItem(), toStack(output).getItemDamage())); - } + removeRecipe((IIngredient)input, (IIngredient)output); } - + @Deprecated @ZenMethod public static void removeRewardFromAllBlocks(IItemStack output) { - Item theItem = toStack(output).getItem(); - int theMeta = toStack(output).getItemDamage(); - MineTweakerAPI.apply(new RemoveRewardFromAllBlocks(theItem, theMeta)); + removeRecipe(IngredientAny.INSTANCE, (IIngredient)output); } + @Deprecated @ZenMethod public static void removeAllRewardsFromBlock(IItemStack output) { - if (isABlock(output)) { - Block theBlock = Block.getBlockFromItem(toStack(output).getItem()); - int theMeta = toStack(output).getItemDamage(); - MineTweakerAPI.apply(new RemoveAllRewardsFromBlock(theBlock, theMeta)); - } + removeRecipe((IIngredient)output, IngredientAny.INSTANCE); + } + + @ZenMethod + public static void removeRecipe(IIngredient input, @Optional IIngredient output) { + if(input == null) { + LogHelper.logError(String.format("Required parameters missing for %s recipe.", name)); + return; + } + + if(output == null) { + output = IngredientAny.INSTANCE; + } + + Map> recipes = new HashMap>(); + + for(Entry> entry : SieveRegistry.getSiftables().entrySet()) { + if(input.matches(toIItemStack(entry.getKey().getStack()))) { + ArrayList list = entry.getValue(); + ArrayList toRemove = new ArrayList(); + + for(SiftingResult recipe : list) { + ItemStack item = new ItemStack(recipe.item, 1, recipe.meta); + + if(matches(output, toIItemStack(item))) { + toRemove.add(recipe); + } + } + + if(!toRemove.isEmpty()) { + recipes.put(entry.getKey(), toRemove); + } + } + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new Remove(recipes)); + } else { + LogHelper.logWarning(String.format("No %s recipes found for %s and %s. Command ignored!", input.toString(), output.toString())); + } } // Removes a recipe, apply is never the same for anything, so will always // need to override it - private static class Remove implements IUndoableAction { - Block source; - int sourceMeta; - Item output; - int outputMeta; - int rarity; + private static class Remove extends BaseMapRemoval> { + public Remove(Map> recipes) { + super(Sieve.name, SieveRegistry.getSiftables(), recipes); + } + + @Override + public void apply() { + for(Entry> entry : recipes.entrySet()) { + ArrayList list = map.get(entry.getKey()); + + for(SiftingResult recipe : entry.getValue()) { + removeFromList(list, recipe); + } + + successful.put(entry.getKey(), entry.getValue()); + } + + // remove empty entries + for(Iterator>> it = map.entrySet().iterator(); it.hasNext(); ) { + Entry> entry = it.next(); + if(entry.getValue().isEmpty()) { + it.remove(); + } + } + } - public Remove(Block source, int sourceMeta, Item output, int outputMeta) { - this.source = source; - this.sourceMeta = sourceMeta; - this.output = output; - this.outputMeta = outputMeta; - } - - public void apply() { - ArrayList results = SieveRegistry.getSiftingOutput(this.source, this.sourceMeta); - - for (SiftingResult res : results) { - if (res.item.equals(this.source) && res.meta == this.outputMeta) - this.rarity = res.rarity; - } - - SieveRegistry.unregisterReward(source, sourceMeta, output, outputMeta); - } - - public boolean canUndo() { - return output != null && source != null; - } - - public String describe() { - return "Removing ExNihilo Sieve Recipe using " + source.getLocalizedName() + " to get the result of " + output.getUnlocalizedName(); - - } - - public String describeUndo() { - return "Restoring ExNihilo Sieve Recipe using " + source.getLocalizedName() + " to get the result of " + output.getUnlocalizedName(); - } - - public Object getOverrideKey() { - return null; - } - - public void undo() { - if (this.canUndo()) - SieveRegistry.register(this.source, this.sourceMeta, this.output, this.outputMeta, rarity); - } - } - - private static class RemoveAllRewardsFromBlock implements IUndoableAction { - Block block; - int blockMeta; - - public RemoveAllRewardsFromBlock(Block block, int blockMeta) { - this.block = block; - this.blockMeta = blockMeta; - } - - public void apply() { - SieveRegistry.unregisterAllRewardsFromBlock(block, blockMeta); - } - - public boolean canUndo() { - return false; - } - - public String describe() { - return "Removing All ExNihilo Sieve rewards from " + block.getLocalizedName(); - - } - - public String describeUndo() { - return ""; - } - - public Object getOverrideKey() { - return null; - } - - public void undo() { - } - } - - private static class RemoveRewardFromAllBlocks implements IUndoableAction { - Item reward; - int rewardMeta; - - public RemoveRewardFromAllBlocks(Item reward, int rewardMeta) { - this.reward = reward; - this.rewardMeta = rewardMeta; - } - - public void apply() { - SieveRegistry.unregisterRewardFromAllBlocks(reward, rewardMeta); - } - - public boolean canUndo() { - return false; - } - - public String describe() { - return "Removing ExNihilo Sieve reward of " + reward.getUnlocalizedName() + " from all Blocks"; - - } - - public String describeUndo() { - return ""; - } - - public Object getOverrideKey() { - return null; - } - - public void undo() { - } + @Override + public void undo() { + for(Entry> entry : successful.entrySet()) { + if(!map.containsKey(entry.getKey())) { + map.put(entry.getKey(), new ArrayList(entry.getValue())); + } else { + map.get(entry.getKey()).addAll(entry.getValue()); + } + } + } + + @Override + protected String getRecipeInfo(Entry> recipe) { + return LogHelper.getStackDescription(recipe.getKey().getStack()) + " (" + recipe.getValue().size() + " entries)"; + } } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + public static ArrayList removeFromList(ArrayList list, SiftingResult s) { + ArrayList removed = new ArrayList(); + ItemInfo toRemove = new ItemInfo(s.item, s.meta); + for(Iterator it = list.iterator(); it.hasNext(); ) { + SiftingResult itS = it.next(); + ItemInfo item = new ItemInfo(itS.item, itS.meta); + + if(item.equals(toRemove)) { + removed.add(itS); + it.remove(); + } + } + + return removed; + } } diff --git a/src/main/java/modtweaker2/mods/extendedworkbench/handlers/ExtendedCrafting.java b/src/main/java/modtweaker2/mods/extendedworkbench/handlers/ExtendedCrafting.java index a0a6837..7db7b57 100644 --- a/src/main/java/modtweaker2/mods/extendedworkbench/handlers/ExtendedCrafting.java +++ b/src/main/java/modtweaker2/mods/extendedworkbench/handlers/ExtendedCrafting.java @@ -2,24 +2,30 @@ package modtweaker2.mods.extendedworkbench.handlers; import static modtweaker2.helpers.InputHelper.toStack; import static modtweaker2.helpers.InputHelper.toStacks; -import static modtweaker2.helpers.StackHelper.areEqual; import java.util.Arrays; +import java.util.List; import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; -import modtweaker2.utils.BaseListAddition; -import modtweaker2.utils.BaseListRemoval; +import modtweaker2.helpers.LogHelper; +import modtweaker2.utils.BaseCraftingAddition; +import modtweaker2.utils.BaseCraftingRemoval; import naruto1310.extendedWorkbench.crafting.ExtendedCraftingManager; import naruto1310.extendedWorkbench.crafting.ExtendedShapedRecipes; import naruto1310.extendedWorkbench.crafting.ExtendedShapelessRecipes; import naruto1310.extendedWorkbench.crafting.IExtendedRecipe; import net.minecraft.item.ItemStack; +import net.minecraft.item.crafting.IRecipe; import stanhebben.zenscript.annotations.ZenClass; import stanhebben.zenscript.annotations.ZenMethod; @ZenClass("mods.extendedworkbench") public class ExtendedCrafting { + + public static final String name = "Extended Workbench"; + @ZenMethod public static void addShaped(IItemStack output, IItemStack[][] ingredients) { MineTweakerAPI.apply(new Add(getShapedRecipe(output, ingredients))); @@ -30,17 +36,10 @@ public class ExtendedCrafting { MineTweakerAPI.apply(new Add(new ExtendedShapelessRecipes(toStack(output), Arrays.asList(toStacks(ingredients))))); } - private static class Add extends BaseListAddition { + private static class Add extends BaseCraftingAddition { public Add(IExtendedRecipe recipe) { - super("Extended Workbench", ExtendedCraftingManager.getInstance().getRecipeList(), recipe); - } - - @Override - public String getRecipeInfo() { - Object out = ((IExtendedRecipe) recipe).getRecipeOutput(); - if (out != null) { - return ((ItemStack) out).getDisplayName(); - } else return super.getRecipeInfo(); + super(ExtendedCrafting.name, ExtendedCraftingManager.getInstance().getRecipeList()); + recipes.add(recipe); } } @@ -71,32 +70,13 @@ public class ExtendedCrafting { ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @ZenMethod - public static void removeRecipe(IItemStack output) { - MineTweakerAPI.apply(new Remove(toStack(output))); - } - - private static class Remove extends BaseListRemoval { - public Remove(ItemStack stack) { - super("Extended Workbench", ExtendedCraftingManager.getInstance().getRecipeList(), stack); - } - - @Override - public void apply() { - for (Object o : ExtendedCraftingManager.getInstance().getRecipeList()) { - if (o instanceof IExtendedRecipe) { - IExtendedRecipe r = (IExtendedRecipe) o; - if (r.getRecipeOutput() != null && areEqual((ItemStack) r.getRecipeOutput(), stack)) { - recipes.add(r); - } - } - } - super.apply(); - } - - @Override - public String getRecipeInfo() { - return stack.getDisplayName(); + public static void removeRecipe(IIngredient output) { + List recipes = BaseCraftingRemoval.getRecipes(ExtendedCraftingManager.getInstance().getRecipeList(), output); + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new BaseCraftingRemoval(ExtendedCrafting.name, ExtendedCraftingManager.getInstance().getRecipeList(), recipes)); + } else { + LogHelper.logWarning(String.format("No %s Recipe found for %s. Command ignored!", ExtendedCrafting.name, output)); } } - } diff --git a/src/main/java/modtweaker2/mods/factorization/FactorizationHelper.java b/src/main/java/modtweaker2/mods/factorization/FactorizationHelper.java index 7e48644..3410022 100644 --- a/src/main/java/modtweaker2/mods/factorization/FactorizationHelper.java +++ b/src/main/java/modtweaker2/mods/factorization/FactorizationHelper.java @@ -2,14 +2,17 @@ package modtweaker2.mods.factorization; import static modtweaker2.helpers.ReflectionHelper.getStaticObject; -import java.lang.reflect.Constructor; import java.util.List; +import modtweaker2.helpers.ReflectionHelper; import net.minecraft.item.ItemStack; public class FactorizationHelper { + @SuppressWarnings("rawtypes") public static List lacerator = null; + @SuppressWarnings("rawtypes") public static List slag = null; + @SuppressWarnings("rawtypes") public static List crystallizer = null; static { @@ -22,36 +25,27 @@ public class FactorizationHelper { private FactorizationHelper() {} - public static Object getLaceratorRecipe(ItemStack input, ItemStack output, float probability) { - try { - Class clazz = Class.forName("factorization.oreprocessing.TileEntityGrinder$GrinderRecipe"); - Constructor constructor = clazz.getDeclaredConstructor(Object.class, ItemStack.class, float.class); - constructor.setAccessible(true); - return constructor.newInstance(input, output, probability); - } catch (Exception e) { - throw new NullPointerException("Failed to instantiate Lacerator Recipe"); - } + public static Object getLaceratorRecipe(Object input, ItemStack output, float probability) { + return ReflectionHelper.getInstance(ReflectionHelper.getConstructor("factorization.oreprocessing.TileEntityGrinder$GrinderRecipe", Object.class, ItemStack.class, float.class), + input, + output, + probability); } public static Object getSlagFurnaceRecipe(ItemStack input, float chance1, ItemStack output1, float chance2, ItemStack output2) { - try { - Class clazz = Class.forName("factorization.oreprocessing.TileEntitySlagFurnace$SmeltingResult"); - Constructor constructor = clazz.getDeclaredConstructor(ItemStack.class, float.class, ItemStack.class, float.class, ItemStack.class); - constructor.setAccessible(true); - return constructor.newInstance(input, chance1, output1, chance2, output2); - } catch (Exception e) { - throw new NullPointerException("Failed to instantiate SmeltingResult"); - } + return ReflectionHelper.getInstance(ReflectionHelper.getConstructor("factorization.oreprocessing.TileEntitySlagFurnace$SmeltingResult", ItemStack.class, float.class, ItemStack.class, float.class, ItemStack.class), + input, + chance1, + output1, + chance2, + output2); } public static Object getCrystallizerRecipe(ItemStack input, ItemStack output, ItemStack solution, float output_count) { - try { - Class clazz = Class.forName("factorization.oreprocessing.TileEntityCrystallizer$CrystalRecipe"); - Constructor constructor = clazz.getDeclaredConstructor(ItemStack.class, ItemStack.class, float.class, ItemStack.class); - constructor.setAccessible(true); - return constructor.newInstance(input, output, output_count, solution); - } catch (Exception e) { - throw new NullPointerException("Failed to instantiate CrystalRecipe"); - } + return ReflectionHelper.getInstance(ReflectionHelper.getConstructor("factorization.oreprocessing.TileEntityCrystallizer$CrystalRecipe", ItemStack.class, ItemStack.class, float.class, ItemStack.class), + input, + output, + output_count, + solution); } } \ No newline at end of file diff --git a/src/main/java/modtweaker2/mods/factorization/commands/FactorizationLogger.java b/src/main/java/modtweaker2/mods/factorization/commands/FactorizationLogger.java new file mode 100644 index 0000000..b7188ad --- /dev/null +++ b/src/main/java/modtweaker2/mods/factorization/commands/FactorizationLogger.java @@ -0,0 +1,73 @@ +package modtweaker2.mods.factorization.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.LogHelper; +import modtweaker2.helpers.ReflectionHelper; +import modtweaker2.helpers.StringHelper; +import modtweaker2.mods.factorization.FactorizationHelper; +import net.minecraft.item.ItemStack; + +public class FactorizationLogger implements ICommandFunction { + + private static final List validArguments = new LinkedList(); + + static { + validArguments.add("crystallizer"); + validArguments.add("lacerator"); + validArguments.add("slagfurnace"); + } + + @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("crystallizer")) { + for(Object recipe : FactorizationHelper.crystallizer) { + MineTweakerAPI.logCommand(String.format("mods.factorization.Crystallizer.addRecipe(%s, %s, %s, %s);", + LogHelper.getStackDescription(ReflectionHelper.getObject(recipe, "input")), + LogHelper.getStackDescription(ReflectionHelper.getObject(recipe, "output")), + LogHelper.getStackDescription(ReflectionHelper.getObject(recipe, "solution")), + ReflectionHelper.getObject(recipe, "output_count"))); + } + } + + if(args.isEmpty() || args.contains("lacerator")) { + for(Object recipe : FactorizationHelper.lacerator) { + MineTweakerAPI.logCommand(String.format("mods.factorization.Lacerator.addRecipe(%s, %s, %s);", + ReflectionHelper.getObject(recipe, "oreName") != null ? + LogHelper.getStackDescription(ReflectionHelper.getObject(recipe, "oreName")) : + LogHelper.getStackDescription(ReflectionHelper.getObject(recipe, "itemstack")), + LogHelper.getStackDescription(ReflectionHelper.getObject(recipe, "output")), + ReflectionHelper.getObject(recipe, "probability"))); + } + } + + if(args.isEmpty() || args.contains("slagfurnace")) { + for(Object recipe : FactorizationHelper.slag) { + MineTweakerAPI.logCommand(String.format("mods.factorization.SlagFurnace.addRecipe(%s, %s, %s, %s, %s);", + LogHelper.getStackDescription(ReflectionHelper.getObject(recipe, "input")), + LogHelper.getStackDescription(ReflectionHelper.getObject(recipe, "output2")), + ReflectionHelper.getObject(recipe, "prob2"), + LogHelper.getStackDescription(ReflectionHelper.getObject(recipe, "output1")), + ReflectionHelper.getObject(recipe, "prob1"))); + } + } + + if (player != null) { + player.sendChat(MineTweakerImplementationAPI.platform.getMessage("List generated; see minetweaker.log in your minecraft dir")); + } + } + } +} \ No newline at end of file diff --git a/src/main/java/modtweaker2/mods/factorization/handlers/Crystallizer.java b/src/main/java/modtweaker2/mods/factorization/handlers/Crystallizer.java index 6778ee6..39e8166 100644 --- a/src/main/java/modtweaker2/mods/factorization/handlers/Crystallizer.java +++ b/src/main/java/modtweaker2/mods/factorization/handlers/Crystallizer.java @@ -1,9 +1,16 @@ package modtweaker2.mods.factorization.handlers; +import static modtweaker2.helpers.InputHelper.toIItemStack; import static modtweaker2.helpers.InputHelper.toStack; -import static modtweaker2.helpers.StackHelper.areEqual; +import static modtweaker2.helpers.StackHelper.matches; + +import java.util.LinkedList; +import java.util.List; + import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; +import modtweaker2.helpers.LogHelper; import modtweaker2.helpers.ReflectionHelper; import modtweaker2.mods.factorization.FactorizationHelper; import modtweaker2.utils.BaseListAddition; @@ -15,59 +22,61 @@ import stanhebben.zenscript.annotations.ZenMethod; @ZenClass("mods.factorization.Crystallizer") public class Crystallizer { + + public static final String name = "Factorization Crystallizer"; + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + @ZenMethod public static void addRecipe(IItemStack input, IItemStack output, IItemStack solution, double output_count) { Object recipe = FactorizationHelper.getCrystallizerRecipe(toStack(input), toStack(output), toStack(solution), (float) output_count); - MineTweakerAPI.apply(new Add(toStack(input), recipe)); + MineTweakerAPI.apply(new Add(recipe)); } - private static class Add extends BaseListAddition { - private final ItemStack output; - - public Add(ItemStack output, Object recipe) { - super("Crystallizer", FactorizationHelper.crystallizer, recipe); - this.output = output; + private static class Add extends BaseListAddition { + @SuppressWarnings("unchecked") + public Add(Object recipe) { + super(Crystallizer.name, FactorizationHelper.crystallizer); + recipes.add(recipe); } @Override - public String getRecipeInfo() { - return output.getDisplayName(); + public String getRecipeInfo(Object recipe) { + return LogHelper.getStackDescription((ItemStack)ReflectionHelper.getObject(recipe, "output")); } } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @ZenMethod - public static void removeRecipe(IItemStack output) { - MineTweakerAPI.apply(new Remove(toStack(output))); - } - - private static class Remove extends BaseListRemoval { - public Remove(ItemStack stack) { - super("Crystallizer", FactorizationHelper.crystallizer, stack); - } - - //Returns the output ItemStack - private ItemStack getOutput(Object o) { - return (ItemStack) ReflectionHelper.getObject(o, "output"); - } - - @Override - public void apply() { - for (Object r : list) { - if (r != null) { - ItemStack output = getOutput(r); - if (output != null && areEqual(output, stack)) { - recipes.add(r); - } + public static void removeRecipe(IIngredient output) { + List recipes = new LinkedList(); + + for (Object r : FactorizationHelper.crystallizer) { + if (r != null) { + ItemStack out = (ItemStack) ReflectionHelper.getObject(r, "output");; + if (out != null && matches(output, toIItemStack(out))) { + recipes.add(r); } } - super.apply(); + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new Remove(recipes)); + } else { + LogHelper.logWarning(String.format("No %s Recipe found for %s. Command ignored!", Crystallizer.name, output.toString())); + } + } + + private static class Remove extends BaseListRemoval { + @SuppressWarnings("unchecked") + public Remove(List recipes) { + super(Crystallizer.name, FactorizationHelper.crystallizer, recipes); } @Override - public String getRecipeInfo() { - return stack.getDisplayName(); + public String getRecipeInfo(Object recipe) { + return LogHelper.getStackDescription((ItemStack)ReflectionHelper.getObject(recipe, "output")); } } } diff --git a/src/main/java/modtweaker2/mods/factorization/handlers/Lacerator.java b/src/main/java/modtweaker2/mods/factorization/handlers/Lacerator.java index de00ed3..c19e906 100644 --- a/src/main/java/modtweaker2/mods/factorization/handlers/Lacerator.java +++ b/src/main/java/modtweaker2/mods/factorization/handlers/Lacerator.java @@ -1,9 +1,17 @@ package modtweaker2.mods.factorization.handlers; +import static modtweaker2.helpers.InputHelper.toIItemStack; +import static modtweaker2.helpers.InputHelper.toObject; import static modtweaker2.helpers.InputHelper.toStack; -import static modtweaker2.helpers.StackHelper.areEqual; +import static modtweaker2.helpers.StackHelper.matches; + +import java.util.LinkedList; +import java.util.List; + import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; +import modtweaker2.helpers.LogHelper; import modtweaker2.helpers.ReflectionHelper; import modtweaker2.mods.factorization.FactorizationHelper; import modtweaker2.utils.BaseListAddition; @@ -14,57 +22,60 @@ import stanhebben.zenscript.annotations.ZenMethod; @ZenClass("mods.factorization.Lacerator") public class Lacerator { + + public static final String name = "Factorization Lacerator"; + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + @ZenMethod - public static void addRecipe(IItemStack input, IItemStack output, double probability) { - Object recipe = FactorizationHelper.getLaceratorRecipe(toStack(input), toStack(output), (float) probability); - MineTweakerAPI.apply(new Add(toStack(input), recipe)); + public static void addRecipe(IIngredient input, IItemStack output, double probability) { + Object recipe = FactorizationHelper.getLaceratorRecipe(toObject(input), toStack(output), (float) probability); + MineTweakerAPI.apply(new Add(recipe)); } - private static class Add extends BaseListAddition { - private final ItemStack output; - - public Add(ItemStack output, Object recipe) { - super("Lacerator", FactorizationHelper.lacerator, recipe); - this.output = output; + private static class Add extends BaseListAddition { + @SuppressWarnings("unchecked") + public Add(Object recipe) { + super(Lacerator.name, FactorizationHelper.lacerator); + recipes.add(recipe); } @Override - public String getRecipeInfo() { - return output.getDisplayName(); + public String getRecipeInfo(Object recipe) { + return LogHelper.getStackDescription((ItemStack) ReflectionHelper.getObject(recipe, "output")); } } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @ZenMethod - public static void removeRecipe(IItemStack output) { - MineTweakerAPI.apply(new Remove(toStack(output))); - } - - private static class Remove extends BaseListRemoval { - public Remove(ItemStack stack) { - super("Lacerator", FactorizationHelper.lacerator, stack); - } - - //Returns the output ItemStack - private ItemStack getOutput(Object o) { - return (ItemStack) ReflectionHelper.getObject(o, "output"); - } - - @Override - public void apply() { - for (Object r : list) { - ItemStack output = getOutput(r); - if (output != null && areEqual(output, stack)) { + public static void removeRecipe(IIngredient output) { + List recipes = new LinkedList(); + for (Object r : FactorizationHelper.lacerator) { + if(r != null) { + ItemStack out = (ItemStack) ReflectionHelper.getObject(r, "output"); + if (output != null && matches(output, toIItemStack(out))) { recipes.add(r); } } - super.apply(); + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new Remove(recipes)); + } else { + LogHelper.logWarning(String.format("No %s Recipe found for %s. Command ignored", Lacerator.name, output.toString())); + } + } + + private static class Remove extends BaseListRemoval { + @SuppressWarnings("unchecked") + public Remove(List recipes) { + super(Lacerator.name, FactorizationHelper.lacerator, recipes); } @Override - public String getRecipeInfo() { - return stack.getDisplayName(); + public String getRecipeInfo(Object recipe) { + return LogHelper.getStackDescription((ItemStack) ReflectionHelper.getObject(recipe, "output")); } } } diff --git a/src/main/java/modtweaker2/mods/factorization/handlers/SlagFurnace.java b/src/main/java/modtweaker2/mods/factorization/handlers/SlagFurnace.java index e09e71f..c35632a 100644 --- a/src/main/java/modtweaker2/mods/factorization/handlers/SlagFurnace.java +++ b/src/main/java/modtweaker2/mods/factorization/handlers/SlagFurnace.java @@ -1,74 +1,82 @@ package modtweaker2.mods.factorization.handlers; +import static modtweaker2.helpers.InputHelper.toIItemStack; import static modtweaker2.helpers.InputHelper.toStack; -import static modtweaker2.helpers.StackHelper.areEqual; +import static modtweaker2.helpers.StackHelper.matches; + +import java.util.LinkedList; +import java.util.List; + import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; +import modtweaker2.helpers.LogHelper; import modtweaker2.helpers.ReflectionHelper; import modtweaker2.mods.factorization.FactorizationHelper; import modtweaker2.utils.BaseListAddition; -import modtweaker2.utils.BaseMultipleListRemoval; -import modtweaker2.utils.BaseMultipleListRemoval.Position; +import modtweaker2.utils.BaseListRemoval; import net.minecraft.item.ItemStack; import stanhebben.zenscript.annotations.ZenClass; import stanhebben.zenscript.annotations.ZenMethod; @ZenClass("mods.factorization.SlagFurnace") public class SlagFurnace { + + public static final String name = "Factorization Slag Furnace"; + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + @ZenMethod public static void addRecipe(IItemStack input, IItemStack output2, double chance2, IItemStack output1, double chance1) { Object recipe = FactorizationHelper.getSlagFurnaceRecipe(toStack(input), (float) chance1, toStack(output1), (float) chance2, toStack(output2)); MineTweakerAPI.apply(new Add(toStack(input), recipe)); } - private static class Add extends BaseListAddition { - private final ItemStack input; - + private static class Add extends BaseListAddition { + @SuppressWarnings("unchecked") public Add(ItemStack input, Object recipe) { - super("Slag Furnace", FactorizationHelper.slag, recipe); - this.input = input; + super(SlagFurnace.name, FactorizationHelper.slag); + recipes.add(recipe); } @Override - public String getRecipeInfo() { - return input.getDisplayName(); + public String getRecipeInfo(Object recipe) { + return LogHelper.getStackDescription((ItemStack) ReflectionHelper.getObject(recipe, "input")); } } - // /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @ZenMethod - public static void removeRecipe(IItemStack input) { - MineTweakerAPI.apply(new Remove(toStack(input), Position.ALL)); + public static void removeRecipe(IIngredient input) { + List recipes = new LinkedList(); + + for(Object r : FactorizationHelper.slag) { + if(r != null) { + ItemStack in = (ItemStack) ReflectionHelper.getObject(r, "input"); + + if(matches(input, toIItemStack(in))) { + recipes.add(r); + } + } + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new Remove(recipes)); + } else { + LogHelper.logWarning(String.format("No %s Recipe found for %s. Command ignored!", SlagFurnace.name, input)); + } } - @ZenMethod - public static void removeFirst(IItemStack output) { - MineTweakerAPI.apply(new Remove(toStack(output), Position.FIRST)); - } - - @ZenMethod - public static void removeLast(IItemStack output) { - MineTweakerAPI.apply(new Remove(toStack(output), Position.LAST)); - } - - private static class Remove extends BaseMultipleListRemoval { - public Remove(ItemStack stack, Position pos) { - super("Slag Furnace", FactorizationHelper.slag, stack, pos); + private static class Remove extends BaseListRemoval { + @SuppressWarnings("unchecked") + public Remove(List recipes) { + super(SlagFurnace.name, FactorizationHelper.slag, recipes); } - @Override - protected boolean isEqual(Object recipe, Object search) { - ItemStack input = (ItemStack) ReflectionHelper.getObject(recipe, "input"); - if (input != null && areEqual(input, (ItemStack) search)) { - return true; - } else - return false; - } - - @Override - public String getRecipeInfo() { - return ((ItemStack) search).getDisplayName(); - } + @Override + public String getRecipeInfo(Object recipe) { + return LogHelper.getStackDescription((ItemStack) ReflectionHelper.getObject(recipe, "input")); + } } } diff --git a/src/main/java/modtweaker2/mods/forestry/ForestryHelper.java b/src/main/java/modtweaker2/mods/forestry/ForestryHelper.java index 5733957..53a6779 100644 --- a/src/main/java/modtweaker2/mods/forestry/ForestryHelper.java +++ b/src/main/java/modtweaker2/mods/forestry/ForestryHelper.java @@ -1,51 +1,30 @@ package modtweaker2.mods.forestry; -import java.lang.reflect.Field; import java.util.ArrayList; import java.util.HashSet; +import modtweaker2.helpers.ReflectionHelper; import net.minecraft.item.ItemStack; import net.minecraftforge.fluids.Fluid; +import forestry.factory.gadgets.MachineCarpenter; public class ForestryHelper { @SuppressWarnings("unchecked") public static void addCarpenterRecipeBox(ItemStack box) { - Class clazz; - try { - clazz = Class.forName("forestry.factory.gadgets.MachineCarpenter$RecipeManager"); - Field field_box = clazz.getDeclaredField("boxes"); - field_box.setAccessible(true); - - ArrayList recipeBoxes = (ArrayList) field_box.get(clazz); - recipeBoxes.add(box); - - } catch (ClassNotFoundException e) { - e.printStackTrace(); - } catch (NoSuchFieldException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } + ArrayList recipeBoxes = (ArrayList) ReflectionHelper.getStaticObject(MachineCarpenter.RecipeManager.class, "boxes"); + + if(recipeBoxes != null) { + recipeBoxes.add(box); + } } @SuppressWarnings("unchecked") public static void addCarpenterRecipeFluids(Fluid newFluid) { - Class clazz; - try { - clazz = Class.forName("forestry.factory.gadgets.MachineCarpenter$RecipeManager"); - Field field_recipeFluids = clazz.getDeclaredField("recipeFluids"); - field_recipeFluids.setAccessible(true); - - HashSet recipeFluids = (HashSet) field_recipeFluids.get(clazz); - recipeFluids.add(newFluid); - - } catch (ClassNotFoundException e) { - e.printStackTrace(); - } catch (NoSuchFieldException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); + HashSet recipeFluids = (HashSet) ReflectionHelper.getStaticObject(MachineCarpenter.RecipeManager.class, "recipeFluids"); + + if(recipeFluids != null) { + recipeFluids.add(newFluid); } } } diff --git a/src/main/java/modtweaker2/mods/forestry/handlers/Carpenter.java b/src/main/java/modtweaker2/mods/forestry/handlers/Carpenter.java index cb6dc7c..ee499ce 100644 --- a/src/main/java/modtweaker2/mods/forestry/handlers/Carpenter.java +++ b/src/main/java/modtweaker2/mods/forestry/handlers/Carpenter.java @@ -1,15 +1,20 @@ package modtweaker2.mods.forestry.handlers; import static modtweaker2.helpers.InputHelper.toFluid; +import static modtweaker2.helpers.InputHelper.toIItemStack; import static modtweaker2.helpers.InputHelper.toStack; import static modtweaker2.helpers.InputHelper.toStacks; +import static modtweaker2.helpers.StackHelper.matches; import java.util.ArrayList; +import java.util.LinkedList; import java.util.List; import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; import minetweaker.api.liquid.ILiquidStack; +import modtweaker2.helpers.LogHelper; import modtweaker2.mods.forestry.ForestryHelper; import modtweaker2.utils.BaseListAddition; import modtweaker2.utils.BaseListRemoval; @@ -24,7 +29,11 @@ import forestry.factory.gadgets.MachineCarpenter.RecipeManager; @ZenClass("mods.forestry.Carpenter") public class Carpenter { + + public static final String name = "Forestry Carpenter"; + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + @ZenMethod public static void addRecipe(int packagingTime, ILiquidStack liquid, IItemStack[] ingredients, IItemStack ingredient, IItemStack product) { ArrayList stacks = new ArrayList(); @@ -45,10 +54,11 @@ public class Carpenter { return null; } - private static class Add extends BaseListAddition { + private static class Add extends BaseListAddition { public Add(Recipe recipe) { - super("Forestry Carpenter", MachineCarpenter.RecipeManager.recipes, recipe); + super(Carpenter.name, MachineCarpenter.RecipeManager.recipes); + recipes.add(recipe); // The Carpenter has a list of valid Fluids, access them via // Relfection because of private @@ -59,37 +69,43 @@ public class Carpenter { ForestryHelper.addCarpenterRecipeBox(recipe.getBox()); } } - + @Override - public String getRecipeInfo(){ - return ((MachineCarpenter.Recipe) recipe).getCraftingResult().getDisplayName(); + protected String getRecipeInfo(Recipe recipe) { + return LogHelper.getStackDescription(recipe.getCraftingResult()); } } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @ZenMethod - public static void removeRecipe(IItemStack output) { - MineTweakerAPI.apply(new Remove(MachineCarpenter.RecipeManager.recipes, toStack(output))); + public static void removeRecipe(IIngredient output) { + List recipes = new LinkedList(); + + for(Recipe recipe : RecipeManager.recipes) { + if(recipe != null && recipe.getCraftingResult() != null && matches(output, toIItemStack(recipe.getCraftingResult()))) { + recipes.add(recipe); + } + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new Remove(recipes)); + } else { + LogHelper.logWarning(String.format("No %s Recipe found for %s. Command ignored!", Carpenter.name, output.toString())); + } + + } - private static class Remove extends BaseListRemoval { + private static class Remove extends BaseListRemoval { - public Remove(List list, ItemStack stack) { - super("Forestry Carpenter", list, stack); + public Remove(List recipes) { + super(Carpenter.name, RecipeManager.recipes, recipes); } - + @Override - public void apply() { - for (Recipe r : RecipeManager.recipes) { - if (r.getCraftingResult() != null && r.getCraftingResult().isItemEqual(stack)) { - recipes.add(r); - } - } - super.apply(); - } - - @Override - public String getRecipeInfo(){ - return "Removed recipe for: " + stack.getDisplayName(); + protected String getRecipeInfo(Recipe recipe) { + return LogHelper.getStackDescription(recipe.getCraftingResult()); } } } diff --git a/src/main/java/modtweaker2/mods/forestry/handlers/Centrifuge.java b/src/main/java/modtweaker2/mods/forestry/handlers/Centrifuge.java index d28c222..fef05f4 100644 --- a/src/main/java/modtweaker2/mods/forestry/handlers/Centrifuge.java +++ b/src/main/java/modtweaker2/mods/forestry/handlers/Centrifuge.java @@ -1,12 +1,17 @@ package modtweaker2.mods.forestry.handlers; +import static modtweaker2.helpers.InputHelper.toIItemStack; import static modtweaker2.helpers.InputHelper.toStack; +import static modtweaker2.helpers.StackHelper.matches; import java.util.HashMap; +import java.util.LinkedList; import java.util.List; import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; +import modtweaker2.helpers.LogHelper; import modtweaker2.utils.BaseListAddition; import modtweaker2.utils.BaseListRemoval; import net.minecraft.item.ItemStack; @@ -19,6 +24,10 @@ import forestry.factory.gadgets.MachineCentrifuge.RecipeManager; @ZenClass("mods.forestry.Centrifuge") public class Centrifuge { + public static final String name = "Forestry Centrifuge"; + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + @ZenMethod public static void addRecipe(int timePerItem, IItemStack itemInput, IItemStack[] output, int[] chances) { HashMap products = new HashMap(); @@ -31,47 +40,47 @@ public class Centrifuge { MineTweakerAPI.apply(new Add(new Recipe(timePerItem, toStack(itemInput), products))); } - @ZenMethod - public static void removeRecipe(IItemStack itemInput) { - MineTweakerAPI.apply(new Remove(RecipeManager.recipes, toStack(itemInput))); + private static class Add extends BaseListAddition { + + public Add(Recipe recipe) { + super(Centrifuge.name, RecipeManager.recipes); + recipes.add(recipe); + } + + @Override + protected String getRecipeInfo(Recipe recipe) { + return LogHelper.getStackDescription(recipe.resource); + } + } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + @ZenMethod + public static void removeRecipe(IIngredient input) { + List recipes = new LinkedList(); + + for(Recipe recipe : RecipeManager.recipes) { + if(recipe != null && matches(input, toIItemStack(recipe.resource))) { + recipes.add(recipe); + } + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new Remove(recipes)); + } else { + LogHelper.logWarning(String.format("No %s Recipe found for %s. Command ignored!", Centrifuge.name, input.toString())); + } } - /* - Implements the actions to add the recipe - */ - private static class Add extends BaseListAddition { + private static class Remove extends BaseListRemoval { - public Add(Recipe recipe) { - super("Forestry Centrifuge", RecipeManager.recipes, recipe); - } - @Override - public String getRecipeInfo() { - return " Input:" + ((Recipe) recipe).resource.getDisplayName(); - } - } - - /* - Implements the actions to remove the recipe - */ - private static class Remove extends BaseListRemoval { - - public Remove(List list, ItemStack input) { - super("Forestry Centrifuge", RecipeManager.recipes, input); + public Remove(List recipes) { + super(Centrifuge.name, RecipeManager.recipes, recipes); } @Override - public void apply() { - for (Recipe r : RecipeManager.recipes) { - if (r.matches(stack)) { - recipes.add(r); - } - } - super.apply(); - } - - @Override - public String getRecipeInfo() { - return " Input:" + stack.getDisplayName(); + protected String getRecipeInfo(Recipe recipe) { + return LogHelper.getStackDescription(recipe.resource); } } } diff --git a/src/main/java/modtweaker2/mods/forestry/handlers/Fermenter.java b/src/main/java/modtweaker2/mods/forestry/handlers/Fermenter.java index 316bc9f..1ae25e2 100644 --- a/src/main/java/modtweaker2/mods/forestry/handlers/Fermenter.java +++ b/src/main/java/modtweaker2/mods/forestry/handlers/Fermenter.java @@ -1,67 +1,178 @@ package modtweaker2.mods.forestry.handlers; -import static modtweaker2.helpers.InputHelper.getFluid; 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.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; import java.util.List; import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; import minetweaker.api.liquid.ILiquidStack; +import modtweaker2.helpers.LogHelper; import modtweaker2.utils.BaseListAddition; import modtweaker2.utils.BaseListRemoval; -import net.minecraft.item.ItemStack; +import net.minecraftforge.fluids.Fluid; import stanhebben.zenscript.annotations.ZenClass; import stanhebben.zenscript.annotations.ZenMethod; -import forestry.factory.gadgets.MachineFermenter; import forestry.factory.gadgets.MachineFermenter.Recipe; import forestry.factory.gadgets.MachineFermenter.RecipeManager; @ZenClass("mods.forestry.Fermenter") public class Fermenter { + public static final String name = "Forestry Fermenter"; + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + @ZenMethod public static void addRecipe(IItemStack resource, ILiquidStack liquid, int fermentationValue, float modifier, ILiquidStack output) { MineTweakerAPI.apply(new Add(new Recipe(toStack(resource), fermentationValue, modifier, toFluid(output), toFluid(liquid)))); - MachineFermenter.RecipeManager.recipeFluidInputs.add(getFluid(liquid)); - MachineFermenter.RecipeManager.recipeFluidOutputs.add(getFluid(output)); } - private static class Add extends BaseListAddition { + private static class Add extends BaseListAddition { public Add(Recipe recipe) { - super("Forestry Fermenter", MachineFermenter.RecipeManager.recipes, recipe); + super(Fermenter.name, RecipeManager.recipes); + recipes.add(recipe); } - - @Override - public String getRecipeInfo() { - return ((Recipe) recipe).output.getLocalizedName(); - } - } - - @ZenMethod - public static void removeRecipe(IItemStack input) { - MineTweakerAPI.apply(new Remove(MachineFermenter.RecipeManager.recipes, toStack(input))); - } - - private static class Remove extends BaseListRemoval { - - public Remove(List list, ItemStack stack) { - super(list, stack); - - } - + @Override public void apply() { - for (Recipe r : RecipeManager.recipes) { - if (r != null && r.resource != null && r.resource.isItemEqual(stack)) { - recipes.add(r); - - } - } - super.apply(); + // add liquids to valid input / output + for(Recipe recipe : successful) { + RecipeManager.recipeFluidInputs.add(recipe.liquid.getFluid()); + RecipeManager.recipeFluidOutputs.add(recipe.output.getFluid()); + } + + super.apply(); + } + + @Override + public void undo() { + super.undo(); + + // Tidy up valid inputs + for(Iterator iter = RecipeManager.recipeFluidInputs.iterator(); iter.hasNext();) { + boolean found = false; + Fluid fluid = iter.next(); + for(Recipe recipe : list) { + if(recipe != null && recipe.liquid != null && recipe.liquid.getFluid().equals(fluid)) { + found = true; + } + } + + if(!found) { + iter.remove(); + } + } + + // Tidy up valid outputs + for(Iterator iter = RecipeManager.recipeFluidOutputs.iterator(); iter.hasNext();) { + boolean found = false; + Fluid fluid = iter.next(); + for(Recipe recipe : list) { + if(recipe != null && recipe.output != null && recipe.output.getFluid().equals(fluid)) { + found = true; + } + } + + if(!found) { + iter.remove(); + } + } } + @Override + public String getRecipeInfo(Recipe recipe) { + return LogHelper.getStackDescription(recipe.output); + } + } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + @ZenMethod + public static void removeRecipe(IIngredient input) { + List recipes = new LinkedList(); + + for(Recipe recipe : RecipeManager.recipes) { + // check for input items + if(recipe != null && recipe.resource != null && matches(input, toIItemStack(recipe.resource))) { + recipes.add(recipe); + } + + // check for input liquids + if(recipe != null && recipe.resource != null && matches(input, toILiquidStack(recipe.liquid))) { + recipes.add(recipe); + } + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new Remove(recipes)); + } else { + LogHelper.logWarning(String.format("No %s Recipe found for %s. Command ignored!", Fermenter.name, input.toString())); + } + } + + private static class Remove extends BaseListRemoval { + + public Remove(List recipes) { + super(Fermenter.name, RecipeManager.recipes, recipes); + } + + @Override + public void apply() { + super.apply(); + + // Tidy up valid inputs + for(Iterator iter = RecipeManager.recipeFluidInputs.iterator(); iter.hasNext();) { + boolean found = false; + Fluid fluid = iter.next(); + for(Recipe recipe : list) { + if(recipe != null && recipe.liquid != null && recipe.liquid.getFluid().equals(fluid)) { + found = true; + } + } + + if(!found) { + iter.remove(); + } + } + + // Tidy up valid outputs + for(Iterator iter = RecipeManager.recipeFluidOutputs.iterator(); iter.hasNext();) { + boolean found = false; + Fluid fluid = iter.next(); + for(Recipe recipe : list) { + if(recipe != null && recipe.output != null && recipe.output.getFluid().equals(fluid)) { + found = true; + } + } + + if(!found) { + iter.remove(); + } + } + } + + @Override + public void undo() { + // add liquids to valid input / output + for(Recipe recipe : successful) { + RecipeManager.recipeFluidInputs.add(recipe.liquid.getFluid()); + RecipeManager.recipeFluidOutputs.add(recipe.output.getFluid()); + } + + super.undo(); + } + + @Override + public String getRecipeInfo(Recipe recipe) { + return LogHelper.getStackDescription(recipe.output); + } } } diff --git a/src/main/java/modtweaker2/mods/forestry/handlers/Moistener.java b/src/main/java/modtweaker2/mods/forestry/handlers/Moistener.java index 242c1d7..52400e5 100644 --- a/src/main/java/modtweaker2/mods/forestry/handlers/Moistener.java +++ b/src/main/java/modtweaker2/mods/forestry/handlers/Moistener.java @@ -1,14 +1,18 @@ package modtweaker2.mods.forestry.handlers; +import static modtweaker2.helpers.InputHelper.toIItemStack; import static modtweaker2.helpers.InputHelper.toStack; +import static modtweaker2.helpers.StackHelper.matches; +import java.util.LinkedList; import java.util.List; import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; +import modtweaker2.helpers.LogHelper; import modtweaker2.utils.BaseListAddition; import modtweaker2.utils.BaseListRemoval; -import net.minecraft.item.ItemStack; import stanhebben.zenscript.annotations.ZenClass; import stanhebben.zenscript.annotations.ZenMethod; import forestry.factory.gadgets.MachineMoistener; @@ -17,6 +21,10 @@ import forestry.factory.gadgets.MachineMoistener.RecipeManager; @ZenClass("mods.forestry.Moistener") public class Moistener { + + public static final String name = "Forestry Moistener"; + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @ZenMethod public static void addRecipe(int timePerItem, IItemStack resource, IItemStack product) { @@ -24,37 +32,45 @@ public class Moistener { } - private static class Add extends BaseListAddition { + private static class Add extends BaseListAddition { public Add(Recipe recipe) { - super("Forestry Moistener", MachineMoistener.RecipeManager.recipes, recipe); + super(Moistener.name, MachineMoistener.RecipeManager.recipes); + recipes.add(recipe); } @Override - public String getRecipeInfo() { - return ((Recipe) recipe).product.getDisplayName(); + public String getRecipeInfo(Recipe recipe) { + return LogHelper.getStackDescription(recipe.product); } } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @ZenMethod - public static void removeRecipe(IItemStack output) { - MineTweakerAPI.apply(new Remove(MachineMoistener.RecipeManager.recipes, toStack(output))); + public static void removeRecipe(IIngredient output) { + List recipes = new LinkedList(); + + for (Recipe recipe : RecipeManager.recipes) { + if (recipe != null && recipe.product != null && matches(output, toIItemStack(recipe.product))) { + recipes.add(recipe); + } + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new Remove(recipes)); + } else { + LogHelper.logWarning(String.format("No %s Recipe found for %s. Command ignored!", Moistener.name, output.toString())); + } } - private static class Remove extends BaseListRemoval { - - public Remove(List list, ItemStack stack) { - super(list, stack); - + private static class Remove extends BaseListRemoval { + public Remove(List recipes) { + super(Moistener.name, RecipeManager.recipes, recipes); } - @Override - public void apply() { - for (Recipe r : RecipeManager.recipes) { - if (r.product != null && r.product.isItemEqual(stack)) { - recipes.add(r); - } - } - super.apply(); - } + @Override + public String getRecipeInfo(Recipe recipe) { + return LogHelper.getStackDescription(recipe.product); + } } } diff --git a/src/main/java/modtweaker2/mods/forestry/handlers/Squeezer.java b/src/main/java/modtweaker2/mods/forestry/handlers/Squeezer.java index 12cbccf..27156bb 100644 --- a/src/main/java/modtweaker2/mods/forestry/handlers/Squeezer.java +++ b/src/main/java/modtweaker2/mods/forestry/handlers/Squeezer.java @@ -2,18 +2,22 @@ package modtweaker2.mods.forestry.handlers; import static modtweaker2.helpers.InputHelper.getFluid; import static modtweaker2.helpers.InputHelper.toFluid; +import static modtweaker2.helpers.InputHelper.toILiquidStack; import static modtweaker2.helpers.InputHelper.toStack; import static modtweaker2.helpers.InputHelper.toStacks; +import static modtweaker2.helpers.StackHelper.matches; import java.util.Arrays; +import java.util.LinkedList; import java.util.List; import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; import minetweaker.api.liquid.ILiquidStack; +import modtweaker2.helpers.LogHelper; import modtweaker2.utils.BaseListAddition; import modtweaker2.utils.BaseListRemoval; -import net.minecraftforge.fluids.FluidStack; import stanhebben.zenscript.annotations.ZenClass; import stanhebben.zenscript.annotations.ZenMethod; import forestry.factory.gadgets.MachineSqueezer; @@ -22,6 +26,10 @@ import forestry.factory.gadgets.MachineSqueezer.RecipeManager; @ZenClass("mods.forestry.Squeezer") public class Squeezer { + + public static final String name = "Forestry Squeezer"; + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @ZenMethod public static void addRecipe(int timePerItem, IItemStack[] resources, ILiquidStack liquid, IItemStack remnants, int chance) { @@ -30,38 +38,53 @@ public class Squeezer { MachineSqueezer.RecipeManager.recipeInputs.addAll(Arrays.asList(toStacks(resources))); } - private static class Add extends BaseListAddition { + private static class Add extends BaseListAddition { public Add(Recipe recipe) { - super("Forestry Squeezer", MachineSqueezer.RecipeManager.recipes, recipe); + super(Squeezer.name, MachineSqueezer.RecipeManager.recipes); + recipes.add(recipe); } @Override - public String getRecipeInfo() { - return ((Recipe) recipe).liquid.getLocalizedName(); + public String getRecipeInfo(Recipe recipe) { + return LogHelper.getStackDescription(recipe.liquid); } } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @ZenMethod - public static void removeRecipe(ILiquidStack liquid) { - MineTweakerAPI.apply(new Remove(MachineSqueezer.RecipeManager.recipes, toFluid(liquid))); + public static void removeRecipe(IIngredient liquid) { + List recipes = new LinkedList(); + + for (Recipe r : RecipeManager.recipes) { + if (r != null && r.liquid != null && matches(liquid, toILiquidStack(r.liquid))) { + recipes.add(r); + } + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new Remove(recipes)); + } else { + LogHelper.logWarning(String.format("No %s Recipe found for %s. Command ignored!", Squeezer.name, liquid.toString())); + } + } - private static class Remove extends BaseListRemoval { - - public Remove(List list, FluidStack liquid) { - super(list, liquid); + private static class Remove extends BaseListRemoval { + public Remove(List recipes) { + super(Squeezer.name, MachineSqueezer.RecipeManager.recipes, recipes); } @Override public void apply() { - for (Recipe r : RecipeManager.recipes) { - if (r.liquid != null && r.liquid.isFluidEqual(fluid)) { - recipes.add(r); - } - } + super.apply(); } - + + @Override + public String getRecipeInfo(Recipe recipe) { + return LogHelper.getStackDescription(recipe.liquid); + } } } diff --git a/src/main/java/modtweaker2/mods/forestry/handlers/Still.java b/src/main/java/modtweaker2/mods/forestry/handlers/Still.java index 7e5ba37..19a29f7 100644 --- a/src/main/java/modtweaker2/mods/forestry/handlers/Still.java +++ b/src/main/java/modtweaker2/mods/forestry/handlers/Still.java @@ -2,14 +2,17 @@ package modtweaker2.mods.forestry.handlers; import static modtweaker2.helpers.InputHelper.getFluid; import static modtweaker2.helpers.InputHelper.toFluid; +import static modtweaker2.helpers.InputHelper.toILiquidStack; +import static modtweaker2.helpers.StackHelper.matches; +import java.util.LinkedList; import java.util.List; import minetweaker.MineTweakerAPI; import minetweaker.api.liquid.ILiquidStack; +import modtweaker2.helpers.LogHelper; import modtweaker2.utils.BaseListAddition; import modtweaker2.utils.BaseListRemoval; -import net.minecraftforge.fluids.FluidStack; import stanhebben.zenscript.annotations.ZenClass; import stanhebben.zenscript.annotations.ZenMethod; import forestry.factory.gadgets.MachineStill; @@ -18,6 +21,10 @@ import forestry.factory.gadgets.MachineStill.RecipeManager; @ZenClass("mods.forestry.Still") public class Still { + + public static final String name = "Forestry Still"; + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @ZenMethod public static void addRecipe(int timePerUnit, ILiquidStack input, ILiquidStack output) { @@ -29,37 +36,46 @@ public class Still { MachineStill.RecipeManager.recipeFluidOutputs.add(getFluid(output)); } - private static class Add extends BaseListAddition { + private static class Add extends BaseListAddition { public Add(Recipe recipe) { - super("Forestry Still", MachineStill.RecipeManager.recipes, recipe); + super("Forestry Still", MachineStill.RecipeManager.recipes); + recipes.add(recipe); } @Override - public String getRecipeInfo() { - return ((Recipe) recipe).output.getLocalizedName(); + public String getRecipeInfo(Recipe recipe) { + return LogHelper.getStackDescription(recipe.output); } } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @ZenMethod public static void removeRecipe(ILiquidStack output, ILiquidStack input) { - MineTweakerAPI.apply(new Remove(MachineStill.RecipeManager.recipes, toFluid(input))); + List recipes = new LinkedList(); + + for (Recipe r : RecipeManager.recipes) { + if (r != null && r.output != null && matches(output, toILiquidStack(r.output))) { + recipes.add(r); + } + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new Remove(recipes)); + } else { + LogHelper.logWarning(String.format("No %s Recipe found for %s. Command ignored!", Still.name, output.toString())); + } } - private static class Remove extends BaseListRemoval { - - public Remove(List list, FluidStack input) { - super(list, input); + private static class Remove extends BaseListRemoval { + public Remove(List recipes) { + super(Still.name, RecipeManager.recipes, recipes); } - - @Override - public void apply() { - for (Recipe r : RecipeManager.recipes) { - if (r.output != null && r.output.isFluidEqual(fluid)) { - recipes.add(r); - } - } - super.apply(); - } + + @Override + public String getRecipeInfo(Recipe recipe) { + return LogHelper.getStackDescription(recipe.output); + } } } diff --git a/src/main/java/modtweaker2/mods/forestry/handlers/ThermionicFabricator.java b/src/main/java/modtweaker2/mods/forestry/handlers/ThermionicFabricator.java index 12c6314..b0dc554 100644 --- a/src/main/java/modtweaker2/mods/forestry/handlers/ThermionicFabricator.java +++ b/src/main/java/modtweaker2/mods/forestry/handlers/ThermionicFabricator.java @@ -1,13 +1,18 @@ package modtweaker2.mods.forestry.handlers; import static modtweaker2.helpers.InputHelper.toFluid; +import static modtweaker2.helpers.InputHelper.toIItemStack; import static modtweaker2.helpers.InputHelper.toStack; +import static modtweaker2.helpers.StackHelper.matches; +import java.util.LinkedList; import java.util.List; import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; import minetweaker.api.liquid.ILiquidStack; +import modtweaker2.helpers.LogHelper; import modtweaker2.utils.BaseListAddition; import modtweaker2.utils.BaseListRemoval; import net.minecraft.item.ItemStack; @@ -21,18 +26,20 @@ import forestry.factory.gadgets.MachineFabricator.Smelting; @ZenClass("mods.forestry.ThermionicFabricator") public class ThermionicFabricator { + + public static final String nameSmelting = "Forestry Thermionic Fabricator (Smelting)"; + public static final String nameCasting = "Forestry Thermionic Fabricator (Casting)"; + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //first production step: smelting item into liquid @ZenMethod public static void addSmelting(IItemStack itemInput, int meltingPoint, int fluidOutput) { //The machines internal tank accept only liquid glass, therefor this function only accept the amount and hardcode the fluid to glass - MineTweakerAPI.apply(new Add(new Smelting(toStack(itemInput), FluidRegistry.getFluidStack("glass", fluidOutput), meltingPoint))); + MineTweakerAPI.apply(new AddSmelting(new Smelting(toStack(itemInput), FluidRegistry.getFluidStack("glass", fluidOutput), meltingPoint))); } - @ZenMethod - public static void removeSmelting(IItemStack itemInput) { - MineTweakerAPI.apply(new Remove(toStack(itemInput), RecipeManager.smeltings, RecipeType.Smelting)); - } + //second step: casting liquid + objetcs + plan into result @ZenMethod @@ -44,86 +51,95 @@ public class ThermionicFabricator { } } - MineTweakerAPI.apply(new Add(new Recipe(toStack(plan), toFluid(fluidInput), new ShapedRecipeCustom(3, 3, flatList, toStack(product))))); - } - - @ZenMethod - public static void removeCasts(IItemStack product) { - MineTweakerAPI.apply(new Remove(toStack(product), RecipeManager.recipes, RecipeType.Casting)); + MineTweakerAPI.apply(new AddCast(new Recipe(toStack(plan), toFluid(fluidInput), new ShapedRecipeCustom(3, 3, flatList, toStack(product))))); } /* Implements the actions to add a recipe Since the machine has two crafting Steps, this is a constructors for both */ - private static class Add extends BaseListAddition { + private static class AddSmelting extends BaseListAddition { - public Add(Smelting recipe) { - super("Forestry Thermionic Fabricator (Smelting)", RecipeManager.smeltings, recipe); + public AddSmelting(Smelting recipe) { + super(ThermionicFabricator.nameSmelting, RecipeManager.smeltings); + recipes.add(recipe); } - public Add(Recipe recipe) { - super("Forestry Thermionic Fabricator (Casting)", RecipeManager.recipes, recipe); + @Override + public String getRecipeInfo(Smelting recipe) { + return LogHelper.getStackDescription(recipe.getResource()); + } + } + + private static class AddCast extends BaseListAddition { + + public AddCast(Recipe recipe) { + super(ThermionicFabricator.nameCasting, RecipeManager.recipes); + recipes.add(recipe); + } + + @Override + public String getRecipeInfo(Recipe recipe) { + return LogHelper.getStackDescription(recipe.getPlan()); + } + } + + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + @ZenMethod + public static void removeSmelting(IIngredient itemInput) { + List recipes = new LinkedList(); + + for (Smelting r : RecipeManager.smeltings) { + if (r != null && r.getResource() != null && matches(itemInput, toIItemStack(r.getResource()))) { + recipes.add(r); + } + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new RemoveSmelting(recipes)); + } else { + LogHelper.logWarning(String.format("No %s Recipe found for %s. Command ignored!", ThermionicFabricator.nameSmelting, itemInput.toString())); + } + } + + @ZenMethod + public static void removeCasts(IIngredient product) { + List recipes = new LinkedList(); + + for (Recipe r : RecipeManager.recipes) { + if (r != null && r.asIRecipe().getRecipeOutput() != null && matches(product, toIItemStack(r.asIRecipe().getRecipeOutput()))) { + recipes.add(r); + } + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new RemoveCasts(recipes)); + } else { + LogHelper.logWarning(String.format("No %s Recipe found for %s. Command ignored!", ThermionicFabricator.nameSmelting, product.toString())); + } + } + + private static class RemoveSmelting extends BaseListRemoval { + public RemoveSmelting(List recipes) { + super(ThermionicFabricator.nameSmelting, RecipeManager.smeltings, recipes); } @Override - public String getRecipeInfo() { - if (recipe instanceof Smelting) - return " Input:" + ((Smelting) recipe).getResource().getDisplayName(); - else - return "Output: " + ((Recipe) recipe).asIRecipe().getRecipeOutput().getDisplayName(); + public String getRecipeInfo(Smelting recipe) { + return LogHelper.getStackDescription(recipe.getResource()); } } + + private static class RemoveCasts extends BaseListRemoval { + public RemoveCasts(List recipes) { + super(ThermionicFabricator.nameCasting, RecipeManager.recipes, recipes); + } - /* - Implements the actions to remove recipes - */ - private static class Remove extends BaseListRemoval { - private final RecipeType type; - - public Remove(ItemStack input, List list, RecipeType type) { - super(String.format("Forestry Thermionic Fabricator (%s)", type.toString()), list, input); - this.type = type; - } - - @Override - public void apply() { - switch(type) { - case Smelting: - for (Object r : list) { - if (((Smelting)r).getResource() != null && ((Smelting)r).getResource().isItemEqual(stack)) { - recipes.add(r); - } - } - break; - - case Casting: - for (Object r : list) { - if (((Recipe)r).asIRecipe().getRecipeOutput() != null && ((Recipe)r).asIRecipe().getRecipeOutput().isItemEqual(stack)) { - recipes.add(r); - } - } - break; - } - super.apply(); - } - - @Override - public String getRecipeInfo() { - switch(type) { - case Smelting: - return " Input:" + stack.getDisplayName(); // + " -- Output:" + ((Smelting) recipe).getProduct().getLocalizedName(); - - case Casting: - return " Output:" + stack.getDisplayName(); // + " -- Output:" + ((Recipe) recipe).asIRecipe().getIngredients()[0].getDisplayName(); - } - - return stack.getDisplayName(); - } - } - - public enum RecipeType { - Smelting, - Casting + @Override + public String getRecipeInfo(Recipe recipe) { + return LogHelper.getStackDescription(recipe.getPlan()); + } } } diff --git a/src/main/java/modtweaker2/mods/fsp/handlers/Crucible.java b/src/main/java/modtweaker2/mods/fsp/handlers/Crucible.java index 13fa184..3686437 100644 --- a/src/main/java/modtweaker2/mods/fsp/handlers/Crucible.java +++ b/src/main/java/modtweaker2/mods/fsp/handlers/Crucible.java @@ -1,12 +1,22 @@ package modtweaker2.mods.fsp.handlers; +import static modtweaker2.helpers.InputHelper.toIItemStack; import static modtweaker2.helpers.InputHelper.toStack; +import static modtweaker2.helpers.StackHelper.matches; import static modtweaker2.mods.fsp.FSPHelper.getLiquid; + +import java.util.HashMap; +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.LogHelper; import modtweaker2.utils.BaseListAddition; import modtweaker2.utils.BaseMapAddition; import modtweaker2.utils.BaseMapRemoval; +import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import org.apache.commons.lang3.tuple.MutablePair; @@ -20,6 +30,11 @@ import flaxbeard.steamcraft.api.Tuple3; @ZenClass("mods.fsp.Crucible") public class Crucible { + + public static final String nameLiquid = "FSP Crucible (Liquid)"; + public static final String nameMelting = "FSP Crucible (Liquid)"; + public static final String nameDunking = "FSP Crucible (Dunking)"; + @ZenMethod public static void addLiquid(String name, IItemStack ingot, IItemStack plate, IItemStack nugget, int r, int g, int b) { MineTweakerAPI.apply(new AddLiquid(new CrucibleLiquid(name, toStack(ingot), toStack(plate), toStack(nugget), null, r, g, b))); @@ -30,14 +45,15 @@ public class Crucible { MineTweakerAPI.apply(new AddLiquid(new CrucibleLiquid(name, toStack(ingot), toStack(plate), toStack(nugget), new CrucibleFormula(getLiquid(l1), n1, getLiquid(l2), n2, n3), r, g, b))); } - private static class AddLiquid extends BaseListAddition { + private static class AddLiquid extends BaseListAddition { public AddLiquid(CrucibleLiquid recipe) { - super("FSP Crucible Liquid", SteamcraftRegistry.liquids, recipe); + super(nameLiquid, SteamcraftRegistry.liquids); + recipes.add(recipe); } @Override - public String getRecipeInfo() { - return ((CrucibleLiquid) recipe).name; + public String getRecipeInfo(CrucibleLiquid recipe) { + return recipe.name; } } @@ -50,41 +66,43 @@ public class Crucible { } } - private static class AddMelting extends BaseMapAddition { - private final ItemStack stack; - - public AddMelting(ItemStack stack, Object key, Object recipe) { - super("FSP Crucible Melting", SteamcraftRegistry.smeltThings, key, recipe); - this.stack = stack; + private static class AddMelting extends BaseMapAddition, MutablePair> { + public AddMelting(ItemStack stack, MutablePair key, MutablePair recipe) { + super(nameMelting, SteamcraftRegistry.smeltThings); + map.put(key, recipe); } @Override - public String getRecipeInfo() { - return stack.getDisplayName(); + protected String getRecipeInfo(Entry, MutablePair> recipe) { + return LogHelper.getStackDescription(new ItemStack(recipe.getKey().left, 1, recipe.getKey().right)); } } @ZenMethod - public static void removeMelting(IItemStack input) { - MineTweakerAPI.apply(new RemoveMelting(toStack(input), MutablePair.of(toStack(input).getItem(), toStack(input).getItemDamage()))); - } - - private static class RemoveMelting extends BaseMapRemoval { - private final ItemStack stack; - - public RemoveMelting(ItemStack stack, Object key) { - super("FSP Crucible Melting", SteamcraftRegistry.smeltThings, key); - this.stack = stack; + public static void removeMelting(IIngredient input) { + Map, MutablePair> recipes = new HashMap, MutablePair>(); + + for(Entry, MutablePair> entry : SteamcraftRegistry.smeltThings.entrySet()) { + if(matches(input, toIItemStack(new ItemStack(entry.getKey().left, 1, entry.getKey().right)))) { + recipes.put(entry.getKey(), entry.getValue()); + } } - public void apply(){ - SteamcraftRegistry.smeltThings.remove(stack); - MutablePair pair; + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new RemoveMelting(recipes)); + } else { + LogHelper.logWarning(String.format("No %s Recipes found for %s", Crucible.nameMelting, input)); + } + } + + private static class RemoveMelting extends BaseMapRemoval, MutablePair> { + public RemoveMelting(Map, MutablePair> recipes) { + super(nameMelting, SteamcraftRegistry.smeltThings, recipes); } @Override - public String getRecipeInfo() { - return stack.getDisplayName(); + protected String getRecipeInfo(Entry, MutablePair> recipe) { + return LogHelper.getStackDescription(new ItemStack(recipe.getKey().left, 1, recipe.getKey().right)); } } @@ -93,40 +111,50 @@ public class Crucible { ItemStack stack = toStack(input); CrucibleLiquid fluid = getLiquid(liquid); if (fluid != null) { - MineTweakerAPI.apply(new AddDunking(toStack(input), new Tuple3(stack.getItem(), stack.getItemDamage(), fluid), MutablePair.of(volume, toStack(output)))); + MineTweakerAPI.apply(new AddDunking(new Tuple3(stack.getItem(), stack.getItemDamage(), fluid), MutablePair.of(volume, toStack(output)))); } } - private static class AddDunking extends BaseMapAddition { - private final ItemStack stack; - - public AddDunking(ItemStack stack, Object key, Object recipe) { - super("FSP Crucible Dunking", SteamcraftRegistry.dunkThings, key, recipe); - this.stack = stack; + private static class AddDunking extends BaseMapAddition> { + public AddDunking(Tuple3 key, MutablePair recipe) { + super(Crucible.nameDunking, SteamcraftRegistry.dunkThings); + recipes.put(key, recipe); } @Override - public String getRecipeInfo() { - return stack.getDisplayName(); + public String getRecipeInfo(Entry> recipe) { + return LogHelper.getStackDescription(recipe.getValue().right); } } @ZenMethod - public static void removeDunking(IItemStack input, String liquid) { - MineTweakerAPI.apply(new RemoveDunking(toStack(input), new Tuple3(toStack(input).getItem(), toStack(input).getItemDamage(), getLiquid(liquid)))); + public static void removeDunking(IIngredient input, String liquid) { + Map> recipes = new HashMap>(); + + for(Entry> recipe : SteamcraftRegistry.dunkThings.entrySet()) { + if(recipe.getValue() != null && recipe.getValue().right != null && matches(input, toIItemStack(recipe.getValue().right))) { + if(((CrucibleLiquid)recipe.getKey().third).equals(getLiquid(liquid))) { + recipes.put(recipe.getKey(), recipe.getValue()); + } + } + + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new RemoveDunking(recipes)); + } else { + LogHelper.logWarning(String.format("No %s recipe found for %s and %s. Command ignored!", Crucible.nameDunking, input.toString(), liquid)); + } } - private static class RemoveDunking extends BaseMapRemoval { - private final ItemStack stack; - - public RemoveDunking(ItemStack stack, Object key) { - super("FSP Crucible Dunking", SteamcraftRegistry.dunkThings, key); - this.stack = stack; + private static class RemoveDunking extends BaseMapRemoval> { + public RemoveDunking(Map> recipes) { + super(Crucible.nameDunking, SteamcraftRegistry.dunkThings, recipes); } @Override - public String getRecipeInfo() { - return stack.getDisplayName(); + public String getRecipeInfo(Entry> recipe) { + return LogHelper.getStackDescription(recipe.getValue().right); } } } diff --git a/src/main/java/modtweaker2/mods/fsp/handlers/Furnace.java b/src/main/java/modtweaker2/mods/fsp/handlers/Furnace.java index 4cc1fa0..f93b7f6 100644 --- a/src/main/java/modtweaker2/mods/fsp/handlers/Furnace.java +++ b/src/main/java/modtweaker2/mods/fsp/handlers/Furnace.java @@ -1,10 +1,20 @@ package modtweaker2.mods.fsp.handlers; +import static modtweaker2.helpers.InputHelper.toIItemStack; import static modtweaker2.helpers.InputHelper.toStack; +import static modtweaker2.helpers.StackHelper.matches; + +import java.util.HashMap; +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.LogHelper; import modtweaker2.utils.BaseMapAddition; import modtweaker2.utils.BaseMapRemoval; +import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import org.apache.commons.lang3.tuple.MutablePair; @@ -15,41 +25,51 @@ import flaxbeard.steamcraft.api.SteamcraftRegistry; @ZenClass("mods.fsp.Furnace") public class Furnace { + + public static final String name = "FSP Furnace (Steamed Foods)"; + @ZenMethod public static void addSteamFood(IItemStack input, IItemStack output) { MineTweakerAPI.apply(new AddSteamFood(toStack(input), MutablePair.of(toStack(input).getItem(), toStack(input).getItemDamage()), MutablePair.of(toStack(output).getItem(), toStack(output).getItemDamage()))); } - private static class AddSteamFood extends BaseMapAddition { - private final ItemStack stack; - - public AddSteamFood(ItemStack stack, MutablePair key, MutablePair recipe) { - super("FSP Furnace - Steam Food", SteamcraftRegistry.steamedFoods, key, recipe); - this.stack = stack; + private static class AddSteamFood extends BaseMapAddition, MutablePair> { + public AddSteamFood(ItemStack stack, MutablePair key, MutablePair recipe) { + super(Furnace.name, SteamcraftRegistry.steamedFoods); + recipes.put(key, recipe); } @Override - public String getRecipeInfo() { - return stack.getDisplayName(); + public String getRecipeInfo(Entry, MutablePair> recipe) { + return LogHelper.getStackDescription(new ItemStack(recipe.getKey().left, 1, recipe.getKey().right)); } } @ZenMethod - public static void removeSteamFood(IItemStack input) { - MineTweakerAPI.apply(new RemoveSteamFood(toStack(input), MutablePair.of(toStack(input).getItem(), toStack(input).getItemDamage()))); + public static void removeSteamFood(IIngredient input) { + Map, MutablePair> recipes = new HashMap, MutablePair>(); + + for(Entry, MutablePair> recipe : SteamcraftRegistry.steamedFoods.entrySet()) { + if(matches(input, toIItemStack(new ItemStack(recipe.getKey().left, 1, recipe.getKey().right)))) { + recipes.put(recipe.getKey(), recipe.getValue()); + } + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new RemoveSteamFood(recipes)); + } else { + LogHelper.logWarning(String.format("No %s Recipe found for %s. Command ignored!", Furnace.name, input.toString())); + } } - private static class RemoveSteamFood extends BaseMapRemoval { - private final ItemStack stack; - - public RemoveSteamFood(ItemStack stack, MutablePair key) { - super("FSP Furnace - Steam Food", SteamcraftRegistry.steamedFoods, key); - this.stack = stack; + private static class RemoveSteamFood extends BaseMapRemoval, MutablePair> { + public RemoveSteamFood(Map, MutablePair> recipes) { + super(Furnace.name, SteamcraftRegistry.steamedFoods, recipes); } @Override - public String getRecipeInfo() { - return stack.getDisplayName(); + public String getRecipeInfo(Entry, MutablePair> recipe) { + return LogHelper.getStackDescription(new ItemStack(recipe.getKey().left, 1, recipe.getKey().right)); } } } diff --git a/src/main/java/modtweaker2/mods/mariculture/MaricultureHelper.java b/src/main/java/modtweaker2/mods/mariculture/MaricultureHelper.java index 3fa258a..2db7fd3 100644 --- a/src/main/java/modtweaker2/mods/mariculture/MaricultureHelper.java +++ b/src/main/java/modtweaker2/mods/mariculture/MaricultureHelper.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.Map; +import mariculture.api.core.FuelInfo; import mariculture.api.core.MaricultureHandlers; import mariculture.api.fishery.Fishing; import mariculture.api.fishery.Loot; @@ -15,7 +16,7 @@ import net.minecraftforge.fluids.FluidStack; import net.minecraftforge.oredict.OreDictionary; public class MaricultureHelper { - public static Map fuels = null; + public static Map fuels = null; public static HashMap> loot = null; static { @@ -27,7 +28,7 @@ public class MaricultureHelper { private MaricultureHelper() {} - //Helper for getting the key that is used when adding/removing fuels + // Helper for getting the key that is used when adding/removing fuels public static String getKey(Object o) { if (o instanceof String) return (String) o; else if (o instanceof ItemStack) { diff --git a/src/main/java/modtweaker2/mods/mariculture/handlers/Anvil.java b/src/main/java/modtweaker2/mods/mariculture/handlers/Anvil.java index 17760da..29680ef 100644 --- a/src/main/java/modtweaker2/mods/mariculture/handlers/Anvil.java +++ b/src/main/java/modtweaker2/mods/mariculture/handlers/Anvil.java @@ -1,33 +1,46 @@ package modtweaker2.mods.mariculture.handlers; +import static modtweaker2.helpers.InputHelper.toIItemStack; import static modtweaker2.helpers.InputHelper.toStack; +import static modtweaker2.helpers.StackHelper.matches; + +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; + import mariculture.api.core.IAnvilHandler.RecipeAnvil; import mariculture.api.core.MaricultureHandlers; import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; +import modtweaker2.helpers.LogHelper; import modtweaker2.mods.mariculture.MaricultureHelper; import modtweaker2.utils.BaseMapAddition; import modtweaker2.utils.BaseMapRemoval; -import net.minecraft.item.ItemStack; import stanhebben.zenscript.annotations.ZenClass; import stanhebben.zenscript.annotations.ZenMethod; @ZenClass("mods.mariculture.Anvil") public class Anvil { - //Adding a Mariculture Anvil recipe + + public static final String name = "Mariculture Anvil"; + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + @ZenMethod public static void addRecipe(IItemStack input, IItemStack output, int hits) { MineTweakerAPI.apply(new Add(new RecipeAnvil(toStack(input), toStack(output), hits))); } - private static class Add extends BaseMapAddition { + private static class Add extends BaseMapAddition { public Add(RecipeAnvil recipe) { - super("Mariculture Anvil", MaricultureHandlers.anvil.getRecipes(), MaricultureHelper.getKey(recipe.input), recipe); + super("Mariculture Anvil", MaricultureHandlers.anvil.getRecipes()); + recipes.put(MaricultureHelper.getKey(recipe.input), recipe); } @Override - public String getRecipeInfo() { - return ((RecipeAnvil) recipe).output.getDisplayName(); + public String getRecipeInfo(Entry recipe) { + return LogHelper.getStackDescription(recipe.getValue().output); } } @@ -35,18 +48,30 @@ public class Anvil { //Removing a Mariculture Anvil recipe @ZenMethod - public static void removeRecipe(IItemStack input) { - MineTweakerAPI.apply(new Remove(toStack(input))); + public static void removeRecipe(IIngredient input) { + Map recipes = new HashMap(); + + for(Entry recipe : MaricultureHandlers.anvil.getRecipes().entrySet()) { + if(recipe != null && recipe.getValue() != null && recipe.getValue().input != null && matches(input, toIItemStack(recipe.getValue().input))) { + recipes.put(recipe.getKey(), recipe.getValue()); + } + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new Remove(recipes)); + } else { + LogHelper.logWarning(String.format("No %s Recipe found for %s. Command ignored!", Anvil.name, input.toString())); + } } - private static class Remove extends BaseMapRemoval { - public Remove(ItemStack stack) { - super("Mariculture Anvil", MaricultureHandlers.anvil.getRecipes(), MaricultureHelper.getKey(stack), stack); + private static class Remove extends BaseMapRemoval { + public Remove(Map recipes) { + super("Mariculture Anvil", MaricultureHandlers.anvil.getRecipes(), recipes); } @Override - public String getRecipeInfo() { - return ((ItemStack) stack).getDisplayName(); + public String getRecipeInfo(Entry recipe) { + return LogHelper.getStackDescription(recipe.getValue().output); } } } diff --git a/src/main/java/modtweaker2/mods/mariculture/handlers/Casting.java b/src/main/java/modtweaker2/mods/mariculture/handlers/Casting.java index 78ffb5b..091dd31 100644 --- a/src/main/java/modtweaker2/mods/mariculture/handlers/Casting.java +++ b/src/main/java/modtweaker2/mods/mariculture/handlers/Casting.java @@ -1,26 +1,36 @@ package modtweaker2.mods.mariculture.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.HashMap; +import java.util.Map; +import java.util.Map.Entry; import mariculture.api.core.MaricultureHandlers; import mariculture.api.core.RecipeCasting; import mariculture.api.core.RecipeCasting.RecipeNuggetCasting; import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; import minetweaker.api.liquid.ILiquidStack; +import modtweaker2.helpers.LogHelper; import modtweaker2.mods.mariculture.MaricultureHelper; import modtweaker2.utils.BaseMapAddition; import modtweaker2.utils.BaseMapRemoval; -import net.minecraft.item.ItemStack; import stanhebben.zenscript.annotations.ZenClass; import stanhebben.zenscript.annotations.ZenMethod; @ZenClass("mods.mariculture.Casting") public class Casting { - //Adding Mariculture Casting Recipes + + public static final String name = "Mariculture Casting"; + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + @ZenMethod public static void addNuggetRecipe(ILiquidStack input, IItemStack output) { MineTweakerAPI.apply(new Add(new RecipeNuggetCasting(toFluid(input), toStack(output)), MaricultureHandlers.casting.getNuggetRecipes())); @@ -36,43 +46,63 @@ public class Casting { MineTweakerAPI.apply(new Add(new RecipeNuggetCasting(toFluid(input), toStack(output)), MaricultureHandlers.casting.getBlockRecipes())); } - private static class Add extends BaseMapAddition { - public Add(RecipeCasting recipe, HashMap map) { - super("Mariculture Casting", map, MaricultureHelper.getKey(recipe.fluid), recipe); + private static class Add extends BaseMapAddition { + public Add(RecipeCasting recipe, HashMap map) { + super(Casting.name, map); + recipes.put(MaricultureHelper.getKey(recipe.fluid), recipe); } @Override - public String getRecipeInfo() { - return ((RecipeCasting) recipe).output.getDisplayName(); + public String getRecipeInfo(Entry recipe) { + return LogHelper.getStackDescription(recipe.getValue().output); } } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - //Removing Mariculture Casting Recipes @ZenMethod - public static void removeNuggetRecipe(IItemStack input) { - MineTweakerAPI.apply(new Remove(toStack(input), MaricultureHandlers.casting.getNuggetRecipes())); + public static void removeNuggetRecipe(IIngredient ingredient) { + removeRecipe(ingredient, MaricultureHandlers.casting.getNuggetRecipes()); } @ZenMethod - public static void removeIngotRecipe(IItemStack input) { - MineTweakerAPI.apply(new Remove(toStack(input), MaricultureHandlers.casting.getIngotRecipes())); + public static void removeIngotRecipe(IIngredient ingredient) { + removeRecipe(ingredient, MaricultureHandlers.casting.getIngotRecipes()); } @ZenMethod - public static void removeBlockRecipe(IItemStack input) { - MineTweakerAPI.apply(new Remove(toStack(input), MaricultureHandlers.casting.getBlockRecipes())); + public static void removeBlockRecipe(IIngredient ingredient) { + removeRecipe(ingredient, MaricultureHandlers.casting.getBlockRecipes()); + } + + public static void removeRecipe(IIngredient ingredient, HashMap map) { + Map recipes = new HashMap(); + + for(Entry recipe : map.entrySet()) { + if(recipe != null && recipe.getValue() != null) { + if(recipe.getValue().fluid != null && matches(ingredient, toILiquidStack(recipe.getValue().fluid))) { + recipes.put(recipe.getKey(), recipe.getValue()); + } + + if(recipe.getValue().output != null && matches(ingredient, toIItemStack(recipe.getValue().output))) { + recipes.put(recipe.getKey(), recipe.getValue()); + } + } + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new Remove(map, recipes)); + } } - private static class Remove extends BaseMapRemoval { - public Remove(ItemStack stack, HashMap map) { - super("Mariculture Casting", map, MaricultureHelper.getKey(stack), stack); + private static class Remove extends BaseMapRemoval { + public Remove(Map map, Map recipes) { + super(Casting.name, map, recipes); } @Override - public String getRecipeInfo() { - return ((ItemStack) stack).getDisplayName(); + public String getRecipeInfo(Entry recipe) { + return LogHelper.getStackDescription(recipe.getValue().output); } } } diff --git a/src/main/java/modtweaker2/mods/mariculture/handlers/Crucible.java b/src/main/java/modtweaker2/mods/mariculture/handlers/Crucible.java index ff94994..a11a738 100644 --- a/src/main/java/modtweaker2/mods/mariculture/handlers/Crucible.java +++ b/src/main/java/modtweaker2/mods/mariculture/handlers/Crucible.java @@ -1,14 +1,24 @@ package modtweaker2.mods.mariculture.handlers; import static modtweaker2.helpers.InputHelper.toFluid; +import static modtweaker2.helpers.InputHelper.toIItemStack; import static modtweaker2.helpers.InputHelper.toStack; -import static modtweaker2.helpers.StackHelper.areEqual; +import static modtweaker2.helpers.StackHelper.matches; + +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + import mariculture.api.core.FuelInfo; import mariculture.api.core.MaricultureHandlers; import mariculture.api.core.RecipeSmelter; import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; import minetweaker.api.liquid.ILiquidStack; +import modtweaker2.helpers.LogHelper; import modtweaker2.mods.mariculture.MaricultureHelper; import modtweaker2.utils.BaseListAddition; import modtweaker2.utils.BaseListRemoval; @@ -21,6 +31,9 @@ import stanhebben.zenscript.annotations.ZenMethod; @ZenClass("mods.mariculture.Crucible") public class Crucible { + + public static final String nameSmelting = "Mariculture Crucible (Smelting)"; + public static final String nameFuel = "Mariculture Crucible (Fuel)"; /********************************************** Mariculture Crucible Recipes **********************************************/ @@ -32,14 +45,15 @@ public class Crucible { } // Passes the list to the base list implementation, and adds the recipe - private static class AddRecipe extends BaseListAddition { + private static class AddRecipe extends BaseListAddition { public AddRecipe(RecipeSmelter recipe) { - super("Mariculture Crucible", MaricultureHandlers.crucible.getRecipes(), recipe); + super(Crucible.nameSmelting, MaricultureHandlers.crucible.getRecipes()); + recipes.add(recipe); } @Override - public String getRecipeInfo() { - return ((RecipeSmelter) recipe).input.getDisplayName(); + public String getRecipeInfo(RecipeSmelter recipe) { + return LogHelper.getStackDescription(recipe.input); } } @@ -47,35 +61,34 @@ public class Crucible { // Removing a Mariculture Crucible recipe @ZenMethod - public static void removeRecipe(IItemStack input) { - MineTweakerAPI.apply(new RemoveRecipe(toStack(input))); + public static void removeRecipe(IIngredient input) { + List recipes = new LinkedList(); + + for (RecipeSmelter r : MaricultureHandlers.crucible.getRecipes()) { + if (r != null) { + if (r.input != null && matches(input, toIItemStack(r.input))) { + recipes.add(r); + } + } + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new RemoveRecipe(recipes)); + } else { + LogHelper.logWarning(String.format("No %s Recipe found for %s. Command ignored", Crucible.nameSmelting, input.toString())); + } + } - // Removes a recipe, apply is never the same for anything, so will always - // need to override it - private static class RemoveRecipe extends BaseListRemoval { - public RemoveRecipe(ItemStack stack) { - super("Mariculture Crucible", MaricultureHandlers.crucible.getRecipes(), stack); - } - - // Loops through the registry, to find the item that matches, saves that - // recipe then removes it - @Override - public void apply() { - for (RecipeSmelter r : MaricultureHandlers.crucible.getRecipes()) { - if (r != null) { - if (r.input != null && stack != null && areEqual(r.input, stack)) { - recipes.add(r); - } - } - } - super.apply(); - } - - @Override - public String getRecipeInfo() { - return stack.getDisplayName(); + private static class RemoveRecipe extends BaseListRemoval { + public RemoveRecipe(List recipes) { + super(Crucible.nameSmelting, MaricultureHandlers.crucible.getRecipes(), recipes); } + + @Override + public String getRecipeInfo(RecipeSmelter recipe) { + return LogHelper.getStackDescription(recipe.input); + } } /********************************************** Crucible Fuels **********************************************/ @@ -95,42 +108,58 @@ public class Crucible { } // Passes the list to the base map implementation, and adds the recipe - private static class AddFuel extends BaseMapAddition { + private static class AddFuel extends BaseMapAddition { public AddFuel(Object o, FuelInfo info) { - super("Mariculture Crucible Fuel", MaricultureHelper.fuels, MaricultureHelper.getKey(o), info); + super(Crucible.nameFuel, MaricultureHelper.fuels); + recipes.put(MaricultureHelper.getKey(o), info); } - @Override - public String getRecipeInfo() { - return (String) key; - } + @Override + public String getRecipeInfo(Entry recipe) { + return (String) recipe.getKey(); + } } // /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @ZenMethod public static void removeFuel(IItemStack fuel) { - MineTweakerAPI.apply(new RemoveFuel(fuel)); + removeFuelEntry(fuel); } @ZenMethod public static void removeFuel(ILiquidStack fuel) { - MineTweakerAPI.apply(new RemoveFuel(fuel)); + removeFuelEntry(fuel); } @ZenMethod public static void removeFuel(String fuel) { - MineTweakerAPI.apply(new RemoveFuel(fuel)); + removeFuelEntry(fuel); + } + + public static void removeFuelEntry(Object o) { + String key = MaricultureHelper.getKey(o); + Map recipes = new HashMap(); + + for(Entry entry : MaricultureHelper.fuels.entrySet()) { + if(key.matches((String)entry.getKey())) { + recipes.put(key, entry.getValue()); + } + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new RemoveFuel(recipes)); + } } // Removes a recipe, will always remove the key, so all should be good - private static class RemoveFuel extends BaseMapRemoval { - public RemoveFuel(Object o) { - super("Mariculture Crucible Fuel", MaricultureHelper.fuels, MaricultureHelper.getKey(o), null); + private static class RemoveFuel extends BaseMapRemoval { + public RemoveFuel(Map recipes) { + super(Crucible.nameFuel, MaricultureHelper.fuels, recipes); } - @Override - public String getRecipeInfo() { - return (String) key; - } + @Override + public String getRecipeInfo(Entry recipe) { + return (String) recipe.getKey(); + } } } diff --git a/src/main/java/modtweaker2/mods/mariculture/handlers/Fishing.java b/src/main/java/modtweaker2/mods/mariculture/handlers/Fishing.java index c957ca7..6d1a749 100644 --- a/src/main/java/modtweaker2/mods/mariculture/handlers/Fishing.java +++ b/src/main/java/modtweaker2/mods/mariculture/handlers/Fishing.java @@ -14,8 +14,7 @@ import minetweaker.MineTweakerAPI; import minetweaker.api.item.IItemStack; import modtweaker2.helpers.ReflectionHelper; import modtweaker2.mods.mariculture.MaricultureHelper; -import modtweaker2.utils.BaseDescriptionAddition; -import modtweaker2.utils.BaseDescriptionRemoval; +import modtweaker2.utils.BaseUndoable; import net.minecraft.item.ItemStack; import stanhebben.zenscript.annotations.Optional; import stanhebben.zenscript.annotations.ZenClass; @@ -67,7 +66,7 @@ public class Fishing { } } - private static class AddLoot extends BaseDescriptionAddition { + private static class AddLoot extends BaseUndoable { private final Loot loot; public AddLoot(Loot loot, String description) { @@ -80,6 +79,7 @@ public class Fishing { ArrayList list = MaricultureHelper.loot.get(loot.rarity); list.add(loot); MaricultureHelper.loot.put(loot.rarity, list); + success = true; } @Override @@ -103,7 +103,7 @@ public class Fishing { MineTweakerAPI.apply(new RemoveLoot(toStack(loot))); } - private static class RemoveLoot extends BaseDescriptionRemoval { + private static class RemoveLoot extends BaseUndoable { private HashMap loot; private final ItemStack stack; @@ -119,6 +119,7 @@ public class Fishing { apply(Rarity.JUNK); apply(Rarity.GOOD); apply(Rarity.RARE); + success = true; } //Performs the apply function on all rarity types diff --git a/src/main/java/modtweaker2/mods/mariculture/handlers/Vat.java b/src/main/java/modtweaker2/mods/mariculture/handlers/Vat.java index ef45f09..fa43000 100644 --- a/src/main/java/modtweaker2/mods/mariculture/handlers/Vat.java +++ b/src/main/java/modtweaker2/mods/mariculture/handlers/Vat.java @@ -1,22 +1,33 @@ package modtweaker2.mods.mariculture.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.areEqual; +import static modtweaker2.helpers.StackHelper.matches; + +import java.util.LinkedList; +import java.util.List; + import mariculture.api.core.MaricultureHandlers; import mariculture.api.core.RecipeVat; import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; import minetweaker.api.liquid.ILiquidStack; +import modtweaker2.helpers.LogHelper; import modtweaker2.utils.BaseListAddition; import modtweaker2.utils.BaseListRemoval; -import net.minecraft.item.ItemStack; -import net.minecraftforge.fluids.FluidStack; import stanhebben.zenscript.annotations.ZenClass; import stanhebben.zenscript.annotations.ZenMethod; @ZenClass("mods.mariculture.Vat") public class Vat { + + public static final String name = "Mariculture Vat"; + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + // Adding a Mariculture Vat recipe @ZenMethod public static void addRecipe(ILiquidStack fluid1, ILiquidStack fluid2, ILiquidStack outputFluid, int time) { @@ -64,74 +75,82 @@ public class Vat { } // Passes the list to the base list implementation, and adds the recipe - private static class Add extends BaseListAddition { + private static class Add extends BaseListAddition { public Add(RecipeVat recipe) { - super("Mariculture Vat", MaricultureHandlers.vat.getRecipes(), recipe); + super("Mariculture Vat", MaricultureHandlers.vat.getRecipes()); + recipes.add(recipe); } @Override - public String getRecipeInfo() { - if (((RecipeVat) recipe).outputItem != null) - return ((RecipeVat) recipe).outputItem.getDisplayName(); - else - return ((RecipeVat) recipe).outputFluid.getFluid().getLocalizedName(); + public String getRecipeInfo(RecipeVat recipe) { + if(recipe.outputItem != null) { + return LogHelper.getStackDescription(recipe.outputItem); + } else { + return LogHelper.getStackDescription(recipe.outputFluid); + } } } - // /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Removing a Mariculture Vat recipe @ZenMethod - public static void removeRecipe(IItemStack outputItem) { - MineTweakerAPI.apply(new Remove(toStack(outputItem), null)); + public static void removeRecipe(IIngredient ingredient) { + List recipes = new LinkedList(); + + for(RecipeVat r : MaricultureHandlers.vat.getRecipes()) { + if(r != null) { + if(r.outputItem != null && matches(ingredient, toIItemStack(r.outputItem))) { + recipes.add(r); + } + else if(r.outputFluid != null && matches(ingredient, toILiquidStack(r.outputFluid))) { + recipes.add(r); + } + } + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new Remove(recipes)); + } else { + LogHelper.logWarning(String.format("No %s Recipes found for %s. Command ignored!", Vat.name, ingredient.toString())); + } } @ZenMethod - public static void removeRecipe(ILiquidStack outputFluid) { - MineTweakerAPI.apply(new Remove(null, toFluid(outputFluid))); - } - - @ZenMethod - public static void removeRecipe(IItemStack outputItem, ILiquidStack outputFluid) { - MineTweakerAPI.apply(new Remove(toStack(outputItem), toFluid(outputFluid))); + public static void removeRecipe(IIngredient outputItem, IIngredient outputFluid) { + List recipes = new LinkedList(); + + for (RecipeVat r : MaricultureHandlers.vat.getRecipes()) { + if (r != null) { + if (r.outputItem != null && matches(outputItem, toIItemStack(r.outputItem))) { + if (r.outputFluid != null && matches(outputFluid, toILiquidStack(r.outputFluid))) { + recipes.add(r); + } + } + } + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new Remove(recipes)); + } else { + LogHelper.logWarning(String.format("No %s Recipes found for %s and %s. Command ignored!", Vat.name, outputItem.toString(), outputFluid.toString())); + } } // Removes a recipe, apply is never the same for anything, so will always // need to override it - private static class Remove extends BaseListRemoval { - private FluidStack fluid = null; - - public Remove(ItemStack stack, FluidStack fluid) { - super("Mariculture Vat", MaricultureHandlers.vat.getRecipes(), stack); - if (fluid != null) - this.fluid = fluid; + private static class Remove extends BaseListRemoval { + public Remove(List recipes) { + super("Mariculture Vat", MaricultureHandlers.vat.getRecipes(), recipes); } - // Loops through the registry, to find the item that matches, saves that - // recipe then removes it - @Override - public void apply() { - for (RecipeVat r : MaricultureHandlers.vat.getRecipes()) { - if (r != null) - if (r.outputItem != null && stack != null && areEqual(r.outputItem, stack)) { - if (r.outputFluid == null || (fluid != null && r.outputFluid.isFluidStackIdentical(fluid))) { - recipes.add(r); - break; - } - } - - if ((r.outputFluid != null && fluid != null && r.outputFluid.isFluidStackIdentical(fluid))) { - recipes.add(r); - break; - } - } - - super.apply(); - } - - @Override - public String getRecipeInfo() { - return stack != null ? stack.getDisplayName() : fluid.getFluid().getLocalizedName(); - } + @Override + public String getRecipeInfo(RecipeVat recipe) { + if(recipe.outputItem != null) { + return LogHelper.getStackDescription(recipe.outputItem); + } else { + return LogHelper.getStackDescription(recipe.outputFluid); + } + } } } diff --git a/src/main/java/modtweaker2/mods/mekanism/Mekanism.java b/src/main/java/modtweaker2/mods/mekanism/Mekanism.java index eb88dcd..c06eff9 100644 --- a/src/main/java/modtweaker2/mods/mekanism/Mekanism.java +++ b/src/main/java/modtweaker2/mods/mekanism/Mekanism.java @@ -1,32 +1,33 @@ package modtweaker2.mods.mekanism; -import java.util.regex.Pattern; - import minetweaker.MineTweakerAPI; import modtweaker2.mods.mekanism.gas.GasBracketHandler; -import modtweaker2.mods.mekanism.handlers.v8.ChemicalInfuser; -import modtweaker2.mods.mekanism.handlers.v8.ChemicalInjection; +import modtweaker2.mods.mekanism.handlers.ChemicalCrystallizer; +import modtweaker2.mods.mekanism.handlers.ChemicalDissolution; +import modtweaker2.mods.mekanism.handlers.ChemicalInfuser; +import modtweaker2.mods.mekanism.handlers.ChemicalInjection; import modtweaker2.mods.mekanism.handlers.ChemicalOxidizer; import modtweaker2.mods.mekanism.handlers.ChemicalWasher; import modtweaker2.mods.mekanism.handlers.Combiner; import modtweaker2.mods.mekanism.handlers.Compressor; import modtweaker2.mods.mekanism.handlers.Crusher; import modtweaker2.mods.mekanism.handlers.Enrichment; +import modtweaker2.mods.mekanism.handlers.Infuser; import modtweaker2.mods.mekanism.handlers.Purification; import modtweaker2.mods.mekanism.handlers.Reaction; import modtweaker2.mods.mekanism.handlers.Sawmill; import modtweaker2.mods.mekanism.handlers.Separator; -import modtweaker2.mods.mekanism.handlers.v8.ChemicalCrystallizer; -import modtweaker2.mods.mekanism.handlers.v8.ChemicalDissolution; -import modtweaker2.mods.mekanism.handlers.v8.Infuser; import modtweaker2.utils.TweakerPlugin; -import cpw.mods.fml.common.Loader; -import cpw.mods.fml.common.ModContainer; public class Mekanism extends TweakerPlugin { - public static final boolean v7 = isV7(); public Mekanism() { MineTweakerAPI.registerBracketHandler(new GasBracketHandler()); + MineTweakerAPI.registerClass(ChemicalCrystallizer.class); + MineTweakerAPI.registerClass(ChemicalDissolution.class); + MineTweakerAPI.registerClass(ChemicalInfuser.class); + MineTweakerAPI.registerClass(ChemicalInjection.class); + MineTweakerAPI.registerClass(ChemicalOxidizer.class); + MineTweakerAPI.registerClass(ChemicalWasher.class); MineTweakerAPI.registerClass(Combiner.class); MineTweakerAPI.registerClass(Compressor.class); MineTweakerAPI.registerClass(Crusher.class); @@ -36,21 +37,5 @@ public class Mekanism extends TweakerPlugin { MineTweakerAPI.registerClass(Reaction.class); MineTweakerAPI.registerClass(Sawmill.class); MineTweakerAPI.registerClass(Separator.class); -// MineTweakerAPI.registerClass(ChemicalCrystallizer.class); - MineTweakerAPI.registerClass(ChemicalCrystallizer.class); - MineTweakerAPI.registerClass(ChemicalDissolution.class); - MineTweakerAPI.registerClass(ChemicalInfuser.class); - MineTweakerAPI.registerClass(ChemicalInjection.class); - MineTweakerAPI.registerClass(ChemicalOxidizer.class); - MineTweakerAPI.registerClass(ChemicalWasher.class); - } - - public static boolean isV7(){ - Pattern version = Pattern.compile("7.*"); - for (ModContainer loadedMod : Loader.instance().getActiveModList()) - { - if (loadedMod.getModId().equals("Mekanism") && version.matcher(loadedMod.getVersion()).find()) return true; - } - return false; } } diff --git a/src/main/java/modtweaker2/mods/mekanism/gas/GasLogger.java b/src/main/java/modtweaker2/mods/mekanism/commands/GasLogger.java similarity index 89% rename from src/main/java/modtweaker2/mods/mekanism/gas/GasLogger.java rename to src/main/java/modtweaker2/mods/mekanism/commands/GasLogger.java index 109f353..6d6e49a 100644 --- a/src/main/java/modtweaker2/mods/mekanism/gas/GasLogger.java +++ b/src/main/java/modtweaker2/mods/mekanism/commands/GasLogger.java @@ -1,4 +1,4 @@ -package modtweaker2.mods.mekanism.gas; +package modtweaker2.mods.mekanism.commands; import java.util.Collections; import java.util.Comparator; @@ -28,7 +28,7 @@ public class GasLogger implements ICommandFunction { Collections.sort(gases, COMPARATOR); for (Gas gas : gases) { System.out.println("Gas " + gas.getName()); - MineTweakerAPI.logCommand("<" + gas.getName() + "> -- " + gas.getLocalizedName()); + MineTweakerAPI.logCommand(" -- " + gas.getLocalizedName()); } if (player != null) { diff --git a/src/main/java/modtweaker2/mods/mekanism/commands/MekanismLogger.java b/src/main/java/modtweaker2/mods/mekanism/commands/MekanismLogger.java new file mode 100644 index 0000000..41c6e62 --- /dev/null +++ b/src/main/java/modtweaker2/mods/mekanism/commands/MekanismLogger.java @@ -0,0 +1,209 @@ +package modtweaker2.mods.mekanism.commands; + +import java.util.Arrays; +import java.util.Collection; +import java.util.LinkedList; +import java.util.List; + +import mekanism.api.gas.GasStack; +import mekanism.common.recipe.RecipeHandler.Recipe; +import mekanism.common.recipe.machines.ChemicalInfuserRecipe; +import mekanism.common.recipe.machines.CombinerRecipe; +import mekanism.common.recipe.machines.CrusherRecipe; +import mekanism.common.recipe.machines.CrystallizerRecipe; +import mekanism.common.recipe.machines.DissolutionRecipe; +import mekanism.common.recipe.machines.EnrichmentRecipe; +import mekanism.common.recipe.machines.InjectionRecipe; +import mekanism.common.recipe.machines.MetallurgicInfuserRecipe; +import mekanism.common.recipe.machines.OxidationRecipe; +import mekanism.common.recipe.machines.PressurizedRecipe; +import mekanism.common.recipe.machines.PurificationRecipe; +import mekanism.common.recipe.machines.SawmillRecipe; +import mekanism.common.recipe.machines.SeparatorRecipe; +import mekanism.common.recipe.machines.WasherRecipe; +import minetweaker.MineTweakerAPI; +import minetweaker.MineTweakerImplementationAPI; +import minetweaker.api.player.IPlayer; +import minetweaker.api.server.ICommandFunction; +import modtweaker2.helpers.LogHelper; +import modtweaker2.helpers.StringHelper; + +public class MekanismLogger implements ICommandFunction { + + private static final List validArguments = new LinkedList(); + + static { + validArguments.add("combiner"); + validArguments.add("compressor"); + validArguments.add("crusher"); + validArguments.add("crystallizer"); + validArguments.add("dissolution"); + validArguments.add("enrichment"); + validArguments.add("infuser"); + validArguments.add("injection"); + validArguments.add("oxidizer"); + validArguments.add("purification"); + validArguments.add("reaction"); + validArguments.add("sawmill"); + validArguments.add("separator"); + validArguments.add("washer"); + } + + @SuppressWarnings({ "unchecked" }) + @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("crystallizer")) { + for(CrystallizerRecipe recipe : (Collection)Recipe.CHEMICAL_CRYSTALLIZER.get().values()) { + MineTweakerAPI.logCommand(String.format("mods.mekanism.chemical.Crystallizer.addRecipe(%s, %s);", + LogHelper.getStackDescription(recipe.recipeInput.ingredient), + LogHelper.getStackDescription(recipe.recipeOutput.output))); + } + } + + if(args.isEmpty() || args.contains("dissolution")) { + for(DissolutionRecipe recipe : (Collection)Recipe.CHEMICAL_DISSOLUTION_CHAMBER.get().values()) { + MineTweakerAPI.logCommand(String.format("mods.mekanism.chemical.Dissolution.addRecipe(%s, %s);", + LogHelper.getStackDescription(recipe.recipeInput.ingredient), + LogHelper.getStackDescription(recipe.recipeOutput.output))); + } + } + + if(args.isEmpty() || args.contains("infuser")) { + for(ChemicalInfuserRecipe recipe : (Collection)Recipe.CHEMICAL_INFUSER.get().values()) { + MineTweakerAPI.logCommand(String.format("mods.mekanism.chemical.Infuser.addRecipe(%s, %s, %s);", + LogHelper.getStackDescription(recipe.recipeInput.leftGas), + LogHelper.getStackDescription(recipe.recipeInput.rightGas), + LogHelper.getStackDescription(recipe.recipeOutput.output))); + } + } + + if(args.isEmpty() || args.contains("injection")) { + for(InjectionRecipe recipe : (Collection)Recipe.CHEMICAL_INJECTION_CHAMBER.get().values()) { + MineTweakerAPI.logCommand(String.format("mods.mekanism.chemical.Injection.addRecipe(%s, %s, %s);", + LogHelper.getStackDescription(recipe.recipeInput.itemStack), + LogHelper.getStackDescription(new GasStack(recipe.recipeInput.gasType, 1)), + LogHelper.getStackDescription(recipe.recipeOutput.output))); + } + } + + if(args.isEmpty() || args.contains("oxidizer")) { + for(OxidationRecipe recipe : (Collection)Recipe.CHEMICAL_OXIDIZER.get().values()) { + MineTweakerAPI.logCommand(String.format("mods.mekanism.chemical.Oxidizer.addRecipe(%s, %s);", + LogHelper.getStackDescription(recipe.recipeInput.ingredient), + LogHelper.getStackDescription(recipe.recipeOutput.output))); + } + } + + if(args.isEmpty() || args.contains("washer")) { + for(WasherRecipe recipe : (Collection)Recipe.CHEMICAL_WASHER.get().values()) { + MineTweakerAPI.logCommand(String.format("mods.mekanism.chemical.Washer.addRecipe(%s, %s);", + LogHelper.getStackDescription(recipe.recipeInput.ingredient), + LogHelper.getStackDescription(recipe.recipeOutput.output))); + } + } + + if(args.isEmpty() || args.contains("combiner")) { + for(CombinerRecipe recipe : (Collection)Recipe.COMBINER.get().values()) { + MineTweakerAPI.logCommand(String.format("mods.mekanism.Combiner.addRecipe(%s, %s, %s);", + LogHelper.getStackDescription(recipe.recipeInput.itemStack), + LogHelper.getStackDescription(recipe.recipeOutput.output), + LogHelper.getStackDescription(new GasStack(recipe.recipeInput.gasType, 1)))); + } + } + + if(args.isEmpty() || args.contains("compressor")) { + for(CombinerRecipe recipe : (Collection)Recipe.COMBINER.get().values()) { + MineTweakerAPI.logCommand(String.format("mods.mekanism.Compressor.addRecipe(%s, %s, %s);", + LogHelper.getStackDescription(recipe.recipeInput.itemStack), + LogHelper.getStackDescription(new GasStack(recipe.recipeInput.gasType, 1)), + LogHelper.getStackDescription(recipe.recipeOutput.output))); + } + } + + if(args.isEmpty() || args.contains("crusher")) { + for(CrusherRecipe recipe : (Collection)Recipe.CRUSHER.get().values()) { + MineTweakerAPI.logCommand(String.format("mods.mekanism.Crusher.addRecipe(%s, %s);", + LogHelper.getStackDescription(recipe.recipeInput.ingredient), + LogHelper.getStackDescription(recipe.recipeOutput.output))); + } + } + + if(args.isEmpty() || args.contains("enrichment")) { + for(EnrichmentRecipe recipe : (Collection)Recipe.ENRICHMENT_CHAMBER.get().values()) { + MineTweakerAPI.logCommand(String.format("mods.mekanism.Enrichment.addRecipe(%s, %s);", + LogHelper.getStackDescription(recipe.recipeInput.ingredient), + LogHelper.getStackDescription(recipe.recipeOutput.output))); + } + } + + if(args.isEmpty() || args.contains("infuser")) { + for(MetallurgicInfuserRecipe recipe : (Collection)Recipe.METALLURGIC_INFUSER.get().values()) { + MineTweakerAPI.logCommand(String.format("mods.mekanism.Infuser.addRecipe(\"%s\", %d, %s, %s);", + recipe.getInput().infuse.type.name, + recipe.getInput().infuse.amount, + LogHelper.getStackDescription(recipe.recipeInput.inputStack), + LogHelper.getStackDescription(recipe.recipeOutput.output))); + } + } + + if(args.isEmpty() || args.contains("purification")) { + for(PurificationRecipe recipe : (Collection)Recipe.PURIFICATION_CHAMBER.get().values()) { + MineTweakerAPI.logCommand(String.format("mods.mekanism.Purification.addRecipe(%s, %s, %s);", + LogHelper.getStackDescription(recipe.recipeInput.itemStack), + LogHelper.getStackDescription(new GasStack(recipe.recipeInput.gasType, 1)), + LogHelper.getStackDescription(recipe.recipeOutput.output))); + } + } + + if(args.isEmpty() || args.contains("reaction")) { + for(PressurizedRecipe recipe : (Collection)Recipe.PRESSURIZED_REACTION_CHAMBER.get().values()) { + MineTweakerAPI.logCommand(String.format("mods.mekanism.Reaction.addRecipe(%s, %s, %s, %s, %s, %s, %d);", + LogHelper.getStackDescription(recipe.recipeInput.getSolid()), + LogHelper.getStackDescription(recipe.recipeInput.getFluid()), + LogHelper.getStackDescription(recipe.recipeInput.getGas()), + LogHelper.getStackDescription(recipe.recipeOutput.getItemOutput()), + LogHelper.getStackDescription(recipe.recipeOutput.getGasOutput()), + recipe.extraEnergy, + recipe.ticks)); + } + } + + if(args.isEmpty() || args.contains("sawmill")) { + for(SawmillRecipe recipe : (Collection)Recipe.PRECISION_SAWMILL.get().values()) { + if(recipe.getOutput().hasSecondary()) { + MineTweakerAPI.logCommand(String.format("mods.mekanism.Sawmill.addRecipe(%s, %s, %s, %s);", + LogHelper.getStackDescription(recipe.recipeInput.ingredient), + LogHelper.getStackDescription(recipe.recipeOutput.primaryOutput), + LogHelper.getStackDescription(recipe.recipeOutput.secondaryOutput), + recipe.recipeOutput.secondaryChance)); + } else { + MineTweakerAPI.logCommand(String.format("mods.mekanism.Sawmill.addRecipe(%s, %s);", + LogHelper.getStackDescription(recipe.recipeInput.ingredient), + LogHelper.getStackDescription(recipe.recipeOutput.primaryOutput))); + } + } + } + + if(args.isEmpty() || args.contains("separator")) { + for(SeparatorRecipe recipe : (Collection)Recipe.ELECTROLYTIC_SEPARATOR.get().values()) { + MineTweakerAPI.logCommand(String.format("mods.mekanism.Separator.addRecipe(%s, %s, %s, %s);", + LogHelper.getStackDescription(recipe.recipeInput.ingredient), + recipe.energyUsage, + LogHelper.getStackDescription(recipe.recipeOutput.leftGas), + LogHelper.getStackDescription(recipe.recipeOutput.rightGas))); + } + } + + 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/mekanism/gas/IGasStack.java b/src/main/java/modtweaker2/mods/mekanism/gas/IGasStack.java index 6865a3e..9e37d33 100644 --- a/src/main/java/modtweaker2/mods/mekanism/gas/IGasStack.java +++ b/src/main/java/modtweaker2/mods/mekanism/gas/IGasStack.java @@ -1,5 +1,6 @@ package modtweaker2.mods.mekanism.gas; +import minetweaker.api.item.IIngredient; import stanhebben.zenscript.annotations.OperatorType; import stanhebben.zenscript.annotations.ZenClass; import stanhebben.zenscript.annotations.ZenGetter; @@ -7,7 +8,7 @@ import stanhebben.zenscript.annotations.ZenMethod; import stanhebben.zenscript.annotations.ZenOperator; @ZenClass("modtweaker.gas.IGasStack") -public interface IGasStack { +public interface IGasStack extends IIngredient { @ZenGetter("definition") public IGasDefinition getDefinition(); diff --git a/src/main/java/modtweaker2/mods/mekanism/gas/MCGasStack.java b/src/main/java/modtweaker2/mods/mekanism/gas/MCGasStack.java index c9d2c6e..20f43dd 100644 --- a/src/main/java/modtweaker2/mods/mekanism/gas/MCGasStack.java +++ b/src/main/java/modtweaker2/mods/mekanism/gas/MCGasStack.java @@ -1,6 +1,14 @@ package modtweaker2.mods.mekanism.gas; +import java.util.List; + import mekanism.api.gas.GasStack; +import minetweaker.api.item.IIngredient; +import minetweaker.api.item.IItemCondition; +import minetweaker.api.item.IItemStack; +import minetweaker.api.item.IItemTransformer; +import minetweaker.api.liquid.ILiquidStack; +import minetweaker.api.player.IPlayer; public class MCGasStack implements IGasStack { @@ -40,4 +48,88 @@ public class MCGasStack implements IGasStack { public Object getInternal() { return stack; } + + @Override + public String toString() { + return ""; + } + + @Override + public IIngredient amount(int amount) { + return withAmount(amount); + } + + @Override + public IItemStack applyTransform(IItemStack arg0, IPlayer arg1) { + // TODO Auto-generated method stub + return null; + } + + @Override + public boolean contains(IIngredient arg0) { + // TODO Auto-generated method stub + return false; + } + + @Override + public List getItems() { + // TODO Auto-generated method stub + return null; + } + + @Override + public List getLiquids() { + // TODO Auto-generated method stub + return null; + } + + @Override + public String getMark() { + // TODO Auto-generated method stub + return null; + } + + @Override + public boolean hasTransformers() { + // TODO Auto-generated method stub + return false; + } + + @Override + public IIngredient marked(String arg0) { + // TODO Auto-generated method stub + return null; + } + + @Override + public boolean matches(IItemStack arg0) { + // TODO Auto-generated method stub + return false; + } + + @Override + public boolean matches(ILiquidStack arg0) { + // TODO Auto-generated method stub + return false; + } + + @Override + public IIngredient only(IItemCondition arg0) { + // TODO Auto-generated method stub + return null; + } + + @Override + public IIngredient or(IIngredient arg0) { + // TODO Auto-generated method stub + return null; + } + + @Override + public IIngredient transform(IItemTransformer arg0) { + // TODO Auto-generated method stub + return null; + } + + } \ No newline at end of file diff --git a/src/main/java/modtweaker2/mods/mekanism/handlers/ChemicalCrystallizer.java b/src/main/java/modtweaker2/mods/mekanism/handlers/ChemicalCrystallizer.java index edf0f0e..4579638 100644 --- a/src/main/java/modtweaker2/mods/mekanism/handlers/ChemicalCrystallizer.java +++ b/src/main/java/modtweaker2/mods/mekanism/handlers/ChemicalCrystallizer.java @@ -2,33 +2,79 @@ package modtweaker2.mods.mekanism.handlers; import static modtweaker2.helpers.InputHelper.toStack; import static modtweaker2.mods.mekanism.MekanismHelper.toGas; + +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; + import mekanism.common.recipe.RecipeHandler.Recipe; +import mekanism.common.recipe.inputs.GasInput; +import mekanism.common.recipe.inputs.MachineInput; import mekanism.common.recipe.machines.CrystallizerRecipe; +import mekanism.common.recipe.machines.MachineRecipe; import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; -import modtweaker2.mods.mekanism.Mekanism; +import minetweaker.api.item.IngredientAny; +import modtweaker2.helpers.InputHelper; +import modtweaker2.helpers.LogHelper; +import modtweaker2.helpers.StackHelper; import modtweaker2.mods.mekanism.gas.IGasStack; +import modtweaker2.mods.mekanism.gas.MCGasStack; import modtweaker2.mods.mekanism.util.AddMekanismRecipe; import modtweaker2.mods.mekanism.util.RemoveMekanismRecipe; +import stanhebben.zenscript.annotations.Optional; import stanhebben.zenscript.annotations.ZenClass; import stanhebben.zenscript.annotations.ZenMethod; @ZenClass("mods.mekanism.chemical.Crystallizer") public class ChemicalCrystallizer { + + public static final String name = "Mekanism Crusher"; + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + @SuppressWarnings("unchecked") @ZenMethod - public static void addRecipe(IGasStack input, IItemStack output) { - if (Mekanism.v7) - MineTweakerAPI.apply(new AddMekanismRecipe("CHEMICAL_CRYSTALLIZER", Recipe.CHEMICAL_CRYSTALLIZER.get(), toGas(input), toStack(output))); - else - { - CrystallizerRecipe recipe = new CrystallizerRecipe(toGas(input), toStack(output)); - MineTweakerAPI.apply(new AddMekanismRecipe("CHEMICAL_CRYSTALLIZER", Recipe.CHEMICAL_CRYSTALLIZER.get(), recipe.getInput(), recipe)); + public static void addRecipe(IGasStack gasInput, IItemStack itemOutput) { + if(gasInput == null || itemOutput == null) { + LogHelper.logError(String.format("Required parameters missing for %s Recipe.", name)); + return; } + + CrystallizerRecipe recipe = new CrystallizerRecipe(toGas(gasInput), toStack(itemOutput)); + + MineTweakerAPI.apply(new AddMekanismRecipe(name, Recipe.CHEMICAL_CRYSTALLIZER.get(), recipe)); } + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + @SuppressWarnings({ "unchecked", "rawtypes" }) @ZenMethod - public static void removeRecipe(IItemStack output) { - if (!Mekanism.v7) throw new UnsupportedOperationException("Function not added to v8 compatibility yet"); - MineTweakerAPI.apply(new RemoveMekanismRecipe("CHEMICAL_CRYSTALLIZER", Recipe.CHEMICAL_CRYSTALLIZER.get(), toStack(output))); + public static void removeRecipe(IIngredient itemOutput, @Optional IIngredient gasInput) { + if(itemOutput == null) { + LogHelper.logError(String.format("Required parameters missing for %s Recipe.", name)); + return; + } + + if(gasInput == null) gasInput = IngredientAny.INSTANCE; + + Map recipes = new HashMap(); + + for(Entry entry : ((Map)Recipe.CHEMICAL_CRYSTALLIZER.get()).entrySet()) { + IGasStack inputGas = new MCGasStack(entry.getKey().ingredient); + IItemStack outputItem = InputHelper.toIItemStack(entry.getValue().recipeOutput.output); + + if(!StackHelper.matches(itemOutput, outputItem)) continue; + if(!StackHelper.matches(gasInput, inputGas)) continue; + + recipes.put(entry.getKey(), entry.getValue()); + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new RemoveMekanismRecipe(name, Recipe.CHEMICAL_CRYSTALLIZER.get(), recipes)); + } else { + LogHelper.logWarning(String.format("No %s recipe found for %s and %s. Command ignored!", name, gasInput.toString(), itemOutput.toString())); + } } } diff --git a/src/main/java/modtweaker2/mods/mekanism/handlers/ChemicalDissolution.java b/src/main/java/modtweaker2/mods/mekanism/handlers/ChemicalDissolution.java index 82aaace..fb7cae8 100644 --- a/src/main/java/modtweaker2/mods/mekanism/handlers/ChemicalDissolution.java +++ b/src/main/java/modtweaker2/mods/mekanism/handlers/ChemicalDissolution.java @@ -1,90 +1,80 @@ package modtweaker2.mods.mekanism.handlers; import static modtweaker2.helpers.InputHelper.toStack; -import static modtweaker2.helpers.StackHelper.areEqual; import static modtweaker2.mods.mekanism.MekanismHelper.toGas; -import java.util.Iterator; -import java.util.List; +import java.util.HashMap; import java.util.Map; +import java.util.Map.Entry; -import mekanism.api.gas.GasStack; import mekanism.common.recipe.RecipeHandler.Recipe; +import mekanism.common.recipe.inputs.ItemStackInput; +import mekanism.common.recipe.inputs.MachineInput; import mekanism.common.recipe.machines.DissolutionRecipe; +import mekanism.common.recipe.machines.MachineRecipe; import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; -import minetweaker.api.oredict.IOreDictEntry; +import minetweaker.api.item.IngredientAny; import modtweaker2.helpers.InputHelper; -import modtweaker2.mods.mekanism.Mekanism; +import modtweaker2.helpers.LogHelper; +import modtweaker2.helpers.StackHelper; import modtweaker2.mods.mekanism.gas.IGasStack; +import modtweaker2.mods.mekanism.gas.MCGasStack; import modtweaker2.mods.mekanism.util.AddMekanismRecipe; import modtweaker2.mods.mekanism.util.RemoveMekanismRecipe; -import net.minecraft.item.ItemStack; -import net.minecraftforge.oredict.OreDictionary; +import stanhebben.zenscript.annotations.Optional; import stanhebben.zenscript.annotations.ZenClass; import stanhebben.zenscript.annotations.ZenMethod; @ZenClass("mods.mekanism.chemical.Dissolution") public class ChemicalDissolution { + + public static final String name = "Mekanism Dissolution"; + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + @SuppressWarnings("unchecked") @ZenMethod - public static void addRecipe(IItemStack input, IGasStack output) { - if (Mekanism.v7) - { - MineTweakerAPI.apply(new AddMekanismRecipe("CHEMICAL_DISSOLUTION_CHAMBER", Recipe.CHEMICAL_DISSOLUTION_CHAMBER.get(), toStack(input), toGas(output))); - } - else { - DissolutionRecipe recipe = new DissolutionRecipe(toStack(input), toGas(output)); - MineTweakerAPI.apply(new AddMekanismRecipe("CHEMICAL_DISSOLUTION_CHAMBER", Recipe.CHEMICAL_DISSOLUTION_CHAMBER.get(), recipe.getInput(), recipe)); + public static void addRecipe(IItemStack itemInput, IGasStack gasOutput) { + if(itemInput == null || gasOutput == null) { + LogHelper.logError(String.format("Required parameters missing for %s Recipe.", name)); + return; } + + DissolutionRecipe recipe = new DissolutionRecipe(toStack(itemInput), toGas(gasOutput)); + + MineTweakerAPI.apply(new AddMekanismRecipe(name, Recipe.CHEMICAL_DISSOLUTION_CHAMBER.get(), recipe)); } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + @SuppressWarnings({ "unchecked", "rawtypes" }) @ZenMethod - public static void removeRecipe(IItemStack input) { - if (!Mekanism.v7) throw new UnsupportedOperationException("Function not added to v8 compatibility yet"); - MineTweakerAPI.apply(new Remove("CHEMICAL_DISSOLUTION_CHAMBER", Recipe.CHEMICAL_DISSOLUTION_CHAMBER.get(), toStack(input))); - } - - @ZenMethod - public static void removeRecipe(IOreDictEntry input) { - if (!Mekanism.v7) throw new UnsupportedOperationException("Function not added to v8 compatibility yet"); - List stacks = OreDictionary.getOres(InputHelper.toString(input)); - for (ItemStack stack : stacks) { - MineTweakerAPI.apply(new Remove("CHEMICAL_DISSOLUTION_CHAMBER", Recipe.CHEMICAL_DISSOLUTION_CHAMBER.get(), stack)); + public static void removeRecipe(IIngredient gasOutput, @Optional IIngredient itemInput) { + if(gasOutput == null) { + LogHelper.logError(String.format("Required parameters missing for %s Recipe.", name)); + return; } - } + + if(itemInput == null) itemInput = IngredientAny.INSTANCE; + + Map recipes = new HashMap(); + + for(Entry entry : ((Map)Recipe.CHEMICAL_DISSOLUTION_CHAMBER.get()).entrySet()) { + IItemStack inputItem = InputHelper.toIItemStack(entry.getKey().ingredient); + IGasStack outputGas = new MCGasStack(entry.getValue().recipeOutput.output); + + if(!StackHelper.matches(gasOutput, outputGas)) continue; + if(!StackHelper.matches(itemInput, inputItem)) continue; - @ZenMethod - public static void removeRecipe(IGasStack input) { - if (!Mekanism.v7) throw new UnsupportedOperationException("Function not added to v8 compatibility yet"); - MineTweakerAPI.apply(new Remove("CHEMICAL_DISSOLUTION_CHAMBER", Recipe.CHEMICAL_DISSOLUTION_CHAMBER.get(), toGas(input))); - } - - private static class Remove extends RemoveMekanismRecipe { - public Remove(String string, Map map, Object key) { - super(string, map, key); + recipes.put(entry.getKey(), entry.getValue()); } - - @Override - public void apply() { - Iterator it = map.entrySet().iterator(); - while (it.hasNext()) { - Map.Entry pairs = (Map.Entry) it.next(); - ItemStack key = (ItemStack) pairs.getKey(); - GasStack value = (GasStack) pairs.getValue(); - if (key != null) { - if (this.key instanceof ItemStack && areEqual(key, (ItemStack) this.key)) { - this.key = key; - break; - } else if (this.key instanceof GasStack && value.isGasEqual((GasStack) this.key)) { - this.key = key; - break; - } - } - - } - - recipe = map.get(key); - map.remove(key); + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new RemoveMekanismRecipe(name, Recipe.CHEMICAL_DISSOLUTION_CHAMBER.get(), recipes)); + } else { + LogHelper.logWarning(String.format("No %s recipe found for %s and %s. Command ignored!", name, gasOutput.toString(), itemInput.toString())); } } } diff --git a/src/main/java/modtweaker2/mods/mekanism/handlers/ChemicalInfuser.java b/src/main/java/modtweaker2/mods/mekanism/handlers/ChemicalInfuser.java index 53277ae..1d2f6fb 100644 --- a/src/main/java/modtweaker2/mods/mekanism/handlers/ChemicalInfuser.java +++ b/src/main/java/modtweaker2/mods/mekanism/handlers/ChemicalInfuser.java @@ -1,36 +1,80 @@ package modtweaker2.mods.mekanism.handlers; import static modtweaker2.mods.mekanism.MekanismHelper.toGas; + +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; + import mekanism.common.recipe.RecipeHandler.Recipe; import mekanism.common.recipe.inputs.ChemicalPairInput; +import mekanism.common.recipe.inputs.MachineInput; import mekanism.common.recipe.machines.ChemicalInfuserRecipe; +import mekanism.common.recipe.machines.MachineRecipe; import minetweaker.MineTweakerAPI; -import modtweaker2.mods.mekanism.Mekanism; +import minetweaker.api.item.IIngredient; +import minetweaker.api.item.IngredientAny; +import modtweaker2.helpers.LogHelper; +import modtweaker2.helpers.StackHelper; import modtweaker2.mods.mekanism.gas.IGasStack; +import modtweaker2.mods.mekanism.gas.MCGasStack; import modtweaker2.mods.mekanism.util.AddMekanismRecipe; import modtweaker2.mods.mekanism.util.RemoveMekanismRecipe; +import stanhebben.zenscript.annotations.Optional; import stanhebben.zenscript.annotations.ZenClass; import stanhebben.zenscript.annotations.ZenMethod; @ZenClass("mods.mekanism.chemical.Infuser") public class ChemicalInfuser { - @ZenMethod - public static void addRecipe(IGasStack left, IGasStack right, IGasStack out) { - if (Mekanism.v7) { - ChemicalPairInput pair = new ChemicalPairInput(toGas(left), toGas(right)); - MineTweakerAPI.apply(new AddMekanismRecipe("CHEMICAL_INFUSER", Recipe.CHEMICAL_INFUSER.get(), pair, toGas(out))); - } else { - ChemicalInfuserRecipe recipe = new ChemicalInfuserRecipe(toGas(left), toGas(right), toGas(out)); - MineTweakerAPI.apply(new AddMekanismRecipe("CHEMICAL_INFUSER", Recipe.CHEMICAL_INFUSER.get(), recipe.getInput(), recipe)); - } - } - @ZenMethod - public static void removeRecipe(IGasStack output) { - if (!Mekanism.v7) - throw new UnsupportedOperationException("Function not added to v8 compatibility yet"); - MineTweakerAPI.apply(new RemoveMekanismRecipe("CHEMICAL_INFUSER", Recipe.CHEMICAL_INFUSER.get(), toGas(output))); - - - } + public static final String name = "Mekanism Infuser"; + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + @SuppressWarnings("unchecked") + @ZenMethod + public static void addRecipe(IGasStack leftGasInput, IGasStack rightGasInput, IGasStack gasOutput) { + if(leftGasInput == null || rightGasInput == null || gasOutput == null) { + LogHelper.logError(String.format("Required parameters missing for %s Recipe.", name)); + return; + } + + ChemicalInfuserRecipe recipe = new ChemicalInfuserRecipe(toGas(leftGasInput), toGas(rightGasInput), toGas(gasOutput)); + + MineTweakerAPI.apply(new AddMekanismRecipe(name, Recipe.CHEMICAL_INFUSER.get(), recipe)); + } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + @SuppressWarnings({ "unchecked", "rawtypes" }) + @ZenMethod + public static void removeRecipe(IIngredient gasOutput, @Optional IIngredient leftGasInput, @Optional IIngredient rightGasInput) { + if(gasOutput == null) { + LogHelper.logError(String.format("Required parameters missing for %s Recipe.", name)); + return; + } + + if(leftGasInput == null) leftGasInput = IngredientAny.INSTANCE; + if(rightGasInput == null) rightGasInput = IngredientAny.INSTANCE; + + Map recipes = new HashMap(); + + for(Entry entry : ((Map)Recipe.CHEMICAL_DISSOLUTION_CHAMBER.get()).entrySet()) { + IGasStack inputGasLeft = new MCGasStack(entry.getKey().leftGas); + IGasStack inputGasRight = new MCGasStack(entry.getKey().rightGas); + IGasStack outputGas = new MCGasStack(entry.getValue().recipeOutput.output); + + if(!StackHelper.matches(gasOutput, outputGas)) continue; + if(!StackHelper.matches(leftGasInput, inputGasLeft)) continue; + if(!StackHelper.matches(rightGasInput, inputGasRight)) continue; + + recipes.put(entry.getKey(), entry.getValue()); + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new RemoveMekanismRecipe(name, Recipe.CHEMICAL_DISSOLUTION_CHAMBER.get(), recipes)); + } else { + LogHelper.logWarning(String.format("No %s recipe found for %s, %s and %s. Command ignored!", name, gasOutput.toString(), leftGasInput.toString(), rightGasInput.toString())); + } + } } diff --git a/src/main/java/modtweaker2/mods/mekanism/handlers/ChemicalInjection.java b/src/main/java/modtweaker2/mods/mekanism/handlers/ChemicalInjection.java index a21395f..844b2a3 100644 --- a/src/main/java/modtweaker2/mods/mekanism/handlers/ChemicalInjection.java +++ b/src/main/java/modtweaker2/mods/mekanism/handlers/ChemicalInjection.java @@ -2,37 +2,87 @@ package modtweaker2.mods.mekanism.handlers; import static modtweaker2.helpers.InputHelper.toStack; import static modtweaker2.mods.mekanism.MekanismHelper.toGas; + +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; + +import mekanism.api.gas.GasStack; import mekanism.common.recipe.RecipeHandler.Recipe; import mekanism.common.recipe.inputs.AdvancedMachineInput; +import mekanism.common.recipe.inputs.MachineInput; import mekanism.common.recipe.machines.InjectionRecipe; +import mekanism.common.recipe.machines.MachineRecipe; +import mekanism.common.recipe.outputs.ItemStackOutput; import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; -import modtweaker2.mods.mekanism.Mekanism; +import minetweaker.api.item.IngredientAny; +import modtweaker2.helpers.InputHelper; +import modtweaker2.helpers.LogHelper; +import modtweaker2.helpers.StackHelper; import modtweaker2.mods.mekanism.gas.IGasStack; +import modtweaker2.mods.mekanism.gas.MCGasStack; import modtweaker2.mods.mekanism.util.AddMekanismRecipe; import modtweaker2.mods.mekanism.util.RemoveMekanismRecipe; +import stanhebben.zenscript.annotations.Optional; import stanhebben.zenscript.annotations.ZenClass; import stanhebben.zenscript.annotations.ZenMethod; @ZenClass("mods.mekanism.chemical.Injection") public class ChemicalInjection { + + public static final String name = "Mekanism Injection"; + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + @SuppressWarnings("unchecked") @ZenMethod - public static void addRecipe(IItemStack input, IGasStack gas, IItemStack output) { - if (Mekanism.v7) - { - AdvancedMachineInput aInput = new AdvancedMachineInput(toStack(input), toGas(gas).getGas()); - MineTweakerAPI.apply(new AddMekanismRecipe("CHEMICAL_INJECTION_CHAMBER", Recipe.CHEMICAL_INJECTION_CHAMBER.get(), aInput, toStack(output))); - } - else - { - InjectionRecipe recipe = new InjectionRecipe(toStack(input), gas.getName(), toStack(output)); - MineTweakerAPI.apply(new AddMekanismRecipe("CHEMICAL_INJECTION_CHAMBER", Recipe.CHEMICAL_INJECTION_CHAMBER.get(), recipe.getInput(), recipe)); + public static void addRecipe(IItemStack itemInput, IGasStack gasInput, IItemStack itemOutput) { + if(itemInput == null || gasInput == null || itemOutput == null) { + LogHelper.logError(String.format("Required parameters missing for %s Recipe.", name)); + return; } + + AdvancedMachineInput input = new AdvancedMachineInput(toStack(itemInput), toGas(gasInput).getGas()); + ItemStackOutput output = new ItemStackOutput(toStack(itemOutput)); + + InjectionRecipe recipe = new InjectionRecipe(input, output); + + MineTweakerAPI.apply(new AddMekanismRecipe(name, Recipe.CHEMICAL_INJECTION_CHAMBER.get(), recipe)); } - + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + @SuppressWarnings({ "rawtypes", "unchecked" }) @ZenMethod - public static void removeRecipe(IItemStack output) { - if (!Mekanism.v7) throw new UnsupportedOperationException("Function not added to v8 compatibility yet"); - MineTweakerAPI.apply(new RemoveMekanismRecipe("CHEMICAL_INJECTION_CHAMBER", Recipe.CHEMICAL_INJECTION_CHAMBER.get(), toStack(output))); + public static void removeRecipe(IIngredient itemOutput, @Optional IIngredient itemInput, @Optional IIngredient gasInput) { + if(itemOutput == null) { + LogHelper.logError(String.format("Required parameters missing for %s Recipe.", name)); + return; + } + + if(itemInput == null) itemInput = IngredientAny.INSTANCE; + if(gasInput == null) gasInput = IngredientAny.INSTANCE; + + Map recipes = new HashMap(); + + for(Entry entry : ((Map)Recipe.CHEMICAL_INJECTION_CHAMBER.get()).entrySet()) { + IItemStack inputItem = InputHelper.toIItemStack(entry.getKey().itemStack); + IGasStack inputGas = new MCGasStack(new GasStack(entry.getKey().gasType, 1)); + IItemStack outputItem = InputHelper.toIItemStack(entry.getValue().recipeOutput.output); + + if(!StackHelper.matches(itemOutput, outputItem)) continue; + if(!StackHelper.matches(itemInput, inputItem)) continue; + if(!StackHelper.matches(gasInput, inputGas)) continue; + + recipes.put(entry.getKey(), entry.getValue()); + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new RemoveMekanismRecipe(name, Recipe.CHEMICAL_INJECTION_CHAMBER.get(), recipes)); + } else { + LogHelper.logWarning(String.format("No %s recipe found for %s, %s and %s. Command ignored!", name, itemOutput.toString(), itemInput.toString(), gasInput.toString())); + } } } diff --git a/src/main/java/modtweaker2/mods/mekanism/handlers/ChemicalOxidizer.java b/src/main/java/modtweaker2/mods/mekanism/handlers/ChemicalOxidizer.java index 5db57c6..a9c3d01 100644 --- a/src/main/java/modtweaker2/mods/mekanism/handlers/ChemicalOxidizer.java +++ b/src/main/java/modtweaker2/mods/mekanism/handlers/ChemicalOxidizer.java @@ -2,35 +2,79 @@ package modtweaker2.mods.mekanism.handlers; import static modtweaker2.helpers.InputHelper.toStack; import static modtweaker2.mods.mekanism.MekanismHelper.toGas; + +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; + import mekanism.common.recipe.RecipeHandler.Recipe; +import mekanism.common.recipe.inputs.ItemStackInput; +import mekanism.common.recipe.inputs.MachineInput; +import mekanism.common.recipe.machines.MachineRecipe; import mekanism.common.recipe.machines.OxidationRecipe; import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; -import modtweaker2.mods.mekanism.Mekanism; +import minetweaker.api.item.IngredientAny; +import modtweaker2.helpers.InputHelper; +import modtweaker2.helpers.LogHelper; +import modtweaker2.helpers.StackHelper; import modtweaker2.mods.mekanism.gas.IGasStack; +import modtweaker2.mods.mekanism.gas.MCGasStack; import modtweaker2.mods.mekanism.util.AddMekanismRecipe; import modtweaker2.mods.mekanism.util.RemoveMekanismRecipe; +import stanhebben.zenscript.annotations.Optional; import stanhebben.zenscript.annotations.ZenClass; import stanhebben.zenscript.annotations.ZenMethod; @ZenClass("mods.mekanism.chemical.Oxidizer") public class ChemicalOxidizer { + + public static final String name = "Mekanism Oxidizer"; + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + @SuppressWarnings("unchecked") @ZenMethod - public static void addRecipe(IItemStack input, IGasStack output) { - if (Mekanism.v7) - { - MineTweakerAPI.apply(new AddMekanismRecipe("CHEMICAL_OXIDIZER", Recipe.CHEMICAL_OXIDIZER.get(), toStack(input), toGas(output))); - } - else - { - OxidationRecipe recipe = new OxidationRecipe(toStack(input), toGas(output)); - MineTweakerAPI.apply(new AddMekanismRecipe("CHEMICAL_OXIDIZER", Recipe.CHEMICAL_OXIDIZER.get(), recipe.getInput(), recipe)); + public static void addRecipe(IItemStack itemInput, IGasStack gasOutput) { + if(itemInput == null || gasOutput == null) { + LogHelper.logError(String.format("Required parameters missing for %s Recipe.", name)); + return; } + + OxidationRecipe recipe = new OxidationRecipe(toStack(itemInput), toGas(gasOutput)); + + MineTweakerAPI.apply(new AddMekanismRecipe(name, Recipe.CHEMICAL_OXIDIZER.get(), recipe)); } - + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + @SuppressWarnings({ "unchecked", "rawtypes" }) @ZenMethod - public static void removeRecipe(IGasStack output) { - if (!Mekanism.v7) throw new UnsupportedOperationException("Function not added to v8 compatibility yet"); - MineTweakerAPI.apply(new RemoveMekanismRecipe("CHEMICAL_OXIDIZER", Recipe.CHEMICAL_OXIDIZER.get(), toGas(output))); + public static void removeRecipe(IIngredient gasOutput, @Optional IIngredient itemInput) { + if(gasOutput == null) { + LogHelper.logError(String.format("Required parameters missing for %s Recipe.", name)); + return; + } + + if(itemInput == null) itemInput = IngredientAny.INSTANCE; + + Map recipes = new HashMap(); + + for(Entry entry : ((Map)Recipe.CHEMICAL_OXIDIZER.get()).entrySet()) { + IItemStack inputItem = InputHelper.toIItemStack(entry.getKey().ingredient); + IGasStack outputGas = new MCGasStack(entry.getValue().recipeOutput.output); + + if(!StackHelper.matches(itemInput, inputItem)) continue; + if(!StackHelper.matches(gasOutput, outputGas)) continue; + + recipes.put(entry.getKey(), entry.getValue()); + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new RemoveMekanismRecipe(name, Recipe.CHEMICAL_OXIDIZER.get(), recipes)); + } else { + LogHelper.logWarning(String.format("No %s recipe found for %s and %s. Command ignored!", name, gasOutput.toString(), itemInput.toString())); + } } } diff --git a/src/main/java/modtweaker2/mods/mekanism/handlers/ChemicalWasher.java b/src/main/java/modtweaker2/mods/mekanism/handlers/ChemicalWasher.java index 523fb1f..2abaf57 100644 --- a/src/main/java/modtweaker2/mods/mekanism/handlers/ChemicalWasher.java +++ b/src/main/java/modtweaker2/mods/mekanism/handlers/ChemicalWasher.java @@ -2,64 +2,76 @@ package modtweaker2.mods.mekanism.handlers; import static modtweaker2.mods.mekanism.MekanismHelper.toGas; -import java.util.Iterator; +import java.util.HashMap; import java.util.Map; +import java.util.Map.Entry; -import mekanism.api.gas.GasStack; import mekanism.common.recipe.RecipeHandler.Recipe; +import mekanism.common.recipe.inputs.GasInput; +import mekanism.common.recipe.inputs.MachineInput; +import mekanism.common.recipe.machines.MachineRecipe; import mekanism.common.recipe.machines.WasherRecipe; import minetweaker.MineTweakerAPI; -import modtweaker2.mods.mekanism.Mekanism; +import minetweaker.api.item.IIngredient; +import minetweaker.api.item.IngredientAny; +import modtweaker2.helpers.LogHelper; +import modtweaker2.helpers.StackHelper; import modtweaker2.mods.mekanism.gas.IGasStack; +import modtweaker2.mods.mekanism.gas.MCGasStack; import modtweaker2.mods.mekanism.util.AddMekanismRecipe; import modtweaker2.mods.mekanism.util.RemoveMekanismRecipe; +import stanhebben.zenscript.annotations.Optional; import stanhebben.zenscript.annotations.ZenClass; import stanhebben.zenscript.annotations.ZenMethod; @ZenClass("mods.mekanism.chemical.Washer") public class ChemicalWasher { + + public static final String name = "Mekanism Washer"; + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + @SuppressWarnings("unchecked") @ZenMethod - public static void addRecipe(IGasStack input, IGasStack output) { - if (Mekanism.v7) - { - MineTweakerAPI.apply(new AddMekanismRecipe("CHEMICAL_WASHER", Recipe.CHEMICAL_WASHER.get(), toGas(input), toGas(output))); - } - else - { - WasherRecipe recipe = new WasherRecipe(toGas(input), toGas(output)); - MineTweakerAPI.apply(new AddMekanismRecipe("CHEMICAL_WASHER", Recipe.CHEMICAL_WASHER.get(), recipe.getInput(), recipe)); + public static void addRecipe(IGasStack gasInput, IGasStack gasOutput) { + if(gasInput == null || gasOutput == null) { + LogHelper.logError(String.format("Required parameters missing for %s Recipe.", name)); + return; } + + WasherRecipe recipe = new WasherRecipe(toGas(gasInput), toGas(gasOutput)); + + MineTweakerAPI.apply(new AddMekanismRecipe(name, Recipe.CHEMICAL_WASHER.get(), recipe)); } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + @SuppressWarnings({ "unchecked", "rawtypes" }) @ZenMethod - public static void removeRecipe(IGasStack output) { - if (!Mekanism.v7) throw new UnsupportedOperationException("Function not added to v8 compatibility yet"); - MineTweakerAPI.apply(new Remove("CHEMICAL_WASHER", Recipe.CHEMICAL_WASHER.get(), toGas(output))); - } - - private static class Remove extends RemoveMekanismRecipe { - public Remove(String string, Map map, Object key) { - super(string, map, key); + public static void removeRecipe(IIngredient gasOutput, @Optional IIngredient gasInput) { + if(gasOutput == null) { + LogHelper.logError(String.format("Required parameters missing for %s Recipe.", name)); + return; } - - @Override - public void apply() { - Iterator it = map.entrySet().iterator(); - while (it.hasNext()) { - Map.Entry pairs = (Map.Entry) it.next(); - GasStack key = (GasStack) pairs.getKey(); - GasStack value = (GasStack) pairs.getValue(); - if (key != null) { - if (this.key instanceof GasStack && value.isGasEqual((GasStack) this.key)) { - this.key = key; - break; - } - } - - } - - recipe = map.get(key); - map.remove(key); + + if(gasInput == null) gasInput = IngredientAny.INSTANCE; + + Map recipes = new HashMap(); + + for(Entry entry : ((Map)Recipe.CHEMICAL_WASHER.get()).entrySet()) { + IGasStack inputGas = new MCGasStack(entry.getKey().ingredient); + IGasStack outputGas = new MCGasStack(entry.getValue().recipeOutput.output); + + if(!StackHelper.matches(gasInput, inputGas)) continue; + if(!StackHelper.matches(gasOutput, outputGas)) continue; + + recipes.put(entry.getKey(), entry.getValue()); + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new RemoveMekanismRecipe(name, Recipe.CHEMICAL_WASHER.get(), recipes)); + } else { + LogHelper.logWarning(String.format("No %s recipe found for %s and %s. Command ignored!", name, gasOutput.toString(), gasInput.toString())); } } } diff --git a/src/main/java/modtweaker2/mods/mekanism/handlers/Combiner.java b/src/main/java/modtweaker2/mods/mekanism/handlers/Combiner.java index 983d79f..04dd7be 100644 --- a/src/main/java/modtweaker2/mods/mekanism/handlers/Combiner.java +++ b/src/main/java/modtweaker2/mods/mekanism/handlers/Combiner.java @@ -1,35 +1,100 @@ package modtweaker2.mods.mekanism.handlers; import static modtweaker2.helpers.InputHelper.toStack; -import mekanism.api.gas.GasRegistry; +import static modtweaker2.mods.mekanism.MekanismHelper.toGas; + +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; + +import mekanism.api.gas.GasStack; import mekanism.common.recipe.RecipeHandler.Recipe; import mekanism.common.recipe.inputs.AdvancedMachineInput; +import mekanism.common.recipe.inputs.MachineInput; import mekanism.common.recipe.machines.CombinerRecipe; +import mekanism.common.recipe.machines.MachineRecipe; +import mekanism.common.recipe.outputs.ItemStackOutput; import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; -import modtweaker2.mods.mekanism.Mekanism; +import minetweaker.api.item.IngredientAny; +import modtweaker2.helpers.InputHelper; +import modtweaker2.helpers.LogHelper; +import modtweaker2.helpers.StackHelper; +import modtweaker2.mods.mekanism.gas.IGasStack; +import modtweaker2.mods.mekanism.gas.MCGasStack; import modtweaker2.mods.mekanism.util.AddMekanismRecipe; import modtweaker2.mods.mekanism.util.RemoveMekanismRecipe; +import stanhebben.zenscript.annotations.Optional; import stanhebben.zenscript.annotations.ZenClass; import stanhebben.zenscript.annotations.ZenMethod; @ZenClass("mods.mekanism.Combiner") public class Combiner { + + public static final String name = "Mekanism Combiner"; + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + @SuppressWarnings({ "unchecked" }) @ZenMethod - public static void addRecipe(IItemStack input, IItemStack output) { - AdvancedMachineInput aInput = new AdvancedMachineInput(toStack(input), GasRegistry.getGas("liquidStone")); - if (Mekanism.v7) - MineTweakerAPI.apply(new AddMekanismRecipe("COMBINER", Recipe.COMBINER.get(), aInput, toStack(output))); - else - { - CombinerRecipe recipe = new CombinerRecipe(toStack(input), toStack(output)); - MineTweakerAPI.apply(new AddMekanismRecipe("COMBINER", Recipe.COMBINER.get(), recipe.getInput(), recipe)); + public static void addRecipe(IItemStack itemInput, IGasStack gasInput, IItemStack itemOutput) { + if(itemInput == null || gasInput == null || itemOutput == null) { + LogHelper.logError(String.format("Required parameters missing for %s Recipe.", name)); + return; + } + + AdvancedMachineInput input = new AdvancedMachineInput(toStack(itemInput), toGas(gasInput).getGas()); + ItemStackOutput output = new ItemStackOutput(toStack(itemOutput)); + CombinerRecipe recipe = new CombinerRecipe(input, output); + + MineTweakerAPI.apply(new AddMekanismRecipe(name, Recipe.COMBINER.get(), recipe)); + } + + @SuppressWarnings("unchecked") + @ZenMethod + public static void addRecipe(IItemStack itemInput, IItemStack itemOutput) { + if(itemInput == null || itemOutput == null) { + LogHelper.logError(String.format("Required parameters missing for %s Recipe.", name)); + return; + } + + CombinerRecipe recipe = new CombinerRecipe(toStack(itemInput), toStack(itemOutput)); + + MineTweakerAPI.apply(new AddMekanismRecipe(name, Recipe.COMBINER.get(), recipe)); + } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + @SuppressWarnings({ "rawtypes", "unchecked" }) + @ZenMethod + public static void removeRecipe(IIngredient itemOutput, @Optional IIngredient itemInput, @Optional IIngredient gasInput) { + if(itemOutput == null) { + LogHelper.logError(String.format("Required parameters missing for %s Recipe.", name)); + return; + } + + if(gasInput == null) gasInput = IngredientAny.INSTANCE; + if(itemInput == null) itemInput = IngredientAny.INSTANCE; + + Map recipes = new HashMap(); + + for(Entry entry : ((Map)Recipe.COMBINER.get()).entrySet()) { + IItemStack inputItem = InputHelper.toIItemStack(entry.getKey().itemStack); + IGasStack inputGas = new MCGasStack(new GasStack(entry.getKey().gasType, 1)); + IItemStack outputItem = InputHelper.toIItemStack(entry.getValue().getOutput().output); + + if(!StackHelper.matches(itemInput, inputItem)) continue; + if(!StackHelper.matches(gasInput, inputGas)) continue; + if(!StackHelper.matches(itemOutput, outputItem)) continue; + + recipes.put(entry.getKey(), entry.getValue()); + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new RemoveMekanismRecipe(name, Recipe.COMBINER.get(), recipes)); + } else { + LogHelper.logWarning(String.format("No %s recipe found for %s, %s and %s. Command ignored!", name, itemInput.toString(), gasInput.toString(), itemOutput.toString())); } } - - @ZenMethod - public static void removeRecipe(IItemStack output) { - if (!Mekanism.v7) throw new UnsupportedOperationException("Function not added to v8 compatibility yet"); - MineTweakerAPI.apply(new RemoveMekanismRecipe("COMBINER", Recipe.COMBINER.get(), toStack(output))); - } } diff --git a/src/main/java/modtweaker2/mods/mekanism/handlers/Compressor.java b/src/main/java/modtweaker2/mods/mekanism/handlers/Compressor.java index 25898a8..2ed1c45 100644 --- a/src/main/java/modtweaker2/mods/mekanism/handlers/Compressor.java +++ b/src/main/java/modtweaker2/mods/mekanism/handlers/Compressor.java @@ -1,35 +1,101 @@ package modtweaker2.mods.mekanism.handlers; import static modtweaker2.helpers.InputHelper.toStack; -import mekanism.api.gas.GasRegistry; +import static modtweaker2.mods.mekanism.MekanismHelper.toGas; + +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; + +import mekanism.api.gas.GasStack; import mekanism.common.recipe.RecipeHandler.Recipe; import mekanism.common.recipe.inputs.AdvancedMachineInput; +import mekanism.common.recipe.inputs.MachineInput; +import mekanism.common.recipe.machines.MachineRecipe; import mekanism.common.recipe.machines.OsmiumCompressorRecipe; +import mekanism.common.recipe.outputs.ItemStackOutput; import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; -import modtweaker2.mods.mekanism.Mekanism; +import minetweaker.api.item.IngredientAny; +import modtweaker2.helpers.InputHelper; +import modtweaker2.helpers.LogHelper; +import modtweaker2.helpers.StackHelper; +import modtweaker2.mods.mekanism.gas.IGasStack; +import modtweaker2.mods.mekanism.gas.MCGasStack; import modtweaker2.mods.mekanism.util.AddMekanismRecipe; import modtweaker2.mods.mekanism.util.RemoveMekanismRecipe; +import stanhebben.zenscript.annotations.Optional; import stanhebben.zenscript.annotations.ZenClass; import stanhebben.zenscript.annotations.ZenMethod; @ZenClass("mods.mekanism.Compressor") public class Compressor { + + public static final String name = "Mekanism Compressor"; + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + @SuppressWarnings("unchecked") @ZenMethod - public static void addRecipe(IItemStack input, IItemStack output) { - AdvancedMachineInput aInput = new AdvancedMachineInput(toStack(input), GasRegistry.getGas("liquidOsmium")); - if (Mekanism.v7) - MineTweakerAPI.apply(new AddMekanismRecipe("OSMIUM_COMPRESSOR", Recipe.OSMIUM_COMPRESSOR.get(), aInput, toStack(output))); - else - { - OsmiumCompressorRecipe recipe = new OsmiumCompressorRecipe(toStack(input), toStack(output)); - MineTweakerAPI.apply(new AddMekanismRecipe("OSMIUM_COMPRESSOR", Recipe.OSMIUM_COMPRESSOR.get(), recipe.getInput(), recipe)); + public static void addRecipe(IItemStack itemInput, IItemStack itemOutput) { + if(itemInput == null || itemOutput == null) { + LogHelper.logError(String.format("Required parameters missing for %s Recipe.", name)); + return; + } + + OsmiumCompressorRecipe recipe = new OsmiumCompressorRecipe(toStack(itemInput), toStack(itemOutput)); + + MineTweakerAPI.apply(new AddMekanismRecipe(name, Recipe.OSMIUM_COMPRESSOR.get(), recipe)); + } + + @SuppressWarnings("unchecked") + @ZenMethod + public static void addRecipe(IItemStack itemInput, IGasStack gasInput, IItemStack itemOutput) { + if(itemInput == null || gasInput == null || itemOutput == null) { + LogHelper.logError(String.format("Required parameters missing for %s Recipe.", name)); + return; + } + + AdvancedMachineInput input = new AdvancedMachineInput(toStack(itemInput), toGas(gasInput).getGas()); + ItemStackOutput output = new ItemStackOutput(toStack(itemOutput)); + + OsmiumCompressorRecipe recipe = new OsmiumCompressorRecipe(input, output); + + MineTweakerAPI.apply(new AddMekanismRecipe(name, Recipe.OSMIUM_COMPRESSOR.get(), recipe)); + } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + @SuppressWarnings({ "unchecked", "rawtypes" }) + @ZenMethod + public static void removeRecipe(IIngredient itemOutput, @Optional IIngredient itemInput, @Optional IIngredient gasInput) { + if(itemOutput == null) { + LogHelper.logError(String.format("Required parameters missing for %s Recipe.", name)); + return; + } + + if(itemInput == null) itemInput = IngredientAny.INSTANCE; + if(gasInput == null) gasInput = IngredientAny.INSTANCE; + + Map recipes = new HashMap(); + + for(Entry entry : ((Map)Recipe.OSMIUM_COMPRESSOR.get()).entrySet()) { + IItemStack inputItem = InputHelper.toIItemStack(entry.getKey().itemStack); + IGasStack inputGas = new MCGasStack(new GasStack(entry.getKey().gasType, 1)); + IItemStack outputItem = InputHelper.toIItemStack(entry.getValue().getOutput().output); + + if(!StackHelper.matches(itemInput, inputItem)) continue; + if(!StackHelper.matches(gasInput, inputGas)) continue; + if(!StackHelper.matches(itemOutput, outputItem)) continue; + + recipes.put(entry.getKey(), entry.getValue()); + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new RemoveMekanismRecipe(name, Recipe.OSMIUM_COMPRESSOR.get(), recipes)); + } else { + LogHelper.logWarning(String.format("No %s recipe found for %s, %s and %s. Command ignored!", name, itemInput.toString(), gasInput.toString(), itemOutput.toString())); } } - - @ZenMethod - public static void removeRecipe(IItemStack output) { - if (!Mekanism.v7) throw new UnsupportedOperationException("Function not added to v8 compatibility yet"); - MineTweakerAPI.apply(new RemoveMekanismRecipe("OSMIUM_COMPRESSOR", Recipe.OSMIUM_COMPRESSOR.get(), toStack(output))); - } } diff --git a/src/main/java/modtweaker2/mods/mekanism/handlers/Crusher.java b/src/main/java/modtweaker2/mods/mekanism/handlers/Crusher.java index 637e8b8..c8026e2 100644 --- a/src/main/java/modtweaker2/mods/mekanism/handlers/Crusher.java +++ b/src/main/java/modtweaker2/mods/mekanism/handlers/Crusher.java @@ -1,32 +1,77 @@ package modtweaker2.mods.mekanism.handlers; import static modtweaker2.helpers.InputHelper.toStack; + +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; + import mekanism.common.recipe.RecipeHandler.Recipe; +import mekanism.common.recipe.inputs.ItemStackInput; +import mekanism.common.recipe.inputs.MachineInput; import mekanism.common.recipe.machines.CrusherRecipe; +import mekanism.common.recipe.machines.MachineRecipe; import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; -import modtweaker2.mods.mekanism.Mekanism; +import minetweaker.api.item.IngredientAny; +import modtweaker2.helpers.InputHelper; +import modtweaker2.helpers.LogHelper; +import modtweaker2.helpers.StackHelper; import modtweaker2.mods.mekanism.util.AddMekanismRecipe; import modtweaker2.mods.mekanism.util.RemoveMekanismRecipe; +import stanhebben.zenscript.annotations.Optional; import stanhebben.zenscript.annotations.ZenClass; import stanhebben.zenscript.annotations.ZenMethod; @ZenClass("mods.mekanism.Crusher") public class Crusher { + + public static final String name = "Mekanism Crusher"; + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + @SuppressWarnings("unchecked") @ZenMethod - public static void addRecipe(IItemStack input, IItemStack output) { - if (Mekanism.v7) - MineTweakerAPI.apply(new AddMekanismRecipe("CRUSHER", Recipe.CRUSHER.get(), toStack(input), toStack(output))); - else - { - CrusherRecipe recipe = new CrusherRecipe(toStack(input), toStack(output)); - MineTweakerAPI.apply(new AddMekanismRecipe("CRUSHER", Recipe.CRUSHER.get(), recipe.getInput(), recipe)); + public static void addRecipe(IItemStack itemInput, IItemStack itemOutput) { + if(itemInput == null || itemOutput == null) { + LogHelper.logError(String.format("Required parameters missing for %s Recipe.", name)); + return; } + + CrusherRecipe recipe = new CrusherRecipe(toStack(itemInput), toStack(itemOutput)); + + MineTweakerAPI.apply(new AddMekanismRecipe(name, Recipe.CRUSHER.get(), recipe)); } + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + @SuppressWarnings({ "unchecked", "rawtypes" }) @ZenMethod - public static void removeRecipe(IItemStack output) { - if (!Mekanism.v7) throw new UnsupportedOperationException("Function not added to v8 compatibility yet"); - MineTweakerAPI.apply(new RemoveMekanismRecipe("CRUSHER", Recipe.CRUSHER.get(), toStack(output))); + public static void removeRecipe(IIngredient itemOutput, @Optional IIngredient itemInput) { + if(itemOutput == null) { + LogHelper.logError(String.format("Required parameters missing for %s Recipe.", name)); + return; + } + + if(itemInput == null) itemInput = IngredientAny.INSTANCE; + + Map recipes = new HashMap(); + + for(Entry entry : ((Map)Recipe.CRUSHER.get()).entrySet() ) { + IItemStack inputItem = InputHelper.toIItemStack(entry.getKey().ingredient); + IItemStack outputItem = InputHelper.toIItemStack(entry.getValue().recipeOutput.output); + + if(!StackHelper.matches(itemOutput, outputItem)) continue; + if(!StackHelper.matches(itemInput, inputItem)) continue; + + recipes.put(entry.getKey(), entry.getValue()); + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new RemoveMekanismRecipe(name, Recipe.CRUSHER.get(), recipes)); + } else { + LogHelper.logWarning(String.format("No %s recipe found for %s and %s. Command ignored!", name, itemInput.toString(), itemOutput.toString())); + } } } diff --git a/src/main/java/modtweaker2/mods/mekanism/handlers/Enrichment.java b/src/main/java/modtweaker2/mods/mekanism/handlers/Enrichment.java index 863ccde..80bf1cb 100644 --- a/src/main/java/modtweaker2/mods/mekanism/handlers/Enrichment.java +++ b/src/main/java/modtweaker2/mods/mekanism/handlers/Enrichment.java @@ -1,32 +1,78 @@ package modtweaker2.mods.mekanism.handlers; import static modtweaker2.helpers.InputHelper.toStack; + +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; + import mekanism.common.recipe.RecipeHandler.Recipe; +import mekanism.common.recipe.inputs.ItemStackInput; +import mekanism.common.recipe.inputs.MachineInput; import mekanism.common.recipe.machines.EnrichmentRecipe; +import mekanism.common.recipe.machines.MachineRecipe; import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; -import modtweaker2.mods.mekanism.Mekanism; +import minetweaker.api.item.IngredientAny; +import modtweaker2.helpers.InputHelper; +import modtweaker2.helpers.LogHelper; +import modtweaker2.helpers.StackHelper; import modtweaker2.mods.mekanism.util.AddMekanismRecipe; import modtweaker2.mods.mekanism.util.RemoveMekanismRecipe; +import stanhebben.zenscript.annotations.Optional; import stanhebben.zenscript.annotations.ZenClass; import stanhebben.zenscript.annotations.ZenMethod; @ZenClass("mods.mekanism.Enrichment") public class Enrichment { + + public static final String name = "Mekanism Enrichment"; + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + @SuppressWarnings("unchecked") @ZenMethod - public static void addRecipe(IItemStack input, IItemStack output) { - if (Mekanism.v7) - MineTweakerAPI.apply(new AddMekanismRecipe("ENRICHMENT_CHAMBER", Recipe.ENRICHMENT_CHAMBER.get(), toStack(input), toStack(output))); - else - { - EnrichmentRecipe recipe = new EnrichmentRecipe(toStack(input), toStack(output)); - MineTweakerAPI.apply(new AddMekanismRecipe("ENRICHMENT_CHAMBER", Recipe.ENRICHMENT_CHAMBER.get(), recipe.getInput(), recipe)); + public static void addRecipe(IItemStack itemInput, IItemStack itemOutput) { + if(itemInput == null || itemOutput == null) { + LogHelper.logError(String.format("Required parameters missing for %s Recipe.", name)); + return; + } + + EnrichmentRecipe recipe = new EnrichmentRecipe(toStack(itemInput), toStack(itemOutput)); + + MineTweakerAPI.apply(new AddMekanismRecipe(name, Recipe.ENRICHMENT_CHAMBER.get(), recipe)); + } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + @SuppressWarnings({ "unchecked", "rawtypes" }) + @ZenMethod + public static void removeRecipe(IIngredient itemInput, @Optional IIngredient itemOutput) { + if(itemInput == null) { + LogHelper.logError(String.format("Required parameters missing for %s Recipe.", name)); + return; + } + + if(itemOutput == null) itemOutput = IngredientAny.INSTANCE; + + Map recipes = new HashMap(); + + for(Entry entry : ((Map)Recipe.ENRICHMENT_CHAMBER.get()).entrySet() ) { + IItemStack inputItem = InputHelper.toIItemStack(entry.getKey().ingredient); + IItemStack outputItem = InputHelper.toIItemStack(entry.getValue().recipeOutput.output); + + if(!StackHelper.matches(itemOutput, outputItem)) continue; + if(!StackHelper.matches(itemInput, inputItem)) continue; + + recipes.put(entry.getKey(), entry.getValue()); + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new RemoveMekanismRecipe(name, Recipe.ENRICHMENT_CHAMBER.get(), recipes)); + } else { + LogHelper.logWarning(String.format("No %s recipe found for %s and %s. Command ignored!", name, itemInput.toString(), itemOutput.toString())); } } - - @ZenMethod - public static void removeRecipe(IItemStack input) { - if (!Mekanism.v7) throw new UnsupportedOperationException("Function not added to v8 compatibility yet"); - MineTweakerAPI.apply(new RemoveMekanismRecipe("ENRICHMENT_CHAMBER", Recipe.ENRICHMENT_CHAMBER.get(), toStack(input))); - } + } diff --git a/src/main/java/modtweaker2/mods/mekanism/handlers/Infuser.java b/src/main/java/modtweaker2/mods/mekanism/handlers/Infuser.java index 6b1c67f..61eab9c 100644 --- a/src/main/java/modtweaker2/mods/mekanism/handlers/Infuser.java +++ b/src/main/java/modtweaker2/mods/mekanism/handlers/Infuser.java @@ -1,87 +1,84 @@ -//package modtweaker2.mods.mekanism.handlers; -// -//import static modtweaker2.helpers.InputHelper.toStack; -//import static modtweaker2.helpers.StackHelper.areEqual; -// -//import java.util.ArrayList; -//import java.util.Iterator; -//import java.util.List; -//import java.util.Map; -// -//import mekanism.api.infuse.InfuseRegistry; -//import mekanism.common.recipe.RecipeHandler.Recipe; -//import mekanism.common.recipe.inputs.InfusionInput; -//import mekanism.common.recipe.machines.MetallurgicInfuserRecipe; -//import mekanism.common.recipe.outputs.MachineOutput; -//import minetweaker.MineTweakerAPI; -//import minetweaker.api.item.IItemStack; -//import modtweaker2.mods.mekanism.Mekanism; -//import modtweaker2.mods.mekanism.util.AddMekanismRecipe; -//import modtweaker2.mods.mekanism.util.RemoveMekanismRecipe; -//import stanhebben.zenscript.annotations.ZenClass; -//import stanhebben.zenscript.annotations.ZenMethod; -// -//@ZenClass("mods.mekanism.Infuser") -//public class Infuser { -// @ZenMethod -// public static void addRecipe(String type, int infuse, IItemStack input, IItemStack output) { -// if (Mekanism.v7) -// { -// InfusionInput infuseIn = new InfusionInput(InfuseRegistry.get(type), infuse, toStack(input)); -// InfusionOutput infuseOut = new InfusionOutput(infuseIn, toStack(output)); -// -// MineTweakerAPI.apply(new AddMekanismRecipe("METALLURGIC_INFUSER", Recipe.METALLURGIC_INFUSER.get(), infuseIn, infuseOut)); -// } -// else -// { -// MetallurgicInfuserRecipe recipe = new MetallurgicInfuserRecipe(new mekanism.common.recipe.inputs.InfusionInput(InfuseRegistry.get(type), infuse, toStack(input)), toStack(output)); -// MineTweakerAPI.apply(new AddMekanismRecipe("METALLURGIC_INFUSER", Recipe.METALLURGIC_INFUSER.get(), recipe.getInput(), recipe)); -// } -// } -// -// @ZenMethod -// public static void removeRecipe(IItemStack output) { -// if (Mekanism.v7) -// { -// MineTweakerAPI.apply(new Remove("METALLURGIC_INFUSER", Recipe.METALLURGIC_INFUSER.get(), new InfusionOutput(null, toStack(output)))); -// } -// else -// { -// throw new UnsupportedOperationException("Function not added to v8 compatibility yet"); -// } -// } -// -// private static class Remove extends RemoveMekanismRecipe { -// public Remove(String string, Map map, Object key) { -// super(string, map, key); -// } -// private List removed = new ArrayList(); -// @Override -// public void apply() { -// Iterator it = map.entrySet().iterator(); -// while (it.hasNext()) { -// Map.Entry pairs = (Map.Entry)it.next(); -// InfusionInput key = (InfusionInput)pairs.getKey(); -// InfusionOutput value = (InfusionOutput)pairs.getValue(); -// if (key != null) -// { -// if (this.key instanceof InfusionOutput && areEqual(value.resource, ((InfusionOutput)this.key).resource)) -// { -// this.key = key; -// removed.add(value); -// it.remove(); -// } -// } -// } -// } -// -// @Override -// public void undo() -// { -// for (Object recipe : removed) -// { -// map.put(key, recipe); -// } -// } -// } -//} +package modtweaker2.mods.mekanism.handlers; + +import static modtweaker2.helpers.InputHelper.toStack; + +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; + +import mekanism.api.infuse.InfuseRegistry; +import mekanism.common.recipe.RecipeHandler.Recipe; +import mekanism.common.recipe.inputs.InfusionInput; +import mekanism.common.recipe.inputs.MachineInput; +import mekanism.common.recipe.machines.MachineRecipe; +import mekanism.common.recipe.machines.MetallurgicInfuserRecipe; +import mekanism.common.recipe.outputs.ItemStackOutput; +import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; +import minetweaker.api.item.IItemStack; +import minetweaker.api.item.IngredientAny; +import modtweaker2.helpers.InputHelper; +import modtweaker2.helpers.LogHelper; +import modtweaker2.helpers.StackHelper; +import modtweaker2.mods.mekanism.util.AddMekanismRecipe; +import modtweaker2.mods.mekanism.util.RemoveMekanismRecipe; +import stanhebben.zenscript.annotations.Optional; +import stanhebben.zenscript.annotations.ZenClass; +import stanhebben.zenscript.annotations.ZenMethod; + +@ZenClass("mods.mekanism.Infuser") +public class Infuser { + + public static final String name = "Mekanism Infuser"; + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + @SuppressWarnings("unchecked") + @ZenMethod + public static void addRecipe(String infuseType, int infuseAmount, IItemStack itemInput, IItemStack itemOutput) { + if(itemInput == null || itemOutput == null || infuseType == null || infuseType.isEmpty()) { + LogHelper.logError(String.format("Required parameters missing for %s Recipe.", name)); + return; + } + + InfusionInput input = new InfusionInput(InfuseRegistry.get(infuseType), infuseAmount, toStack(itemInput)); + ItemStackOutput output = new ItemStackOutput(toStack(itemOutput)); + + MetallurgicInfuserRecipe recipe = new MetallurgicInfuserRecipe(input, output); + + MineTweakerAPI.apply(new AddMekanismRecipe(name, Recipe.METALLURGIC_INFUSER.get(), recipe)); + } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + @SuppressWarnings({ "unchecked", "rawtypes" }) + public static void removeRecipe(IIngredient itemOutput, @Optional IIngredient itemInput, @Optional String infuseType) { + if(itemOutput == null) { + LogHelper.logError(String.format("Required parameters missing for %s Recipe.", name)); + return; + } + + if(itemInput == null) itemInput = IngredientAny.INSTANCE; + if(infuseType == null) infuseType = ""; + + Map recipes = new HashMap(); + + for(Entry entry : ((Map)Recipe.METALLURGIC_INFUSER.get()).entrySet() ) { + IItemStack inputItem = InputHelper.toIItemStack(entry.getKey().inputStack); + String typeInfuse = entry.getKey().infuse.type.name; + IItemStack outputItem = InputHelper.toIItemStack(entry.getValue().recipeOutput.output); + + if(!StackHelper.matches(itemOutput, outputItem)) continue; + if(!StackHelper.matches(itemInput, inputItem)) continue; + if(!infuseType.isEmpty() && !infuseType.equalsIgnoreCase(typeInfuse)) continue; + + recipes.put(entry.getKey(), entry.getValue()); + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new RemoveMekanismRecipe(name, Recipe.METALLURGIC_INFUSER.get(), recipes)); + } else { + LogHelper.logWarning(String.format("No %s recipe found for %s and %s. Command ignored!", name, itemInput.toString(), itemOutput.toString())); + } + } +} diff --git a/src/main/java/modtweaker2/mods/mekanism/handlers/Purification.java b/src/main/java/modtweaker2/mods/mekanism/handlers/Purification.java index 5f8a95d..baf3263 100644 --- a/src/main/java/modtweaker2/mods/mekanism/handlers/Purification.java +++ b/src/main/java/modtweaker2/mods/mekanism/handlers/Purification.java @@ -1,62 +1,101 @@ package modtweaker2.mods.mekanism.handlers; import static modtweaker2.helpers.InputHelper.toStack; -import static modtweaker2.helpers.StackHelper.areEqual; +import static modtweaker2.mods.mekanism.MekanismHelper.toGas; +import java.util.HashMap; import java.util.Map; +import java.util.Map.Entry; -import mekanism.api.gas.GasRegistry; +import mekanism.api.gas.GasStack; import mekanism.common.recipe.RecipeHandler.Recipe; import mekanism.common.recipe.inputs.AdvancedMachineInput; +import mekanism.common.recipe.inputs.MachineInput; +import mekanism.common.recipe.machines.MachineRecipe; import mekanism.common.recipe.machines.PurificationRecipe; -import mekanism.common.recipe.outputs.MachineOutput; +import mekanism.common.recipe.outputs.ItemStackOutput; import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; -import modtweaker2.mods.mekanism.Mekanism; +import minetweaker.api.item.IngredientAny; +import modtweaker2.helpers.InputHelper; +import modtweaker2.helpers.LogHelper; +import modtweaker2.helpers.StackHelper; +import modtweaker2.mods.mekanism.gas.IGasStack; +import modtweaker2.mods.mekanism.gas.MCGasStack; import modtweaker2.mods.mekanism.util.AddMekanismRecipe; -import modtweaker2.utils.BaseMapRemoval; -import net.minecraft.item.ItemStack; +import modtweaker2.mods.mekanism.util.RemoveMekanismRecipe; +import stanhebben.zenscript.annotations.Optional; import stanhebben.zenscript.annotations.ZenClass; import stanhebben.zenscript.annotations.ZenMethod; @ZenClass("mods.mekanism.Purification") public class Purification { + + public static final String name = "Mekanism Purification"; + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + @SuppressWarnings("unchecked") @ZenMethod - public static void addRecipe(IItemStack input, IItemStack output) { - if (Mekanism.v7) - { - AdvancedMachineInput aInput = new AdvancedMachineInput(toStack(input), GasRegistry.getGas("oxygen")); - MineTweakerAPI.apply(new AddMekanismRecipe("PURIFICATION_CHAMBER", Recipe.PURIFICATION_CHAMBER.get(), aInput, toStack(output))); - } - else - { - PurificationRecipe recipe = new PurificationRecipe(toStack(input), toStack(output)); - MineTweakerAPI.apply(new AddMekanismRecipe("PURIFICATION_CHAMBER", Recipe.PURIFICATION_CHAMBER.get(), recipe.getInput(), recipe)); + public static void addRecipe(IItemStack itemInput, IItemStack itemOutput) { + if(itemInput == null || itemOutput == null) { + LogHelper.logError(String.format("Required parameters missing for %s Recipe.", name)); + return; } + + PurificationRecipe recipe = new PurificationRecipe(toStack(itemInput), toStack(itemOutput)); + + MineTweakerAPI.apply(new AddMekanismRecipe(name, Recipe.PURIFICATION_CHAMBER.get(), recipe)); } - + + @SuppressWarnings("unchecked") @ZenMethod - public static void removeRecipe(IItemStack output) { - if (!Mekanism.v7) throw new UnsupportedOperationException("Function not added to v8 compatibility yet"); - MineTweakerAPI.apply(new Remove(toStack(output))); - } - - private static class Remove extends BaseMapRemoval { - public Remove(ItemStack stack) { - super("Purification Chamber", Recipe.PURIFICATION_CHAMBER.get(), stack); + public static void addRecipe(IItemStack itemInput, IGasStack gasInput, IItemStack itemOutput) { + if(itemInput == null || gasInput == null || itemOutput == null) { + LogHelper.logError(String.format("Required parameters missing for %s Recipe.", name)); + return; } - //We must search through the recipe entries so that we can assign the correct key for removal - @Override - public void apply() { - for (Map.Entry entry : ((Map) map).entrySet()) { - if (entry.getValue() != null && areEqual(entry.getValue(), (ItemStack) stack)) { - key = entry.getKey(); - break; - } - } - - super.apply(); + AdvancedMachineInput input = new AdvancedMachineInput(toStack(itemInput), toGas(gasInput).getGas()); + ItemStackOutput output = new ItemStackOutput(toStack(itemOutput)); + + PurificationRecipe recipe = new PurificationRecipe(input, output); + + MineTweakerAPI.apply(new AddMekanismRecipe(name, Recipe.PURIFICATION_CHAMBER.get(), recipe)); + } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + @SuppressWarnings({ "rawtypes", "unchecked" }) + @ZenMethod + public static void removeRecipe(IIngredient itemOutput, @Optional IIngredient itemInput, @Optional IIngredient gasInput) { + if(itemOutput == null) { + LogHelper.logError(String.format("Required parameters missing for %s Recipe.", name)); + return; + } + + if(itemInput == null) itemInput = IngredientAny.INSTANCE; + if(gasInput == null) gasInput = IngredientAny.INSTANCE; + + Map recipes = new HashMap(); + + for(Entry entry : ((Map)Recipe.PURIFICATION_CHAMBER.get()).entrySet()) { + IItemStack inputItem = InputHelper.toIItemStack(entry.getKey().itemStack); + IGasStack inputGas = new MCGasStack(new GasStack(entry.getKey().gasType, 1)); + IItemStack outputItem = InputHelper.toIItemStack(entry.getValue().recipeOutput.output); + + if(!StackHelper.matches(itemOutput, outputItem)) continue; + if(!StackHelper.matches(itemInput, inputItem)) continue; + if(!StackHelper.matches(gasInput, inputGas)) continue; + + recipes.put(entry.getKey(), entry.getValue()); + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new RemoveMekanismRecipe(name, Recipe.PURIFICATION_CHAMBER.get(), recipes)); + } else { + LogHelper.logWarning(String.format("No %s recipe found for %s, %s and %s. Command ignored!", name, itemOutput.toString(), itemInput.toString(), gasInput.toString())); } } } diff --git a/src/main/java/modtweaker2/mods/mekanism/handlers/Reaction.java b/src/main/java/modtweaker2/mods/mekanism/handlers/Reaction.java index 5bfb04a..975c406 100644 --- a/src/main/java/modtweaker2/mods/mekanism/handlers/Reaction.java +++ b/src/main/java/modtweaker2/mods/mekanism/handlers/Reaction.java @@ -3,40 +3,93 @@ package modtweaker2.mods.mekanism.handlers; import static modtweaker2.helpers.InputHelper.toFluid; import static modtweaker2.helpers.InputHelper.toStack; import static modtweaker2.mods.mekanism.MekanismHelper.toGas; + +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; + import mekanism.common.recipe.RecipeHandler.Recipe; +import mekanism.common.recipe.inputs.MachineInput; import mekanism.common.recipe.inputs.PressurizedInput; +import mekanism.common.recipe.machines.MachineRecipe; import mekanism.common.recipe.machines.PressurizedRecipe; import mekanism.common.recipe.outputs.PressurizedOutput; import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; +import minetweaker.api.item.IngredientAny; import minetweaker.api.liquid.ILiquidStack; -import modtweaker2.mods.mekanism.Mekanism; +import modtweaker2.helpers.InputHelper; +import modtweaker2.helpers.LogHelper; +import modtweaker2.helpers.StackHelper; import modtweaker2.mods.mekanism.gas.IGasStack; +import modtweaker2.mods.mekanism.gas.MCGasStack; import modtweaker2.mods.mekanism.util.AddMekanismRecipe; import modtweaker2.mods.mekanism.util.RemoveMekanismRecipe; +import stanhebben.zenscript.annotations.Optional; import stanhebben.zenscript.annotations.ZenClass; import stanhebben.zenscript.annotations.ZenMethod; @ZenClass("mods.mekanism.Reaction") public class Reaction { + + public static final String name = "Mekanism Reaction"; + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + @SuppressWarnings("unchecked") @ZenMethod - public static void addRecipe(IItemStack solid, ILiquidStack liquid, IGasStack gas, IItemStack outItem, IGasStack outGas, double energy, int ticks) { - if (Mekanism.v7) - { - PressurizedInput input = new PressurizedInput(toStack(solid), toFluid(liquid), toGas(gas)); - PressurizedRecipe recipe = new PressurizedRecipe(input, new PressurizedOutput(toStack(outItem), toGas(outGas)), energy, ticks); - MineTweakerAPI.apply(new AddMekanismRecipe("PRESSURIZED_REACTION_CHAMBER", Recipe.PRESSURIZED_REACTION_CHAMBER.get(), input, recipe)); - } - else { - mekanism.common.recipe.machines.PressurizedRecipe recipe = new mekanism.common.recipe.machines.PressurizedRecipe(toStack(solid), toFluid(liquid), toGas(gas), toStack(outItem), toGas(outGas), energy, ticks); - MineTweakerAPI.apply(new AddMekanismRecipe("PRESSURIZED_REACTION_CHAMBER", Recipe.PRESSURIZED_REACTION_CHAMBER.get(), recipe.getInput(), recipe)); + public static void addRecipe(IItemStack itemInput, ILiquidStack liquidInput, IGasStack gasInput, IItemStack itemOutput, IGasStack gasOutput, double energy, int duration) { + if(itemInput == null || liquidInput == null || gasInput == null || itemOutput == null || gasOutput == null) { + LogHelper.logError(String.format("Required parameters missing for %s Recipe.", name)); + return; } + + PressurizedInput input = new PressurizedInput(toStack(itemInput), toFluid(liquidInput), toGas(gasInput)); + PressurizedOutput output = new PressurizedOutput(toStack(itemOutput), toGas(gasOutput)); + + PressurizedRecipe recipe = new PressurizedRecipe(input, output, energy, duration); + + MineTweakerAPI.apply(new AddMekanismRecipe(name, Recipe.PRESSURIZED_REACTION_CHAMBER.get(), recipe)); } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + @SuppressWarnings({ "unchecked", "rawtypes" }) @ZenMethod - public static void removeRecipe(IItemStack outItem, IGasStack outGas) { - if (!Mekanism.v7) throw new UnsupportedOperationException("Function not added to v8 compatibility yet"); - PressurizedOutput output = new PressurizedOutput(toStack(outItem), toGas(outGas)); - MineTweakerAPI.apply(new RemoveMekanismRecipe("PRESSURIZED_REACTION_CHAMBER", Recipe.PRESSURIZED_REACTION_CHAMBER.get(), output)); + public static void removeRecipe(IIngredient itemOutput, IIngredient gasOutput, @Optional IIngredient itemInput, @Optional IIngredient liquidInput, IIngredient gasInput) { + if(itemOutput == null || gasOutput == null) { + LogHelper.logError(String.format("Required parameters missing for %s Recipe.", name)); + return; + } + + if(itemInput == null) itemInput = IngredientAny.INSTANCE; + if(liquidInput == null) liquidInput = IngredientAny.INSTANCE; + if(gasInput == null) gasInput = IngredientAny.INSTANCE; + + Map recipes = new HashMap(); + + for(Entry entry : ((Map)Recipe.PRESSURIZED_REACTION_CHAMBER.get()).entrySet() ) { + IItemStack inputItem = InputHelper.toIItemStack(entry.getKey().getSolid()); + ILiquidStack inputLiquid = InputHelper.toILiquidStack(entry.getKey().getFluid()); + IGasStack inputGas = new MCGasStack(entry.getKey().getGas()); + IItemStack outputItem = InputHelper.toIItemStack(entry.getValue().recipeOutput.getItemOutput()); + IGasStack outputGas = new MCGasStack(entry.getValue().recipeOutput.getGasOutput()); + + if(!StackHelper.matches(itemInput, inputItem)) continue; + if(!StackHelper.matches(liquidInput, inputLiquid)) continue; + if(!StackHelper.matches(gasInput, inputGas)) continue; + if(!StackHelper.matches(itemOutput, outputItem)) continue; + if(!StackHelper.matches(gasOutput, outputGas)) continue; + + recipes.put(entry.getKey(), entry.getValue()); + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new RemoveMekanismRecipe(name, Recipe.PRESSURIZED_REACTION_CHAMBER.get(), recipes)); + } else { + LogHelper.logWarning(String.format("No %s recipe found for %s, %s, %s, %s and %s. Command ignored!", name, itemOutput.toString(), gasOutput.toString(), itemInput.toString(), liquidInput.toString(), gasInput.toString())); + } } + } diff --git a/src/main/java/modtweaker2/mods/mekanism/handlers/Sawmill.java b/src/main/java/modtweaker2/mods/mekanism/handlers/Sawmill.java index cb9eefa..288781e 100644 --- a/src/main/java/modtweaker2/mods/mekanism/handlers/Sawmill.java +++ b/src/main/java/modtweaker2/mods/mekanism/handlers/Sawmill.java @@ -1,61 +1,84 @@ package modtweaker2.mods.mekanism.handlers; import static modtweaker2.helpers.InputHelper.toStack; -import static modtweaker2.helpers.StackHelper.areEqual; +import java.util.HashMap; import java.util.Map; +import java.util.Map.Entry; import mekanism.common.recipe.RecipeHandler.Recipe; +import mekanism.common.recipe.inputs.ItemStackInput; +import mekanism.common.recipe.inputs.MachineInput; +import mekanism.common.recipe.machines.MachineRecipe; import mekanism.common.recipe.machines.SawmillRecipe; import mekanism.common.recipe.outputs.ChanceOutput; import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; -import modtweaker2.mods.mekanism.Mekanism; +import minetweaker.api.item.IngredientAny; +import modtweaker2.helpers.InputHelper; +import modtweaker2.helpers.LogHelper; +import modtweaker2.helpers.StackHelper; import modtweaker2.mods.mekanism.util.AddMekanismRecipe; -import modtweaker2.utils.BaseMapRemoval; -import net.minecraft.item.ItemStack; +import modtweaker2.mods.mekanism.util.RemoveMekanismRecipe; import stanhebben.zenscript.annotations.Optional; import stanhebben.zenscript.annotations.ZenClass; import stanhebben.zenscript.annotations.ZenMethod; @ZenClass("mods.mekanism.Sawmill") public class Sawmill { + + public static final String name = "Mekanism Sawmill"; + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + @SuppressWarnings("unchecked") @ZenMethod - public static void addRecipe(IItemStack input, IItemStack output1, @Optional IItemStack output2, @Optional double chance) { - if (Mekanism.v7) - { - ChanceOutput chanceOutput = new ChanceOutput(toStack(output1), toStack(output2), chance); - MineTweakerAPI.apply(new AddMekanismRecipe("PRECISION_SAWMILL", Recipe.PRECISION_SAWMILL.get(), toStack(input), chanceOutput)); - } - else - { - SawmillRecipe recipe = chance>0?new SawmillRecipe(toStack(input), toStack(output1), toStack(output2), chance) : new SawmillRecipe(toStack(input), toStack(output1)); - MineTweakerAPI.apply(new AddMekanismRecipe("PRECISION_SAWMILL", Recipe.PRECISION_SAWMILL.get(), recipe.getInput(), recipe)); + public static void addRecipe(IItemStack itemInput, IItemStack itemOutput, @Optional IItemStack optionalItemOutput, @Optional double optionalChance) { + if(itemInput == null || itemOutput == null) { + LogHelper.logError(String.format("Required parameters missing for %s Recipe.", name)); + return; } + + ItemStackInput input = new ItemStackInput(toStack(itemInput)); + ChanceOutput output = optionalItemOutput == null ? new ChanceOutput(toStack(itemOutput)) : + new ChanceOutput(toStack(itemOutput), toStack(optionalItemOutput), optionalChance); + + SawmillRecipe recipe = new SawmillRecipe(input, output); + + MineTweakerAPI.apply(new AddMekanismRecipe(name, Recipe.PRECISION_SAWMILL.get(), recipe)); } - - @ZenMethod - public static void removeRecipe(IItemStack input) { - if (!Mekanism.v7) throw new UnsupportedOperationException("Function not added to v8 compatibility yet"); - MineTweakerAPI.apply(new Remove(toStack(input))); - } - - private static class Remove extends BaseMapRemoval { - public Remove(ItemStack stack) { - super("Precision Sawmill", Recipe.PRECISION_SAWMILL.get(), stack); + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + @SuppressWarnings({ "unchecked", "rawtypes" }) + public static void removeRecipe(IIngredient itemInput, @Optional IIngredient itemOutput, @Optional IIngredient optionalItemOutput) { + if(itemInput == null) { + LogHelper.logError(String.format("Required parameters missing for %s Recipe.", name)); + return; } - - //We must search through the recipe entries so that we can assign the correct key for removal - @Override - public void apply() { - for (Map.Entry entry : ((Map) map).entrySet()) { - if (entry.getKey() != null && areEqual(entry.getKey(), (ItemStack) stack)) { - key = entry.getKey(); - break; - } - } - - super.apply(); + + if(itemOutput == null) itemOutput = IngredientAny.INSTANCE; + if(optionalItemOutput == null) optionalItemOutput = IngredientAny.INSTANCE; + + Map recipes = new HashMap(); + + for(Entry entry : ((Map)Recipe.PRECISION_SAWMILL.get()).entrySet() ) { + IItemStack inputItem = InputHelper.toIItemStack(entry.getKey().ingredient); + IItemStack outputItem = InputHelper.toIItemStack(entry.getValue().recipeOutput.primaryOutput); + IItemStack outputItemOptional = InputHelper.toIItemStack(entry.getValue().recipeOutput.secondaryOutput); + + if(!StackHelper.matches(itemOutput, outputItem)) continue; + if(!StackHelper.matches(itemInput, inputItem)) continue; + if(!StackHelper.matches(optionalItemOutput, outputItemOptional)) continue; + + recipes.put(entry.getKey(), entry.getValue()); + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new RemoveMekanismRecipe(name, Recipe.PRECISION_SAWMILL.get(), recipes)); + } else { + LogHelper.logWarning(String.format("No %s recipe found for %s and %s. Command ignored!", name, itemInput.toString(), itemOutput.toString())); } } } diff --git a/src/main/java/modtweaker2/mods/mekanism/handlers/Separator.java b/src/main/java/modtweaker2/mods/mekanism/handlers/Separator.java index af08c3f..09c2b7d 100644 --- a/src/main/java/modtweaker2/mods/mekanism/handlers/Separator.java +++ b/src/main/java/modtweaker2/mods/mekanism/handlers/Separator.java @@ -3,70 +3,81 @@ package modtweaker2.mods.mekanism.handlers; import static modtweaker2.helpers.InputHelper.toFluid; import static modtweaker2.mods.mekanism.MekanismHelper.toGas; +import java.util.HashMap; import java.util.Map; +import java.util.Map.Entry; import mekanism.common.recipe.RecipeHandler.Recipe; -import mekanism.common.recipe.inputs.ChemicalPairInput; +import mekanism.common.recipe.inputs.FluidInput; +import mekanism.common.recipe.inputs.MachineInput; +import mekanism.common.recipe.machines.MachineRecipe; import mekanism.common.recipe.machines.SeparatorRecipe; -import mekanism.common.recipe.outputs.ChemicalPairOutput; import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; +import minetweaker.api.item.IngredientAny; import minetweaker.api.liquid.ILiquidStack; -import modtweaker2.mods.mekanism.Mekanism; +import modtweaker2.helpers.InputHelper; +import modtweaker2.helpers.LogHelper; +import modtweaker2.helpers.StackHelper; import modtweaker2.mods.mekanism.gas.IGasStack; +import modtweaker2.mods.mekanism.gas.MCGasStack; import modtweaker2.mods.mekanism.util.AddMekanismRecipe; -import modtweaker2.utils.BaseMapRemoval; -import net.minecraftforge.fluids.FluidStack; +import modtweaker2.mods.mekanism.util.RemoveMekanismRecipe; +import stanhebben.zenscript.annotations.Optional; import stanhebben.zenscript.annotations.ZenClass; import stanhebben.zenscript.annotations.ZenMethod; @ZenClass("mods.mekanism.Separator") public class Separator { + + public static final String name = "Mekanism Separator"; + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + @SuppressWarnings("unchecked") @ZenMethod - public static void addRecipe(ILiquidStack input, IGasStack gas1, IGasStack gas2) { - if (Mekanism.v7) - { - ChemicalPairInput pair = new ChemicalPairInput(toGas(gas1), toGas(gas2)); - MineTweakerAPI.apply(new AddMekanismRecipe("ELECTROLYTIC_SEPARATOR", Recipe.ELECTROLYTIC_SEPARATOR.get(), toFluid(input), pair)); - } else - { - throw new UnsupportedOperationException("Syntax for v8 is: Fluid, Energy, Gas, Gas"); + public static void addRecipe(ILiquidStack liquidInput, double energy, IGasStack leftGasOutput, IGasStack rightGasOutput) { + if(liquidInput == null || leftGasOutput == null || rightGasOutput == null) { + LogHelper.logError(String.format("Required parameters missing for %s Recipe.", name)); + return; } + + SeparatorRecipe recipe = new SeparatorRecipe(toFluid(liquidInput), energy, toGas(leftGasOutput), toGas(rightGasOutput)); + + MineTweakerAPI.apply(new AddMekanismRecipe(name, Recipe.ELECTROLYTIC_SEPARATOR.get(), recipe)); } - + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + @SuppressWarnings({ "rawtypes", "unchecked" }) @ZenMethod - public static void addRecipe(ILiquidStack input, double energy, IGasStack gas1, IGasStack gas2) { - if (Mekanism.v7) - { - throw new UnsupportedOperationException("Syntax for v7 is: Fluid, Gas, Gas"); - } else - { - SeparatorRecipe recipe = new SeparatorRecipe(toFluid(input), energy, toGas(gas1), toGas(gas2)); - MineTweakerAPI.apply(new AddMekanismRecipe("ELECTROLYTIC_SEPARATOR", Recipe.ELECTROLYTIC_SEPARATOR.get(), recipe.getInput(), recipe)); + public static void removeRecipe(IIngredient liquidInput, @Optional IIngredient leftGasInput, @Optional IIngredient rightGasInput) { + if(liquidInput == null) { + LogHelper.logError(String.format("Required parameters missing for %s Recipe.", name)); + return; } - } - - @ZenMethod - public static void removeRecipe(ILiquidStack input) { - if (!Mekanism.v7) throw new UnsupportedOperationException("Function not added to v8 compatibility yet"); - MineTweakerAPI.apply(new Remove(toFluid(input))); - } - - private static class Remove extends BaseMapRemoval { - public Remove(FluidStack stack) { - super("Electrolytic Separator", Recipe.ELECTROLYTIC_SEPARATOR.get(), stack); + + if(leftGasInput == null) leftGasInput = IngredientAny.INSTANCE; + if(rightGasInput == null) leftGasInput = IngredientAny.INSTANCE; + + Map recipes = new HashMap(); + + for(Entry entry : ((Map)Recipe.ELECTROLYTIC_SEPARATOR.get()).entrySet() ) { + ILiquidStack inputLiquid = InputHelper.toILiquidStack(entry.getKey().ingredient); + IGasStack outputItemLeft = new MCGasStack(entry.getValue().recipeOutput.leftGas); + IGasStack outputItemRight = new MCGasStack(entry.getValue().recipeOutput.rightGas); + + if(!StackHelper.matches(liquidInput, inputLiquid)) continue; + if(!StackHelper.matches(leftGasInput, outputItemLeft)) continue; + if(!StackHelper.matches(rightGasInput, outputItemRight)) continue; + + recipes.put(entry.getKey(), entry.getValue()); } - - //We must search through the recipe entries so that we can assign the correct key for removal - @Override - public void apply() { - for (Map.Entry entry : ((Map) map).entrySet()) { - if (entry.getKey() != null && entry.getKey().isFluidEqual((FluidStack) stack)) { - key = entry.getKey(); - break; - } - } - - super.apply(); + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new RemoveMekanismRecipe(name, Recipe.ELECTROLYTIC_SEPARATOR.get(), recipes)); + } else { + LogHelper.logWarning(String.format("No %s recipe found for %s, %s and %s. Command ignored!", name, liquidInput.toString(), leftGasInput.toString(), rightGasInput.toString())); } } } diff --git a/src/main/java/modtweaker2/mods/mekanism/handlers/v8/ChemicalCrystallizer.java b/src/main/java/modtweaker2/mods/mekanism/handlers/v8/ChemicalCrystallizer.java deleted file mode 100644 index b8e37e4..0000000 --- a/src/main/java/modtweaker2/mods/mekanism/handlers/v8/ChemicalCrystallizer.java +++ /dev/null @@ -1,85 +0,0 @@ -package modtweaker2.mods.mekanism.handlers.v8; - -import static modtweaker2.mods.mekanism.MekanismHelper.toGas; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import mekanism.api.gas.GasStack; -import mekanism.common.KeySync.KeySet; -import mekanism.common.recipe.RecipeHandler; -import mekanism.common.recipe.RecipeHandler.Recipe; -import mekanism.common.recipe.inputs.GasInput; -import mekanism.common.recipe.machines.CrystallizerRecipe; -import minetweaker.MineTweakerAPI; -import minetweaker.api.item.IItemStack; -import modtweaker2.helpers.InputHelper; -import modtweaker2.mods.mekanism.gas.IGasStack; -import modtweaker2.utils.BaseMapAddition; -import modtweaker2.utils.BaseMapRemoval; -import net.minecraft.item.ItemStack; -import stanhebben.zenscript.annotations.ZenClass; -import stanhebben.zenscript.annotations.ZenMethod; - -@ZenClass("mods.mekanism.chemical.Crystallizer") -public class ChemicalCrystallizer { - - @ZenMethod - public static void addRecipe(IItemStack output, IGasStack input) { - MineTweakerAPI.apply(new Add(new ChemicalCrystallizerRecipe(toGas(input), InputHelper.toStack(output)))); - } - - private static class Add extends BaseMapAddition { - - public Add(ChemicalCrystallizerRecipe recipe) { - super("Chemical Crystallizer", Recipe.CHEMICAL_CRYSTALLIZER.get(), recipe.inputGas, recipe.outputItem); - } - - @Override - public void apply() { - RecipeHandler.addChemicalCrystallizerRecipe((GasStack) key, (ItemStack) recipe); - } - - } - - private static class ChemicalCrystallizerRecipe { - public GasStack inputGas; - public ItemStack outputItem; - - public ChemicalCrystallizerRecipe(GasStack inputGas, ItemStack outputItem) { - this.inputGas = inputGas; - this.outputItem = outputItem; - - } - } - - @ZenMethod - public static void removeRecipe(IItemStack output) { - MineTweakerAPI.apply(new Remove(InputHelper.toStack(output))); - } - - private static class Remove extends BaseMapRemoval { - - public Remove(ItemStack stack) { - super("Chemical Crystallizer", Recipe.CHEMICAL_CRYSTALLIZER.get(), stack); - } - - @SuppressWarnings("unchecked") - @Override - public void apply() { - List gasesToRemove = new ArrayList(); - for(GasInput stack : (Set)Recipe.CHEMICAL_CRYSTALLIZER.get().keySet()){ - CrystallizerRecipe crystalRecipe= (CrystallizerRecipe)Recipe.CHEMICAL_CRYSTALLIZER.get().get(stack); - if(crystalRecipe.getOutput().output.isItemEqual((ItemStack) this.stack)){ - gasesToRemove.add(stack); - } - } - for(GasInput stack : gasesToRemove){ - Recipe.CHEMICAL_CRYSTALLIZER.get().remove(stack); - } - } - - } -} diff --git a/src/main/java/modtweaker2/mods/mekanism/handlers/v8/ChemicalDissolution.java b/src/main/java/modtweaker2/mods/mekanism/handlers/v8/ChemicalDissolution.java deleted file mode 100644 index 0cd5c61..0000000 --- a/src/main/java/modtweaker2/mods/mekanism/handlers/v8/ChemicalDissolution.java +++ /dev/null @@ -1,84 +0,0 @@ -package modtweaker2.mods.mekanism.handlers.v8; - -import java.util.ArrayList; -import java.util.List; -import java.util.Set; - -import mekanism.api.gas.GasStack; -import mekanism.common.recipe.RecipeHandler; -import mekanism.common.recipe.RecipeHandler.Recipe; -import mekanism.common.recipe.inputs.ItemStackInput; -import minetweaker.MineTweakerAPI; -import minetweaker.api.item.IItemStack; -import modtweaker2.helpers.InputHelper; -import modtweaker2.mods.mekanism.MekanismHelper; -import modtweaker2.mods.mekanism.gas.IGasStack; -import modtweaker2.utils.BaseMapAddition; -import modtweaker2.utils.BaseMapRemoval; -import net.minecraft.item.ItemStack; -import stanhebben.zenscript.annotations.ZenClass; -import stanhebben.zenscript.annotations.ZenMethod; - -@ZenClass("mods.mekanism.chemical.Dissolution") -public class ChemicalDissolution { - - @ZenMethod - public static void addRecipe(IGasStack output, IItemStack input) { - MineTweakerAPI.apply(new Add(new DissolutionRecipe(MekanismHelper.toGas(output), InputHelper.toStack(input)))); - } - - private static class Add extends BaseMapAddition { - - public DissolutionRecipe recipe; - - public Add(DissolutionRecipe recipe) { - super("Dissolution Infuser", Recipe.CHEMICAL_DISSOLUTION_CHAMBER.get(), recipe.input, recipe.output); - this.recipe = recipe; - } - - @Override - public void apply() { - RecipeHandler.addChemicalDissolutionChamberRecipe(recipe.input, recipe.output); - - } - } - - private static class DissolutionRecipe { - public GasStack output; - public ItemStack input; - - public DissolutionRecipe(GasStack output, ItemStack input) { - this.output = output; - this.input = input; - } - - } - - @ZenMethod - public static void removeRecipe(IGasStack output) { - MineTweakerAPI.apply(new Remove(MekanismHelper.toGas(output))); - } - - private static class Remove extends BaseMapRemoval { - - public Remove(GasStack stack) { - super("Dissolution Infuser", Recipe.CHEMICAL_DISSOLUTION_CHAMBER.get(), stack); - } - - @SuppressWarnings("unchecked") - @Override - public void apply() { - List toRemove = new ArrayList(); - for (ItemStackInput stack : (Set) Recipe.CHEMICAL_DISSOLUTION_CHAMBER.get().keySet()) { - mekanism.common.recipe.machines.DissolutionRecipe recipe = (mekanism.common.recipe.machines.DissolutionRecipe) Recipe.CHEMICAL_DISSOLUTION_CHAMBER.get().get(stack); - if (recipe.getOutput().output.isGasEqual((GasStack) this.stack)) { - toRemove.add(stack); - } - } - for (ItemStackInput stack : toRemove) { - Recipe.CHEMICAL_DISSOLUTION_CHAMBER.get().remove(stack); - } - } - - } -} diff --git a/src/main/java/modtweaker2/mods/mekanism/handlers/v8/ChemicalInfuser.java b/src/main/java/modtweaker2/mods/mekanism/handlers/v8/ChemicalInfuser.java deleted file mode 100644 index fae3b50..0000000 --- a/src/main/java/modtweaker2/mods/mekanism/handlers/v8/ChemicalInfuser.java +++ /dev/null @@ -1,85 +0,0 @@ -package modtweaker2.mods.mekanism.handlers.v8; - -import java.util.ArrayList; -import java.util.List; -import java.util.Set; - -import mekanism.api.gas.GasStack; -import mekanism.common.recipe.RecipeHandler; -import mekanism.common.recipe.RecipeHandler.Recipe; -import mekanism.common.recipe.inputs.ChemicalPairInput; -import mekanism.common.recipe.machines.ChemicalInfuserRecipe; -import mekanism.common.recipe.outputs.GasOutput; -import minetweaker.MineTweakerAPI; -import modtweaker2.mods.mekanism.MekanismHelper; -import modtweaker2.mods.mekanism.gas.IGasStack; -import modtweaker2.utils.BaseMapAddition; -import modtweaker2.utils.BaseMapRemoval; -import stanhebben.zenscript.annotations.ZenClass; -import stanhebben.zenscript.annotations.ZenMethod; - -@ZenClass("mods.mekanism.chemical.Infuser") -public class ChemicalInfuser { - - @ZenMethod - public static void addRecipe(IGasStack output, IGasStack inputLeft, IGasStack inputRight) { - MineTweakerAPI.apply(new Add(new InfuserRecipe(MekanismHelper.toGas(output), MekanismHelper.toGas(inputLeft), MekanismHelper.toGas(inputRight)))); - } - - private static class Add extends BaseMapAddition { - - public InfuserRecipe recipe; - - public Add(InfuserRecipe recipe) { - super("Chemical Infuser", Recipe.CHEMICAL_INFUSER.get(), new ChemicalPairInput(recipe.inputLeft, recipe.inputRight), new GasOutput(recipe.output)); - this.recipe = recipe; - } - - @Override - public void apply() { - RecipeHandler.addChemicalInfuserRecipe(recipe.inputLeft, recipe.inputRight, recipe.output); - - } - } - - private static class InfuserRecipe { - public GasStack output; - public GasStack inputLeft; - public GasStack inputRight; - - public InfuserRecipe(GasStack output, GasStack inputLeft, GasStack inputRight) { - this.output = output; - this.inputLeft = inputLeft; - this.inputRight = inputRight; - } - - } - - @ZenMethod - public static void removeRecipe(IGasStack output) { - MineTweakerAPI.apply(new Remove(MekanismHelper.toGas(output))); - } - - private static class Remove extends BaseMapRemoval { - - public Remove(GasStack stack) { - super("Chemical Infuser", Recipe.CHEMICAL_INFUSER.get(), stack); - } - - @SuppressWarnings("unchecked") - @Override - public void apply() { - List toRemove = new ArrayList(); - for (ChemicalPairInput stack : (Set) Recipe.CHEMICAL_INFUSER.get().keySet()) { - ChemicalInfuserRecipe recipe = (ChemicalInfuserRecipe) Recipe.CHEMICAL_INFUSER.get().get(stack); - if (recipe.getOutput().output.isGasEqual((GasStack)this.stack)) { - toRemove.add(stack); - } - } - for (ChemicalPairInput stack : toRemove) { - Recipe.CHEMICAL_INFUSER.get().remove(stack); - } - } - - } -} diff --git a/src/main/java/modtweaker2/mods/mekanism/handlers/v8/ChemicalInjection.java b/src/main/java/modtweaker2/mods/mekanism/handlers/v8/ChemicalInjection.java deleted file mode 100644 index 1a6eab0..0000000 --- a/src/main/java/modtweaker2/mods/mekanism/handlers/v8/ChemicalInjection.java +++ /dev/null @@ -1,86 +0,0 @@ -package modtweaker2.mods.mekanism.handlers.v8; - -import java.util.ArrayList; -import java.util.List; -import java.util.Set; - -import mekanism.api.gas.GasStack; -import mekanism.common.recipe.RecipeHandler; -import mekanism.common.recipe.RecipeHandler.Recipe; -import mekanism.common.recipe.inputs.AdvancedMachineInput; -import mekanism.common.recipe.outputs.ItemStackOutput; -import minetweaker.MineTweakerAPI; -import minetweaker.api.item.IItemStack; -import modtweaker2.helpers.InputHelper; -import modtweaker2.mods.mekanism.MekanismHelper; -import modtweaker2.mods.mekanism.gas.IGasStack; -import modtweaker2.utils.BaseMapAddition; -import modtweaker2.utils.BaseMapRemoval; -import net.minecraft.item.ItemStack; -import stanhebben.zenscript.annotations.ZenClass; -import stanhebben.zenscript.annotations.ZenMethod; - -@ZenClass("mods.mekanism.chemical.Injection") -public class ChemicalInjection { - - @ZenMethod - public static void addRecipe(IItemStack output, IItemStack input, IGasStack gas) { - MineTweakerAPI.apply(new Add(new InjectionRecipe(InputHelper.toStack(output), InputHelper.toStack(input), MekanismHelper.toGas(gas)))); - } - - private static class Add extends BaseMapAddition { - - public InjectionRecipe recipe; - - public Add(InjectionRecipe recipe) { - super("Chemical Injection", Recipe.CHEMICAL_INJECTION_CHAMBER.get(), recipe.input, recipe.output); - this.recipe = recipe; - } - - @Override - public void apply() { - RecipeHandler.addChemicalInjectionChamberRecipe(recipe.input, recipe.gas.getGas().getLocalizedName(), recipe.output); - } - } - - private static class InjectionRecipe { - public ItemStack output; - public ItemStack input; - public GasStack gas; - - public InjectionRecipe(ItemStack output, ItemStack input, GasStack gas) { - this.output = output; - this.input = input; - this.gas = gas; - } - - } - - @ZenMethod - public static void removeRecipe(IItemStack output) { - MineTweakerAPI.apply(new Remove(InputHelper.toStack(output))); - } - - private static class Remove extends BaseMapRemoval { - - public Remove(ItemStack output) { - super("Chemical Injection", Recipe.CHEMICAL_DISSOLUTION_CHAMBER.get(), output); - } - - @SuppressWarnings("unchecked") - @Override - public void apply() { - List toRemove = new ArrayList(); - for (AdvancedMachineInput stack : (Set) Recipe.CHEMICAL_DISSOLUTION_CHAMBER.get().keySet()) { - ItemStackOutput recipe = (ItemStackOutput) Recipe.CHEMICAL_DISSOLUTION_CHAMBER.get().get(stack); - if (recipe.output.isItemEqual((ItemStack)this.stack)) { - toRemove.add(stack); - } - } - for (AdvancedMachineInput stack : toRemove) { - Recipe.CHEMICAL_DISSOLUTION_CHAMBER.get().remove(stack); - } - } - - } -} diff --git a/src/main/java/modtweaker2/mods/mekanism/handlers/v8/Infuser.java b/src/main/java/modtweaker2/mods/mekanism/handlers/v8/Infuser.java deleted file mode 100644 index 00dfaec..0000000 --- a/src/main/java/modtweaker2/mods/mekanism/handlers/v8/Infuser.java +++ /dev/null @@ -1,90 +0,0 @@ -package modtweaker2.mods.mekanism.handlers.v8; - -import java.util.ArrayList; -import java.util.List; -import java.util.Set; - -import mekanism.api.infuse.InfuseRegistry; -import mekanism.api.infuse.InfuseType; -import mekanism.common.recipe.RecipeHandler; -import mekanism.common.recipe.RecipeHandler.Recipe; -import mekanism.common.recipe.inputs.InfusionInput; -import mekanism.common.recipe.machines.MetallurgicInfuserRecipe; -import mekanism.common.recipe.outputs.ItemStackOutput; -import minetweaker.MineTweakerAPI; -import minetweaker.api.item.IItemStack; -import modtweaker2.helpers.InputHelper; -import modtweaker2.utils.BaseMapAddition; -import modtweaker2.utils.BaseMapRemoval; -import net.minecraft.item.ItemStack; -import stanhebben.zenscript.annotations.ZenClass; -import stanhebben.zenscript.annotations.ZenMethod; - -@ZenClass("mods.mekanism.Infuser") -public class Infuser { - - @ZenMethod - public static void addRecipe(IItemStack output, IItemStack input, int amount, String infuseType) { - InfuseType type = InfuseRegistry.get(infuseType); - MineTweakerAPI.apply(new Add(new InfuserRecipe(InputHelper.toStack(output), InputHelper.toStack(input), amount, type))); - } - - private static class Add extends BaseMapAddition { - - public InfuserRecipe recipe; - - public Add(InfuserRecipe recipe) { - super("Metallurgic Infuser", Recipe.METALLURGIC_INFUSER.get(), new InfusionInput(recipe.type, recipe.amount, recipe.input), new ItemStackOutput(recipe.output)); - this.recipe = recipe; - } - - @Override - public void apply() { - RecipeHandler.addMetallurgicInfuserRecipe(recipe.type, recipe.amount, recipe.input, recipe.output); - - } - } - - private static class InfuserRecipe { - public ItemStack output; - public ItemStack input; - public int amount; - public InfuseType type; - - public InfuserRecipe(ItemStack output, ItemStack input, int amount, InfuseType type) { - this.output = output; - this.input = input; - this.amount = amount; - this.type = type; - } - - } - - @ZenMethod - public static void removeRecipe(IItemStack output) { - MineTweakerAPI.apply(new Remove(InputHelper.toStack(output))); - } - - private static class Remove extends BaseMapRemoval { - - public Remove(ItemStack stack) { - super("Metallurgic Infuser", Recipe.METALLURGIC_INFUSER.get(), stack); - } - - @SuppressWarnings("unchecked") - @Override - public void apply() { - List toRemove = new ArrayList(); - for (InfusionInput stack : (Set) Recipe.METALLURGIC_INFUSER.get().keySet()) { - MetallurgicInfuserRecipe recipe = (MetallurgicInfuserRecipe) Recipe.METALLURGIC_INFUSER.get().get(stack); - if (recipe.getOutput().output.isItemEqual((ItemStack) this.stack)) { - toRemove.add(stack); - } - } - for (InfusionInput stack : toRemove) { - Recipe.METALLURGIC_INFUSER.get().remove(stack); - } - } - - } -} diff --git a/src/main/java/modtweaker2/mods/mekanism/util/AddMekanismRecipe.java b/src/main/java/modtweaker2/mods/mekanism/util/AddMekanismRecipe.java index 92d3ad8..148c948 100644 --- a/src/main/java/modtweaker2/mods/mekanism/util/AddMekanismRecipe.java +++ b/src/main/java/modtweaker2/mods/mekanism/util/AddMekanismRecipe.java @@ -1,67 +1,43 @@ package modtweaker2.mods.mekanism.util; import java.util.Map; +import java.util.Map.Entry; -import mekanism.api.gas.GasStack; -import mekanism.common.recipe.inputs.ChemicalPairInput; -import mekanism.common.recipe.inputs.InfusionInput; -import mekanism.common.recipe.machines.PressurizedRecipe; -import mekanism.common.recipe.outputs.ChanceOutput; -import mekanism.common.recipe.outputs.ChemicalPairOutput; -import mekanism.common.recipe.outputs.PressurizedOutput; -import modtweaker2.mods.mekanism.Mekanism; +import mekanism.common.recipe.inputs.MachineInput; +import mekanism.common.recipe.machines.CombinerRecipe; +import mekanism.common.recipe.machines.CrusherRecipe; +import mekanism.common.recipe.machines.CrystallizerRecipe; +import mekanism.common.recipe.machines.MachineRecipe; +import mekanism.common.recipe.outputs.ItemStackOutput; +import mekanism.common.recipe.outputs.MachineOutput; +import modtweaker2.helpers.LogHelper; import modtweaker2.utils.BaseMapAddition; -import net.minecraft.item.ItemStack; -import net.minecraftforge.fluids.FluidStack; -public class AddMekanismRecipe extends BaseMapAddition { - public AddMekanismRecipe(String str, Map map, Object key, Object recipe) { - super(str.toLowerCase(), map, key, recipe); +@SuppressWarnings("rawtypes") +public class AddMekanismRecipe extends BaseMapAddition { + public AddMekanismRecipe(String name, Map map, Map recipes) { + super(name, map, recipes); } - - @Override - public String describe() { - if (Mekanism.v7) { - if (recipe instanceof ItemStack) - return "Adding " + description + " Recipe for : " + ((ItemStack) recipe).getDisplayName(); - else if (recipe instanceof FluidStack) - return "Adding " + description + " Recipe for : " + ((FluidStack) recipe).getFluid().getLocalizedName(); - else if (recipe instanceof ChemicalPairInput) - return "Adding " + description + " Recipe for : " + ((ChemicalPairInput) recipe).leftGas.getGas().getLocalizedName(); - else if (recipe instanceof ChanceOutput) - return "Adding " + description + " Recipe for : " + ((ChanceOutput) recipe).primaryOutput.getDisplayName(); - else if (recipe instanceof GasStack) - return "Adding " + description + " Recipe for : " + ((GasStack) recipe).getGas().getLocalizedName(); - else if (recipe instanceof PressurizedRecipe) - return "Adding " + description + " Recipe for : " + ((PressurizedRecipe) recipe).getOutput().getItemOutput().getDisplayName(); - else if (recipe instanceof InfusionInput) - return "Adding " + description + " Recipe for : " + ((InfusionInput) recipe).inputStack.getDisplayName(); - else - return super.getRecipeInfo(); - } - return ""; + + public AddMekanismRecipe(String name, Map map, MachineRecipe recipe) { + super(name, map); + + recipes.put(recipe.getInput(), recipe); } - + @Override - public String describeUndo() { - if (Mekanism.isV7()) { - if (recipe instanceof ItemStack) - return "Removing " + description + " Recipe for : " + ((ItemStack) recipe).getDisplayName(); - else if (recipe instanceof FluidStack) - return "Removing " + description + " Recipe for : " + ((FluidStack) recipe).getFluid().getLocalizedName(); - else if (recipe instanceof ChemicalPairOutput) - return "Removing " + description + " Recipe for : " + ((ChemicalPairOutput) recipe).leftGas.getGas().getLocalizedName(); - else if (recipe instanceof ChanceOutput) - return "Removing " + description + " Recipe for : " + ((ChanceOutput) recipe).primaryOutput.getDisplayName(); - else if (recipe instanceof GasStack) - return "Removing " + description + " Recipe for : " + ((GasStack) recipe).getGas().getLocalizedName(); - else if (recipe instanceof PressurizedOutput) - return "Removing " + description + " Recipe for : " + ((PressurizedOutput) recipe).getItemOutput().getDisplayName(); - else if (recipe instanceof InfusionInput) - return "Removing " + description + " Recipe for : " + ((InfusionInput) recipe).inputStack.getDisplayName(); - else - return super.getRecipeInfo(); - } - return ""; + protected String getRecipeInfo(Entry recipe) { + MachineRecipe machineRecipe = recipe.getValue(); + MachineOutput output = recipe.getValue().recipeOutput; + + if(machineRecipe instanceof CombinerRecipe) { + return LogHelper.getStackDescription(((ItemStackOutput)output).output); + } else if (machineRecipe instanceof CrusherRecipe) { + return LogHelper.getStackDescription(((ItemStackOutput)output).output); + } else if (machineRecipe instanceof CrystallizerRecipe) { + return LogHelper.getStackDescription(((ItemStackOutput)output).output); + } + + return null; } } diff --git a/src/main/java/modtweaker2/mods/mekanism/util/RemoveMekanismRecipe.java b/src/main/java/modtweaker2/mods/mekanism/util/RemoveMekanismRecipe.java index 7b83eb3..d77e2dd 100644 --- a/src/main/java/modtweaker2/mods/mekanism/util/RemoveMekanismRecipe.java +++ b/src/main/java/modtweaker2/mods/mekanism/util/RemoveMekanismRecipe.java @@ -1,150 +1,37 @@ package modtweaker2.mods.mekanism.util; -import static modtweaker2.helpers.StackHelper.areEqual; - -import java.util.Iterator; import java.util.Map; +import java.util.Map.Entry; -import mekanism.api.gas.GasStack; -import mekanism.common.recipe.inputs.InfusionInput; -import mekanism.common.recipe.inputs.PressurizedInput; -import mekanism.common.recipe.machines.PressurizedRecipe; -import mekanism.common.recipe.outputs.ChanceOutput; -import mekanism.common.recipe.outputs.ChemicalPairOutput; -import mekanism.common.recipe.outputs.PressurizedOutput; +import mekanism.common.recipe.inputs.MachineInput; +import mekanism.common.recipe.machines.CombinerRecipe; +import mekanism.common.recipe.machines.CrusherRecipe; +import mekanism.common.recipe.machines.CrystallizerRecipe; +import mekanism.common.recipe.machines.MachineRecipe; +import mekanism.common.recipe.outputs.ItemStackOutput; +import mekanism.common.recipe.outputs.MachineOutput; +import modtweaker2.helpers.LogHelper; import modtweaker2.utils.BaseMapRemoval; -import net.minecraft.item.ItemStack; -import net.minecraftforge.fluids.FluidStack; -public class RemoveMekanismRecipe extends BaseMapRemoval { - private Object tmp; - - public RemoveMekanismRecipe(String string, Map map, Object key) { - super(string.toLowerCase().replace('_', ' '), map, key, null); - this.tmp = key; - } - - @Override - public void apply() { - try { - Iterator it = map.entrySet().iterator(); - - while (it.hasNext()) { - Map.Entry pairs = (Map.Entry) it.next(); - if (pairs != null) { - Object value = pairs.getValue(); - if (value != null && tmp != null) { - if (tmp instanceof ItemStack && value instanceof ItemStack) { - if (areEqual(((ItemStack) tmp), (ItemStack) value)) { - key = pairs.getKey(); - break; - } - } - - if (tmp instanceof FluidStack && value instanceof FluidStack) { - if (((FluidStack) tmp).isFluidEqual((FluidStack) value)) { - key = pairs.getKey(); - break; - } - } - - if (tmp instanceof GasStack && value instanceof GasStack) { - if (((GasStack) tmp).isGasEqual((GasStack) value)) { - key = pairs.getKey(); - break; - } - } - - if (tmp instanceof ItemStack && value instanceof InfusionInput) { - if (areEqual(((ItemStack) tmp), ((InfusionInput) value).inputStack)) { - key = pairs.getKey(); - break; - } - } - - if (tmp instanceof ChemicalPairOutput && value instanceof ChemicalPairOutput) { - ChemicalPairOutput par1 = (ChemicalPairOutput) tmp; - ChemicalPairOutput par2 = (ChemicalPairOutput) value; - if (par1.leftGas.isGasEqual(par2.leftGas) && par1.rightGas.isGasEqual(par2.rightGas)) { - key = pairs.getKey(); - break; - } - } - - if (tmp instanceof ChanceOutput && value instanceof ChanceOutput) { - ChanceOutput par1 = (ChanceOutput) tmp; - ChanceOutput par2 = (ChanceOutput) value; - if (areEqual(par1.primaryOutput, par2.primaryOutput)) { - if (par1.secondaryOutput == null || (par1.secondaryOutput != null && par2.secondaryOutput != null && areEqual(par1.secondaryOutput, par2.secondaryOutput))) { - key = pairs.getKey(); - break; - } - } - } - - if (tmp instanceof PressurizedOutput && value instanceof PressurizedRecipe) { - PressurizedOutput par1 = (PressurizedOutput) tmp; - PressurizedOutput par2 = ((PressurizedRecipe) value).recipeOutput; - if (areEqual(par1.getItemOutput(), (par2.getItemOutput()))) { - if (par1.getGasOutput().isGasEqual(par2.getGasOutput())) { - key = pairs.getKey(); - break; - - } - } - } - } - - it.remove(); - } - } - - recipe = map.get(key); - - } catch (Exception e) { - e.printStackTrace(); - } - - map.remove(key); - } - - @Override - public String describe() { - if (tmp instanceof ItemStack) - return "Removing " + description + " Recipe for : " + ((ItemStack) tmp).getDisplayName(); - else if (tmp instanceof FluidStack) - return "Removing " + description + " Recipe for : " + ((FluidStack) tmp).getFluid().getLocalizedName(); - else if (tmp instanceof ChemicalPairOutput) - return "Removing " + description + " Recipe for : " + ((ChemicalPairOutput) tmp).leftGas.getGas().getLocalizedName(); - else if (tmp instanceof ChanceOutput) - return "Removing " + description + " Recipe for : " + ((ChanceOutput) tmp).primaryOutput.getDisplayName(); - else if (tmp instanceof GasStack) - return "Removing " + description + " Recipe for : " + ((GasStack) tmp).getGas().getLocalizedName(); - else if (tmp instanceof PressurizedInput) - return "Removing " + description + " Recipe for : " + ((PressurizedInput) tmp).getSolid().getDisplayName(); - else if (tmp instanceof InfusionInput) - return "Removing " + description + " Recipe for : " + ((InfusionInput) tmp).inputStack.getDisplayName(); - else - return super.getRecipeInfo(); - } - - @Override - public String describeUndo() { - if (tmp instanceof ItemStack) - return "Restoring " + description + " Recipe for : " + ((ItemStack) tmp).getDisplayName(); - else if (tmp instanceof FluidStack) - return "Restoring " + description + " Recipe for : " + ((FluidStack) tmp).getFluid().getLocalizedName(); - else if (tmp instanceof ChemicalPairOutput) - return "Restoring " + description + " Recipe for : " + ((ChemicalPairOutput) tmp).leftGas.getGas().getLocalizedName(); - else if (tmp instanceof ChanceOutput) - return "Restoring " + description + " Recipe for : " + ((ChanceOutput) tmp).primaryOutput.getDisplayName(); - else if (tmp instanceof GasStack) - return "Restoring " + description + " Recipe for : " + ((GasStack) tmp).getGas().getLocalizedName(); - else if (tmp instanceof PressurizedInput) - return "Restoring " + description + " Recipe for : " + ((PressurizedInput) tmp).getSolid().getDisplayName(); - else if (tmp instanceof InfusionInput) - return "Restoring " + description + " Recipe for : " + ((InfusionInput) tmp).inputStack.getDisplayName(); - else - return super.getRecipeInfo(); +@SuppressWarnings("rawtypes") +public class RemoveMekanismRecipe extends BaseMapRemoval { + public RemoveMekanismRecipe(String name, Map map, Map recipes) { + super(name, map, recipes); } + + @Override + protected String getRecipeInfo(Entry recipe) { + MachineRecipe machineRecipe = recipe.getValue(); + MachineOutput output = recipe.getValue().recipeOutput; + + if(machineRecipe instanceof CombinerRecipe) { + return LogHelper.getStackDescription(((ItemStackOutput)output).output); + } else if (machineRecipe instanceof CrusherRecipe) { + return LogHelper.getStackDescription(((ItemStackOutput)output).output); + } else if (machineRecipe instanceof CrystallizerRecipe) { + return LogHelper.getStackDescription(((ItemStackOutput)output).output); + } + + return null; + } } diff --git a/src/main/java/modtweaker2/mods/metallurgy/handlers/Alloyer.java b/src/main/java/modtweaker2/mods/metallurgy/handlers/Alloyer.java index dc1587f..35db6b3 100644 --- a/src/main/java/modtweaker2/mods/metallurgy/handlers/Alloyer.java +++ b/src/main/java/modtweaker2/mods/metallurgy/handlers/Alloyer.java @@ -1,13 +1,19 @@ package modtweaker2.mods.metallurgy.handlers; +import static modtweaker2.helpers.InputHelper.toIItemStack; import static modtweaker2.helpers.InputHelper.toStack; -import static modtweaker2.helpers.StackHelper.areEqual; +import static modtweaker2.helpers.StackHelper.matches; + +import java.util.LinkedList; +import java.util.List; + import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; +import modtweaker2.helpers.LogHelper; import modtweaker2.mods.metallurgy.MetallurgyHelper; import modtweaker2.utils.BaseListAddition; import modtweaker2.utils.BaseListRemoval; -import net.minecraft.item.ItemStack; import stanhebben.zenscript.annotations.ZenClass; import stanhebben.zenscript.annotations.ZenMethod; @@ -15,52 +21,50 @@ import com.teammetallurgy.metallurgy.recipes.AlloyerRecipes.AlloyRecipe; @ZenClass("mods.metallurgy.Alloyer") public class Alloyer { - //Adding a Metallurgy Alloyer recipe + + public static final String name = "Metallurgy Alloyer"; + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + @ZenMethod public static void addRecipe(IItemStack first, IItemStack base, IItemStack result) { MineTweakerAPI.apply(new Add(MetallurgyHelper.getAlloyRecipe(toStack(first), toStack(base), toStack(result)))); } - //Passes the list to the base list implementation, and adds the recipe - private static class Add extends BaseListAddition { + private static class Add extends BaseListAddition { public Add(AlloyRecipe recipe) { - super("Metallurgy Alloyer", MetallurgyHelper.alloyerRecipes, recipe); + super(Alloyer.name, MetallurgyHelper.alloyerRecipes); + recipes.add(recipe); } @Override - public String getRecipeInfo() { - return ((AlloyRecipe) recipe).getCraftingResult().getDisplayName(); + public String getRecipeInfo(AlloyRecipe recipe) { + return LogHelper.getStackDescription(recipe.getCraftingResult()); } } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - //Removing a Metallurgy Alloyer recipe @ZenMethod - public static void removeRecipe(IItemStack output) { - MineTweakerAPI.apply(new Remove(toStack(output))); + public static void removeRecipe(IIngredient output) { + List recipes = new LinkedList(); + + for (AlloyRecipe r : MetallurgyHelper.alloyerRecipes) { + if (r != null && r.getCraftingResult() != null && matches(output, toIItemStack(r.getCraftingResult()))) { + recipes.add(r); + } + } + MineTweakerAPI.apply(new Remove(recipes)); } - //Removes a recipe, apply is never the same for anything, so will always need to override it - private static class Remove extends BaseListRemoval { - public Remove(ItemStack stack) { - super("Metallurgy Alloyer", MetallurgyHelper.alloyerRecipes, stack); - } - - //Loops through the registry, to find the item that matches, saves that recipe then removes it - @Override - public void apply() { - for (AlloyRecipe r : MetallurgyHelper.alloyerRecipes) { - if (r.getCraftingResult() != null && areEqual(r.getCraftingResult(), stack)) { - recipes.add(r); - } - } - super.apply(); + private static class Remove extends BaseListRemoval { + public Remove(List recipes) { + super(Alloyer.name, MetallurgyHelper.alloyerRecipes, recipes); } @Override - public String getRecipeInfo() { - return stack.getDisplayName(); + public String getRecipeInfo(AlloyRecipe recipe) { + return LogHelper.getStackDescription(recipe.getCraftingResult()); } } } diff --git a/src/main/java/modtweaker2/mods/metallurgy/handlers/Crusher.java b/src/main/java/modtweaker2/mods/metallurgy/handlers/Crusher.java index 9fda247..4ba5a7c 100644 --- a/src/main/java/modtweaker2/mods/metallurgy/handlers/Crusher.java +++ b/src/main/java/modtweaker2/mods/metallurgy/handlers/Crusher.java @@ -2,8 +2,12 @@ package modtweaker2.mods.metallurgy.handlers; import static modtweaker2.helpers.InputHelper.toStack; import static modtweaker2.helpers.StackHelper.areEqual; + +import java.util.Map.Entry; + import minetweaker.MineTweakerAPI; import minetweaker.api.item.IItemStack; +import modtweaker2.helpers.LogHelper; import modtweaker2.mods.metallurgy.MetallurgyHelper; import modtweaker2.utils.BaseMapAddition; import modtweaker2.utils.BaseMapRemoval; @@ -19,24 +23,34 @@ public class Crusher { MineTweakerAPI.apply(new Add(toStack(input), toStack(output))); } - private static class Add extends BaseMapAddition { + private static class Add extends BaseMapAddition { private final ItemStack stack; public Add(ItemStack input, ItemStack output) { - super("Metallurgy Crusher", MetallurgyHelper.crusherMetaList, MetallurgyHelper.getCrusherKey(input), output); + super("Metallurgy Crusher", MetallurgyHelper.crusherMetaList); this.stack = input; + recipes.put(MetallurgyHelper.getCrusherKey(input), output); } @Override public void apply() { super.apply(); - addToInput(stack, (ItemStack) recipe); + for(Entry recipe : successful.entrySet()) { + addToInput(stack, recipe.getValue()); + } } @Override public void undo() { super.undo(); - removeFromInput(stack, (ItemStack) recipe); + for(Entry recipe : successful.entrySet()) { + removeFromInput(stack, recipe.getValue()); + } + } + + @Override + protected String getRecipeInfo(Entry recipe) { + return LogHelper.getStackDescription(recipe.getValue()); } } @@ -48,26 +62,35 @@ public class Crusher { MineTweakerAPI.apply(new Remove(toStack(input))); } - private static class Remove extends BaseMapRemoval { + private static class Remove extends BaseMapRemoval { + private ItemStack stack; + public Remove(ItemStack stack) { - super("Metallurgy Crusher", MetallurgyHelper.crusherMetaList, MetallurgyHelper.getCrusherKey(stack), stack); - } - - @Override - public String getRecipeInfo() { - return ((ItemStack) stack).getDisplayName(); + super("Metallurgy Crusher", MetallurgyHelper.crusherMetaList); + + map.put(MetallurgyHelper.getCrusherKey(stack), MetallurgyHelper.crusherMetaList.get(MetallurgyHelper.getCrusherKey(stack))); + this.stack = stack; } @Override public void apply() { super.apply(); - removeFromInput(((ItemStack) stack), (ItemStack) recipe); + for(Entry recipe : successful.entrySet()) { + removeFromInput(stack, recipe.getValue()); + } } @Override public void undo() { super.undo(); - addToInput(((ItemStack) stack), (ItemStack) recipe); + for(Entry recipe : successful.entrySet()) { + addToInput(stack, recipe.getValue()); + } + } + + @Override + protected String getRecipeInfo(Entry recipe) { + return LogHelper.getStackDescription(recipe.getValue()); } } diff --git a/src/main/java/modtweaker2/mods/pneumaticcraft/handlers/Assembly.java b/src/main/java/modtweaker2/mods/pneumaticcraft/handlers/Assembly.java index 29d8947..529be79 100644 --- a/src/main/java/modtweaker2/mods/pneumaticcraft/handlers/Assembly.java +++ b/src/main/java/modtweaker2/mods/pneumaticcraft/handlers/Assembly.java @@ -1,12 +1,17 @@ package modtweaker2.mods.pneumaticcraft.handlers; +import static modtweaker2.helpers.InputHelper.toIItemStack; import static modtweaker2.helpers.InputHelper.toStack; import static modtweaker2.helpers.StackHelper.areEqual; +import static modtweaker2.helpers.StackHelper.matches; +import java.util.LinkedList; import java.util.List; import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; +import modtweaker2.helpers.LogHelper; import modtweaker2.utils.BaseListAddition; import modtweaker2.utils.BaseListRemoval; import net.minecraft.item.ItemStack; @@ -16,68 +21,97 @@ import stanhebben.zenscript.annotations.ZenMethod; @ZenClass("mods.pneumaticcraft.Assembly") public class Assembly { + + public static final String name = "PneumaticCraft Assembly"; + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + @ZenMethod public static void addDrillRecipe(IItemStack input, IItemStack output) { - MineTweakerAPI.apply(new Add(new AssemblyRecipe(toStack(input), toStack(output)), AssemblyRecipe.drillRecipes)); + addRecipe(input, output, AssemblyRecipe.drillRecipes); } @ZenMethod public static void addLaserRecipe(IItemStack input, IItemStack output) { - MineTweakerAPI.apply(new Add(new AssemblyRecipe(toStack(input), toStack(output)), AssemblyRecipe.laserRecipes)); + addRecipe(input, output, AssemblyRecipe.laserRecipes); } @ZenMethod public static void addLaserDrillRecipe(IItemStack input, IItemStack output) { - MineTweakerAPI.apply(new Add(new AssemblyRecipe(toStack(input), toStack(output)), AssemblyRecipe.drillLaserRecipes)); + addRecipe(input, output, AssemblyRecipe.drillLaserRecipes); + } + + public static void addRecipe(IItemStack input, IItemStack output, List list) { + if(input == null || output == null) { + LogHelper.logError(String.format("Required parameters missing for %s Recipe.", name)); + return; + } + + ItemStack in = toStack(input); + + for(AssemblyRecipe recipe : list) { + if(areEqual(recipe.getInput(), in)) { + LogHelper.logWarning(String.format("Duplicate %s Recipe found for %s. Command ignored!", name, LogHelper.getStackDescription(in))); + return; + } + } + + MineTweakerAPI.apply(new Add(new AssemblyRecipe(toStack(input), toStack(output)), list)); } - private static class Add extends BaseListAddition { - public Add(AssemblyRecipe recipe, List list) { - super("Pneumaticraft Assembly", list, recipe); + private static class Add extends BaseListAddition { + public Add(AssemblyRecipe recipe, List list) { + super(Assembly.name, list); + recipes.add(recipe); } @Override - public String getRecipeInfo() { - return ((AssemblyRecipe) recipe).getOutput().getDisplayName(); + public String getRecipeInfo(AssemblyRecipe recipe) { + return LogHelper.getStackDescription(recipe.getOutput()); } } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @ZenMethod - public static void removeDrillRecipe(IItemStack output) { - MineTweakerAPI.apply(new Remove(toStack(output), AssemblyRecipe.drillRecipes)); + public static void removeDrillRecipe(IIngredient output) { + removeRecipe(AssemblyRecipe.drillRecipes, output); } @ZenMethod - public static void removeLaserRecipe(IItemStack output) { - MineTweakerAPI.apply(new Remove(toStack(output), AssemblyRecipe.laserRecipes)); + public static void removeLaserRecipe(IIngredient output) { + removeRecipe(AssemblyRecipe.laserRecipes, output); } @ZenMethod - public static void removeLaserDrillRecipe(IItemStack output) { - MineTweakerAPI.apply(new Remove(toStack(output), AssemblyRecipe.drillLaserRecipes)); + public static void removeLaserDrillRecipe(IIngredient output) { + removeRecipe(AssemblyRecipe.drillLaserRecipes, output); } - - private static class Remove extends BaseListRemoval { - public Remove(ItemStack stack, List list) { - super("Pneumaticraft Assembly", list, stack); - } - - @Override - public void apply() { - for (AssemblyRecipe r : (List) list) { - if (r.getOutput() != null && areEqual(r.getOutput(), stack)) { - recipes.add(r); - } + + public static void removeRecipe(List list, IIngredient output) { + List recipes = new LinkedList(); + + for (AssemblyRecipe r : list) { + if (r.getOutput() != null && matches(output, toIItemStack(r.getOutput()))) { + recipes.add(r); } + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new Remove(list, recipes)); + } else { + LogHelper.logWarning(String.format("No %s Recipe found for %s. Command ignored!", name, output.toString())); + } + } - super.apply(); + private static class Remove extends BaseListRemoval { + public Remove(List list, List recipes) { + super(Assembly.name, list, recipes); } @Override - public String getRecipeInfo() { - return stack.getDisplayName(); + public String getRecipeInfo(AssemblyRecipe recipe) { + return LogHelper.getStackDescription(recipe.getOutput()); } } } diff --git a/src/main/java/modtweaker2/mods/pneumaticcraft/handlers/Pressure.java b/src/main/java/modtweaker2/mods/pneumaticcraft/handlers/Pressure.java index 440a225..615fdd0 100644 --- a/src/main/java/modtweaker2/mods/pneumaticcraft/handlers/Pressure.java +++ b/src/main/java/modtweaker2/mods/pneumaticcraft/handlers/Pressure.java @@ -1,9 +1,16 @@ package modtweaker2.mods.pneumaticcraft.handlers; +import static modtweaker2.helpers.InputHelper.toIItemStack; import static modtweaker2.helpers.InputHelper.toStacks; -import static modtweaker2.helpers.StackHelper.areEqual; +import static modtweaker2.helpers.StackHelper.matches; + +import java.util.LinkedList; +import java.util.List; + import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; +import modtweaker2.helpers.LogHelper; import modtweaker2.utils.BaseListAddition; import modtweaker2.utils.BaseListRemoval; import net.minecraft.item.ItemStack; @@ -13,48 +20,46 @@ import stanhebben.zenscript.annotations.ZenMethod; @ZenClass("mods.pneumaticcraft.Pressure") public class Pressure { + + public static final String name = "PneumaticCraft Pressure Chamber"; + @ZenMethod public static void addRecipe(IItemStack[] input, double pressure, IItemStack[] output, boolean asBlock) { + if(input == null || output == null) { + LogHelper.logError(String.format("Required parameters missing for %s Recipe.", name)); + return; + } + MineTweakerAPI.apply(new Add(new PressureChamberRecipe(toStacks(input), (float) pressure, toStacks(output), asBlock))); } - private static class Add extends BaseListAddition { - private ItemStack stack; - + private static class Add extends BaseListAddition { public Add(PressureChamberRecipe recipe) { - super("Pneumaticraft Pressure Chamber", PressureChamberRecipe.chamberRecipes, recipe); - this.stack = recipe.output[0]; + super(Pressure.name, PressureChamberRecipe.chamberRecipes); + recipes.add(recipe); } @Override - public String getRecipeInfo() { - return stack.getDisplayName(); + public String getRecipeInfo(PressureChamberRecipe recipe) { + return LogHelper.getStackDescription(recipe.output[0]); } } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @ZenMethod - public static void removeRecipe(IItemStack[] output) { - MineTweakerAPI.apply(new Remove(toStacks(output))); - } - - private static class Remove extends BaseListRemoval { - private final ItemStack[] stacks; - - public Remove(ItemStack[] stacks) { - super("Pneumaticraft Pressure Chamber", PressureChamberRecipe.chamberRecipes, stacks[0]); - this.stacks = stacks; - } - - @Override - public void apply() { - for (PressureChamberRecipe r : PressureChamberRecipe.chamberRecipes) { + public static void removeRecipe(IIngredient[] output) { + List recipes = new LinkedList(); + + for (PressureChamberRecipe r : PressureChamberRecipe.chamberRecipes) { + if(r != null) { boolean matches = true; - for (int i = 0; i < stacks.length; i++) { - if (!areEqual(stacks[i], r.output[i])) { // possible IndexOutOfBoundsException on r.output[i] - matches = false; - break; + for (IIngredient ingredient : output) { + for(ItemStack stack : r.output) { + if (!matches(ingredient, toIItemStack(stack))) { + matches = false; + break; + } } } @@ -62,12 +67,19 @@ public class Pressure { recipes.add(r); } } - super.apply(); } + MineTweakerAPI.apply(new Remove(recipes)); + } + + private static class Remove extends BaseListRemoval { + public Remove(List recipes) { + super(Pressure.name, PressureChamberRecipe.chamberRecipes, recipes); + } + @Override - public String getRecipeInfo() { - return stack.getDisplayName(); + public String getRecipeInfo(PressureChamberRecipe recipe) { + return LogHelper.getStackDescription(recipe.output[0]); } } } diff --git a/src/main/java/modtweaker2/mods/railcraft/RailcraftHelper.java b/src/main/java/modtweaker2/mods/railcraft/RailcraftHelper.java index 9d048b1..a4a3c31 100644 --- a/src/main/java/modtweaker2/mods/railcraft/RailcraftHelper.java +++ b/src/main/java/modtweaker2/mods/railcraft/RailcraftHelper.java @@ -1,12 +1,15 @@ package modtweaker2.mods.railcraft; -import java.lang.reflect.Constructor; import java.util.List; import mods.railcraft.api.crafting.IBlastFurnaceRecipe; import mods.railcraft.api.crafting.ICokeOvenRecipe; import mods.railcraft.api.crafting.IRockCrusherRecipe; import mods.railcraft.api.crafting.RailcraftCraftingManager; +import mods.railcraft.common.util.crafting.BlastFurnaceCraftingManager.BlastFurnaceRecipe; +import mods.railcraft.common.util.crafting.CokeOvenCraftingManager.CokeOvenRecipe; +import mods.railcraft.common.util.crafting.RockCrusherCraftingManager.CrusherRecipe; +import modtweaker2.helpers.ReflectionHelper; import net.minecraft.item.ItemStack; import net.minecraft.item.crafting.IRecipe; import net.minecraftforge.fluids.FluidStack; @@ -28,35 +31,28 @@ public class RailcraftHelper { private RailcraftHelper() {} public static IBlastFurnaceRecipe getBlastFurnaceRecipe(ItemStack input, boolean matchDamage, boolean matchNBT, int cookTime, ItemStack output) { - try { - Class clazz = Class.forName("mods.railcraft.common.util.crafting.BlastFurnaceCraftingManager$BlastFurnaceRecipe"); - Constructor constructor = clazz.getDeclaredConstructor(ItemStack.class, boolean.class, boolean.class, int.class, ItemStack.class); - constructor.setAccessible(true); - return (IBlastFurnaceRecipe) constructor.newInstance(input, matchDamage, matchNBT, cookTime, output); - } catch (Exception e) { - throw new NullPointerException("Failed to instantiate BlastFurnaceRecipe"); - } + return ReflectionHelper.getInstance(ReflectionHelper.getConstructor(BlastFurnaceRecipe.class,ItemStack.class, boolean.class, boolean.class, int.class, ItemStack.class), + input, + matchDamage, + matchNBT, + cookTime, + output); } public static ICokeOvenRecipe getCokeOvenRecipe(ItemStack input, boolean matchDamage, boolean matchNBT, ItemStack output, FluidStack fluidOutput, int cookTime) { - try { - Class clazz = Class.forName("mods.railcraft.common.util.crafting.CokeOvenCraftingManager$CokeOvenRecipe"); - Constructor constructor = clazz.getDeclaredConstructor(ItemStack.class, boolean.class, boolean.class, ItemStack.class, FluidStack.class, int.class); - constructor.setAccessible(true); - return (ICokeOvenRecipe) constructor.newInstance(input, matchDamage, matchNBT, output, fluidOutput, cookTime); - } catch (Exception e) { - throw new NullPointerException("Failed to instantiate CokeOvenRecipe"); - } + return ReflectionHelper.getInstance(ReflectionHelper.getConstructor(CokeOvenRecipe.class, ItemStack.class, boolean.class, boolean.class, ItemStack.class, FluidStack.class, int.class), + input, + matchDamage, + matchNBT, + output, + fluidOutput, + cookTime); } public static IRockCrusherRecipe getRockCrusherRecipe(ItemStack stack, boolean matchDamage, boolean matchNBT) { - try { - Class clazz = Class.forName("mods.railcraft.common.util.crafting.RockCrusherCraftingManager$CrusherRecipe"); - Constructor constructor = clazz.getDeclaredConstructor(ItemStack.class, boolean.class, boolean.class); - constructor.setAccessible(true); - return (IRockCrusherRecipe) constructor.newInstance(stack, matchDamage, matchNBT); - } catch (Exception e) { - throw new NullPointerException("Failed to instantiate CrusherRecipe"); - } + return ReflectionHelper.getInstance(ReflectionHelper.getConstructor(CrusherRecipe.class, ItemStack.class, boolean.class, boolean.class), + stack, + matchDamage, + matchNBT); } } diff --git a/src/main/java/modtweaker2/mods/railcraft/commands/RailcraftLogger.java b/src/main/java/modtweaker2/mods/railcraft/commands/RailcraftLogger.java new file mode 100644 index 0000000..9e2b33b --- /dev/null +++ b/src/main/java/modtweaker2/mods/railcraft/commands/RailcraftLogger.java @@ -0,0 +1,110 @@ +package modtweaker2.mods.railcraft.commands; + +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; +import java.util.Map.Entry; + +import minetweaker.MineTweakerAPI; +import minetweaker.MineTweakerImplementationAPI; +import minetweaker.api.player.IPlayer; +import minetweaker.api.server.ICommandFunction; +import mods.railcraft.api.crafting.IBlastFurnaceRecipe; +import mods.railcraft.api.crafting.ICokeOvenRecipe; +import mods.railcraft.api.crafting.IRockCrusherRecipe; +import mods.railcraft.api.crafting.RailcraftCraftingManager; +import modtweaker2.helpers.LogHelper; +import modtweaker2.helpers.ReflectionHelper; +import modtweaker2.helpers.StringHelper; +import modtweaker2.mods.railcraft.RailcraftHelper; +import net.minecraft.item.ItemStack; +import net.minecraft.item.crafting.IRecipe; + +public class RailcraftLogger implements ICommandFunction { + + private static final List validArguments = new LinkedList(); + + static { + validArguments.add("blastfurnace"); + validArguments.add("cokeoven"); + validArguments.add("rockcrusher"); + validArguments.add("rollingmachine"); + } + + @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("blastfurnace")) { + for(IBlastFurnaceRecipe recipe : RailcraftHelper.furnace) { + MineTweakerAPI.logCommand(String.format("mods.railcraft.BlastFurnace.addRecipe(%s, %s, %s, %d, %s);", + LogHelper.getStackDescription(recipe.getInput()), + ReflectionHelper.getObject(recipe, "matchDamage"), + ReflectionHelper.getObject(recipe, "matchNBT"), + recipe.getCookTime(), + LogHelper.getStackDescription(recipe.getOutput()))); + } + } + + if(args.isEmpty() || args.contains("cokeoven")) { + for(ICokeOvenRecipe recipe : RailcraftCraftingManager.cokeOven.getRecipes()) { + MineTweakerAPI.logCommand(String.format("mods.railcraft.CokeOven.addRecipe(%s, %s, %s, %s, %s, %d);", + LogHelper.getStackDescription(recipe.getInput()), + ReflectionHelper.getObject(recipe, "matchDamage"), + ReflectionHelper.getObject(recipe, "matchNBT"), + LogHelper.getStackDescription(recipe.getOutput()), + LogHelper.getStackDescription(recipe.getFluidOutput()), + recipe.getCookTime())); + } + } + + if(args.isEmpty() || args.contains("rockcrusher")) { + for(IRockCrusherRecipe recipe : RailcraftHelper.crusher) { + MineTweakerAPI.logCommand(String.format("mods.railcraft.RockCrusher.addRecipe(%s, %s, %s, %s);", + LogHelper.getStackDescription(recipe.getInput()), + ReflectionHelper.getObject(recipe, "matchDamage"), + ReflectionHelper.getObject(recipe, "matchNBT"), + getRockCrusherOutputDescription(recipe.getOutputs()))); + } + } + + if(args.isEmpty() || args.contains("rollingmachine")) { + for(IRecipe recipe : RailcraftHelper.rolling) { + MineTweakerAPI.logCommand(String.format("mods.railcraft.RollingMachine.addRecipe(%s, %s);", + LogHelper.getStackDescription(recipe.getRecipeOutput()), + LogHelper.getCraftingDescription(recipe))); + } + } + + if (player != null) { + player.sendChat(MineTweakerImplementationAPI.platform.getMessage("List generated; see minetweaker.log in your minecraft dir")); + } + } + } + + private Object getRockCrusherOutputDescription(List> outputs) { + if(outputs.isEmpty()) { + return "[], []"; + } + + StringBuilder sb = new StringBuilder(); + sb.append('['); + for(Entry entry : outputs) { + sb.append(LogHelper.getStackDescription(entry.getKey())).append(", "); + } + sb.setLength(sb.length() - 2); + sb.append("], ["); + for(Entry entry : outputs) { + sb.append(entry.getValue()).append(", "); + } + sb.setLength(sb.length() - 2); + sb.append("]"); + + return sb.toString(); + } +} diff --git a/src/main/java/modtweaker2/mods/railcraft/handlers/BlastFurnace.java b/src/main/java/modtweaker2/mods/railcraft/handlers/BlastFurnace.java index 4c5efa6..6fdea70 100644 --- a/src/main/java/modtweaker2/mods/railcraft/handlers/BlastFurnace.java +++ b/src/main/java/modtweaker2/mods/railcraft/handlers/BlastFurnace.java @@ -1,62 +1,72 @@ package modtweaker2.mods.railcraft.handlers; +import static modtweaker2.helpers.InputHelper.toIItemStack; import static modtweaker2.helpers.InputHelper.toStack; -import static modtweaker2.helpers.StackHelper.areEqual; +import static modtweaker2.helpers.StackHelper.matches; -import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; import mods.railcraft.api.crafting.IBlastFurnaceRecipe; -import mods.railcraft.api.crafting.RailcraftCraftingManager; +import modtweaker2.helpers.LogHelper; import modtweaker2.mods.railcraft.RailcraftHelper; import modtweaker2.utils.BaseListAddition; import modtweaker2.utils.BaseListRemoval; -import net.minecraft.item.ItemStack; import stanhebben.zenscript.annotations.ZenClass; import stanhebben.zenscript.annotations.ZenMethod; @ZenClass("mods.railcraft.BlastFurnace") public class BlastFurnace { + + public static final String name = "Railcraft Blast Furnace"; + @ZenMethod public static void addRecipe(IItemStack input, boolean matchDamage, boolean matchNBT, int cookTime, IItemStack output) { MineTweakerAPI.apply(new Add(RailcraftHelper.getBlastFurnaceRecipe(toStack(input), matchDamage, matchNBT, cookTime, toStack(output)))); } - private static class Add extends BaseListAddition { - public Add(IBlastFurnaceRecipe recipe) { - super("Blast Furnace", RailcraftHelper.furnace, recipe); + private static class Add extends BaseListAddition { + @SuppressWarnings("unchecked") + public Add(IBlastFurnaceRecipe recipe) { + super(BlastFurnace.name, (List)RailcraftHelper.furnace); + recipes.add(recipe); } @Override - public String getRecipeInfo() { - return ((IBlastFurnaceRecipe) recipe).getOutput().getDisplayName(); + public String getRecipeInfo(IBlastFurnaceRecipe recipe) { + return LogHelper.getStackDescription(recipe.getOutput()); } } @ZenMethod - public static void removeRecipe(IItemStack output) { - MineTweakerAPI.apply(new Remove(toStack(output))); + public static void removeRecipe(IIngredient output) { + List recipes = new LinkedList(); + + for (IBlastFurnaceRecipe r : RailcraftHelper.furnace) { + if (r != null && r.getOutput() != null && matches(output, toIItemStack(r.getOutput()))) { + recipes.add(r); + } + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new Remove(recipes)); + } else { + LogHelper.logWarning(String.format("No %s Recipe found for %s. Command ignored!", BlastFurnace.name, output.toString())); + } } - private static class Remove extends BaseListRemoval { - public Remove(ItemStack stack) { - super("Blast Furnace", RailcraftHelper.furnace, stack); + private static class Remove extends BaseListRemoval { + @SuppressWarnings("unchecked") + public Remove(List recipes) { + super(BlastFurnace.name, (List)RailcraftHelper.furnace, recipes); } - @Override - public void apply() { - for (IBlastFurnaceRecipe r : RailcraftHelper.furnace) { - if (r.getOutput() != null && stack.isItemEqual(r.getOutput())) { - recipes.add(r); - } - } - super.apply(); - } - - @Override - public String getRecipeInfo() { - return stack.getDisplayName(); - } + @Override + public String getRecipeInfo(IBlastFurnaceRecipe recipe) { + return LogHelper.getStackDescription(recipe.getOutput()); + } } } diff --git a/src/main/java/modtweaker2/mods/railcraft/handlers/CokeOven.java b/src/main/java/modtweaker2/mods/railcraft/handlers/CokeOven.java index 928f335..ea051d6 100644 --- a/src/main/java/modtweaker2/mods/railcraft/handlers/CokeOven.java +++ b/src/main/java/modtweaker2/mods/railcraft/handlers/CokeOven.java @@ -1,18 +1,24 @@ package modtweaker2.mods.railcraft.handlers; import static modtweaker2.helpers.InputHelper.toFluid; +import static modtweaker2.helpers.InputHelper.toIItemStack; import static modtweaker2.helpers.InputHelper.toStack; +import static modtweaker2.helpers.StackHelper.matches; + +import java.util.LinkedList; +import java.util.List; + import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; import minetweaker.api.liquid.ILiquidStack; -import minetweaker.mc1710.item.MCItemStack; import mods.railcraft.api.crafting.ICokeOvenRecipe; import mods.railcraft.api.crafting.RailcraftCraftingManager; import mods.railcraft.common.util.crafting.CokeOvenCraftingManager.CokeOvenRecipe; +import modtweaker2.helpers.LogHelper; import modtweaker2.mods.railcraft.RailcraftHelper; import modtweaker2.utils.BaseListAddition; import modtweaker2.utils.BaseListRemoval; -import net.minecraft.item.ItemStack; import net.minecraftforge.oredict.OreDictionary; import stanhebben.zenscript.annotations.ZenClass; import stanhebben.zenscript.annotations.ZenMethod; @@ -20,6 +26,8 @@ import stanhebben.zenscript.annotations.ZenMethod; @ZenClass("mods.railcraft.CokeOven") public class CokeOven { + public static final String name = "Railcraft Coke Oven"; + /** * Adds a recipe for the Coke Oven * @@ -74,43 +82,49 @@ public class CokeOven { MineTweakerAPI.apply(new Add( new CokeOvenRecipe(toStack(input), matchDamage, matchNBT, toStack(output), toFluid(fluidOutput), cookTime) )); } - private static class Add extends BaseListAddition { + private static class Add extends BaseListAddition { - public Add(CokeOvenRecipe recipe) { - super("Railcraft CokeOven", RailcraftCraftingManager.cokeOven.getRecipes(), recipe); + @SuppressWarnings("unchecked") + public Add(ICokeOvenRecipe recipe) { + super(CokeOven.name, (List)RailcraftCraftingManager.cokeOven.getRecipes()); + recipes.add(recipe); } + @Override - public String getRecipeInfo() { - return " " + new MCItemStack(((ICokeOvenRecipe) recipe).getInput()) + " (Input)"; + public String getRecipeInfo(ICokeOvenRecipe recipe) { + return LogHelper.getStackDescription(recipe.getInput()); } } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @ZenMethod - public static void removeRecipe(IItemStack output) { - MineTweakerAPI.apply(new Remove(toStack(output))); + public static void removeRecipe(IIngredient output) { + List recipes = new LinkedList(); + + for (ICokeOvenRecipe r : RailcraftHelper.oven) { + if (r.getOutput() != null && matches(output, toIItemStack(r.getOutput()))) { + recipes.add(r); + } + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new Remove(recipes)); + } else { + LogHelper.logWarning(String.format("No %s Recipe found for %s. Command ignored!", CokeOven.name, output.toString())); + } } - private static class Remove extends BaseListRemoval { - public Remove(ItemStack stack) { - super("Coke Oven", RailcraftHelper.oven, stack); - } - - @SuppressWarnings("unchecked") - @Override - public void apply() { - for (ICokeOvenRecipe r : RailcraftHelper.oven) { - if (r.getOutput() != null && r.getOutput().isItemEqual(stack) && ItemStack.areItemStackTagsEqual(r.getOutput(), stack)) { - recipes.add(r); - } - } - super.apply(); + private static class Remove extends BaseListRemoval { + + @SuppressWarnings("unchecked") + public Remove(List recipes) { + super(CokeOven.name, (List)RailcraftHelper.oven, recipes); } @Override - public String getRecipeInfo() { - return " " + new MCItemStack(stack) + " (Input)"; + public String getRecipeInfo(ICokeOvenRecipe recipe) { + return LogHelper.getStackDescription(recipe.getInput()); } } } diff --git a/src/main/java/modtweaker2/mods/railcraft/handlers/RockCrusher.java b/src/main/java/modtweaker2/mods/railcraft/handlers/RockCrusher.java index dafcf83..ebb1a9f 100644 --- a/src/main/java/modtweaker2/mods/railcraft/handlers/RockCrusher.java +++ b/src/main/java/modtweaker2/mods/railcraft/handlers/RockCrusher.java @@ -1,63 +1,82 @@ package modtweaker2.mods.railcraft.handlers; +import static modtweaker2.helpers.InputHelper.toIItemStack; import static modtweaker2.helpers.InputHelper.toStack; -import static modtweaker2.helpers.StackHelper.areEqual; +import static modtweaker2.helpers.StackHelper.matches; + +import java.util.LinkedList; +import java.util.List; + import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; import mods.railcraft.api.crafting.IRockCrusherRecipe; +import modtweaker2.helpers.LogHelper; import modtweaker2.mods.railcraft.RailcraftHelper; import modtweaker2.utils.BaseListAddition; import modtweaker2.utils.BaseListRemoval; -import net.minecraft.item.ItemStack; import stanhebben.zenscript.annotations.ZenClass; import stanhebben.zenscript.annotations.ZenMethod; @ZenClass("mods.railcraft.RockCrusher") public class RockCrusher { + + public static final String name = "Railcraft Rock Crusher"; + @ZenMethod public static void addRecipe(IItemStack input, boolean matchDamage, boolean matchNBT, IItemStack[] output, double[] chances) { IRockCrusherRecipe recipe = RailcraftHelper.getRockCrusherRecipe(toStack(input), matchDamage, matchNBT); - for (int i = 0; i < output.length; i++) + + for (int i = 0; i < output.length; i++) { recipe.addOutput(toStack(output[i]), (float) chances[i]); + } + MineTweakerAPI.apply(new Add(recipe)); } - private static class Add extends BaseListAddition { + private static class Add extends BaseListAddition { + + @SuppressWarnings("unchecked") public Add(IRockCrusherRecipe recipe) { - super("Rock Crusher", RailcraftHelper.crusher, recipe); + super(RockCrusher.name, (List)RailcraftHelper.crusher); + recipes.add(recipe); } @Override - public String getRecipeInfo() { - return ((IRockCrusherRecipe) recipe).getInput().getDisplayName(); + public String getRecipeInfo(IRockCrusherRecipe recipe) { + return LogHelper.getStackDescription(recipe.getInput()); } } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @ZenMethod - public static void removeRecipe(IItemStack input) { - MineTweakerAPI.apply(new Remove(toStack(input))); + public static void removeRecipe(IIngredient input) { + List recipes = new LinkedList(); + + for (IRockCrusherRecipe r : RailcraftHelper.crusher) { + if (r.getInput() != null && matches(input, toIItemStack(r.getInput()))) { + recipes.add(r); + } + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new Remove(recipes)); + } else { + LogHelper.logWarning(String.format("No %s Recipe found for %s. Command ignored!", RockCrusher.name, input.toString())); + } } - private static class Remove extends BaseListRemoval { - public Remove(ItemStack stack) { - super("Rock Crusher", RailcraftHelper.crusher, stack); + private static class Remove extends BaseListRemoval { + + @SuppressWarnings("unchecked") + public Remove(List recipes) { + super("Rock Crusher", (List)RailcraftHelper.crusher, recipes); } @Override - public void apply() { - for (IRockCrusherRecipe r : RailcraftHelper.crusher) { - if (r.getInput() != null && areEqual(r.getInput(), stack)) { - recipes.add(r); - } - } - super.apply(); - } - - @Override - public String getRecipeInfo() { - return stack.getDisplayName(); + public String getRecipeInfo(IRockCrusherRecipe recipe) { + return LogHelper.getStackDescription(recipe.getInput()); } } } diff --git a/src/main/java/modtweaker2/mods/railcraft/handlers/RollingMachine.java b/src/main/java/modtweaker2/mods/railcraft/handlers/RollingMachine.java index a561ed1..fa51768 100644 --- a/src/main/java/modtweaker2/mods/railcraft/handlers/RollingMachine.java +++ b/src/main/java/modtweaker2/mods/railcraft/handlers/RollingMachine.java @@ -3,15 +3,17 @@ package modtweaker2.mods.railcraft.handlers; import static modtweaker2.helpers.InputHelper.toObjects; import static modtweaker2.helpers.InputHelper.toShapedObjects; import static modtweaker2.helpers.InputHelper.toStack; + +import java.util.List; + import minetweaker.MineTweakerAPI; import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; -import mods.railcraft.api.crafting.RailcraftCraftingManager; -import modtweaker2.helpers.InputHelper; +import modtweaker2.helpers.LogHelper; import modtweaker2.mods.railcraft.RailcraftHelper; import modtweaker2.utils.BaseCraftingAddition; import modtweaker2.utils.BaseCraftingRemoval; -import net.minecraft.item.ItemStack; +import net.minecraft.item.crafting.IRecipe; import net.minecraftforge.oredict.ShapedOreRecipe; import net.minecraftforge.oredict.ShapelessOreRecipe; import stanhebben.zenscript.annotations.ZenClass; @@ -19,42 +21,42 @@ import stanhebben.zenscript.annotations.ZenMethod; @ZenClass("mods.railcraft.Rolling") public class RollingMachine { + + public static final String name = "Railcraft Rolling Machine"; + @ZenMethod public static void addShaped(IItemStack output, IIngredient[][] ingredients) { - MineTweakerAPI.apply(new Add(false, toStack(output), toShapedObjects(ingredients))); + MineTweakerAPI.apply(new Add(new ShapedOreRecipe(toStack(output), toShapedObjects(ingredients)))); } @ZenMethod public static void addShapeless(IItemStack output, IIngredient[] ingredients) { - MineTweakerAPI.apply(new Add(true, toStack(output), toObjects(ingredients))); + MineTweakerAPI.apply(new Add(new ShapelessOreRecipe(toStack(output), toObjects(ingredients)))); } private static class Add extends BaseCraftingAddition { - public Add(boolean shapeless, ItemStack output, Object... recipe) { - super("Rolling Machine", shapeless, RailcraftHelper.rolling, output, recipe); - } - - @Override - public void applyShaped() { - RailcraftHelper.rolling.add(new ShapedOreRecipe(output, recipe)); - } - - @Override - public void applyShapeless() { - RailcraftHelper.rolling.add(new ShapelessOreRecipe(output, recipe)); + public Add(IRecipe recipe) { + super(RollingMachine.name, RailcraftHelper.rolling); + recipes.add(recipe); } } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @ZenMethod - public static void removeRecipe(IItemStack output) { - MineTweakerAPI.apply(new Remove(toStack(output))); + public static void removeRecipe(IIngredient ingredient) { + List recipes = BaseCraftingRemoval.getRecipes(RailcraftHelper.rolling, ingredient); + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new Remove(recipes)); + } else { + LogHelper.logWarning(String.format("No %s Recipes found for %s. Command ignored!", RollingMachine.name, ingredient.toString())); + } } private static class Remove extends BaseCraftingRemoval { - public Remove(ItemStack stack) { - super("Rolling Machine", RailcraftHelper.rolling, stack); + public Remove(List recipes) { + super(RollingMachine.name, RailcraftHelper.rolling, recipes); } } } diff --git a/src/main/java/modtweaker2/mods/tconstruct/MaterialLogger.java b/src/main/java/modtweaker2/mods/tconstruct/MaterialLogger.java index 0f0d568..2f66b28 100644 --- a/src/main/java/modtweaker2/mods/tconstruct/MaterialLogger.java +++ b/src/main/java/modtweaker2/mods/tconstruct/MaterialLogger.java @@ -13,9 +13,9 @@ import tconstruct.library.TConstructRegistry; import tconstruct.library.tools.ToolMaterial; public class MaterialLogger implements ICommandFunction { - private static ArrayList materials = new ArrayList(); + private static ArrayList materials = new ArrayList(); static { - materials = new ArrayList(); + materials = new ArrayList(); for (Map.Entry entry : TConstructRegistry.toolMaterialStrings.entrySet()) { materials.add(entry.getKey()); } diff --git a/src/main/java/modtweaker2/mods/tconstruct/TConstructHelper.java b/src/main/java/modtweaker2/mods/tconstruct/TConstructHelper.java index 3202059..3362ed4 100644 --- a/src/main/java/modtweaker2/mods/tconstruct/TConstructHelper.java +++ b/src/main/java/modtweaker2/mods/tconstruct/TConstructHelper.java @@ -1,14 +1,15 @@ package modtweaker2.mods.tconstruct; -import java.lang.reflect.Constructor; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import mantle.utils.ItemMetaWrapper; +import modtweaker2.helpers.ReflectionHelper; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; +import net.minecraftforge.fluids.Fluid; import net.minecraftforge.fluids.FluidStack; import tconstruct.library.TConstructRegistry; import tconstruct.library.crafting.AlloyMix; @@ -20,13 +21,14 @@ import tconstruct.library.modifier.ItemModifier; import tconstruct.library.tools.ToolMaterial; public class TConstructHelper { - public static HashMap mappings = new HashMap(); + public static HashMap mappings = new HashMap(); public static List alloys = null; public static ArrayList basinCasting = null; public static ArrayList tableCasting = null; public static Map smeltingList = null; public static Map temperatureList = null; public static Map renderIndex = null; + public static Map fuelList = null; public static List modifiers = null; public static List modifiers_clone = null; @@ -39,12 +41,13 @@ public class TConstructHelper { basinCasting = TConstructRegistry.getBasinCasting().getCastingRecipes(); tableCasting = TConstructRegistry.getTableCasting().getCastingRecipes(); modifiers = ModifyBuilder.instance.itemModifiers; - modifiers_clone = new ArrayList(modifiers); + modifiers_clone = new ArrayList(modifiers); for (Map.Entry entry : TConstructRegistry.toolMaterials.entrySet()) { mappings.put(entry.getValue().materialName, entry.getKey()); } - + + fuelList = ReflectionHelper.getObject(tconstruct.library.crafting.Smeltery.instance, "smelteryFuels"); } catch (Exception e) {} } @@ -58,14 +61,10 @@ public class TConstructHelper { //Returns a Drying Recipe, using reflection as the constructor is not visible public static DryingRecipe getDryingRecipe(ItemStack input, int time, ItemStack output) { - try { - Constructor constructor = DryingRecipe.class.getDeclaredConstructor(ItemStack.class, int.class, ItemStack.class); - constructor.setAccessible(true); - return (DryingRecipe) constructor.newInstance(input, time, output); - } catch (Exception e) { - e.printStackTrace(); - throw new NullPointerException("Failed to instantiate DryingRecipe"); - } + return ReflectionHelper.getInstance(ReflectionHelper.getConstructor(DryingRecipe.class, ItemStack.class, int.class, ItemStack.class), + input, + time, + output); } public static String getMaterialFromID(int id) { @@ -73,13 +72,10 @@ public class TConstructHelper { } public static ItemKey getItemKey(Item item, int damage, int value, String material) { - try { - Constructor constructor = ItemKey.class.getDeclaredConstructor(ItemStack.class, int.class, int.class, String.class); - constructor.setAccessible(true); - return (ItemKey) constructor.newInstance(item, damage, value, material); - } catch (Exception e) { - e.printStackTrace(); - throw new NullPointerException("Failed to instantiate ItemKey"); - } + return ReflectionHelper.getInstance(ReflectionHelper.getConstructor(ItemKey.class, ItemStack.class, int.class, int.class, String.class), + item, + damage, + value, + material); } } diff --git a/src/main/java/modtweaker2/mods/tconstruct/commands/TConstructLogger.java b/src/main/java/modtweaker2/mods/tconstruct/commands/TConstructLogger.java new file mode 100644 index 0000000..3c5f964 --- /dev/null +++ b/src/main/java/modtweaker2/mods/tconstruct/commands/TConstructLogger.java @@ -0,0 +1,103 @@ +package modtweaker2.mods.tconstruct.commands; + +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; +import java.util.Map.Entry; + +import mantle.utils.ItemMetaWrapper; +import minetweaker.MineTweakerAPI; +import minetweaker.MineTweakerImplementationAPI; +import minetweaker.api.player.IPlayer; +import minetweaker.api.server.ICommandFunction; +import modtweaker2.helpers.LogHelper; +import modtweaker2.helpers.StringHelper; +import modtweaker2.mods.tconstruct.TConstructHelper; +import net.minecraft.item.ItemStack; +import net.minecraftforge.fluids.Fluid; +import net.minecraftforge.fluids.FluidStack; +import tconstruct.library.crafting.AlloyMix; +import tconstruct.library.crafting.CastingRecipe; +import tconstruct.library.crafting.DryingRackRecipes; +import tconstruct.library.crafting.DryingRackRecipes.DryingRecipe; + +public class TConstructLogger implements ICommandFunction { + private static final List validArguments = new LinkedList(); + + static { + validArguments.add("casting"); + validArguments.add("drying"); + validArguments.add("smeltery"); + } + + @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("casting")) { + for(CastingRecipe recipe : TConstructHelper.basinCasting) { + MineTweakerAPI.logCommand(String.format("mods.tconstruct.Casting.addBasinRecipe(%s, %s, %s, %s, %d);", + LogHelper.getStackDescription(recipe.output), + LogHelper.getStackDescription(recipe.castingMetal), + LogHelper.getStackDescription(recipe.cast), + recipe.consumeCast, + recipe.coolTime)); + } + + for(CastingRecipe recipe : TConstructHelper.tableCasting) { + MineTweakerAPI.logCommand(String.format("mods.tconstruct.Casting.addTableRecipe(%s, %s, %s, %s, %d);", + LogHelper.getStackDescription(recipe.output), + LogHelper.getStackDescription(recipe.castingMetal), + LogHelper.getStackDescription(recipe.cast), + recipe.consumeCast, + recipe.coolTime)); + } + } + + if(args.isEmpty() || args.contains("drying")) { + for(DryingRecipe recipe : DryingRackRecipes.recipes) { + MineTweakerAPI.logCommand(String.format("mods.tconstruct.Drying.addRecipe(%s, %s, %d);", + LogHelper.getStackDescription(recipe.input), + LogHelper.getStackDescription(recipe.result), + recipe.time)); + } + } + + if(args.isEmpty() || args.contains("smeltery")) { + + for(Entry recipe : TConstructHelper.smeltingList.entrySet()) { + int temperature = TConstructHelper.temperatureList.get(recipe.getKey()); + ItemStack renderItem = TConstructHelper.renderIndex.get(recipe.getKey()); + + MineTweakerAPI.logCommand(String.format("mods.tconstruct.Smeltery.addMelting(%s, %s, %d, %s);", + LogHelper.getStackDescription(new ItemStack(recipe.getKey().item, 1, recipe.getKey().meta)), + LogHelper.getStackDescription(recipe.getValue()), + temperature, + LogHelper.getStackDescription(renderItem))); + } + + for(AlloyMix recipe : TConstructHelper.alloys) { + MineTweakerAPI.logCommand(String.format("mods.tconstruct.Smeltery.addAlloy(%s, %s);", + LogHelper.getStackDescription(recipe.result), + LogHelper.getListDescription(recipe.mixers))); + } + + for(Entry fuel : TConstructHelper.fuelList.entrySet()) { + MineTweakerAPI.logCommand(String.format("mods.tconstruct.Smeltery.addFuel(%s, %d, %d);", + LogHelper.getStackDescription(new FluidStack(fuel.getKey(), 1)), + fuel.getValue()[0], + fuel.getValue()[1])); + } + } + + 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/tconstruct/handlers/Casting.java b/src/main/java/modtweaker2/mods/tconstruct/handlers/Casting.java index f744c98..62b656e 100644 --- a/src/main/java/modtweaker2/mods/tconstruct/handlers/Casting.java +++ b/src/main/java/modtweaker2/mods/tconstruct/handlers/Casting.java @@ -1,21 +1,24 @@ package modtweaker2.mods.tconstruct.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.areEqual; +import static modtweaker2.helpers.StackHelper.matches; import java.util.ArrayList; import java.util.LinkedList; -import java.util.Iterator; +import java.util.List; import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; +import minetweaker.api.item.IngredientAny; import minetweaker.api.liquid.ILiquidStack; +import modtweaker2.helpers.LogHelper; import modtweaker2.mods.tconstruct.TConstructHelper; import modtweaker2.utils.BaseListAddition; import modtweaker2.utils.BaseListRemoval; -import net.minecraft.item.ItemStack; -import net.minecraftforge.fluids.FluidStack; import stanhebben.zenscript.annotations.Optional; import stanhebben.zenscript.annotations.ZenClass; import stanhebben.zenscript.annotations.ZenMethod; @@ -23,116 +26,108 @@ import tconstruct.library.crafting.CastingRecipe; @ZenClass("mods.tconstruct.Casting") public class Casting { - //Adding a TConstruct Casting recipe + + protected static final String name = "TConstruct Casting"; + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + @ZenMethod public static void addBasinRecipe(IItemStack output, ILiquidStack metal, @Optional IItemStack cast, @Optional boolean consume, int delay) { + if(metal == null || output == null) { + LogHelper.logError(String.format("Required parameters missing for %s Recipe.", name)); + return; + } + MineTweakerAPI.apply(new Add(new CastingRecipe(toStack(output), toFluid(metal), toStack(cast), consume, delay, null), TConstructHelper.basinCasting)); } @ZenMethod public static void addTableRecipe(IItemStack output, ILiquidStack metal, @Optional IItemStack cast, @Optional boolean consume, int delay) { + if(metal == null || output == null) { + LogHelper.logError(String.format("Required parameters missing for %s Recipe.", name)); + return; + } + MineTweakerAPI.apply(new Add(new CastingRecipe(toStack(output), toFluid(metal), toStack(cast), consume, delay, null), TConstructHelper.tableCasting)); } //Passes the list to the base list implementation, and adds the recipe - private static class Add extends BaseListAddition { - public Add(CastingRecipe recipe, ArrayList list) { - super("TConstruct Casting", list, recipe); + private static class Add extends BaseListAddition { + public Add(CastingRecipe recipe, ArrayList list) { + super(Casting.name, list); + + this.recipes.add(recipe); } @Override - public String getRecipeInfo() { - return ((CastingRecipe) recipe).output.getDisplayName(); + protected String getRecipeInfo(CastingRecipe recipe) { + return LogHelper.getStackDescription(recipe.output); } } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - //Removing a TConstruct Casting recipe @ZenMethod - public static void removeBasinRecipe(IItemStack output) { - MineTweakerAPI.apply(new Remove((toStack(output)), TConstructHelper.basinCasting, RecipeComponent.Output)); - } - - @ZenMethod - public static void removeTableRecipe(IItemStack output) { - MineTweakerAPI.apply(new Remove((toStack(output)), TConstructHelper.tableCasting, RecipeComponent.Output)); + public static void removeTableRecipe(IIngredient output, @Optional IIngredient material, @Optional IIngredient cast) { + removeRecipe(output, material, cast, TConstructHelper.tableCasting); } @ZenMethod - public static void removeBasinMaterial(ILiquidStack fluid) { - MineTweakerAPI.apply(new Remove((toFluid(fluid)), TConstructHelper.basinCasting)); - } + public static void removeBasinRecipe(IIngredient output, @Optional IIngredient material, @Optional IIngredient cast) { - @ZenMethod - public static void removeTableMaterial(ILiquidStack fluid) { - MineTweakerAPI.apply(new Remove((toFluid(fluid)), TConstructHelper.tableCasting)); + removeRecipe(output, material, cast, TConstructHelper.basinCasting); } - - @ZenMethod - public static void removeCastRecipes(IItemStack cast) { - MineTweakerAPI.apply(new Remove((toStack(cast)), TConstructHelper.tableCasting, RecipeComponent.Cast)); + + public static void removeRecipe(IIngredient output, IIngredient material, IIngredient cast, List list) { + if(output == null) { + LogHelper.logError(String.format("Required parameters missing for %s Recipe.", name)); + return; + } + + if(material == null) { + material = IngredientAny.INSTANCE; + } + + if(cast == null) { + cast = IngredientAny.INSTANCE; + } + + List recipes = new LinkedList(); + + for(CastingRecipe recipe : list) { + if(recipe != null) { + if (!matches(output, toIItemStack(recipe.output))) { + continue; + } + + if (!matches(material, toILiquidStack(recipe.castingMetal))) { + continue; + } + + if(!matches(cast, toIItemStack(recipe.cast))) { + continue; + } + + recipes.add(recipe); + } + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new Remove(list, recipes)); + } else { + LogHelper.logWarning(String.format("No %s Recipe found for output %s, material %s and cast %s. Command ignored!", Casting.name, output.toString(), material.toString(), cast.toString())); + } } // Removes all matching recipes, apply is never the same for anything, so will always need to override it - private static class Remove extends BaseListRemoval { - protected final RecipeComponent component; - - public Remove(ItemStack item, ArrayList list, RecipeComponent component) { - super("TConstruct Casting", list, item); - this.component = component; - } - - public Remove(FluidStack fluid, ArrayList list) { - super("TConstruct Casting", list, fluid); - this.component = RecipeComponent.Material; - } - - // Loops through the registry, to find all items that matches, then removes them - @Override - public void apply() { - for (Iterator iterator = ((ArrayList)list).iterator(); iterator.hasNext();) { - CastingRecipe r = iterator.next(); - - switch(component) - { - case Cast: - if (r.cast != null && areEqual(r.cast, stack)) { - recipes.add(r); - } - - break; - - case Material: - if (r.castingMetal != null && r.castingMetal.isFluidEqual(fluid)) { - recipes.add(r); - } - - break; - - case Output: - if (r.output != null && areEqual(r.output, stack)) { - recipes.add(r); - } - - break; - } - } - super.apply(); - } + private static class Remove extends BaseListRemoval { + public Remove(List list, List recipes) { + super(Casting.name, list, recipes); + } @Override - public String getRecipeInfo() { - if(component == RecipeComponent.Material) - return fluid.getLocalizedName(); - else - return stack.getDisplayName(); + protected String getRecipeInfo(CastingRecipe recipe) { + return LogHelper.getStackDescription(recipe.output); } } - - public enum RecipeComponent { - Output, - Cast, - Material - } } diff --git a/src/main/java/modtweaker2/mods/tconstruct/handlers/Drying.java b/src/main/java/modtweaker2/mods/tconstruct/handlers/Drying.java index 4969659..de8baff 100644 --- a/src/main/java/modtweaker2/mods/tconstruct/handlers/Drying.java +++ b/src/main/java/modtweaker2/mods/tconstruct/handlers/Drying.java @@ -1,16 +1,18 @@ package modtweaker2.mods.tconstruct.handlers; +import static modtweaker2.helpers.InputHelper.toIItemStack; import static modtweaker2.helpers.InputHelper.toStack; -import static modtweaker2.helpers.StackHelper.areEqual; -import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; +import modtweaker2.helpers.LogHelper; import modtweaker2.mods.tconstruct.TConstructHelper; import modtweaker2.utils.BaseListAddition; import modtweaker2.utils.BaseListRemoval; -import net.minecraft.item.ItemStack; import stanhebben.zenscript.annotations.ZenClass; import stanhebben.zenscript.annotations.ZenMethod; import tconstruct.library.crafting.DryingRackRecipes; @@ -18,21 +20,31 @@ import tconstruct.library.crafting.DryingRackRecipes.DryingRecipe; @ZenClass("mods.tconstruct.Drying") public class Drying { - //Adding a TConstruct Drying Rack recipe + + protected static final String name = "TConstruct Drying Rack"; + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + @ZenMethod public static void addRecipe(IItemStack input, IItemStack output, int time) { + if(input == null || output == null) { + LogHelper.logError(String.format("Required parameters missing for %s Recipe.", name)); + return; + } + MineTweakerAPI.apply(new Add(TConstructHelper.getDryingRecipe(toStack(input), time, toStack(output)))); } //Passes the list to the base list implementation, and adds the recipe - private static class Add extends BaseListAddition { + private static class Add extends BaseListAddition { public Add(DryingRecipe recipe) { - super("TConstruct Drying Rack", DryingRackRecipes.recipes, recipe); + super(Drying.name, DryingRackRecipes.recipes); + this.recipes.add(recipe); } @Override - public String getRecipeInfo() { - return ((DryingRecipe) recipe).result.getDisplayName(); + protected String getRecipeInfo(DryingRecipe recipe) { + return LogHelper.getStackDescription(recipe.result); } } @@ -40,30 +52,31 @@ public class Drying { //Removing a TConstruct Drying Rack recipe @ZenMethod - public static void removeRecipe(IItemStack output) { - MineTweakerAPI.apply(new Remove((toStack(output)))); + public static void removeRecipe(IIngredient ingredient) { + List recipes = new LinkedList(); + + for (DryingRecipe recipe : DryingRackRecipes.recipes) { + if (recipe != null && recipe.result != null && ingredient.matches(toIItemStack(recipe.result))) { + recipes.add(recipe); + } + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new Remove(recipes)); + } else { + LogHelper.logWarning(String.format("No %s Recipe found for %s. Command ignored!", Drying.name, ingredient.toString())); + } } //Removes a recipe, apply is never the same for anything, so will always need to override it - private static class Remove extends BaseListRemoval { - public Remove(ItemStack output) { - super("TConstruct Drying Rack", DryingRackRecipes.recipes, output); - } - - //Loops through the registry, to find the item that matches, saves that recipe then removes it - @Override - public void apply() { - for (DryingRecipe r : (ArrayList) list) { - if (r.result != null && areEqual(r.result, stack)) { - recipes.add(r); - } - } - super.apply(); + private static class Remove extends BaseListRemoval { + public Remove(List list) { + super(Drying.name, DryingRackRecipes.recipes, list); } @Override - public String getRecipeInfo() { - return stack.getDisplayName(); + protected String getRecipeInfo(DryingRecipe recipe) { + return LogHelper.getStackDescription(recipe.result); } } } diff --git a/src/main/java/modtweaker2/mods/tconstruct/handlers/Modifiers.java b/src/main/java/modtweaker2/mods/tconstruct/handlers/Modifiers.java index b791e4a..3778718 100644 --- a/src/main/java/modtweaker2/mods/tconstruct/handlers/Modifiers.java +++ b/src/main/java/modtweaker2/mods/tconstruct/handlers/Modifiers.java @@ -1,8 +1,10 @@ package modtweaker2.mods.tconstruct.handlers; +import java.util.LinkedList; import java.util.List; import minetweaker.MineTweakerAPI; +import modtweaker2.helpers.LogHelper; import modtweaker2.mods.tconstruct.TConstructHelper; import modtweaker2.utils.BaseListRemoval; import stanhebben.zenscript.annotations.ZenClass; @@ -11,28 +13,36 @@ import tconstruct.library.modifier.ItemModifier; @ZenClass("mods.tconstruct.Modifiers") public class Modifiers { + + protected static final String name = "TConstruct Modifier"; + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + @ZenMethod public static void remove(String mod) { - MineTweakerAPI.apply(new Remove(mod)); + List recipes = new LinkedList(); + + for (ItemModifier recipe : TConstructHelper.modifiers) { + if (recipe.key.equals(mod)) { + recipes.add(recipe); + } + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new Remove(recipes)); + } else { + LogHelper.logWarning(String.format("No %s Recipe found for %s. Command ignored!", Modifiers.name, mod)); + } } - //Searches through the modifiers and removes the first valid entry - private static class Remove extends BaseListRemoval { - private final String check; - - public Remove(String check) { - super("Modifier", TConstructHelper.modifiers); - this.check = check; + private static class Remove extends BaseListRemoval{ + public Remove(List recipes) { + super(Modifiers.name, TConstructHelper.modifiers, recipes); } @Override - public void apply() { - for (ItemModifier m : (List) list) { - if (m.key.equals(check)) { - recipes. add(m); - } - } - super.apply(); + protected String getRecipeInfo(ItemModifier recipe) { + return recipe.key; } } } diff --git a/src/main/java/modtweaker2/mods/tconstruct/handlers/Smeltery.java b/src/main/java/modtweaker2/mods/tconstruct/handlers/Smeltery.java index 2f8efee..09db6e4 100644 --- a/src/main/java/modtweaker2/mods/tconstruct/handlers/Smeltery.java +++ b/src/main/java/modtweaker2/mods/tconstruct/handlers/Smeltery.java @@ -3,26 +3,34 @@ package modtweaker2.mods.tconstruct.handlers; import static modtweaker2.helpers.InputHelper.isABlock; import static modtweaker2.helpers.InputHelper.toFluid; import static modtweaker2.helpers.InputHelper.toFluids; +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.ArrayList; import java.util.Arrays; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; import mantle.utils.ItemMetaWrapper; import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; import minetweaker.api.liquid.ILiquidStack; -import minetweaker.api.minecraft.MineTweakerMC; -import minetweaker.api.oredict.IOreDictEntry; +import modtweaker2.helpers.LogHelper; import modtweaker2.mods.tconstruct.TConstructHelper; -import modtweaker2.utils.BaseDescriptionAddition; -import modtweaker2.utils.BaseDescriptionRemoval; import modtweaker2.utils.BaseListAddition; import modtweaker2.utils.BaseListRemoval; +import modtweaker2.utils.BaseMapAddition; +import modtweaker2.utils.BaseMapRemoval; import net.minecraft.block.Block; import net.minecraft.item.ItemStack; +import net.minecraftforge.fluids.Fluid; import net.minecraftforge.fluids.FluidStack; -import net.minecraftforge.oredict.OreDictionary; import stanhebben.zenscript.annotations.Optional; import stanhebben.zenscript.annotations.ZenClass; import stanhebben.zenscript.annotations.ZenMethod; @@ -30,24 +38,34 @@ import tconstruct.library.crafting.AlloyMix; @ZenClass("mods.tconstruct.Smeltery") public class Smeltery { + + public static final String nameFuel = "TConstruct Smeltery - Fuel"; + public static final String nameMelting = "TConstruct Smeltery - Melting"; + public static final String nameAlloy = "TConstruct Smeltery - Alloy"; /********************************************** TConstruct Alloy Recipes **********************************************/ // Adding a TConstruct Alloy recipe @ZenMethod public static void addAlloy(ILiquidStack output, ILiquidStack[] input) { + if(input == null || output == null) { + LogHelper.logError(String.format("Required parameters missing for %s Recipe.", nameAlloy)); + return; + } + MineTweakerAPI.apply(new AddAlloy(new AlloyMix(toFluid(output), new ArrayList(Arrays.asList(toFluids(input)))))); } // Passes the list to the base list implementation, and adds the recipe - private static class AddAlloy extends BaseListAddition { + private static class AddAlloy extends BaseListAddition { public AddAlloy(AlloyMix recipe) { - super("Smeltery - Alloy", TConstructHelper.alloys, recipe); + super("TConstruct Smeltery - Alloy", TConstructHelper.alloys); + this.recipes.add(recipe); } @Override - public String getRecipeInfo() { - return ((AlloyMix) recipe).result.getFluid().getName(); + protected String getRecipeInfo(AlloyMix recipe) { + return LogHelper.getStackDescription(recipe.result); } } @@ -55,146 +73,228 @@ public class Smeltery { // Removing a TConstruct Alloy recipe @ZenMethod - public static void removeAlloy(ILiquidStack output) { - MineTweakerAPI.apply(new RemoveAlloy((toFluid(output)))); + public static void removeAlloy(IIngredient output) { + List recipes = new LinkedList(); + + for(AlloyMix r : TConstructHelper.alloys) { + if(r != null && matches(output, toILiquidStack(r.result))) { + recipes.add(r); + } + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new RemoveAlloy(recipes)); + } else { + LogHelper.logError(String.format("No %s recipes found for %s. Command ignored!", nameAlloy, output.toString())); + } + } // Removes a recipe, apply is never the same for anything, so will always // need to override it - private static class RemoveAlloy extends BaseListRemoval { - public RemoveAlloy(FluidStack output) { - super("Smeltery - Alloy", TConstructHelper.alloys, output); + private static class RemoveAlloy extends BaseListRemoval { + public RemoveAlloy(List recipes) { + super(nameAlloy, TConstructHelper.alloys, recipes); } - // Loops through the registry, to find the item that matches, saves that - // recipe then removes it - @Override - public void apply() { - for (AlloyMix r : TConstructHelper.alloys) { - if (r.result != null && fluid != null && r.result.getFluid() == fluid.getFluid()) { - recipes.add(r); - } - } - super.apply(); - } - - @Override - public String getRecipeInfo() { - return fluid.getFluid().getName(); - } + @Override + protected String getRecipeInfo(AlloyMix recipe) { + return LogHelper.getStackDescription(recipe.result); + } } /********************************************** TConstruct Melting Recipes **********************************************/ // Adding a TConstruct Melting recipe @ZenMethod - public static void addMelting(IItemStack input, ILiquidStack output, int temp, @Optional IItemStack block) { - if (block == null) - block = input; - if (isABlock(block)) { - Block theBlock = Block.getBlockFromItem(toStack(block).getItem()); - int theMeta = toStack(block).getItemDamage(); - MineTweakerAPI.apply(new AddMelting(toStack(input), theBlock, theMeta, temp, toFluid(output))); - } + public static void addMelting(IIngredient input, ILiquidStack output, int temp, @Optional IItemStack block) { + + if(input == null || output == null) { + LogHelper.logError(String.format("Required parameters missing for %s Recipe.", nameMelting)); + return; + } + + List recipes = new LinkedList(); + + for(IItemStack in : input.getItems()) { + if(block == null && !isABlock(toStack(in))) { + LogHelper.logWarning(String.format("Item %s is not a block and no block renderer is provided for %s recipe. Input ignored!", in.toString(), nameMelting)); + } else { + recipes.add(new MeltingRecipe(toStack(in), toFluid(output), temp, block == null ? toStack(in) : toStack(block))); + } + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new AddMelting(recipes)); + } else { + LogHelper.logError(String.format("No %s recipes could be added for input %s.", nameMelting, input.toString())); + } } - @ZenMethod - public static void addMelting(IOreDictEntry input, ILiquidStack output, int temp, @Optional IItemStack block) { - for (ItemStack stack : OreDictionary.getOres(input.getName())) { - addMelting(MineTweakerMC.getIItemStack(stack), output, temp, block); - } - } + private static class AddMelting extends BaseListAddition { - // Takes all the variables and saves them in place - private static class AddMelting extends BaseDescriptionAddition { - private final ItemStack input; - private final Block block; - private final int meta; - private final int temp; - private final FluidStack output; - - public AddMelting(ItemStack input, Block block, int meta, int temp, FluidStack output) { - super("Smeltery - Melting"); - this.input = input; - this.block = block; - this.meta = meta; - this.temp = temp; - this.output = output; + public AddMelting(List recipes) { + super(nameMelting, null, recipes); } - // Adds the Melting recipe @Override public void apply() { - tconstruct.library.crafting.Smeltery.instance.addMelting(input, block, meta, temp, output); - } - - // Removes the Melting recipe from the hashmaps - @Override - public void undo() { - ItemMetaWrapper in = new ItemMetaWrapper(input); - TConstructHelper.smeltingList.remove(in); - TConstructHelper.temperatureList.remove(in); - TConstructHelper.renderIndex.remove(in); + for(MeltingRecipe recipe : recipes) { + tconstruct.library.crafting.Smeltery.addMelting(recipe.input, recipe.getRendererBlock(), recipe.getRendererMeta(), recipe.temperature, recipe.fluid); + successful.add(recipe); + } } @Override - public String getRecipeInfo() { - return input.getDisplayName(); + public void undo() { + for(MeltingRecipe recipe : successful) { + TConstructHelper.smeltingList.remove(recipe.meta); + TConstructHelper.temperatureList.remove(recipe.meta); + TConstructHelper.renderIndex.remove(recipe.meta); + } } + + @Override + public String getRecipeInfo(MeltingRecipe recipe) { + return LogHelper.getStackDescription(recipe.input); + } } - // /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Removing a TConstruct Melting recipe @ZenMethod - public static void removeMelting(IItemStack input) { - MineTweakerAPI.apply(new RemoveMelting((toStack(input)))); + public static void removeMelting(IIngredient input) { + List recipes = new LinkedList(); + + for(ItemMetaWrapper meta : TConstructHelper.smeltingList.keySet()) { + ItemStack in = new ItemStack(meta.item, 1, meta.meta); + if(matches(input, toIItemStack(in))) { + recipes.add(new MeltingRecipe( + in, + TConstructHelper.smeltingList.get(meta), + TConstructHelper.temperatureList.get(meta), + TConstructHelper.renderIndex.get(meta))); + } + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new RemoveMelting(recipes)); + } else { + LogHelper.logWarning(String.format("No %s Recipe found for %s. Command ignored!", nameMelting, input.toString())); + } } - @ZenMethod - public static void removeMelting(IOreDictEntry input) { - for (ItemStack stack : OreDictionary.getOres(input.getName())) { - removeMelting(MineTweakerMC.getIItemStack(stack)); - } - } - - // Removes a recipe, apply is never the same for anything, so will always - // need to override it - private static class RemoveMelting extends BaseDescriptionRemoval { - private final ItemStack input; - private FluidStack fluid; - private Integer temp; - private ItemStack renderer; - - public RemoveMelting(ItemStack input) { - super("Smeltery - Melting"); - this.input = input; + private static class RemoveMelting extends BaseListRemoval { + + public RemoveMelting(List recipes) { + super(nameMelting, null, recipes); } - // Gets the current values, and saves, them removes them from the - // hashmaps @Override public void apply() { - ItemMetaWrapper in = new ItemMetaWrapper(input); - fluid = TConstructHelper.smeltingList.get(in); - temp = TConstructHelper.temperatureList.get(in); - renderer = TConstructHelper.renderIndex.get(in); - TConstructHelper.smeltingList.remove(in); - TConstructHelper.temperatureList.remove(in); - TConstructHelper.renderIndex.remove(in); + for(MeltingRecipe recipe : recipes) { + TConstructHelper.smeltingList.remove(recipe.meta); + TConstructHelper.temperatureList.remove(recipe.meta); + TConstructHelper.renderIndex.remove(recipe.meta); + + successful.add(recipe); + } } - // Reads the Melting recipe - @Override + @Override public void undo() { - // tconstruct.library.crafting.Smeltery.instance.addMelting(input, - // Block.getBlockFromItem(renderer.getItem()), - // renderer.getItemDamage(), temp, fluid); + for(MeltingRecipe recipe : successful) { + tconstruct.library.crafting.Smeltery.addMelting(recipe.input, recipe.getRendererBlock(), recipe.getRendererMeta(), recipe.temperature, recipe.fluid); + } } @Override - public String getRecipeInfo() { - return input.getDisplayName(); + public String getRecipeInfo(MeltingRecipe recipe) { + return LogHelper.getStackDescription(recipe.input); } } + + protected static class MeltingRecipe { + public final ItemMetaWrapper meta; + public final ItemStack input; + public final FluidStack fluid; + public final Integer temperature; + public final ItemStack renderer; + + protected MeltingRecipe(ItemStack input, FluidStack fluid, int temperature, ItemStack renderer) { + this.input = input; + this.fluid = fluid; + this.temperature = temperature; + this.renderer = renderer; + this.meta = new ItemMetaWrapper(input); + } + + public Block getRendererBlock() { + return Block.getBlockFromItem(renderer.getItem()); + } + + public int getRendererMeta() { + return renderer.getItemDamage(); + } + } + + /********************************************** TConstruct Fuel Recipes **********************************************/ + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + @ZenMethod + public static void removeFuel(IIngredient input) { + Map recipes = new HashMap(); + + for(Entry fuel : TConstructHelper.fuelList.entrySet()) { + if(fuel != null && fuel.getKey() != null && matches(input, toILiquidStack(new FluidStack(fuel.getKey(), 1)))) { + recipes.put(fuel.getKey(), fuel.getValue()); + } + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new RemoveFuel(recipes)); + } else { + LogHelper.logWarning(String.format("No %s Recipe for %s found. Command ignored!", Smeltery.nameFuel, input.toString())); + } + } + + public static class RemoveFuel extends BaseMapRemoval { + public RemoveFuel(Map recipes) { + super(Smeltery.nameFuel, TConstructHelper.fuelList, recipes); + } + + @Override + public String getRecipeInfo(Entry recipe) { + return LogHelper.getStackDescription(new FluidStack(recipe.getKey(), 1)); + } + } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + @ZenMethod + public static void addFuel(ILiquidStack liquid, int power, int duration) { + if(liquid == null) { + LogHelper.logError(String.format("Required parameters missing for %s Recipe.", nameFuel)); + return; + } + + Map recipes = new HashMap(); + + recipes.put(toFluid(liquid).getFluid(), new Integer[] {power, duration}); + + MineTweakerAPI.apply(new AddFuel(recipes)); + } + + public static class AddFuel extends BaseMapAddition { + public AddFuel(Map recipes) { + super(Smeltery.nameFuel, TConstructHelper.fuelList, recipes); + } + + @Override + public String getRecipeInfo(Entry recipe) { + return LogHelper.getStackDescription(new FluidStack(recipe.getKey(), 1)); + } + } } diff --git a/src/main/java/modtweaker2/mods/tconstruct/handlers/TiCTweaks.java b/src/main/java/modtweaker2/mods/tconstruct/handlers/TiCTweaks.java index 5b616da..f13030c 100644 --- a/src/main/java/modtweaker2/mods/tconstruct/handlers/TiCTweaks.java +++ b/src/main/java/modtweaker2/mods/tconstruct/handlers/TiCTweaks.java @@ -1,8 +1,15 @@ package modtweaker2.mods.tconstruct.handlers; +import static modtweaker2.helpers.InputHelper.toIItemStack; import static modtweaker2.helpers.InputHelper.toStack; + +import java.util.LinkedList; +import java.util.List; + import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; +import modtweaker2.helpers.LogHelper; import modtweaker2.mods.tconstruct.TConstructHelper; import modtweaker2.utils.BaseListAddition; import modtweaker2.utils.BaseListRemoval; @@ -63,51 +70,49 @@ public class TiCTweaks { } // Tweaks for setting repair materials - private static class Add extends BaseListAddition { + private static class Add extends BaseListAddition { public Add(ItemKey recipe) { - super("Repair Material", PatternBuilder.instance.materials, recipe); + super("Repair Material", PatternBuilder.instance.materials); + recipes.add(recipe); } @Override - public String getRecipeInfo() { - return new ItemStack(((ItemKey) recipe).item, 1, ((ItemKey) recipe).damage).getDisplayName(); + protected String getRecipeInfo(ItemKey recipe) { + return LogHelper.getStackDescription(new ItemStack(recipe.item, 1, recipe.damage)); } } - // ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + @ZenMethod - public static void removeRepairMaterial(IItemStack output, @Optional String material) { - MineTweakerAPI.apply(new Remove(toStack(output), material)); + public static void removeRepairMaterial(IIngredient output, @Optional String material) { + + List recipes = new LinkedList(); + + for (ItemKey recipe : PatternBuilder.instance.materials) { + IItemStack clone = toIItemStack(new ItemStack(recipe.item, 1, recipe.damage)); + if ((material != null && material.equalsIgnoreCase(recipe.key)) || (material == null)) { + if (output.matches(clone)) { + recipes.add(recipe); + } + } + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new Remove(recipes)); + } } // Removes a recipe, apply is never the same for anything, so will always // need to override it - private static class Remove extends BaseListRemoval { - private final String material; - - public Remove(ItemStack stack, String material) { - super("Repair Material", PatternBuilder.instance.materials, stack); - this.material = material; - } - - // Loops through the registry, to find the item that matches, saves that - // recipe then removes it - @Override - public void apply() { - for (ItemKey r : PatternBuilder.instance.materials) { - ItemStack clone = new ItemStack(r.item, 1, r.damage); - if ((material != null && material.equalsIgnoreCase(r.key)) || (material == null)) { - if (clone.isItemEqual(stack)) { - recipes.add(r); - } - } - } - super.apply(); + private static class Remove extends BaseListRemoval { + public Remove(List recipes) { + super("Repair Material", PatternBuilder.instance.materials, recipes); } @Override - public String getRecipeInfo() { - return stack.getDisplayName(); + protected String getRecipeInfo(ItemKey recipe) { + return LogHelper.getStackDescription(new ItemStack(recipe.item, 1, recipe.damage)); } } } diff --git a/src/main/java/modtweaker2/mods/tfcraft/handlers/Anvil.java b/src/main/java/modtweaker2/mods/tfcraft/handlers/Anvil.java index 824de8e..e5c52bf 100644 --- a/src/main/java/modtweaker2/mods/tfcraft/handlers/Anvil.java +++ b/src/main/java/modtweaker2/mods/tfcraft/handlers/Anvil.java @@ -1,14 +1,17 @@ package modtweaker2.mods.tfcraft.handlers; -import static modtweaker2.helpers.StackHelper.areEqual; -import static modtweaker2.helpers.InputHelper.toStack; import static com.bioxx.tfc.api.Crafting.AnvilReq.getReqFromInt; +import static modtweaker2.helpers.InputHelper.toIItemStack; +import static modtweaker2.helpers.InputHelper.toStack; +import static modtweaker2.helpers.StackHelper.matches; -import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; -import net.minecraft.item.ItemStack; import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; +import modtweaker2.helpers.LogHelper; import modtweaker2.mods.tfcraft.TFCHelper; import modtweaker2.utils.BaseListAddition; import modtweaker2.utils.BaseListRemoval; @@ -21,10 +24,15 @@ import com.bioxx.tfc.api.Crafting.AnvilRecipe; @ZenClass("mods.tfcraft.Anvil") public class Anvil { + + public static final String nameRegular = "TFC Anvil-Regular"; + public static final String nameWeld = "TFC Anvil-Weld"; + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + @ZenMethod public static void add(IItemStack input1, IItemStack input2, String plan, int value, boolean flux, int req, IItemStack output){ - MineTweakerAPI.apply(new Add(new AnvilRecipe(toStack(input1), toStack(input2), plan, value, flux, req, toStack(output)))); + addRecipe(nameRegular, TFCHelper.anvilRecipes, new AnvilRecipe(toStack(input1), toStack(input2), plan, value, flux, req, toStack(output))); } /** @@ -33,60 +41,63 @@ public class Anvil { */ @ZenMethod public static void addWeld(IItemStack input1, IItemStack input2, int req, IItemStack output){ - MineTweakerAPI.apply(new AddWeld(new AnvilRecipe(toStack(input1), toStack(input2), true, getReqFromInt(req), toStack(output)))); + addRecipe(nameWeld, TFCHelper.anvilWeldRecipes, new AnvilRecipe(toStack(input1), toStack(input2), true, getReqFromInt(req), toStack(output))); } - @ZenMethod - public static void remove(IItemStack stack){ - MineTweakerAPI.apply(new Remove(toStack(stack))); - } - - @ZenMethod - public static void removeWeld(IItemStack stack){ - MineTweakerAPI.apply(new RemoveWeld(toStack(stack))); - } - - private static class Add extends BaseListAddition{ - public Add(AnvilRecipe recipe) { - super("Anvil-Regular", TFCHelper.anvilRecipes, recipe); - } - } - - private static class AddWeld extends BaseListAddition{ - public AddWeld(AnvilRecipe recipe) { - super("Anvil-Weld", TFCHelper.anvilWeldRecipes, recipe); - } - } - - private static class Remove extends BaseListRemoval{ - public Remove(ItemStack out){ - super("Anvil-Remove-Regular", TFCHelper.anvilRecipes, out); - } + public static void addRecipe(String name, List list, AnvilRecipe recipe) { + List recipes = new LinkedList(); + recipes.add(recipe); + MineTweakerAPI.apply(new Add(name, list, recipes)); + } - @Override - public void apply() { - for (AnvilRecipe recipe : AnvilManager.getInstance().getRecipeList()){ - if (recipe.getCraftingResult() != null && areEqual(recipe.getCraftingResult(), stack)){ - recipes.add(recipe); - } - } - super.apply(); + private static class Add extends BaseListAddition { + public Add(String name, List list, List recipes) { + super(name, list, recipes); } + + @Override + protected String getRecipeInfo(AnvilRecipe recipe) { + return LogHelper.getStackDescription(recipe.getCraftingResult()); + } } - private static class RemoveWeld extends BaseListRemoval{ - public RemoveWeld(ItemStack out){ - super("Anvil-Remove-Weld", TFCHelper.anvilWeldRecipes, out); + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + @ZenMethod + public static void remove(IIngredient ingredient) { + removeRecipe(nameRegular, TFCHelper.anvilRecipes, ingredient); + } + + @ZenMethod + public static void removeWeld(IIngredient ingredient) { + removeRecipe(nameWeld, TFCHelper.anvilWeldRecipes, ingredient); + } + + + public static void removeRecipe(String name, List list, IIngredient ingredient) { + List recipes = new LinkedList(); + + for (AnvilRecipe recipe : AnvilManager.getInstance().getRecipeList()){ + if (recipe.getCraftingResult() != null && matches(ingredient, toIItemStack(recipe.getCraftingResult()))) { + recipes.add(recipe); + } + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new Remove(name, list, recipes)); + } else { + LogHelper.logWarning(String.format("No %s Recipes found for %s. Command ignored.", name, ingredient.toString())); + } + } + + private static class Remove extends BaseListRemoval { + public Remove(String name, List list, List recipes){ + super(name, list, recipes); } @Override - public void apply() { - for (AnvilRecipe recipe : AnvilManager.getInstance().getRecipeList()){ - if (recipe.getCraftingResult() != null && recipe.getCraftingResult() == stack){ - recipes.add(recipe); - } - } - super.apply(); + protected String getRecipeInfo(AnvilRecipe recipe) { + return LogHelper.getStackDescription(recipe.getCraftingResult()); } } } diff --git a/src/main/java/modtweaker2/mods/tfcraft/handlers/Barrel.java b/src/main/java/modtweaker2/mods/tfcraft/handlers/Barrel.java index 33ae95a..ac2e6cb 100644 --- a/src/main/java/modtweaker2/mods/tfcraft/handlers/Barrel.java +++ b/src/main/java/modtweaker2/mods/tfcraft/handlers/Barrel.java @@ -1,19 +1,22 @@ package modtweaker2.mods.tfcraft.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.areEqual; +import static modtweaker2.helpers.StackHelper.matches; -import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; import minetweaker.api.liquid.ILiquidStack; +import modtweaker2.helpers.LogHelper; import modtweaker2.mods.tfcraft.TFCHelper; import modtweaker2.utils.BaseListAddition; import modtweaker2.utils.BaseListRemoval; -import net.minecraft.item.ItemStack; -import net.minecraftforge.fluids.FluidStack; import stanhebben.zenscript.annotations.ZenClass; import stanhebben.zenscript.annotations.ZenMethod; @@ -23,6 +26,10 @@ import com.bioxx.tfc.api.Crafting.BarrelRecipe; @ZenClass("mods.tfcraft.Barrel") public class Barrel { + + public static final String name = "TFC Barrel"; + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /** * @param outputFluid If you want this recipe consume fluid, outputFluid=inputFluid @@ -53,44 +60,63 @@ public class Barrel { MineTweakerAPI.apply(new AddRecipe(new BarrelLiquidToLiquidRecipe(toFluid(inputInBarrel), toFluid(input), toFluid(output)))); } - @ZenMethod - public static void remove(IItemStack output){ - MineTweakerAPI.apply(new RemoveRecipe(toStack(output))); - } - - @ZenMethod - public static void remove(ILiquidStack output){ - MineTweakerAPI.apply(new RemoveRecipe(toFluid(output))); - } - - private static class AddRecipe extends BaseListAddition{ + private static class AddRecipe extends BaseListAddition { public AddRecipe(BarrelRecipe recipe){ - super("Barrel", TFCHelper.barrelRecipes, recipe); - } - } - - private static class RemoveRecipe extends BaseListRemoval{ - public RemoveRecipe(ItemStack stack){ - super("Barrel-Remove", TFCHelper.barrelRecipes, stack); + super(Barrel.name, TFCHelper.barrelRecipes); + recipes.add(recipe); } - public RemoveRecipe(FluidStack fluid){ - super("Barrel-Remove", TFCHelper.barrelRecipes, fluid); - } - @Override - public void apply() { - for (BarrelRecipe recipe : BarrelManager.getInstance().getRecipes()){ - if (recipe.getRecipeOutIS() != null && areEqual(recipe.getRecipeOutIS(), stack)){ - recipes.add(recipe); - } - } - for (BarrelRecipe recipe : BarrelManager.getInstance().getRecipes()){ - if (recipe.getRecipeOutFluid() != null && recipe.getRecipeOutFluid().isFluidEqual(fluid)){ - recipes.add(recipe); - } - } - super.apply(); + protected String getRecipeInfo(BarrelRecipe recipe) { + if(recipe.getRecipeOutIS() != null) + return LogHelper.getStackDescription(recipe.getRecipeOutIS()); + else if(recipe.getRecipeOutFluid() != null) + return LogHelper.getStackDescription(recipe.getRecipeOutFluid()); + else + return "Unknown output"; } } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + @ZenMethod + public static void remove(IIngredient output){ + List recipes = new LinkedList(); + + // Test for item outputs + for (BarrelRecipe recipe : BarrelManager.getInstance().getRecipes()){ + if (recipe.getRecipeOutIS() != null && matches(output, toIItemStack(recipe.getRecipeOutIS()))) { + recipes.add(recipe); + } + } + + // Test for liquid outputs + for (BarrelRecipe recipe : BarrelManager.getInstance().getRecipes()){ + if (recipe.getRecipeOutFluid() != null && matches(output, toILiquidStack(recipe.getRecipeOutFluid()))) { + recipes.add(recipe); + } + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new RemoveRecipe(recipes)); + } else { + LogHelper.logWarning(String.format("No %s Recipe found for %s. Command ignored!", Barrel.name, output.toString())); + } + } + + private static class RemoveRecipe extends BaseListRemoval { + public RemoveRecipe(List recipes){ + super(Barrel.name, TFCHelper.barrelRecipes, recipes); + } + + @Override + protected String getRecipeInfo(BarrelRecipe recipe) { + if(recipe.getRecipeOutIS() != null) + return LogHelper.getStackDescription(recipe.getRecipeOutIS()); + else if(recipe.getRecipeOutFluid() != null) + return LogHelper.getStackDescription(recipe.getRecipeOutFluid()); + else + return "Unknown output"; + } + } } diff --git a/src/main/java/modtweaker2/mods/tfcraft/handlers/Kiln.java b/src/main/java/modtweaker2/mods/tfcraft/handlers/Kiln.java index 4ec58fa..8a357fc 100644 --- a/src/main/java/modtweaker2/mods/tfcraft/handlers/Kiln.java +++ b/src/main/java/modtweaker2/mods/tfcraft/handlers/Kiln.java @@ -1,16 +1,19 @@ package modtweaker2.mods.tfcraft.handlers; +import static modtweaker2.helpers.InputHelper.toIItemStack; import static modtweaker2.helpers.InputHelper.toStack; -import static modtweaker2.helpers.StackHelper.areEqual; +import static modtweaker2.helpers.StackHelper.matches; -import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; +import modtweaker2.helpers.LogHelper; import modtweaker2.mods.tfcraft.TFCHelper; import modtweaker2.utils.BaseListAddition; import modtweaker2.utils.BaseListRemoval; -import net.minecraft.item.ItemStack; import stanhebben.zenscript.annotations.ZenClass; import stanhebben.zenscript.annotations.ZenMethod; @@ -19,6 +22,8 @@ import com.bioxx.tfc.api.Crafting.KilnRecipe; @ZenClass("mods.tfcraft.Kiln") public class Kiln { + + public static final String name = "TFC Kiln"; /** Deprecated, since pottery kiln will check x instanceof ItemPotteryBase *

If you have a such item, you might consider to use it @@ -29,31 +34,40 @@ public class Kiln { MineTweakerAPI.apply(new AddRecipe(new KilnRecipe(toStack(input), lv, toStack(output)))); } - @ZenMethod - public static void remove(IItemStack stack){ - MineTweakerAPI.apply(new RemoveRecipe(toStack(stack))); - } - - private static class AddRecipe extends BaseListAddition{ + private static class AddRecipe extends BaseListAddition { public AddRecipe(KilnRecipe recipe) { - super("Kiln", TFCHelper.kilnRecipes, recipe); + super(Kiln.name, TFCHelper.kilnRecipes); + recipes.add(recipe); + } + + @Override + protected String getRecipeInfo(KilnRecipe recipe) { + return LogHelper.getStackDescription(recipe.getCraftingResult()); } } - - private static class RemoveRecipe extends BaseListRemoval{ - public RemoveRecipe(ItemStack out){ - super("Kiln-Remove", TFCHelper.kilnRecipes, out); - } - @Override - public void apply() { - for (KilnRecipe recipe : KilnCraftingManager.getInstance().getRecipeList()){ - if (recipe.getCraftingResult() != null && areEqual(recipe.getCraftingResult(), stack)){ - recipes.add(recipe); - } - } - super.apply(); + @ZenMethod + public static void remove(IIngredient output){ + List recipes = new LinkedList(); + + for (KilnRecipe recipe : KilnCraftingManager.getInstance().getRecipeList()){ + if (recipe.getCraftingResult() != null && matches(output, toIItemStack(recipe.getCraftingResult()))) { + recipes.add(recipe); + } + } + + MineTweakerAPI.apply(new RemoveRecipe(recipes)); + } + + private static class RemoveRecipe extends BaseListRemoval { + public RemoveRecipe(List recipes) { + super(Kiln.name, TFCHelper.kilnRecipes, recipes); } + + @Override + protected String getRecipeInfo(KilnRecipe recipe) { + return LogHelper.getStackDescription(recipe.getCraftingResult()); + } } } diff --git a/src/main/java/modtweaker2/mods/tfcraft/handlers/Quern.java b/src/main/java/modtweaker2/mods/tfcraft/handlers/Quern.java index f5271d4..2c16703 100644 --- a/src/main/java/modtweaker2/mods/tfcraft/handlers/Quern.java +++ b/src/main/java/modtweaker2/mods/tfcraft/handlers/Quern.java @@ -1,13 +1,16 @@ package modtweaker2.mods.tfcraft.handlers; +import static modtweaker2.helpers.InputHelper.toIItemStack; import static modtweaker2.helpers.InputHelper.toStack; -import static modtweaker2.helpers.StackHelper.areEqual; +import static modtweaker2.helpers.StackHelper.matches; -import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; -import net.minecraft.item.ItemStack; import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; +import modtweaker2.helpers.LogHelper; import modtweaker2.mods.tfcraft.TFCHelper; import modtweaker2.utils.BaseListAddition; import modtweaker2.utils.BaseListRemoval; @@ -19,39 +22,70 @@ import com.bioxx.tfc.api.Crafting.QuernRecipe; @ZenClass("mods.tfcraft.Quern") public class Quern { + + public static final String name = "TFC Quern"; + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @ZenMethod public static void add(IItemStack input, IItemStack output) { MineTweakerAPI.apply(new AddRecipe(new QuernRecipe(toStack(input), toStack(output)))); } - @ZenMethod - public static void remove(IItemStack input){ - MineTweakerAPI.apply(new RemoveRecipe(toStack(input))); - } - - private static class AddRecipe extends BaseListAddition{ + private static class AddRecipe extends BaseListAddition { public AddRecipe(QuernRecipe recipe) { - super("Quern", TFCHelper.quernRecipes, recipe); - TFCHelper.quernVaildItems.add(recipe.getInItem()); + super(Quern.name, TFCHelper.quernRecipes); + recipes.add(recipe); + } + + @Override + public void apply() { + super.apply(); + + for (QuernRecipe recipe : successful){ + TFCHelper.quernVaildItems.add(recipe.getInItem()); + } + } + + @Override + public void undo() { + for (QuernRecipe recipe : successful){ + TFCHelper.quernVaildItems.remove(recipe.getInItem()); + } + + super.undo(); + } + + @Override + protected String getRecipeInfo(QuernRecipe recipe) { + return LogHelper.getStackDescription(recipe.getResult()); } } - private static class RemoveRecipe extends BaseListRemoval{ - public RemoveRecipe(ItemStack stack) { - super("Quern-Remove", TFCHelper.quernRecipes, stack); + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + @ZenMethod + public static void remove(IIngredient output) { + List recipes = new LinkedList(); + + for (QuernRecipe recipe : QuernManager.getInstance().getRecipes()) { + if (recipe != null && recipe.getResult() != null && matches(output, toIItemStack(recipe.getResult()))){ + recipes.add(recipe); + } + } + + MineTweakerAPI.apply(new RemoveRecipe(recipes)); + } + + private static class RemoveRecipe extends BaseListRemoval { + public RemoveRecipe(List recipes) { + super(Quern.name, TFCHelper.quernRecipes, recipes); } @Override public void apply() { - for (QuernRecipe recipe : QuernManager.getInstance().getRecipes()){ - if (recipe.getResult() !=null && areEqual(recipe.getResult(), stack)){ - recipes.add(recipe); - } - } - - for (Object aRecipe : recipes){ - TFCHelper.quernVaildItems.remove(((QuernRecipe)aRecipe).getInItem()); + for (QuernRecipe recipe : recipes) { + TFCHelper.quernVaildItems.remove(recipe.getInItem()); } super.apply(); @@ -59,11 +93,16 @@ public class Quern { @Override public void undo() { - for (Object aRecipe : recipes){ - TFCHelper.quernVaildItems.add(((QuernRecipe)aRecipe).getInItem()); - } - super.undo(); + + for (QuernRecipe recipe : successful){ + TFCHelper.quernVaildItems.add(recipe.getInItem()); + } + } + + @Override + protected String getRecipeInfo(QuernRecipe recipe) { + return LogHelper.getStackDescription(recipe.getResult()); } } diff --git a/src/main/java/modtweaker2/mods/thaumcraft/Thaumcraft.java b/src/main/java/modtweaker2/mods/thaumcraft/Thaumcraft.java index 4d9954f..2e183fa 100644 --- a/src/main/java/modtweaker2/mods/thaumcraft/Thaumcraft.java +++ b/src/main/java/modtweaker2/mods/thaumcraft/Thaumcraft.java @@ -5,9 +5,9 @@ import modtweaker2.mods.thaumcraft.handlers.Arcane; import modtweaker2.mods.thaumcraft.handlers.Aspects; import modtweaker2.mods.thaumcraft.handlers.Crucible; import modtweaker2.mods.thaumcraft.handlers.Infusion; +import modtweaker2.mods.thaumcraft.handlers.Loot; import modtweaker2.mods.thaumcraft.handlers.Research; import modtweaker2.mods.thaumcraft.handlers.Warp; -import modtweaker2.mods.thaumcraft.handlers.Loot; public class Thaumcraft { public Thaumcraft() { diff --git a/src/main/java/modtweaker2/mods/thaumcraft/handlers/Arcane.java b/src/main/java/modtweaker2/mods/thaumcraft/handlers/Arcane.java index cd14b3b..09d35df 100644 --- a/src/main/java/modtweaker2/mods/thaumcraft/handlers/Arcane.java +++ b/src/main/java/modtweaker2/mods/thaumcraft/handlers/Arcane.java @@ -1,18 +1,21 @@ package modtweaker2.mods.thaumcraft.handlers; +import static modtweaker2.helpers.InputHelper.toIItemStack; import static modtweaker2.helpers.InputHelper.toObjects; import static modtweaker2.helpers.InputHelper.toShapedObjects; import static modtweaker2.helpers.InputHelper.toStack; +import static modtweaker2.helpers.StackHelper.matches; -import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; import minetweaker.MineTweakerAPI; import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; +import modtweaker2.helpers.LogHelper; import modtweaker2.mods.thaumcraft.ThaumcraftHelper; import modtweaker2.utils.BaseListAddition; import modtweaker2.utils.BaseListRemoval; -import modtweaker2.utils.TweakerPlugin; import net.minecraft.item.ItemStack; import stanhebben.zenscript.annotations.ZenClass; import stanhebben.zenscript.annotations.ZenMethod; @@ -23,61 +26,74 @@ import thaumcraft.api.crafting.ShapelessArcaneRecipe; @ZenClass("mods.thaumcraft.Arcane") public class Arcane { + + public static final String name = "Thaumcraft Arcane Worktable"; + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + @ZenMethod public static void addShaped(String key, IItemStack output, String aspects, IIngredient[][] ingredients) { - - MineTweakerAPI.apply(new Add(new ShapedArcaneRecipe(key, toStack(output), ThaumcraftHelper.parseAspects(aspects), toShapedObjects(ingredients)))); + MineTweakerAPI.apply(new Add(new ShapedArcaneRecipe(key, toStack(output), ThaumcraftHelper.parseAspects(aspects), toShapedObjects(ingredients)))); } @ZenMethod public static void addShapeless(String key, IItemStack output, String aspects, IIngredient[] ingredients) { - - MineTweakerAPI.apply(new Add(new ShapelessArcaneRecipe(key, toStack(output), ThaumcraftHelper.parseAspects(aspects), toObjects(ingredients)))); + MineTweakerAPI.apply(new Add(new ShapelessArcaneRecipe(key, toStack(output), ThaumcraftHelper.parseAspects(aspects), toObjects(ingredients)))); } - private static class Add extends BaseListAddition { + private static class Add extends BaseListAddition { public Add(IArcaneRecipe recipe) { - super("Thaumcraft Arcane Worktable", ThaumcraftApi.getCraftingRecipes(), recipe); + super(Arcane.name, ThaumcraftApi.getCraftingRecipes()); + recipes.add(recipe); } @Override - public String getRecipeInfo() { - Object out = ((IArcaneRecipe) recipe).getRecipeOutput(); - if (out instanceof ItemStack) { - return ((ItemStack) out).getDisplayName(); - } else - return super.getRecipeInfo(); + protected String getRecipeInfo(IArcaneRecipe recipe) { + ItemStack stack = recipe.getRecipeOutput(); + + if(stack instanceof ItemStack) + return LogHelper.getStackDescription(stack); + else + return "Unknown output"; } } - // /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @ZenMethod - public static void removeRecipe(IItemStack output) { - MineTweakerAPI.apply(new Remove(toStack(output))); + public static void removeRecipe(IIngredient output) { + List recipes = new LinkedList(); + + for(Object o : ThaumcraftApi.getCraftingRecipes()) { + if(o != null && o instanceof IArcaneRecipe) { + IArcaneRecipe recipe = (IArcaneRecipe)o; + + if(recipe.getRecipeOutput() != null && matches(output, toIItemStack(recipe.getRecipeOutput()))) { + recipes.add(recipe); + } + } + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new Remove(recipes)); + } else { + LogHelper.logWarning(String.format("No %s Recipe found for %s. Command Ignored", Arcane.name, output.toString())); + } } - private static class Remove extends BaseListRemoval { - public Remove(ItemStack stack) { - super("Thaumcraft Arcane Worktable", ThaumcraftApi.getCraftingRecipes(), stack); + private static class Remove extends BaseListRemoval { + public Remove(List recipes) { + super(Arcane.name, ThaumcraftApi.getCraftingRecipes(), recipes); } - @Override - public void apply() { - for (Object o : ThaumcraftApi.getCraftingRecipes()) { - if (o != null && o instanceof IArcaneRecipe) { - IArcaneRecipe r = (IArcaneRecipe) o; - if (r.getRecipeOutput() != null && r.getRecipeOutput().isItemEqual(stack)) { - recipes.add(r); - } - } - } - super.apply(); - } - - @Override - public String getRecipeInfo() { - return stack.getDisplayName(); - } + @Override + protected String getRecipeInfo(IArcaneRecipe recipe) { + ItemStack stack = recipe.getRecipeOutput(); + + if(stack instanceof ItemStack) + return LogHelper.getStackDescription(stack); + else + return "Unknown output"; + } } } diff --git a/src/main/java/modtweaker2/mods/thaumcraft/handlers/Aspects.java b/src/main/java/modtweaker2/mods/thaumcraft/handlers/Aspects.java index 0d4d680..ad11a57 100644 --- a/src/main/java/modtweaker2/mods/thaumcraft/handlers/Aspects.java +++ b/src/main/java/modtweaker2/mods/thaumcraft/handlers/Aspects.java @@ -7,9 +7,7 @@ import java.util.Arrays; import minetweaker.MineTweakerAPI; import minetweaker.api.item.IItemStack; import modtweaker2.mods.thaumcraft.ThaumcraftHelper; -import modtweaker2.utils.BaseDescriptionAddition; -import modtweaker2.utils.BaseDescriptionRemoval; -import modtweaker2.utils.TweakerPlugin; +import modtweaker2.utils.BaseUndoable; import net.minecraft.entity.EntityList; import net.minecraft.item.ItemStack; import stanhebben.zenscript.annotations.ZenClass; @@ -34,7 +32,7 @@ public class Aspects { } // Adds or sets Aspects - private static class Add extends BaseDescriptionAddition { + private static class Add extends BaseUndoable { private final ItemStack stack; private final String aspects; private final boolean replace; @@ -55,7 +53,10 @@ public class Aspects { newList = ThaumcraftHelper.parseAspects(oldList, aspects); else newList = ThaumcraftHelper.parseAspects(aspects); + ThaumcraftApi.objectTags.put(Arrays.asList(stack.getItem(), stack.getItemDamage()), newList); + + success = true; } @Override @@ -80,7 +81,7 @@ public class Aspects { MineTweakerAPI.apply(new Remove(toStack(stack), aspects)); } - private static class Remove extends BaseDescriptionRemoval { + private static class Remove extends BaseUndoable { private final ItemStack stack; private final String aspects; private AspectList oldList; @@ -99,6 +100,8 @@ public class Aspects { newList = ThaumcraftHelper.removeAspects(oldList, aspects); ThaumcraftApi.objectTags.put(Arrays.asList(stack.getItem(), stack.getItemDamage()), newList); } + + success = true; } @Override @@ -139,7 +142,7 @@ public class Aspects { } // Adds or sets Aspects - private static class AddEntity extends BaseDescriptionAddition { + private static class AddEntity extends BaseUndoable { private final String entityName; private final String aspects; private final boolean replace; @@ -162,6 +165,8 @@ public class Aspects { newList = ThaumcraftHelper.parseAspects(aspects); ThaumcraftHelper.removeEntityAspects(entityName); ThaumcraftApi.registerEntityTag(entityName, newList); + + success = true; } @Override @@ -189,7 +194,7 @@ public class Aspects { MineTweakerAPI.apply(new RemoveEntity(entityName, aspects)); } - private static class RemoveEntity extends BaseDescriptionRemoval { + private static class RemoveEntity extends BaseUndoable { private final String entityName; private final String aspects; private AspectList oldList; @@ -209,6 +214,8 @@ public class Aspects { ThaumcraftHelper.removeEntityAspects(entityName); ThaumcraftApi.registerEntityTag(entityName, newList); } + + success = true; } @Override diff --git a/src/main/java/modtweaker2/mods/thaumcraft/handlers/Crucible.java b/src/main/java/modtweaker2/mods/thaumcraft/handlers/Crucible.java index 29b0dd6..f362ab0 100644 --- a/src/main/java/modtweaker2/mods/thaumcraft/handlers/Crucible.java +++ b/src/main/java/modtweaker2/mods/thaumcraft/handlers/Crucible.java @@ -1,16 +1,20 @@ package modtweaker2.mods.thaumcraft.handlers; +import static modtweaker2.helpers.InputHelper.toIItemStack; import static modtweaker2.helpers.InputHelper.toObject; import static modtweaker2.helpers.InputHelper.toStack; -import static modtweaker2.helpers.StackHelper.areEqual; +import static modtweaker2.helpers.StackHelper.matches; + +import java.util.LinkedList; +import java.util.List; + import minetweaker.MineTweakerAPI; import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; +import modtweaker2.helpers.LogHelper; import modtweaker2.mods.thaumcraft.ThaumcraftHelper; import modtweaker2.utils.BaseListAddition; import modtweaker2.utils.BaseListRemoval; -import modtweaker2.utils.TweakerPlugin; -import net.minecraft.item.ItemStack; import stanhebben.zenscript.annotations.ZenClass; import stanhebben.zenscript.annotations.ZenMethod; import thaumcraft.api.ThaumcraftApi; @@ -18,52 +22,56 @@ import thaumcraft.api.crafting.CrucibleRecipe; @ZenClass("mods.thaumcraft.Crucible") public class Crucible { + + public static final String name = "Thaumcraft Crucible"; + @ZenMethod public static void addRecipe(String key, IItemStack result, IIngredient catalyst, String aspects) { - - MineTweakerAPI.apply(new Add(new CrucibleRecipe(key, toStack(result), toObject(catalyst), ThaumcraftHelper.parseAspects(aspects)))); + MineTweakerAPI.apply(new Add(new CrucibleRecipe(key, toStack(result), toObject(catalyst), ThaumcraftHelper.parseAspects(aspects)))); } - - private static class Add extends BaseListAddition { + + private static class Add extends BaseListAddition { public Add(CrucibleRecipe recipe) { - super("Thaumcraft Crucible", ThaumcraftApi.getCraftingRecipes(), recipe); + super(Crucible.name, ThaumcraftApi.getCraftingRecipes()); + recipes.add(recipe); } - + @Override - public String getRecipeInfo() { - return ((CrucibleRecipe) recipe).getRecipeOutput().getDisplayName(); + protected String getRecipeInfo(CrucibleRecipe recipe) { + return LogHelper.getStackDescription(recipe.getRecipeOutput()); } } // /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @ZenMethod - public static void removeRecipe(IItemStack output) { - - MineTweakerAPI.apply(new Remove(toStack(output))); + public static void removeRecipe(IIngredient output) { + List recipes = new LinkedList(); + + for (Object o : ThaumcraftApi.getCraftingRecipes()) { + if (o instanceof CrucibleRecipe) { + CrucibleRecipe r = (CrucibleRecipe) o; + if (r.getRecipeOutput() != null && matches(output, toIItemStack(r.getRecipeOutput()))) { + recipes.add(r); + } + } + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new Remove(recipes)); + } else { + LogHelper.logWarning(String.format("No %s Recipe found for %s. Command ignored!", Crucible.name, output.toString())); + } } - private static class Remove extends BaseListRemoval { - public Remove(ItemStack stack) { - super("Thaumcraft Crucible", ThaumcraftApi.getCraftingRecipes(), stack); + private static class Remove extends BaseListRemoval { + public Remove(List recipes) { + super(Crucible.name, ThaumcraftApi.getCraftingRecipes(), recipes); } - @Override - public void apply() { - for (Object o : ThaumcraftApi.getCraftingRecipes()) { - if (o instanceof CrucibleRecipe) { - CrucibleRecipe r = (CrucibleRecipe) o; - if (r.getRecipeOutput() != null && areEqual(r.getRecipeOutput(), stack)) { - recipes.add(r); - } - } - } - super.apply(); - } - - @Override - public String getRecipeInfo() { - return stack.getDisplayName(); - } + @Override + protected String getRecipeInfo(CrucibleRecipe recipe) { + return LogHelper.getStackDescription(recipe.getRecipeOutput()); + } } } diff --git a/src/main/java/modtweaker2/mods/thaumcraft/handlers/Infusion.java b/src/main/java/modtweaker2/mods/thaumcraft/handlers/Infusion.java index 9515cd0..f242d4e 100644 --- a/src/main/java/modtweaker2/mods/thaumcraft/handlers/Infusion.java +++ b/src/main/java/modtweaker2/mods/thaumcraft/handlers/Infusion.java @@ -1,16 +1,21 @@ package modtweaker2.mods.thaumcraft.handlers; +import static modtweaker2.helpers.InputHelper.toIItemStack; import static modtweaker2.helpers.InputHelper.toStack; import static modtweaker2.helpers.InputHelper.toStacks; -import static modtweaker2.helpers.StackHelper.areEqual; -import minetweaker.IUndoableAction; +import static modtweaker2.helpers.StackHelper.matches; + +import java.util.LinkedList; +import java.util.List; + import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; +import modtweaker2.helpers.LogHelper; import modtweaker2.mods.thaumcraft.ThaumcraftHelper; import modtweaker2.mods.thaumcraft.recipe.MTInfusionRecipe; import modtweaker2.utils.BaseListAddition; import modtweaker2.utils.BaseListRemoval; -import modtweaker2.utils.TweakerPlugin; import net.minecraft.enchantment.Enchantment; import net.minecraft.item.ItemStack; import stanhebben.zenscript.annotations.ZenClass; @@ -21,6 +26,10 @@ import thaumcraft.api.crafting.InfusionRecipe; @ZenClass("mods.thaumcraft.Infusion") public class Infusion { + + public static final String name = "Thaumcraft Infusion"; + public static final String enchName = name + " Enchantment"; + @ZenMethod public static void addRecipe(String key, IItemStack input, IItemStack[] recipe, String aspects, IItemStack result, int instability) { @@ -31,131 +40,112 @@ public class Infusion { // fuzzy or not @ZenMethod public static void addRecipe(String key, IItemStack input, IItemStack[] recipe, String aspects, IItemStack result, int instability, boolean fuzzyCentre, boolean[] fuzzyRecipe) { - - MineTweakerAPI.apply(new Add(new MTInfusionRecipe(key, toStack(result), instability, ThaumcraftHelper.parseAspects(aspects), toStack(input), toStacks(recipe), fuzzyCentre, fuzzyRecipe))); + MineTweakerAPI.apply(new Add(new MTInfusionRecipe(key, toStack(result), instability, ThaumcraftHelper.parseAspects(aspects), toStack(input), toStacks(recipe), fuzzyCentre, fuzzyRecipe))); } @ZenMethod public static void addEnchantment(String key, int enchantID, int instability, String aspects, IItemStack[] recipe) { - - MineTweakerAPI.apply(new AddEnchant(new InfusionEnchantmentRecipe(key, Enchantment.enchantmentsList[enchantID], instability, ThaumcraftHelper.parseAspects(aspects), toStacks(recipe)))); + MineTweakerAPI.apply(new AddEnchant(new InfusionEnchantmentRecipe(key, Enchantment.enchantmentsList[enchantID], instability, ThaumcraftHelper.parseAspects(aspects), toStacks(recipe)))); } - private static class Add extends BaseListAddition { + private static class Add extends BaseListAddition { public Add(InfusionRecipe recipe) { - super("Thaumcraft Infusion", ThaumcraftApi.getCraftingRecipes(), recipe); + super(Infusion.name, ThaumcraftApi.getCraftingRecipes()); + recipes.add(recipe); } @Override - public String getRecipeInfo() { - Object out = ((InfusionRecipe) recipe).getRecipeOutput(); - if (out instanceof ItemStack) { - return ((ItemStack) out).getDisplayName(); - } else - return super.getRecipeInfo(); + protected String getRecipeInfo(InfusionRecipe recipe) { + Object out = ((InfusionRecipe) recipe).getRecipeOutput(); + if (out instanceof ItemStack) { + return ((ItemStack) out).getDisplayName(); + } else + return "Unknown item"; } } - private static class AddEnchant implements IUndoableAction { - InfusionEnchantmentRecipe recipe; - + private static class AddEnchant extends BaseListAddition { public AddEnchant(InfusionEnchantmentRecipe inp) { - recipe = inp; - } - - @Override - public void apply() { - ThaumcraftApi.getCraftingRecipes().add(recipe); - } - - @Override - public String describe() { - return "Adding Infusion Enchantment Recipe: " + recipe.enchantment.getName(); - } - - @Override - public boolean canUndo() { - return recipe != null; - } - - @Override - public void undo() { - ThaumcraftApi.getCraftingRecipes().remove(recipe); - } - - @Override - public String describeUndo() { - return "Removing Infusion Enchantment Recipe: " + recipe.enchantment.getName(); - } - - @Override - public String getOverrideKey() { - return null; + super(Infusion.enchName, ThaumcraftApi.getCraftingRecipes()); + recipes.add(inp); } + + @Override + protected String getRecipeInfo(InfusionEnchantmentRecipe recipe) { + return recipe.getEnchantment().getName(); + } } // /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @ZenMethod - public static void removeRecipe(IItemStack output) { - - MineTweakerAPI.apply(new Remove(toStack(output))); + public static void removeRecipe(IIngredient output) { + List recipes = new LinkedList(); + + for (Object o : ThaumcraftApi.getCraftingRecipes()) { + if (o instanceof InfusionRecipe) { + InfusionRecipe r = (InfusionRecipe) o; + if (r.getRecipeOutput() != null && r.getRecipeOutput() instanceof ItemStack && matches(output, toIItemStack((ItemStack)r.getRecipeOutput()))) { + recipes.add(r); + } + } + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new Remove(recipes)); + } else { + LogHelper.logWarning(String.format("No %s Recipe found for %s. Command ignored!", Infusion.name, output.toString())); + } } @ZenMethod public static void removeEnchant(int id) { - - MineTweakerAPI.apply(new RemoveEnchant(Enchantment.enchantmentsList[id])); - } - - private static class Remove extends BaseListRemoval { - public Remove(ItemStack stack) { - super("Thaumcraft Infusion", ThaumcraftApi.getCraftingRecipes(), stack); - } - - @Override - public void apply() { - for (Object o : ThaumcraftApi.getCraftingRecipes()) { - if (o instanceof InfusionRecipe) { - InfusionRecipe r = (InfusionRecipe) o; - if (r.getRecipeOutput() != null && r.getRecipeOutput() instanceof ItemStack && areEqual((ItemStack) r.getRecipeOutput(), stack)) { - recipes.add(r); - } - } - } - super.apply(); - } - - @Override - public String getRecipeInfo() { - return stack.getDisplayName(); - } - } - - private static class RemoveEnchant extends BaseListRemoval { - Enchantment enchant; - - public RemoveEnchant(Enchantment ench) { - super("Thaumcraft Infusion Enchantment", ThaumcraftApi.getCraftingRecipes()); - enchant = ench; - } - - @Override - public void apply() { - for (Object recipe : list) { - if (recipe instanceof InfusionEnchantmentRecipe) { - InfusionEnchantmentRecipe enchRecipe = (InfusionEnchantmentRecipe) recipe; - if (enchRecipe.getEnchantment() == enchant) { - recipes.add(enchRecipe); - } - } - } - super.apply(); - } - - @Override - public String getRecipeInfo() { - return enchant.getName(); + List recipes = new LinkedList(); + Enchantment ench = Enchantment.enchantmentsList[id]; + + for (Object recipe : ThaumcraftApi.getCraftingRecipes()) { + if (recipe instanceof InfusionEnchantmentRecipe) { + InfusionEnchantmentRecipe enchRecipe = (InfusionEnchantmentRecipe) recipe; + if (enchRecipe.getEnchantment() == ench) { + recipes.add(enchRecipe); + } + } + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new RemoveEnchant(recipes)); + } else { + LogHelper.logWarning(String.format("No %s Recipe found for %s. Command ignored!", Infusion.enchName + " Enchantment", ench.getName())); } } + + private static class Remove extends BaseListRemoval { + public Remove(List recipes) { + super(Infusion.name, ThaumcraftApi.getCraftingRecipes(), recipes); + } + + @Override + protected String getRecipeInfo(InfusionRecipe recipe) { + + Object o = recipe.getRecipeOutput(); + + if(o instanceof ItemStack) { + return LogHelper.getStackDescription((ItemStack)o); + } else { + return "Unknown Item"; + } + } + } + + private static class RemoveEnchant extends BaseListRemoval { + public RemoveEnchant(List recipes) { + super(Infusion.enchName, ThaumcraftApi.getCraftingRecipes(), recipes); + } + + @Override + protected String getRecipeInfo(InfusionEnchantmentRecipe recipe) { + return recipe.getEnchantment().getName(); + } + } } + diff --git a/src/main/java/modtweaker2/mods/thaumcraft/handlers/Loot.java b/src/main/java/modtweaker2/mods/thaumcraft/handlers/Loot.java index 5af1a2c..4a67908 100644 --- a/src/main/java/modtweaker2/mods/thaumcraft/handlers/Loot.java +++ b/src/main/java/modtweaker2/mods/thaumcraft/handlers/Loot.java @@ -1,20 +1,28 @@ package modtweaker2.mods.thaumcraft.handlers; +import static modtweaker2.helpers.InputHelper.toIItemStack; +import static modtweaker2.helpers.StackHelper.matches; + +import java.util.LinkedList; import java.util.List; -import net.minecraft.item.ItemStack; import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; import modtweaker2.helpers.InputHelper; +import modtweaker2.helpers.LogHelper; import modtweaker2.utils.BaseListAddition; import modtweaker2.utils.BaseListRemoval; import stanhebben.zenscript.annotations.ZenClass; import stanhebben.zenscript.annotations.ZenMethod; -import thaumcraft.api.ThaumcraftApi; import thaumcraft.api.internal.WeightedRandomLoot; @ZenClass("mods.thaumcraft.Loot") public class Loot { + + public static final String name = "Thaumcraft LootBag"; + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @ZenMethod public static void addCommonLoot(IItemStack stack, int weight) { @@ -31,40 +39,59 @@ public class Loot { MineTweakerAPI.apply(new Add(WeightedRandomLoot.lootBagRare, new WeightedRandomLoot(InputHelper.toStack(stack), weight))); } - public static class Add extends BaseListAddition { - public Add(List list, WeightedRandomLoot recipe) { - super(list, recipe); + public static class Add extends BaseListAddition { + public Add(List list, WeightedRandomLoot recipe) { + super(Loot.name, list); + recipes.add(recipe); } + + @Override + protected String getRecipeInfo(WeightedRandomLoot recipe) { + return LogHelper.getStackDescription(recipe.item); + } + } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + @ZenMethod + public static void removeCommonLoot(IIngredient stack) { + removeLoot(WeightedRandomLoot.lootBagCommon, stack); } @ZenMethod - public static void removeCommonLoot(IItemStack stack) { - MineTweakerAPI.apply(new Remove(WeightedRandomLoot.lootBagCommon, InputHelper.toStack(stack))); + public static void removeUncommonLoot(IIngredient stack) { + removeLoot(WeightedRandomLoot.lootBagUncommon, stack); } @ZenMethod - public static void removeUncommonLoot(IItemStack stack) { - MineTweakerAPI.apply(new Remove(WeightedRandomLoot.lootBagUncommon, InputHelper.toStack(stack))); + public static void removeRareLoot(IIngredient stack) { + removeLoot(WeightedRandomLoot.lootBagRare, stack); + } + + public static void removeLoot(List list, IIngredient ingredient) { + List recipes = new LinkedList(); + + for (WeightedRandomLoot loot : list) { + if (matches(ingredient, toIItemStack(loot.item))) { + recipes.add(loot); + } + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new Remove(list, recipes)); + } else { + LogHelper.logWarning(String.format("No %s Recipe found for %s. Command ignored.", Loot.name, ingredient.toString())); + } } - @ZenMethod - public static void removeRareLoot(IItemStack stack) { - MineTweakerAPI.apply(new Remove(WeightedRandomLoot.lootBagRare, InputHelper.toStack(stack))); - } - - public static class Remove extends BaseListRemoval { - public Remove(List list, ItemStack stack) { - super(list, stack); + public static class Remove extends BaseListRemoval { + public Remove(List list, List recipes) { + super(Loot.name, list, recipes); } - + @Override - public void apply() { - for (WeightedRandomLoot stack : (List) list) { - if (stack.item.isItemEqual(this.stack)) { - recipes.add(stack); - } - } - super.apply(); + protected String getRecipeInfo(WeightedRandomLoot recipe) { + return LogHelper.getStackDescription(recipe.item); } } } diff --git a/src/main/java/modtweaker2/mods/thaumcraft/handlers/Research.java b/src/main/java/modtweaker2/mods/thaumcraft/handlers/Research.java index 5fc8ca7..e4447c0 100644 --- a/src/main/java/modtweaker2/mods/thaumcraft/handlers/Research.java +++ b/src/main/java/modtweaker2/mods/thaumcraft/handlers/Research.java @@ -21,7 +21,6 @@ import modtweaker2.mods.thaumcraft.research.RemoveResearch; import modtweaker2.mods.thaumcraft.research.RemoveTab; import modtweaker2.mods.thaumcraft.research.SetAspects; import modtweaker2.mods.thaumcraft.research.SetResearch; -import modtweaker2.utils.TweakerPlugin; import net.minecraft.enchantment.Enchantment; import net.minecraft.util.ResourceLocation; import stanhebben.zenscript.annotations.Optional; diff --git a/src/main/java/modtweaker2/mods/thaumcraft/handlers/Warp.java b/src/main/java/modtweaker2/mods/thaumcraft/handlers/Warp.java index 300cab0..fb82247 100644 --- a/src/main/java/modtweaker2/mods/thaumcraft/handlers/Warp.java +++ b/src/main/java/modtweaker2/mods/thaumcraft/handlers/Warp.java @@ -10,7 +10,6 @@ import minetweaker.IUndoableAction; import minetweaker.MineTweakerAPI; import minetweaker.api.item.IItemStack; import modtweaker2.mods.thaumcraft.ThaumcraftHelper; -import modtweaker2.utils.TweakerPlugin; import net.minecraft.item.ItemStack; import stanhebben.zenscript.annotations.ZenClass; import stanhebben.zenscript.annotations.ZenMethod; diff --git a/src/main/java/modtweaker2/mods/thaumcraft/research/AddPage.java b/src/main/java/modtweaker2/mods/thaumcraft/research/AddPage.java index 88c83ee..b8d6867 100644 --- a/src/main/java/modtweaker2/mods/thaumcraft/research/AddPage.java +++ b/src/main/java/modtweaker2/mods/thaumcraft/research/AddPage.java @@ -1,5 +1,6 @@ package modtweaker2.mods.thaumcraft.research; +import static modtweaker2.helpers.StackHelper.areEqual; import minetweaker.IUndoableAction; import modtweaker2.mods.thaumcraft.ThaumcraftHelper; import net.minecraft.enchantment.Enchantment; @@ -14,8 +15,6 @@ import thaumcraft.api.crafting.InfusionRecipe; import thaumcraft.api.research.ResearchCategories; import thaumcraft.api.research.ResearchPage; import thaumcraft.api.research.ResearchPage.PageType; -import static modtweaker2.helpers.InputHelper.*; -import static modtweaker2.helpers.StackHelper.*; public class AddPage implements IUndoableAction { String key; diff --git a/src/main/java/modtweaker2/mods/thaumcraft/research/RefreshResearch.java b/src/main/java/modtweaker2/mods/thaumcraft/research/RefreshResearch.java index c430b21..251cad5 100644 --- a/src/main/java/modtweaker2/mods/thaumcraft/research/RefreshResearch.java +++ b/src/main/java/modtweaker2/mods/thaumcraft/research/RefreshResearch.java @@ -1,5 +1,6 @@ package modtweaker2.mods.thaumcraft.research; +import static modtweaker2.helpers.StackHelper.areEqual; import minetweaker.IUndoableAction; import modtweaker2.mods.thaumcraft.ThaumcraftHelper; import net.minecraft.item.ItemStack; @@ -12,8 +13,6 @@ import thaumcraft.api.crafting.InfusionRecipe; import thaumcraft.api.research.ResearchCategories; import thaumcraft.api.research.ResearchItem; import thaumcraft.api.research.ResearchPage; -import static modtweaker2.helpers.InputHelper.*; -import static modtweaker2.helpers.StackHelper.*; public class RefreshResearch implements IUndoableAction { String research; diff --git a/src/main/java/modtweaker2/mods/thaumcraft/research/commands/ResearchLogger.java b/src/main/java/modtweaker2/mods/thaumcraft/research/commands/ResearchLogger.java index 6763068..d19b52c 100644 --- a/src/main/java/modtweaker2/mods/thaumcraft/research/commands/ResearchLogger.java +++ b/src/main/java/modtweaker2/mods/thaumcraft/research/commands/ResearchLogger.java @@ -1,11 +1,11 @@ package modtweaker2.mods.thaumcraft.research.commands; +import static modtweaker2.helpers.LogHelper.log; +import static modtweaker2.helpers.LogHelper.logPrinted; import minetweaker.MineTweakerAPI; import minetweaker.api.player.IPlayer; import minetweaker.api.server.ICommandFunction; import thaumcraft.api.research.ResearchCategories; -import static modtweaker2.helpers.InputHelper.*; -import static modtweaker2.helpers.LogHelper.*; public class ResearchLogger implements ICommandFunction { @Override diff --git a/src/main/java/modtweaker2/mods/thermalexpansion/ThermalExpansion.java b/src/main/java/modtweaker2/mods/thermalexpansion/ThermalExpansion.java index 69436ed..fc1f304 100644 --- a/src/main/java/modtweaker2/mods/thermalexpansion/ThermalExpansion.java +++ b/src/main/java/modtweaker2/mods/thermalexpansion/ThermalExpansion.java @@ -3,6 +3,7 @@ package modtweaker2.mods.thermalexpansion; import minetweaker.MineTweakerAPI; import modtweaker2.mods.thermalexpansion.handlers.Crucible; import modtweaker2.mods.thermalexpansion.handlers.Furnace; +import modtweaker2.mods.thermalexpansion.handlers.Insolator; import modtweaker2.mods.thermalexpansion.handlers.Pulverizer; import modtweaker2.mods.thermalexpansion.handlers.Sawmill; import modtweaker2.mods.thermalexpansion.handlers.Smelter; @@ -13,6 +14,7 @@ public class ThermalExpansion { public ThermalExpansion(){ MineTweakerAPI.registerClass(Crucible.class); MineTweakerAPI.registerClass(Furnace.class); + MineTweakerAPI.registerClass(Insolator.class); MineTweakerAPI.registerClass(Pulverizer.class); MineTweakerAPI.registerClass(Sawmill.class); MineTweakerAPI.registerClass(Smelter.class); diff --git a/src/main/java/modtweaker2/mods/thermalexpansion/ThermalHelper.java b/src/main/java/modtweaker2/mods/thermalexpansion/ThermalHelper.java index 2afd17b..8b4b867 100644 --- a/src/main/java/modtweaker2/mods/thermalexpansion/ThermalHelper.java +++ b/src/main/java/modtweaker2/mods/thermalexpansion/ThermalHelper.java @@ -1,79 +1,259 @@ package modtweaker2.mods.thermalexpansion; import static modtweaker2.helpers.ReflectionHelper.getConstructor; -import static modtweaker2.helpers.ReflectionHelper.getStaticObject; +import static modtweaker2.helpers.StackHelper.areEqualOrNull; import java.lang.reflect.Constructor; -import java.util.List; -import java.util.Map; -import java.util.Set; +import modtweaker2.helpers.LogHelper; import net.minecraft.item.ItemStack; import net.minecraftforge.fluids.FluidStack; -import cofh.lib.inventory.ComparableItemStackSafe; +import cofh.thermalexpansion.util.crafting.CrucibleManager.RecipeCrucible; +import cofh.thermalexpansion.util.crafting.FurnaceManager.RecipeFurnace; +import cofh.thermalexpansion.util.crafting.InsolatorManager.RecipeInsolator; +import cofh.thermalexpansion.util.crafting.PulverizerManager.RecipePulverizer; +import cofh.thermalexpansion.util.crafting.SawmillManager.RecipeSawmill; 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 crucible; - - private static Map transposerFill; - private static Map transposerEmpty; - public static Set transposerValid; - - public static Constructor smelterRecipe; - public static Constructor transposerRecipe; - + public static Constructor crucibleRecipe; + public static Constructor furanceRecipe; + public static Constructor insolatorRecipe; + public static Constructor pulverizerRecipe; + public static Constructor sawmillRecipe; + public static Constructor smelterRecipe; + public static Constructor transposerRecipe; + static { try { - smelterRecipe = getConstructor("cofh.thermalexpansion.util.crafting.SmelterManager$RecipeSmelter", ItemStack.class, ItemStack.class, ItemStack.class, ItemStack.class, int.class, int.class); - transposerRecipe = getConstructor("cofh.thermalexpansion.util.crafting.TransposerManager$RecipeTransposer", ItemStack.class, ItemStack.class, FluidStack.class, int.class, int.class); - } catch (Exception e) {} + crucibleRecipe = getConstructor(RecipeCrucible.class, ItemStack.class, FluidStack.class, int.class); + furanceRecipe = getConstructor(RecipeFurnace.class, ItemStack.class, ItemStack.class, int.class); + insolatorRecipe = getConstructor(RecipeInsolator.class, ItemStack.class, ItemStack.class, ItemStack.class, ItemStack.class, int.class, int.class); + pulverizerRecipe = getConstructor(RecipePulverizer.class, ItemStack.class, ItemStack.class, ItemStack.class, int.class, int.class); + sawmillRecipe = getConstructor(RecipeSawmill.class, ItemStack.class, ItemStack.class, ItemStack.class, int.class, int.class); + smelterRecipe = getConstructor(RecipeSmelter.class, ItemStack.class, ItemStack.class, ItemStack.class, ItemStack.class, int.class, int.class); + transposerRecipe = getConstructor(RecipeTransposer.class, ItemStack.class, ItemStack.class, FluidStack.class, int.class, int.class); + } catch (Exception e) { LogHelper.logError("Exception getting constructor for Thermal Expansion recipes!", e); } } - /** 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() { - try { - smelter = getStaticObject(Class.forName("cofh.thermalexpansion.util.crafting.SmelterManager"), "recipeMap"); - smelterValid = getStaticObject(Class.forName("cofh.thermalexpansion.util.crafting.SmelterManager"), "validationSet"); - } catch (Exception e) {} + public static boolean equals(RecipeCrucible r1, RecipeCrucible r2) { + if(r1 == r2) { + return true; + } + + if(r1 == null || r2 == null) { + return false; + } + + if(!areEqualOrNull(r1.getInput(), r2.getInput())) { + return false; + } + + if(r1.getEnergy() != r2.getEnergy()) { + return false; + } + + if(!areEqualOrNull(r1.getOutput(), r2.getOutput())) { + return false; + } - return smelter; + return true; } - - public static Map getFillMap() { - try { - transposerFill = getStaticObject(Class.forName("cofh.thermalexpansion.util.crafting.TransposerManager"), "recipeMapFill"); - transposerValid = getStaticObject(Class.forName("cofh.thermalexpansion.util.crafting.TransposerManager"), "validationSet"); - } catch (Exception e) {} - - return transposerFill; + + public static boolean equals(RecipeFurnace r1, RecipeFurnace r2) { + if(r1 == r2) { + return true; + } + + if(r1 == null || r2 == null) { + return false; + } + + if(!areEqualOrNull(r1.getInput(), r2.getInput())) { + return false; + } + + if(r1.getEnergy() != r2.getEnergy()) { + return false; + } + + if(!areEqualOrNull(r1.getOutput(), r2.getOutput())) { + return false; + } + + return true; } - - public static Map getExtractMap() { - try { - transposerEmpty = getStaticObject(Class.forName("cofh.thermalexpansion.util.crafting.TransposerManager"), "recipeMapExtraction"); - transposerValid = getStaticObject(Class.forName("cofh.thermalexpansion.util.crafting.TransposerManager"), "validationSet"); - } catch (Exception e) {} - - return transposerEmpty; + + public static boolean equals(RecipePulverizer r1, RecipePulverizer r2) { + if(r1 == r2) { + return true; + } + + if(r1 == null || r2 == null) { + return false; + } + + if(!areEqualOrNull(r1.getInput(), r2.getInput())) { + return false; + } + + if(r1.getEnergy() != r2.getEnergy()) { + return false; + } + + if(!areEqualOrNull(r1.getPrimaryOutput(), r2.getPrimaryOutput())) { + return false; + } + + if(!areEqualOrNull(r1.getSecondaryOutput(), r2.getSecondaryOutput())) { + return false; + } + + if(r1.getSecondaryOutput() != null && r2.getSecondaryOutput() != null && + r1.getSecondaryOutputChance() != r2.getSecondaryOutputChance()) { + return false; + } + + return true; } - - public static boolean removeCrucibleRecipe(ItemStack input) { - try { - crucible = getStaticObject(Class.forName("cofh.thermalexpansion.util.crafting.CrucibleManager"), "recipeMap"); - } catch (Exception e) {} - - return crucible.remove(new ComparableItemStackSafe(input)) != null; + + public static boolean equals(RecipeSawmill r1, RecipeSawmill r2) { + if(r1 == r2) { + return true; + } + + if(r1 == null || r2 == null) { + return false; + } + + if(!areEqualOrNull(r1.getInput(), r2.getInput())) { + return false; + } + + if(r1.getEnergy() != r2.getEnergy()) { + return false; + } + + if(!areEqualOrNull(r1.getPrimaryOutput(), r2.getPrimaryOutput())) { + return false; + } + + if(!areEqualOrNull(r1.getSecondaryOutput(), r2.getSecondaryOutput())) { + return false; + } + + if(r1.getSecondaryOutput() != null && r2.getSecondaryOutput() != null && + r1.getSecondaryOutputChance() != r2.getSecondaryOutputChance()) { + return false; + } + + return true; } - - public static Object getTERecipe(Constructor constructor, Object... items) { - try { - return constructor.newInstance(items); - } catch (Exception e) {} - - return null; + + public static boolean equals(RecipeSmelter r1, RecipeSmelter r2) { + if(r1 == r2) { + return true; + } + + if(r1 == null || r2 == null) { + return false; + } + + if(!areEqualOrNull(r1.getPrimaryInput(), r2.getPrimaryInput())) { + return false; + } + + if(!areEqualOrNull(r1.getSecondaryInput(), r2.getSecondaryInput())) { + return false; + } + + if(r1.getEnergy() != r2.getEnergy()) { + return false; + } + + if(!areEqualOrNull(r1.getPrimaryOutput(), r2.getPrimaryOutput())) { + return false; + } + + if(!areEqualOrNull(r1.getSecondaryOutput(), r2.getSecondaryOutput())) { + return false; + } + + if(r1.getSecondaryOutput() != null && r2.getSecondaryOutput() != null && + r1.getSecondaryOutputChance() != r2.getSecondaryOutputChance()) { + return false; + } + + return true; + } + + public static boolean equals(RecipeInsolator r1, RecipeInsolator r2) { + if(r1 == r2) { + return true; + } + + if(r1 == null || r2 == null) { + return false; + } + + if(!areEqualOrNull(r1.getPrimaryInput(), r2.getPrimaryInput())) { + return false; + } + + if(!areEqualOrNull(r1.getSecondaryInput(), r2.getSecondaryInput())) { + return false; + } + + if(r1.getEnergy() != r2.getEnergy()) { + return false; + } + + if(!areEqualOrNull(r1.getPrimaryOutput(), r2.getPrimaryOutput())) { + return false; + } + + if(!areEqualOrNull(r1.getSecondaryOutput(), r2.getSecondaryOutput())) { + return false; + } + + if(r1.getSecondaryOutput() != null && r2.getSecondaryOutput() != null && + r1.getSecondaryOutputChance() != r2.getSecondaryOutputChance()) { + return false; + } + + return true; + } + + public static boolean equals(RecipeTransposer r1, RecipeTransposer r2) { + if(r1 == r2) { + return true; + } + + if(r1 == null || r2 == null) { + return false; + } + + if(!areEqualOrNull(r1.getInput(), r2.getInput())) { + return false; + } + + if(r1.getEnergy() != r2.getEnergy()) { + return false; + } + + if(!areEqualOrNull(r1.getOutput(), r2.getOutput())) { + return false; + } + + if(!areEqualOrNull(r1.getFluid(), r2.getFluid())) { + return false; + } + + if(r1.getChance() != r2.getChance()) { + return false; + } + + return true; } } 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..ac90c1c --- /dev/null +++ b/src/main/java/modtweaker2/mods/thermalexpansion/commands/ThermalExpansionLogger.java @@ -0,0 +1,168 @@ +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.LogHelper; +import modtweaker2.helpers.StringHelper; +import cofh.thermalexpansion.util.crafting.CrucibleManager; +import cofh.thermalexpansion.util.crafting.CrucibleManager.RecipeCrucible; +import cofh.thermalexpansion.util.crafting.FurnaceManager; +import cofh.thermalexpansion.util.crafting.FurnaceManager.RecipeFurnace; +import cofh.thermalexpansion.util.crafting.InsolatorManager; +import cofh.thermalexpansion.util.crafting.InsolatorManager.RecipeInsolator; +import cofh.thermalexpansion.util.crafting.PulverizerManager; +import cofh.thermalexpansion.util.crafting.PulverizerManager.RecipePulverizer; +import cofh.thermalexpansion.util.crafting.SawmillManager; +import cofh.thermalexpansion.util.crafting.SawmillManager.RecipeSawmill; +import cofh.thermalexpansion.util.crafting.SmelterManager; +import cofh.thermalexpansion.util.crafting.SmelterManager.RecipeSmelter; +import cofh.thermalexpansion.util.crafting.TransposerManager; +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("insolator"); + validArguments.add("pulverizer"); + validArguments.add("sawmill"); + validArguments.add("smelter"); + 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("crucible")) { + for(RecipeCrucible recipe : CrucibleManager.getRecipeList()) { + MineTweakerAPI.logCommand(String.format("mods.thermalexpansion.Crucible.addRecipe(%d, %s, %s);", + recipe.getEnergy(), + LogHelper.getStackDescription(recipe.getInput()), + LogHelper.getStackDescription(recipe.getOutput()))); + } + } + + if(args.isEmpty() || args.contains("furnace")) { + for(RecipeFurnace recipe : FurnaceManager.getRecipeList()) { + MineTweakerAPI.logCommand(String.format("mods.thermalexpansion.Furnace.addRecipe(%d, %s, %s);", + recipe.getEnergy(), + LogHelper.getStackDescription(recipe.getInput()), + LogHelper.getStackDescription(recipe.getOutput()))); + } + } + + if(args.isEmpty() || args.contains("insolator")) { + for(RecipeInsolator recipe : InsolatorManager.getRecipeList()) { + if(recipe.getSecondaryOutput() != null) { + MineTweakerAPI.logCommand(String.format("mods.thermalexpansion.Insolator.addRecipe(%d, %s, %s, %s, %s, %d);", + recipe.getEnergy(), + LogHelper.getStackDescription(recipe.getPrimaryInput()), + LogHelper.getStackDescription(recipe.getSecondaryInput()), + LogHelper.getStackDescription(recipe.getPrimaryOutput()), + LogHelper.getStackDescription(recipe.getSecondaryOutput()), + recipe.getSecondaryOutputChance())); + } else { + MineTweakerAPI.logCommand(String.format("mods.thermalexpansion.Insolator.addRecipe(%d, %s, %s, %s);", + recipe.getEnergy(), + LogHelper.getStackDescription(recipe.getPrimaryInput()), + LogHelper.getStackDescription(recipe.getSecondaryInput()), + LogHelper.getStackDescription(recipe.getPrimaryOutput()))); + } + } + } + + if(args.isEmpty() || args.contains("pulverizer")) { + for(RecipePulverizer recipe : PulverizerManager.getRecipeList()) { + if(recipe.getSecondaryOutput() != null) { + MineTweakerAPI.logCommand(String.format("mods.thermalexpansion.Pulverizer.addRecipe(%d, %s, %s, %s, %d);", + recipe.getEnergy(), + LogHelper.getStackDescription(recipe.getInput()), + LogHelper.getStackDescription(recipe.getPrimaryOutput()), + LogHelper.getStackDescription(recipe.getSecondaryOutput()), + recipe.getSecondaryOutputChance())); + } else { + MineTweakerAPI.logCommand(String.format("mods.thermalexpansion.Pulverizer.addRecipe(%d, %s, %s);", + recipe.getEnergy(), + LogHelper.getStackDescription(recipe.getInput()), + LogHelper.getStackDescription(recipe.getPrimaryOutput()))); + } + } + } + + if(args.isEmpty() || args.contains("sawmill")) { + for(RecipeSawmill recipe : SawmillManager.getRecipeList()) { + if(recipe.getSecondaryOutput() != null) { + MineTweakerAPI.logCommand(String.format("mods.thermalexpansion.Sawmill.addRecipe(%d, %s, %s, %s, %d);", + recipe.getEnergy(), + LogHelper.getStackDescription(recipe.getInput()), + LogHelper.getStackDescription(recipe.getPrimaryOutput()), + LogHelper.getStackDescription(recipe.getSecondaryOutput()), + recipe.getSecondaryOutputChance())); + } else { + MineTweakerAPI.logCommand(String.format("mods.thermalexpansion.Sawmill.addRecipe(%d, %s, %s);", + recipe.getEnergy(), + LogHelper.getStackDescription(recipe.getInput()), + LogHelper.getStackDescription(recipe.getPrimaryOutput()))); + } + } + } + + if(args.isEmpty() || args.contains("smelter")) { + for(RecipeSmelter recipe : SmelterManager.getRecipeList()) { + if(recipe.getSecondaryOutput() != null) { + MineTweakerAPI.logCommand(String.format("mods.thermalexpansion.Smelter.addRecipe(%d, %s, %s, %s, %s, %d);", + recipe.getEnergy(), + LogHelper.getStackDescription(recipe.getPrimaryInput()), + LogHelper.getStackDescription(recipe.getSecondaryInput()), + LogHelper.getStackDescription(recipe.getPrimaryOutput()), + LogHelper.getStackDescription(recipe.getSecondaryOutput()), + recipe.getSecondaryOutputChance())); + } else { + MineTweakerAPI.logCommand(String.format("mods.thermalexpansion.Smelter.addRecipe(%d, %s, %s, %s);", + recipe.getEnergy(), + LogHelper.getStackDescription(recipe.getPrimaryInput()), + LogHelper.getStackDescription(recipe.getSecondaryInput()), + LogHelper.getStackDescription(recipe.getPrimaryOutput()))); + } + } + } + + if(args.isEmpty() || args.contains("transposer")) { + for(RecipeTransposer recipe : TransposerManager.getFillRecipeList()) { + MineTweakerAPI.logCommand(String.format("mods.thermalexpansion.Transposer.addFillRecipe(%d, %s, %s, %s);", + recipe.getEnergy(), + LogHelper.getStackDescription(recipe.getInput()), + LogHelper.getStackDescription(recipe.getOutput()), + LogHelper.getStackDescription(recipe.getFluid()))); + } + + for(RecipeTransposer recipe : TransposerManager.getExtractionRecipeList()) { + MineTweakerAPI.logCommand(String.format("mods.thermalexpansion.Transposer.addExtractRecipe(%d, %s, %s, %s, %d);", + recipe.getEnergy(), + LogHelper.getStackDescription(recipe.getInput()), + LogHelper.getStackDescription(recipe.getOutput()), + LogHelper.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/Crucible.java b/src/main/java/modtweaker2/mods/thermalexpansion/handlers/Crucible.java index 025f699..6b57c73 100644 --- a/src/main/java/modtweaker2/mods/thermalexpansion/handlers/Crucible.java +++ b/src/main/java/modtweaker2/mods/thermalexpansion/handlers/Crucible.java @@ -1,15 +1,23 @@ package modtweaker2.mods.thermalexpansion.handlers; import static modtweaker2.helpers.InputHelper.toFluid; +import static modtweaker2.helpers.InputHelper.toIItemStack; import static modtweaker2.helpers.InputHelper.toStack; -import static modtweaker2.mods.thermalexpansion.ThermalHelper.removeCrucibleRecipe; +import static modtweaker2.helpers.StackHelper.matches; + +import java.util.LinkedList; +import java.util.List; + import minetweaker.IUndoableAction; import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; import minetweaker.api.liquid.ILiquidStack; -import modtweaker2.utils.TweakerPlugin; -import net.minecraft.item.ItemStack; -import net.minecraftforge.fluids.FluidStack; +import modtweaker2.helpers.LogHelper; +import modtweaker2.helpers.ReflectionHelper; +import modtweaker2.mods.thermalexpansion.ThermalHelper; +import modtweaker2.utils.BaseListAddition; +import modtweaker2.utils.BaseListRemoval; import stanhebben.zenscript.annotations.ZenClass; import stanhebben.zenscript.annotations.ZenMethod; import cofh.thermalexpansion.util.crafting.CrucibleManager; @@ -17,91 +25,152 @@ import cofh.thermalexpansion.util.crafting.CrucibleManager.RecipeCrucible; @ZenClass("mods.thermalexpansion.Crucible") public class Crucible { + + public static final String name = "Thermal Expansion Crucible"; + @ZenMethod public static void addRecipe(int energy, IItemStack input, ILiquidStack output) { - - MineTweakerAPI.apply(new Add(energy, toStack(input), toFluid(output))); + if(input == null || output == null) { + LogHelper.logError(String.format("Required parameters missing for %s Recipe.", name)); + return; + } + + if(CrucibleManager.recipeExists(toStack(input))) { + LogHelper.logWarning(String.format("Duplicate %s Recipe found for %s. Command ignored!", name, LogHelper.getStackDescription(toStack(input)))); + return; + } + + RecipeCrucible recipe = ReflectionHelper.getInstance(ThermalHelper.crucibleRecipe, toStack(input), toFluid(output), energy); + + if(recipe != null) { + MineTweakerAPI.apply(new Add(recipe)); + } else { + LogHelper.logError(String.format("Error while creating instance for %s recipe.", name)); + } } - private static class Add implements IUndoableAction { - ItemStack input; - FluidStack output; - int energy; - boolean applied = false; + private static class Add extends BaseListAddition { - public Add(int rf, ItemStack inp, FluidStack out) { - energy = rf; - input = inp; - output = out; + public Add(RecipeCrucible recipe) { + super(Crucible.name, null); + recipes.add(recipe); } public void apply() { - applied = CrucibleManager.addRecipe(energy, input, output); - } - - public boolean canUndo() { - return input != null && applied; - } - - public String describe() { - return "Adding TE Magma Crucible Recipe using " + input.getDisplayName(); + for(RecipeCrucible recipe : recipes) { + boolean applied = CrucibleManager.addRecipe( + recipe.getEnergy(), + recipe.getInput(), + recipe.getOutput()); + + if(applied) { + successful.add(recipe); + } + } } public void undo() { - removeCrucibleRecipe(input); + for(RecipeCrucible recipe : successful) { + CrucibleManager.removeRecipe(recipe.getInput()); + } } - - public String describeUndo() { - return "Removing TE Magma Crucible Recipe using " + input.getDisplayName(); + + @Override + protected boolean equals(RecipeCrucible recipe, RecipeCrucible otherRecipe) { + return ThermalHelper.equals(recipe, otherRecipe); } - - public Object getOverrideKey() { - return null; + + @Override + protected String getRecipeInfo(RecipeCrucible recipe) { + return LogHelper.getStackDescription(recipe.getInput()); } - } - // /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @ZenMethod - public static void removeRecipe(IItemStack input) { - - MineTweakerAPI.apply(new Remove(toStack(input))); + public static void removeRecipe(IIngredient input) { + List recipes = new LinkedList(); + + for(RecipeCrucible recipe : CrucibleManager.getRecipeList()) { + if(recipe != null && matches(input, toIItemStack(recipe.getInput()))) { + recipes.add(recipe); + } + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new Remove(recipes)); + } else { + LogHelper.logWarning(String.format("No %s Recipe found for %s.", name, input.toString())); + } } - private static class Remove implements IUndoableAction { - ItemStack input; - RecipeCrucible removed; + private static class Remove extends BaseListRemoval { - public Remove(ItemStack inp) { - input = inp; + public Remove(List recipes) { + super(Crucible.name, null, recipes); } public void apply() { - removed = CrucibleManager.getRecipe(input); - removeCrucibleRecipe(input); - } - - public boolean canUndo() { - return removed != null; - } - - public String describe() { - return "Removing TE Magma Crucible Recipe using " + input.getDisplayName(); + for(RecipeCrucible recipe : recipes) { + boolean removed = CrucibleManager.removeRecipe(recipe.getInput()); + + if(removed) { + successful.add(recipe); + } + } } public void undo() { - CrucibleManager.addRecipe(removed.getEnergy(), removed.getInput(), removed.getOutput()); + for(RecipeCrucible recipe : successful) { + CrucibleManager.addRecipe( + recipe.getEnergy(), + recipe.getInput(), + recipe.getOutput()); + } } - - public String describeUndo() { - return "Restoring TE Magma Crucible Recipe using " + input.getDisplayName(); - } - - public Object getOverrideKey() { - return null; - } - + + @Override + protected boolean equals(RecipeCrucible recipe, RecipeCrucible otherRecipe) { + return ThermalHelper.equals(recipe, otherRecipe); + } + + @Override + protected String getRecipeInfo(RecipeCrucible recipe) { + return LogHelper.getStackDescription(recipe.getInput()); + } } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + @ZenMethod + public static void refreshRecipes() { + MineTweakerAPI.apply(new Refresh()); + } + private static class Refresh implements IUndoableAction { + + public void apply() { + CrucibleManager.refreshRecipes(); + } + + public boolean canUndo() { + return true; + } + + public String describe() { + return "Refreshing " + Crucible.name + " recipes"; + } + + public void undo() { + } + + public String describeUndo() { + return "Ignoring undo of " + Crucible.name + " recipe refresh"; + } + + public Object getOverrideKey() { + return null; + } + } } diff --git a/src/main/java/modtweaker2/mods/thermalexpansion/handlers/Furnace.java b/src/main/java/modtweaker2/mods/thermalexpansion/handlers/Furnace.java index 8bceade..d40851a 100644 --- a/src/main/java/modtweaker2/mods/thermalexpansion/handlers/Furnace.java +++ b/src/main/java/modtweaker2/mods/thermalexpansion/handlers/Furnace.java @@ -1,11 +1,21 @@ 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.LinkedList; +import java.util.List; + import minetweaker.IUndoableAction; import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; -import modtweaker2.utils.TweakerPlugin; -import net.minecraft.item.ItemStack; +import modtweaker2.helpers.LogHelper; +import modtweaker2.helpers.ReflectionHelper; +import modtweaker2.mods.thermalexpansion.ThermalHelper; +import modtweaker2.utils.BaseListAddition; +import modtweaker2.utils.BaseListRemoval; import stanhebben.zenscript.annotations.ZenClass; import stanhebben.zenscript.annotations.ZenMethod; import cofh.thermalexpansion.util.crafting.FurnaceManager; @@ -13,94 +23,127 @@ import cofh.thermalexpansion.util.crafting.FurnaceManager.RecipeFurnace; @ZenClass("mods.thermalexpansion.Furnace") public class Furnace { + + public static final String name = "Thermal Expansion Furnace"; + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + @ZenMethod public static void addRecipe(int energy, IItemStack input, IItemStack output) { - - MineTweakerAPI.apply(new Add(energy, toStack(input), toStack(output))); + if(input == null || output == null) { + LogHelper.logError(String.format("Required parameters missing for %s Recipe.", name)); + return; + } + + if(FurnaceManager.recipeExists(toStack(input))) { + LogHelper.logWarning(String.format("Duplicate %s Recipe found for %s. Command ignored!", name, LogHelper.getStackDescription(toStack(input)))); + return; + } + + RecipeFurnace recipe = ReflectionHelper.getInstance(ThermalHelper.furanceRecipe, toStack(input), toStack(output), energy); + + if(recipe != null) { + MineTweakerAPI.apply(new Add(recipe)); + } else { + LogHelper.logError(String.format("Error while creating instance for %s recipe.", name)); + } } - private static class Add implements IUndoableAction { - ItemStack input; - ItemStack output; - int energy; - boolean applied = false; - - public Add(int rf, ItemStack inp, ItemStack out) { - energy = rf; - input = inp; - output = out; + private static class Add extends BaseListAddition { + public Add(RecipeFurnace recipe) { + super(Furnace.name, null); + recipes.add(recipe); } public void apply() { - FurnaceManager.addRecipe(energy, input, output, false); - } + for(RecipeFurnace recipe : recipes) { + boolean applied = FurnaceManager.addRecipe( + recipe.getEnergy(), + recipe.getInput(), + recipe.getOutput(), + false); + + if(applied) { + successful.add(recipe); + } + } - public boolean canUndo() { - return input != null; - } - - public String describe() { - return "Adding Redstone Furnace Recipe using " + input.getDisplayName(); } public void undo() { - FurnaceManager.removeRecipe(input); + for(RecipeFurnace recipe : recipes) { + FurnaceManager.removeRecipe(recipe.getInput()); + } + + } + + @Override + protected boolean equals(RecipeFurnace recipe, RecipeFurnace otherRecipe) { + return ThermalHelper.equals(recipe, otherRecipe); } - public String describeUndo() { - return "Removing Redstone Furnace Recipe using " + input.getDisplayName(); + @Override + protected String getRecipeInfo(RecipeFurnace recipe) { + return LogHelper.getStackDescription(recipe.getOutput()); } - - public Object getOverrideKey() { - return null; - } - } - // /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @ZenMethod - public static void removeRecipe(IItemStack input) { + public static void removeRecipe(IIngredient input) { + List recipes = new LinkedList(); - MineTweakerAPI.apply(new Remove(toStack(input))); + for(RecipeFurnace recipe : FurnaceManager.getRecipeList()) { + if(recipe != null && matches(input, toIItemStack(recipe.getInput()))) { + recipes.add(recipe); + } + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new Remove(recipes)); + } else { + LogHelper.logWarning(String.format("No %s Recipe found for %s.", name, input.toString())); + } } - private static class Remove implements IUndoableAction { - ItemStack input; - RecipeFurnace removed; - - public Remove(ItemStack inp) { - input = inp; + private static class Remove extends BaseListRemoval { + public Remove(List recipes) { + super(Furnace.name, null, recipes); } public void apply() { - removed = FurnaceManager.getRecipe(input); - FurnaceManager.removeRecipe(input); - } - - public boolean canUndo() { - return removed != null; - } - - public String describe() { - return "Removing Redstone Furnace Recipe using " + input.getDisplayName(); + for(RecipeFurnace recipe : recipes) { + boolean removed = FurnaceManager.removeRecipe(recipe.getInput()); + + if(removed) { + successful.add(recipe); + } + } } public void undo() { - FurnaceManager.addRecipe(removed.getEnergy(), removed.getInput(), removed.getOutput(), false); - } - - public String describeUndo() { - return "Restoring Redstone Furnace Recipe using " + input.getDisplayName(); - } - - public Object getOverrideKey() { - return null; + for(RecipeFurnace recipe : successful) { + FurnaceManager.addRecipe( + recipe.getEnergy(), + recipe.getInput(), + recipe.getOutput(), + false); + } } + + @Override + protected boolean equals(RecipeFurnace recipe, RecipeFurnace otherRecipe) { + return ThermalHelper.equals(recipe, otherRecipe); + } + @Override + protected String getRecipeInfo(RecipeFurnace recipe) { + return LogHelper.getStackDescription(recipe.getOutput()); + } } - // /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @ZenMethod public static void refreshRecipes() { @@ -110,28 +153,26 @@ public class Furnace { private static class Refresh implements IUndoableAction { public void apply() { - FurnaceManager.loadRecipes(); + FurnaceManager.refreshRecipes(); } public boolean canUndo() { - return false; + return true; } public String describe() { - return "Refreshing Redstone Furnace Recipes"; + return "Refreshing " + Furnace.name + " recipes"; } public void undo() { } public String describeUndo() { - return "Can't Undo Redstone Furnace Refresh"; + return "Ignoring undo of " + Furnace.name + " recipe refresh"; } public Object getOverrideKey() { return null; } - } - } diff --git a/src/main/java/modtweaker2/mods/thermalexpansion/handlers/Insolator.java b/src/main/java/modtweaker2/mods/thermalexpansion/handlers/Insolator.java new file mode 100644 index 0000000..7c49d08 --- /dev/null +++ b/src/main/java/modtweaker2/mods/thermalexpansion/handlers/Insolator.java @@ -0,0 +1,190 @@ +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.LinkedList; +import java.util.List; + +import minetweaker.IUndoableAction; +import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; +import minetweaker.api.item.IItemStack; +import modtweaker2.helpers.LogHelper; +import modtweaker2.helpers.ReflectionHelper; +import modtweaker2.mods.thermalexpansion.ThermalHelper; +import modtweaker2.utils.BaseListAddition; +import modtweaker2.utils.BaseListRemoval; +import stanhebben.zenscript.annotations.Optional; +import stanhebben.zenscript.annotations.ZenClass; +import stanhebben.zenscript.annotations.ZenMethod; +import cofh.thermalexpansion.util.crafting.InsolatorManager; +import cofh.thermalexpansion.util.crafting.InsolatorManager.RecipeInsolator; + +@ZenClass("mods.thermalexpansion.Insolator") +public class Insolator { + + public static final String name = "Thermal Expansion Insolator"; + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + @ZenMethod + public static void addRecipe(int energy, IItemStack primaryInput, IItemStack secondaryInput, IItemStack primaryOutput, @Optional IItemStack secondaryOutput, @Optional int secondaryChance) { + if(primaryInput == null || secondaryInput == null || primaryOutput == null) { + LogHelper.logError(String.format("Required parameters missing for %s Recipe.", name)); + return; + } + + if(InsolatorManager.recipeExists(toStack(primaryInput), toStack(secondaryInput))) { + LogHelper.logWarning(String.format("Duplicate %s Recipe found for %s and %s. Command ignored!", name, primaryInput.toString(), secondaryInput.toString())); + return; + } + + RecipeInsolator recipe = ReflectionHelper.getInstance(ThermalHelper.insolatorRecipe, toStack(primaryInput), toStack(secondaryInput), toStack(primaryOutput), toStack(secondaryOutput), secondaryChance, energy); + + if(recipe != null) { + MineTweakerAPI.apply(new Add(recipe)); + } else { + LogHelper.logError(String.format("Error while creating instance for %s recipe.", name)); + } + } + + private static class Add extends BaseListAddition { + public Add(RecipeInsolator recipe) { + super(Insolator.name, null); + recipes.add(recipe); + } + + public void apply() { + for(RecipeInsolator recipe : recipes) { + boolean applied = InsolatorManager.addRecipe( + recipe.getEnergy(), + recipe.getPrimaryInput(), + recipe.getSecondaryInput(), + recipe.getPrimaryOutput(), + recipe.getSecondaryOutput(), + recipe.getSecondaryOutputChance(), + false); + + if(applied) { + successful.add(recipe); + } + } + + } + + public void undo() { + for(RecipeInsolator recipe : recipes) { + InsolatorManager.removeRecipe(recipe.getPrimaryInput(), recipe.getSecondaryInput()); + } + + } + + @Override + protected boolean equals(RecipeInsolator recipe, RecipeInsolator otherRecipe) { + return ThermalHelper.equals(recipe, otherRecipe); + } + + @Override + protected String getRecipeInfo(RecipeInsolator recipe) { + return LogHelper.getStackDescription(recipe.getPrimaryOutput()); + } + } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + @ZenMethod + public static void removeRecipe(IIngredient input1, IIngredient input2) { + List recipes = new LinkedList(); + + for(RecipeInsolator recipe : InsolatorManager.getRecipeList()) { + if(recipe != null && matches(input1, toIItemStack(recipe.getPrimaryInput())) && matches(input2, toIItemStack(recipe.getSecondaryInput()))) { + recipes.add(recipe); + } + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new Remove(recipes)); + } else { + LogHelper.logWarning(String.format("No %s Recipes found for %s and %s.", name, input1.toString(), input2.toString())); + } + } + + private static class Remove extends BaseListRemoval { + + public Remove(List recipes) { + super(Insolator.name, null, recipes); + } + + @Override + public void apply() { + for(RecipeInsolator recipe : recipes) { + boolean removed = InsolatorManager.removeRecipe(recipe.getPrimaryInput(), recipe.getSecondaryInput()); + + if(removed) { + successful.add(recipe); + } + } + } + + @Override + public void undo() { + for(RecipeInsolator recipe : recipes) { + InsolatorManager.addRecipe( + recipe.getEnergy(), + recipe.getPrimaryInput(), + recipe.getSecondaryInput(), + recipe.getPrimaryOutput(), + recipe.getSecondaryOutput(), + recipe.getSecondaryOutputChance()); + } + } + + @Override + protected boolean equals(RecipeInsolator recipe, RecipeInsolator otherRecipe) { + return ThermalHelper.equals(recipe, otherRecipe); + } + + @Override + public String getRecipeInfo(RecipeInsolator recipe) { + return LogHelper.getStackDescription(recipe.getPrimaryOutput()); + } + } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + @ZenMethod + public static void refreshRecipes() { + MineTweakerAPI.apply(new Refresh()); + } + + private static class Refresh implements IUndoableAction { + + public void apply() { + InsolatorManager.refreshRecipes(); + } + + public boolean canUndo() { + return true; + } + + public String describe() { + return "Refreshing " + Insolator.name + " recipes"; + } + + public void undo() { + } + + public String describeUndo() { + return "Ignoring undo of " + Insolator.name + " recipe refresh"; + } + + public Object getOverrideKey() { + return null; + } + } + +} diff --git a/src/main/java/modtweaker2/mods/thermalexpansion/handlers/Pulverizer.java b/src/main/java/modtweaker2/mods/thermalexpansion/handlers/Pulverizer.java index eebe53e..dc997ad 100644 --- a/src/main/java/modtweaker2/mods/thermalexpansion/handlers/Pulverizer.java +++ b/src/main/java/modtweaker2/mods/thermalexpansion/handlers/Pulverizer.java @@ -1,11 +1,21 @@ 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.LinkedList; +import java.util.List; + import minetweaker.IUndoableAction; import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; -import modtweaker2.utils.TweakerPlugin; -import net.minecraft.item.ItemStack; +import modtweaker2.helpers.LogHelper; +import modtweaker2.helpers.ReflectionHelper; +import modtweaker2.mods.thermalexpansion.ThermalHelper; +import modtweaker2.utils.BaseListAddition; +import modtweaker2.utils.BaseListRemoval; import stanhebben.zenscript.annotations.ZenClass; import stanhebben.zenscript.annotations.ZenMethod; import cofh.thermalexpansion.util.crafting.PulverizerManager; @@ -13,101 +23,163 @@ import cofh.thermalexpansion.util.crafting.PulverizerManager.RecipePulverizer; @ZenClass("mods.thermalexpansion.Pulverizer") public class Pulverizer { + + public static final String name = "Thermal Expansion Pulverizer"; + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + @ZenMethod public static void addRecipe(int energy, IItemStack input, IItemStack output) { - - MineTweakerAPI.apply(new Add(energy, toStack(input), toStack(output), null, 0)); + addRecipe(energy, input, output, null, 0); } @ZenMethod public static void addRecipe(int energy, IItemStack input, IItemStack output, IItemStack secondary, int secondaryChance) { - - MineTweakerAPI.apply(new Add(energy, toStack(input), toStack(output), toStack(secondary), secondaryChance)); + if(input == null || output == null) { + LogHelper.logError(String.format("Required parameters missing for %s Recipe.", name)); + return; + } + + if(PulverizerManager.recipeExists(toStack(input))) { + LogHelper.logWarning(String.format("Duplicate %s Recipe found for %s. Command ignored!", name, LogHelper.getStackDescription(toStack(input)))); + return; + } + + RecipePulverizer recipe = ReflectionHelper.getInstance(ThermalHelper.pulverizerRecipe, toStack(input), toStack(output), toStack(secondary), secondaryChance, energy); + + if(recipe != null) { + MineTweakerAPI.apply(new Add(recipe)); + } else { + LogHelper.logError(String.format("Error while creating instance for %s recipe.", name)); + } } - private static class Add implements IUndoableAction { - ItemStack input; - ItemStack output; - ItemStack secondary; - int secondaryChance; - int energy; - boolean applied = false; - - public Add(int rf, ItemStack inp, ItemStack out, ItemStack sec, int chance) { - energy = rf; - input = inp; - output = out; - secondary = sec; - secondaryChance = chance; + private static class Add extends BaseListAddition { + public Add(RecipePulverizer recipe) { + super(Pulverizer.name, null); + recipes.add(recipe); } public void apply() { - applied = PulverizerManager.addRecipe(energy, input, output, secondary, secondaryChance); - } - - public boolean canUndo() { - return input != null && applied; - } - - public String describe() { - return "Adding Pulverizer Recipe using " + input.getDisplayName(); + for(RecipePulverizer recipe : recipes) { + boolean applied = PulverizerManager.addRecipe( + recipe.getEnergy(), + recipe.getInput(), + recipe.getPrimaryOutput(), + recipe.getSecondaryOutput(), + recipe.getSecondaryOutputChance()); + + if(applied) { + successful.add(recipe); + } + } } public void undo() { - PulverizerManager.removeRecipe(input); + for(RecipePulverizer recipe : successful) { + PulverizerManager.removeRecipe(recipe.getInput()); + } + } + + @Override + protected boolean equals(RecipePulverizer recipe, RecipePulverizer otherRecipe) { + return ThermalHelper.equals(recipe, otherRecipe); } - public String describeUndo() { - return "Removing Pulverizer Recipe using " + input.getDisplayName(); + @Override + protected String getRecipeInfo(RecipePulverizer recipe) { + return LogHelper.getStackDescription(recipe.getInput()); } - - public Object getOverrideKey() { - return null; - } - } // /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @ZenMethod - public static void removeRecipe(IItemStack input) { - - MineTweakerAPI.apply(new Remove(toStack(input))); + public static void removeRecipe(IIngredient input) { + List recipes = new LinkedList(); + RecipePulverizer[] list = PulverizerManager.getRecipeList(); + + for(RecipePulverizer recipe : list) { + if(recipe != null && recipe.getInput() != null && matches(input, toIItemStack(recipe.getInput()))) { + recipes.add(recipe); + } + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new Remove(recipes)); + } else { + LogHelper.logWarning(String.format("No %s Recipe found for %s.", name, input.toString())); + } + } - private static class Remove implements IUndoableAction { - ItemStack input; - RecipePulverizer removed; - - public Remove(ItemStack inp) { - input = inp; + private static class Remove extends BaseListRemoval { + public Remove(List recipes) { + super(Pulverizer.name, null, recipes); } public void apply() { - removed = PulverizerManager.getRecipe(input); - PulverizerManager.removeRecipe(input); - } - - public boolean canUndo() { - return removed != null; - } - - public String describe() { - return "Removing Pulverizer Recipe using " + input.getDisplayName(); + for(RecipePulverizer recipe : recipes) { + boolean removed = PulverizerManager.removeRecipe(recipe.getInput()); + + if(removed) { + successful.add(recipe); + } + } } public void undo() { - PulverizerManager.addRecipe(removed.getEnergy(), removed.getInput(), removed.getPrimaryOutput(), removed.getSecondaryOutput(), removed.getSecondaryOutputChance()); - } - - public String describeUndo() { - return "Restoring Pulverizer Recipe using " + input.getDisplayName(); - } - - public Object getOverrideKey() { - return null; + for(RecipePulverizer recipe : successful) { + PulverizerManager.addRecipe( + recipe.getEnergy(), + recipe.getInput(), + recipe.getPrimaryOutput(), + recipe.getSecondaryOutput(), + recipe.getSecondaryOutputChance()); + } } + + @Override + protected boolean equals(RecipePulverizer recipe, RecipePulverizer otherRecipe) { + return ThermalHelper.equals(recipe, otherRecipe); + } + @Override + protected String getRecipeInfo(RecipePulverizer recipe) { + return LogHelper.getStackDescription(recipe.getInput()); + } } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + @ZenMethod + public static void refreshRecipes() { + MineTweakerAPI.apply(new Refresh()); + } + + private static class Refresh implements IUndoableAction { + + public void apply() { + PulverizerManager.refreshRecipes(); + } + + public boolean canUndo() { + return true; + } + + public String describe() { + return "Refreshing " + Pulverizer.name + " recipes"; + } + + public void undo() { + } + + public String describeUndo() { + return "Ignoring undo of " + Pulverizer.name + " recipe refresh"; + } + + public Object getOverrideKey() { + return null; + } + } } diff --git a/src/main/java/modtweaker2/mods/thermalexpansion/handlers/Sawmill.java b/src/main/java/modtweaker2/mods/thermalexpansion/handlers/Sawmill.java index 2c19138..e98455e 100644 --- a/src/main/java/modtweaker2/mods/thermalexpansion/handlers/Sawmill.java +++ b/src/main/java/modtweaker2/mods/thermalexpansion/handlers/Sawmill.java @@ -1,11 +1,21 @@ 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.LinkedList; +import java.util.List; + import minetweaker.IUndoableAction; import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; -import modtweaker2.utils.TweakerPlugin; -import net.minecraft.item.ItemStack; +import modtweaker2.helpers.LogHelper; +import modtweaker2.helpers.ReflectionHelper; +import modtweaker2.mods.thermalexpansion.ThermalHelper; +import modtweaker2.utils.BaseListAddition; +import modtweaker2.utils.BaseListRemoval; import stanhebben.zenscript.annotations.ZenClass; import stanhebben.zenscript.annotations.ZenMethod; import cofh.thermalexpansion.util.crafting.SawmillManager; @@ -13,99 +23,165 @@ import cofh.thermalexpansion.util.crafting.SawmillManager.RecipeSawmill; @ZenClass("mods.thermalexpansion.Sawmill") public class Sawmill { + + public static final String name = "Thermal Expansion Sawmill"; + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + @ZenMethod public static void addRecipe(int energy, IItemStack input, IItemStack output) { - MineTweakerAPI.apply(new Add(energy, toStack(input), toStack(output), null, 0)); + addRecipe(energy, input, output, null, 0); } @ZenMethod public static void addRecipe(int energy, IItemStack input, IItemStack output, IItemStack secondary, int secondaryChance) { - MineTweakerAPI.apply(new Add(energy, toStack(input), toStack(output), toStack(secondary), secondaryChance)); + if(input == null || output == null) { + LogHelper.logError(String.format("Required parameters missing for %s Recipe.", name)); + return; + } + + if(SawmillManager.recipeExists(toStack(input))) { + LogHelper.logWarning(String.format("Duplicate %s Recipe found for %s. Command ignored!", name, LogHelper.getStackDescription(toStack(input)))); + return; + } + + RecipeSawmill recipe = ReflectionHelper.getInstance(ThermalHelper.sawmillRecipe, toStack(input), toStack(output), toStack(secondary), secondaryChance, energy); + + if(recipe != null) { + MineTweakerAPI.apply(new Add(recipe)); + } else { + LogHelper.logError(String.format("Error while creating instance for %s recipe.", name)); + } } - private static class Add implements IUndoableAction { - ItemStack input; - ItemStack output; - ItemStack secondary; - int secondaryChance; - int energy; - boolean applied = false; - - public Add(int rf, ItemStack inp, ItemStack out, ItemStack sec, int chance) { - energy = rf; - input = inp; - output = out; - secondary = sec; - secondaryChance = chance; + private static class Add extends BaseListAddition { + public Add(RecipeSawmill recipe) { + super(Sawmill.name, null); + recipes.add(recipe); } + @Override public void apply() { - applied = SawmillManager.addRecipe(energy, input, output, secondary, secondaryChance); - } - - public boolean canUndo() { - return input != null && applied; - } - - public String describe() { - return "Adding Sawmill Recipe using " + input.getDisplayName(); + for(RecipeSawmill recipe : recipes) { + boolean applied = SawmillManager.addRecipe( + recipe.getEnergy(), + recipe.getInput(), + recipe.getPrimaryOutput(), + recipe.getSecondaryOutput(), + recipe.getSecondaryOutputChance()); + + if(applied) { + successful.add(recipe); + } + } } + @Override public void undo() { - SawmillManager.removeRecipe(input); + for(RecipeSawmill recipe : successful) { + SawmillManager.removeRecipe(recipe.getInput()); + } + } + + @Override + protected boolean equals(RecipeSawmill recipe, RecipeSawmill otherRecipe) { + return ThermalHelper.equals(recipe, otherRecipe); } - public String describeUndo() { - return "Removing Sawmill Recipe using " + input.getDisplayName(); - } - - public Object getOverrideKey() { - return null; - } + @Override + protected String getRecipeInfo(RecipeSawmill recipe) { + return LogHelper.getStackDescription(recipe.getInput()); + } } // /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @ZenMethod - public static void removeRecipe(IItemStack input) { - - MineTweakerAPI.apply(new Remove(toStack(input))); + public static void removeRecipe(IIngredient input) { + List recipes = new LinkedList(); + RecipeSawmill[] list = SawmillManager.getRecipeList(); + + for(RecipeSawmill recipe : list) { + if(recipe != null && recipe.getInput() != null && matches(input, toIItemStack(recipe.getInput()))) { + recipes.add(recipe); + } + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new Remove(recipes)); + } else { + LogHelper.logWarning(String.format("No %s Recipe found for %s.", name, input.toString())); + } } - private static class Remove implements IUndoableAction { - ItemStack input; - RecipeSawmill removed; - - public Remove(ItemStack inp) { - input = inp; + private static class Remove extends BaseListRemoval { + public Remove(List recipes) { + super(Sawmill.name, null, recipes); } public void apply() { - removed = SawmillManager.getRecipe(input); - SawmillManager.removeRecipe(input); - } - - public boolean canUndo() { - return removed != null; - } - - public String describe() { - return "Removing Sawmill Recipe using " + input.getDisplayName(); + for(RecipeSawmill recipe : recipes) { + boolean removed = SawmillManager.removeRecipe(recipe.getInput()); + if(removed) { + successful.add(recipe); + } + + } } public void undo() { - SawmillManager.addRecipe(removed.getEnergy(), removed.getInput(), removed.getPrimaryOutput(), removed.getSecondaryOutput(), removed.getSecondaryOutputChance()); + for(RecipeSawmill recipe : successful) { + SawmillManager.addRecipe( + recipe.getEnergy(), + recipe.getInput(), + recipe.getPrimaryOutput(), + recipe.getSecondaryOutput(), + recipe.getSecondaryOutputChance()); + } } - - public String describeUndo() { - return "Restoring Sawmill Recipe using " + input.getDisplayName(); + + @Override + protected boolean equals(RecipeSawmill recipe, RecipeSawmill otherRecipe) { + return ThermalHelper.equals(recipe, otherRecipe); + } + + @Override + protected String getRecipeInfo(RecipeSawmill recipe) { + return LogHelper.getStackDescription(recipe.getInput()); } - - public Object getOverrideKey() { - return null; - } - } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + @ZenMethod + public static void refreshRecipes() { + MineTweakerAPI.apply(new Refresh()); + } + + private static class Refresh implements IUndoableAction { + + public void apply() { + SawmillManager.refreshRecipes(); + } + + public boolean canUndo() { + return true; + } + + public String describe() { + return "Refreshing " + Sawmill.name + " recipes"; + } + + public void undo() { + } + + public String describeUndo() { + return "Ignoring undo of " + Sawmill.name + " recipe refresh"; + } + + public Object getOverrideKey() { + return null; + } + } } diff --git a/src/main/java/modtweaker2/mods/thermalexpansion/handlers/Smelter.java b/src/main/java/modtweaker2/mods/thermalexpansion/handlers/Smelter.java index b729d4d..ef1daa9 100644 --- a/src/main/java/modtweaker2/mods/thermalexpansion/handlers/Smelter.java +++ b/src/main/java/modtweaker2/mods/thermalexpansion/handlers/Smelter.java @@ -1,40 +1,32 @@ 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.LinkedList; import java.util.List; -import java.util.Map; +import minetweaker.IUndoableAction; import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; +import modtweaker2.helpers.LogHelper; +import modtweaker2.helpers.ReflectionHelper; import modtweaker2.mods.thermalexpansion.ThermalHelper; -import modtweaker2.utils.BaseDescriptionAddition; -import modtweaker2.utils.BaseDescriptionRemoval; -import modtweaker2.utils.TweakerPlugin; -import net.minecraft.item.ItemStack; +import modtweaker2.utils.BaseListAddition; +import modtweaker2.utils.BaseListRemoval; 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.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) { @@ -43,98 +35,156 @@ public class Smelter { @ZenMethod public static void addRecipe(int energy, IItemStack input, IItemStack input2, IItemStack output, IItemStack output2, int chance) { - ItemStack in1 = toStack(input); - 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)); + if(input == null || input2 == null || output == null) { + LogHelper.logError(String.format("Required parameters missing for %s Recipe.", name)); + return; + } + + if(SmelterManager.recipeExists(toStack(input), toStack(input2))) { + LogHelper.logWarning(String.format("Duplicate %s Recipe found for %s and %s. Command ignored!", name, LogHelper.getStackDescription(toStack(input)), LogHelper.getStackDescription(toStack(input2)))); + return; + } + + RecipeSmelter recipe = ReflectionHelper.getInstance(ThermalHelper.smelterRecipe, toStack(input), toStack(input2), toStack(output), toStack(output2), chance, energy); + + if(recipe != null) { + MineTweakerAPI.apply(new Add(recipe)); + } else { + LogHelper.logError(String.format("Error while creating instance for %s recipe.", name)); + } } - private static class Add extends BaseDescriptionAddition { - 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 BaseListAddition { + public Add(RecipeSmelter recipe) { + super(Smelter.name, null); + recipes.add(recipe); } @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; + for(RecipeSmelter recipe : recipes) { + boolean applied = SmelterManager.addRecipe( + recipe.getEnergy(), + recipe.getPrimaryInput(), + recipe.getSecondaryInput(), + recipe.getPrimaryOutput(), + recipe.getSecondaryOutput(), + recipe.getSecondaryOutputChance()); + + if(applied) { + successful.add(recipe); + } + } } @Override public void undo() { - ThermalHelper.getSmelterMap().remove(key); - removeValidated(input1); - removeValidated(input2); + for(RecipeSmelter recipe : successful) { + SmelterManager.removeRecipe(recipe.getPrimaryInput(), recipe.getSecondaryInput()); + } + } + + @Override + protected boolean equals(RecipeSmelter recipe, RecipeSmelter otherRecipe) { + return ThermalHelper.equals(recipe, otherRecipe); } @Override - public String getRecipeInfo() { - return ((RecipeSmelter) recipe).getPrimaryOutput().getDisplayName(); + public String getRecipeInfo(RecipeSmelter recipe) { + return LogHelper.getStackDescription(recipe.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) { + List recipes = new LinkedList(); + + for(RecipeSmelter recipe : SmelterManager.getRecipeList()) { + if(recipe != null && matches(input1, toIItemStack(recipe.getPrimaryInput())) && matches(input2, toIItemStack(recipe.getSecondaryInput()))) { + recipes.add(recipe); + } + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new Remove(recipes)); + } else { + LogHelper.logWarning(String.format("No %s Recipes found for %s and %s.", name, input1.toString(), input2.toString())); + } } - private static class Remove extends BaseDescriptionRemoval { - private final ComparableItemStackSafe input1; - private final ComparableItemStackSafe input2; - private final List key; - private RecipeSmelter recipe; + private static class Remove extends BaseListRemoval { - 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(List recipes) { + super(Smelter.name, null, recipes); } @Override public void apply() { - recipe = ThermalHelper.getSmelterMap().get(key); - ThermalHelper.getSmelterMap().remove(key); - removeValidated(input1); - removeValidated(input2); + for(RecipeSmelter recipe : recipes) { + boolean removed = SmelterManager.removeRecipe(recipe.getPrimaryInput(), recipe.getSecondaryInput()); + + if(removed) { + successful.add(recipe); + } + } } - - @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); + for(RecipeSmelter recipe : recipes) { + SmelterManager.addRecipe( + recipe.getEnergy(), + recipe.getPrimaryInput(), + recipe.getSecondaryInput(), + recipe.getPrimaryOutput(), + recipe.getSecondaryOutput(), + recipe.getSecondaryOutputChance()); + } } + + @Override + protected boolean equals(RecipeSmelter recipe, RecipeSmelter otherRecipe) { + return ThermalHelper.equals(recipe, otherRecipe); + } - @Override - public String getRecipeInfo() { - return input1.toItemStack().getDisplayName() + " + " + input2.toItemStack().getDisplayName(); - } + @Override + public String getRecipeInfo(RecipeSmelter recipe) { + return LogHelper.getStackDescription(recipe.getPrimaryOutput()); + } } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + @ZenMethod + public static void refreshRecipes() { + MineTweakerAPI.apply(new Refresh()); + } + + private static class Refresh implements IUndoableAction { + + public void apply() { + SmelterManager.refreshRecipes(); + } + + public boolean canUndo() { + return true; + } + + public String describe() { + return "Refreshing " + Smelter.name + " recipes"; + } + + public void undo() { + } + + public String describeUndo() { + return "Ignoring undo of " + Smelter.name + " recipe refresh"; + } + + public Object getOverrideKey() { + return null; + } + } } diff --git a/src/main/java/modtweaker2/mods/thermalexpansion/handlers/Transposer.java b/src/main/java/modtweaker2/mods/thermalexpansion/handlers/Transposer.java index 5381d6a..0901e44 100644 --- a/src/main/java/modtweaker2/mods/thermalexpansion/handlers/Transposer.java +++ b/src/main/java/modtweaker2/mods/thermalexpansion/handlers/Transposer.java @@ -1,184 +1,273 @@ 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.LinkedList; import java.util.List; -import java.util.Map; +import minetweaker.IUndoableAction; import minetweaker.MineTweakerAPI; +import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; +import minetweaker.api.item.IngredientAny; import minetweaker.api.liquid.ILiquidStack; +import modtweaker2.helpers.LogHelper; +import modtweaker2.helpers.ReflectionHelper; import modtweaker2.mods.thermalexpansion.ThermalHelper; -import modtweaker2.utils.BaseDescriptionAddition; -import modtweaker2.utils.BaseDescriptionRemoval; -import modtweaker2.utils.TweakerPlugin; -import net.minecraft.item.ItemStack; -import net.minecraftforge.fluids.FluidStack; +import modtweaker2.utils.BaseListAddition; +import modtweaker2.utils.BaseListRemoval; 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); + if(input == null || output == null || liquid == null) { + LogHelper.logError(String.format("Required parameters missing for %s Recipe.", nameFill)); + return; + } + + if(TransposerManager.fillRecipeExists(toStack(input), toFluid(liquid))) { + LogHelper.logWarning(String.format("Duplicate %s Recipe found for %s and %s. Command ignored!", Transposer.nameFill, LogHelper.getStackDescription(toStack(input)), LogHelper.getStackDescription(toFluid(liquid)))); + return; + } + + RecipeTransposer recipe = ReflectionHelper.getInstance(ThermalHelper.transposerRecipe, toStack(input), toStack(output), toFluid(liquid), energy, 100); + + if(recipe != null) { + MineTweakerAPI.apply(new Add(recipe, RecipeType.Fill)); + } else { + LogHelper.logError(String.format("Error while creating instance for %s recipe.", nameFill)); + } } @ZenMethod public static void addExtractRecipe(int energy, IItemStack input, IItemStack output, ILiquidStack liquid, int chance) { - addRecipe(energy, input, output, liquid, chance, false); + if(input == null || output == null || liquid == null) { + LogHelper.logError(String.format("Required parameters missing for %s Recipe.", nameExtract)); + return; + } + + if(TransposerManager.extractionRecipeExists(toStack(input), toFluid(liquid))) { + LogHelper.logWarning(String.format("Duplicate %s Recipe found for %s and %s. Command ignored!", Transposer.nameExtract, LogHelper.getStackDescription(toStack(input)), LogHelper.getStackDescription(toFluid(liquid)))); + return; + } + + RecipeTransposer recipe = ReflectionHelper.getInstance(ThermalHelper.transposerRecipe, toStack(input), toStack(output), toFluid(liquid), energy, 100); + + if(recipe != null) { + MineTweakerAPI.apply(new Add(recipe, RecipeType.Extract)); + } else { + LogHelper.logError(String.format("Error while creating instance for %s recipe.", nameExtract)); + } + } + + private static class Add extends BaseListAddition { + private RecipeType type; + + protected Add(RecipeTransposer recipe, RecipeType type) { + super(type == RecipeType.Fill ? Transposer.nameFill : Transposer.nameExtract, null); + this.type = type; + recipes.add(recipe); + } + + @Override + public void apply() { + for(RecipeTransposer recipe : recipes) { + boolean applied = false; + switch(type) { + case Fill: + applied = TransposerManager.addFillRecipe( + recipe.getEnergy(), + recipe.getInput(), + recipe.getOutput(), + recipe.getFluid(), + false); + break; + + case Extract: + applied = TransposerManager.addExtractionRecipe( + recipe.getEnergy(), + recipe.getInput(), + recipe.getOutput(), + recipe.getFluid(), + recipe.getChance(), + false); + break; + } + + if(applied) { + successful.add(recipe); + } + } + } + + @Override + public void undo() { + for(RecipeTransposer recipe : successful) { + switch(type) + { + case Fill: + TransposerManager.removeFillRecipe(recipe.getInput(), recipe.getFluid()); + break; + case Extract: + TransposerManager.removeExtractionRecipe(recipe.getInput()); + break; + } + } + } + + @Override + protected boolean equals(RecipeTransposer recipe, RecipeTransposer otherRecipe) { + return ThermalHelper.equals(recipe, otherRecipe); + } + + @Override + protected String getRecipeInfo(RecipeTransposer recipe) { + return LogHelper.getStackDescription(recipe.getInput()); + } + } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + @ZenMethod + public static void removeFillRecipe(IIngredient input, IIngredient liquid) { + removeRecipe(input, liquid, RecipeType.Fill); } @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) { + removeRecipe(input, IngredientAny.INSTANCE, RecipeType.Extract); } - private static class Add extends BaseDescriptionAddition { - private List key; - private boolean isFillRecipe; - private final ComparableItemStackSafe input; - private final RecipeTransposer recipe; - - public Add(ItemStack input, FluidStack fluid, RecipeTransposer recipe, boolean isFillRecipe) { - super("Transposer"); - this.isFillRecipe = isFillRecipe; - this.input = new ComparableItemStackSafe(input); - - 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(); - } + public static void removeRecipe(IIngredient input, IIngredient liquid, RecipeType type) { + List recipes = new LinkedList(); + + for(RecipeTransposer recipe : type == RecipeType.Fill ? TransposerManager.getFillRecipeList() : TransposerManager.getExtractionRecipeList()) { + if(recipe != null && matches(input, toIItemStack(recipe.getInput())) && matches(liquid, toILiquidStack(recipe.getFluid()))) { + recipes.add(recipe); + } + } + + if(!recipes.isEmpty()) { + MineTweakerAPI.apply(new Remove(recipes, type)); + } else { + LogHelper.logWarning(String.format("No %s Recipe found for %s and %s.", type == RecipeType.Fill ? Transposer.nameFill : Transposer.nameExtract, input.toString(), liquid.toString())); + } } - // /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - @ZenMethod - public static void removeFillRecipe(IItemStack input, ILiquidStack liquid) { - removeRecipe(input, liquid, true); + private static class Remove extends BaseListRemoval { + private RecipeType type; + + protected Remove(List recipes, RecipeType type) { + super(type == RecipeType.Fill ? Transposer.nameFill : Transposer.nameExtract, null, recipes); + this.type = type; + } + + @Override + public void apply() { + for(RecipeTransposer recipe : recipes) { + boolean removed = false; + switch(type) { + case Fill: + removed = TransposerManager.removeFillRecipe(recipe.getInput(), recipe.getFluid()); + break; + case Extract: + removed = TransposerManager.removeExtractionRecipe(recipe.getInput()); + break; + } + + if(removed) { + successful.add(recipe); + } + } + } + + @Override + public void undo() { + for(RecipeTransposer recipe : successful) { + switch(type) { + case Fill: + TransposerManager.addFillRecipe( + recipe.getEnergy(), + recipe.getInput(), + recipe.getOutput(), + recipe.getFluid(), + false); + break; + + case Extract: + TransposerManager.addExtractionRecipe( + recipe.getEnergy(), + recipe.getInput(), + recipe.getOutput(), + recipe.getFluid(), + recipe.getChance(), + false); + break; + } + } + } + + @Override + protected boolean equals(RecipeTransposer recipe, RecipeTransposer otherRecipe) { + return ThermalHelper.equals(recipe, otherRecipe); + } + + @Override + protected String getRecipeInfo(RecipeTransposer recipe) { + return LogHelper.getStackDescription(recipe.getOutput()); + } } + + protected enum RecipeType { + Fill, + Extract + } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - @ZenMethod - public static void removeExtractRecipe(IItemStack input) { - removeRecipe(input, null, false); - } + @ZenMethod + public static void refreshRecipes() { + MineTweakerAPI.apply(new Refresh()); + } - @ZenMethod - public static void removeRecipe(IItemStack input, ILiquidStack liquid, boolean isFillRecipe) { - - MineTweakerAPI.apply(new Remove(toStack(input), toFluid(liquid), isFillRecipe)); - } + private static class Refresh implements IUndoableAction { - private static class Remove extends BaseDescriptionRemoval { - private final boolean isFillRecipe; - private final ComparableItemStackSafe input; - private List key; - private RecipeTransposer recipe; + public void apply() { + TransposerManager.refreshRecipes(); + } - 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) }); - } - } + public boolean canUndo() { + return true; + } - @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); - } + public String describe() { + return "Refreshing " + Transposer.nameFill + " & " + Transposer.nameExtract + " recipes"; + } - removeValidated(input); - } + public void undo() { + } - @Override - public boolean canUndo() { - return input != null; - } + public String describeUndo() { + return "Ignoring undo of " + Transposer.nameFill + " & " + Transposer.nameExtract + " recipe refresh"; + } - @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(); - } - } + public Object getOverrideKey() { + return null; + } + } } diff --git a/src/main/java/modtweaker2/utils/ArrayUtils.java b/src/main/java/modtweaker2/utils/ArrayUtils.java index 07804bc..3297f67 100644 --- a/src/main/java/modtweaker2/utils/ArrayUtils.java +++ b/src/main/java/modtweaker2/utils/ArrayUtils.java @@ -2,8 +2,6 @@ package modtweaker2.utils; import java.util.ArrayList; -import net.minecraftforge.common.util.ForgeDirection; - public class ArrayUtils { diff --git a/src/main/java/modtweaker2/utils/BaseCraftingAddition.java b/src/main/java/modtweaker2/utils/BaseCraftingAddition.java index 8022a4d..b33a1e1 100644 --- a/src/main/java/modtweaker2/utils/BaseCraftingAddition.java +++ b/src/main/java/modtweaker2/utils/BaseCraftingAddition.java @@ -1,56 +1,22 @@ package modtweaker2.utils; -import net.minecraft.item.ItemStack; -import net.minecraft.item.crafting.IRecipe; - import java.util.List; -import static modtweaker2.helpers.StackHelper.areEqual; +import modtweaker2.helpers.LogHelper; +import net.minecraft.item.crafting.IRecipe; -public abstract class BaseCraftingAddition extends BaseDescriptionAddition { - protected final List list; - protected final boolean shapeless; - protected final ItemStack output; - protected final Object[] recipe; +public abstract class BaseCraftingAddition extends BaseListAddition { - public BaseCraftingAddition(String name, boolean shapeless, List list, ItemStack output, Object... recipe) { - super(name); - this.shapeless = shapeless; - this.output = output; - this.recipe = recipe; - this.list = list; - } - - @Override - public void apply() { - if (shapeless) applyShapeless(); - else applyShaped(); - } - - public abstract void applyShaped(); - - public abstract void applyShapeless(); - - @Override - public boolean canUndo() { - return list != null; - } - - @Override - public void undo() { - IRecipe remove = null; - for (IRecipe recipe : list) { - if (recipe.getRecipeOutput() != null && areEqual(recipe.getRecipeOutput(), output)) { - remove = recipe; - break; - } - } - - list.remove(remove); - } - - @Override - public String getRecipeInfo() { - return output.getDisplayName(); - } + protected BaseCraftingAddition(String name, List list) { + super(name, list); + } + + protected BaseCraftingAddition(String name, List list, List recipes) { + super(name, list, recipes); + } + + @Override + protected String getRecipeInfo(IRecipe recipe) { + return LogHelper.getStackDescription(recipe.getRecipeOutput()); + } } diff --git a/src/main/java/modtweaker2/utils/BaseCraftingRemoval.java b/src/main/java/modtweaker2/utils/BaseCraftingRemoval.java index 4fa3b53..3cfa78e 100644 --- a/src/main/java/modtweaker2/utils/BaseCraftingRemoval.java +++ b/src/main/java/modtweaker2/utils/BaseCraftingRemoval.java @@ -1,29 +1,35 @@ package modtweaker2.utils; +import static modtweaker2.helpers.InputHelper.toIItemStack; +import static modtweaker2.helpers.StackHelper.matches; + +import java.util.LinkedList; +import java.util.List; + +import minetweaker.api.item.IIngredient; +import modtweaker2.helpers.LogHelper; import net.minecraft.item.ItemStack; import net.minecraft.item.crafting.IRecipe; -import java.util.List; - -import static modtweaker2.helpers.StackHelper.areEqual; - -public class BaseCraftingRemoval extends BaseListRemoval { - public BaseCraftingRemoval(String name, List list, ItemStack stack) { - super(name, list, stack); +public class BaseCraftingRemoval extends BaseListRemoval { + public BaseCraftingRemoval(String name, List list, List recipes) { + super(name, list, recipes); } - - @Override - public void apply() { - for (IRecipe r : (List) list) { - if (r.getRecipeOutput() != null && r.getRecipeOutput() instanceof ItemStack && areEqual(r.getRecipeOutput(), stack)) { - recipes.add(r); - } - } - super.apply(); - } - - @Override - public String getRecipeInfo() { - return stack.getDisplayName(); - } -} + + @Override + public String getRecipeInfo(IRecipe recipe) { + return LogHelper.getStackDescription(recipe.getRecipeOutput()); + } + + public static List getRecipes(List list, IIngredient ingredient) { + List recipes = new LinkedList(); + + for (IRecipe r : list) { + if (r != null && r.getRecipeOutput() != null && r.getRecipeOutput() instanceof ItemStack && matches(ingredient, toIItemStack(r.getRecipeOutput()))) { + recipes.add(r); + } + } + + return recipes; + } +} \ No newline at end of file diff --git a/src/main/java/modtweaker2/utils/BaseDescriptionAddition.java b/src/main/java/modtweaker2/utils/BaseDescriptionAddition.java deleted file mode 100644 index 7b9b908..0000000 --- a/src/main/java/modtweaker2/utils/BaseDescriptionAddition.java +++ /dev/null @@ -1,36 +0,0 @@ -package modtweaker2.utils; - -import net.minecraft.item.ItemStack; -import minetweaker.IUndoableAction; - -public abstract class BaseDescriptionAddition implements IUndoableAction { - protected String description; - - public BaseDescriptionAddition(String description) { - this.description = description; - } - - @Override - public boolean canUndo() { - return true; - } - - public String getRecipeInfo() { - return "Unknown Item"; - } - - @Override - public String describe() { - return "Adding " + description + " Recipe for :" + getRecipeInfo(); - } - - @Override - public String describeUndo() { - return "Removing " + description + " Recipe for :" + getRecipeInfo(); - } - - @Override - public Object getOverrideKey() { - return null; - } -} diff --git a/src/main/java/modtweaker2/utils/BaseDescriptionRemoval.java b/src/main/java/modtweaker2/utils/BaseDescriptionRemoval.java deleted file mode 100644 index 9b2b82e..0000000 --- a/src/main/java/modtweaker2/utils/BaseDescriptionRemoval.java +++ /dev/null @@ -1,35 +0,0 @@ -package modtweaker2.utils; - -import minetweaker.IUndoableAction; - -public abstract class BaseDescriptionRemoval implements IUndoableAction { - protected String description; - - public BaseDescriptionRemoval(String description) { - this.description = description; - } - - @Override - public boolean canUndo() { - return true; - } - - public String getRecipeInfo() { - return "Unknown Item"; - } - - @Override - public String describe() { - return "Removing " + description + " Recipe for :" + getRecipeInfo(); - } - - @Override - public String describeUndo() { - return "Restoring " + description + " Recipe for :" + getRecipeInfo(); - } - - @Override - public Object getOverrideKey() { - return null; - } -} diff --git a/src/main/java/modtweaker2/utils/BaseListAddition.java b/src/main/java/modtweaker2/utils/BaseListAddition.java index 9757f84..9e16460 100644 --- a/src/main/java/modtweaker2/utils/BaseListAddition.java +++ b/src/main/java/modtweaker2/utils/BaseListAddition.java @@ -1,65 +1,65 @@ package modtweaker2.utils; -import minetweaker.IUndoableAction; -import net.minecraft.item.ItemStack; -import net.minecraftforge.fluids.FluidStack; - import java.util.List; -public abstract class BaseListAddition implements IUndoableAction { - protected final List list; - protected final Object recipe; - protected String description; +import modtweaker2.helpers.LogHelper; - public BaseListAddition(String description, List list, Object recipe) { - this(list, recipe); - this.description = description; - } +public abstract class BaseListAddition extends BaseListModification { - public BaseListAddition(List list, Object recipe) { - this.list = list; - this.recipe = recipe; - } + protected BaseListAddition(String name, List list) { + super(name, list); + } + + protected BaseListAddition(String name, List list, List recipies) { + this(name, list); + if(recipes != null) { + recipes.addAll(recipies); + } + } - @Override - public void apply() { - list.add(recipe); - } + @Override + public void apply() { + if(recipes.isEmpty()) { + return; + } - @Override - public boolean canUndo() { - return list != null; - } + for(T recipe : recipes) { + if(recipe != null) { + if(list.add(recipe)) { + successful.add(recipe); + } else { + LogHelper.logError(String.format("Error adding %s Recipe for %s", name, getRecipeInfo(recipe))); + } + } else { + LogHelper.logError(String.format("Error adding %s Recipe: null object", name)); + } + } + } + + @Override + public void undo() { + if(this.successful.isEmpty()) { + return; + } + + for(T recipe : successful) { + if(recipe != null) { + 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", name)); + } + } + } - @Override - public void undo() { - list.remove(recipe); - } + @Override + public String describe() { + return String.format("[ModTweaker2] Adding %d %s Recipe(s) for %s", recipes.size(), name, getRecipeInfo()); + } - public String getRecipeInfo() { - return "Unknown Item"; - } - - @Override - public String describe() { - if (recipe instanceof ItemStack) - return "Adding " + description + " Recipe for :" + ((ItemStack) recipe).getDisplayName(); - else if (recipe instanceof FluidStack) - return "Adding " + description + " Recipe for :" + ((FluidStack) recipe).getFluid().getLocalizedName(); - else return "Adding " + description + " Recipe for :" + getRecipeInfo(); - } - - @Override - public String describeUndo() { - if (recipe instanceof ItemStack) - return "Removing " + description + " Recipe for :" + ((ItemStack) recipe).getDisplayName(); - else if (recipe instanceof FluidStack) - return "Removing " + description + " Recipe for :" + ((FluidStack) recipe).getFluid().getLocalizedName(); - else return "Removing " + description + " Recipe for :" + getRecipeInfo(); - } - - @Override - public Object getOverrideKey() { - return null; - } -} + @Override + public String describeUndo() { + 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/BaseListModification.java b/src/main/java/modtweaker2/utils/BaseListModification.java new file mode 100644 index 0000000..f9798c7 --- /dev/null +++ b/src/main/java/modtweaker2/utils/BaseListModification.java @@ -0,0 +1,122 @@ +package modtweaker2.utils; + +import java.util.LinkedList; +import java.util.List; + +public abstract class BaseListModification extends BaseUndoable { + protected final List list; + protected final LinkedList recipes; + protected final LinkedList successful; + + protected BaseListModification(String name, List list) { + super(name); + this.list = list; + this.recipes = new LinkedList(); + this.successful = new LinkedList(); + } + + @Override + public boolean canUndo() { + if(recipes.isEmpty() || successful.isEmpty()) + return false; + + return true; + } + + @Override + protected String getRecipeInfo() { + if(!recipes.isEmpty()) + { + StringBuilder sb = new StringBuilder(); + for(T recipe : recipes) { + if(recipe != null) { + sb.append(getRecipeInfo(recipe)).append(", "); + } + } + + if(sb.length() > 0 ) { + sb.setLength(sb.length() - 2); + } + + return sb.toString(); + } + + return super.getRecipeInfo(); + } + + /** + * This method must be overwritten by the extending classes. It should return + * the name of the key item, for which the recipe is for. For example for machines + * which produce new items, it should return the name of the ouput. For machines + * which are processing items (like a pulverizer) it should return the name of the + * the input. Another example would be the name of the enchantmant for a thaumcraft + * infusion recipe. + */ + protected abstract String getRecipeInfo(T recipe); + + @Override + public int hashCode() { + return 37 * super.hashCode() ^ (list != null ? 43 * list.hashCode() : 0); + } + + @Override + public boolean equals(Object obj) { + if(obj == this) + return true; + + if (obj == null) + return false; + + // Do both actions have the same base? + if(!super.equals(obj)) + return false; + + if (!(obj instanceof BaseListModification)) + return false; + + BaseListModification mod = (BaseListModification)obj; + + // Do both actions reference the same list? + if(this.list != mod.list) { + return false; + } + + // Do both actions contain the same recipes? + if(recipes.size() != mod.recipes.size()) { + return false; + } + + for(T recipe : recipes) { + boolean found = false; + for(T otherRecipe : (List)mod.recipes) { + if(equals(recipe, otherRecipe)) { + found = true; + break; + } + } + + if(!found) { + return false; + } + } + + return true; + } + + /** + * Compares two recipes if they are equal. If the mod does not overwrite equals() in + * their recipe object, this method should be overwritten by the extending classes to + * add a custom equals method + */ + protected boolean equals(T recipe, T otherRecipe) { + if(recipe == otherRecipe) { + return true; + } + + if(!recipe.equals(otherRecipe)) { + return false; + } + + return true; + } +} \ No newline at end of file diff --git a/src/main/java/modtweaker2/utils/BaseListRemoval.java b/src/main/java/modtweaker2/utils/BaseListRemoval.java index 27d467f..06f0518 100644 --- a/src/main/java/modtweaker2/utils/BaseListRemoval.java +++ b/src/main/java/modtweaker2/utils/BaseListRemoval.java @@ -1,133 +1,65 @@ package modtweaker2.utils; -import minetweaker.IUndoableAction; -import minetweaker.MineTweakerAPI; -import modtweaker2.helpers.LogHelper; -import net.minecraft.item.ItemStack; -import net.minecraftforge.fluids.FluidStack; - -import java.util.LinkedList; import java.util.List; -public abstract class BaseListRemoval implements IUndoableAction { - protected final String description; - protected final List list; +import modtweaker2.helpers.LogHelper; - protected final FluidStack fluid; - protected final ItemStack stack; - protected final LinkedList recipes = new LinkedList(); - - public BaseListRemoval(String description, List list, ItemStack stack, FluidStack fluid) { - this.list = list; - this.stack = stack; - this.description = description; - this.fluid = fluid; - } - - public BaseListRemoval(String description, List list, ItemStack stack) { - this(description, list, stack, null); - } - - public BaseListRemoval(String description, List list, FluidStack fluid) { - this(description, list, null, fluid); - } - - public BaseListRemoval(List list, ItemStack stack) { - this(null, list, stack); - } - - public BaseListRemoval(List list, FluidStack stack) { - this(null, list, stack); - } - - public BaseListRemoval(String description, List list) { - this(description, list, null, null); - } - - @Override - public void apply() { - if(recipes.size() == 0) { - MineTweakerAPI.logWarning(String.format("No %s Recipes to remove for: %s", this.description, getRecipeInfo())); - return; - } - - for(Object recipe : recipes) { - list.remove(recipe); - } - } - - @Override - public boolean canUndo() { - return recipes.size() > 0; - } - - @Override - public void undo() { - for(Object recipe : recipes) { - list.add(recipe); - } - } - - public String getRecipeInfo() { - return "Unknown Item"; - } - - @Override - public String describe() { - if(recipes.size() > 0) { - return String.format("Removing %d %s Recipe(s) for: %s", recipes.size(), this.description, getRecipeNames(recipes)); - } else { - return String.format("Removing %s Recipe(s) for: %s", this.description, getRecipeInfo()); - } - } - - @Override - public String describeUndo() { - if(recipes.size() > 0) { - return String.format("Restoring %d %s Recipe(s) for: %s", recipes.size(), this.description, getRecipeNames(recipes)); - } else { - return String.format("No %s Recipes found to restore for: %s", this.description, getRecipeInfo()); +public abstract class BaseListRemoval extends BaseListModification { + + protected BaseListRemoval(String name, List list) { + super(name, list); + } + + protected BaseListRemoval(String name, List list, List recipies) { + this(name, list); + + if(recipes != null) { + recipes.addAll(recipies); } - } + } + + @Override + public void apply() { + if(recipes.isEmpty()) { + return; + } + + for(T recipe : this.recipes) { + if(recipe != null) { + if(this.list.remove(recipe)) { + successful.add(recipe); + } else { + LogHelper.logError(String.format("Error removing %s Recipe for %s", name, getRecipeInfo(recipe))); + } + } else { + LogHelper.logError(String.format("Error removing %s Recipe: null object", name)); + } + } + } + + @Override + public void undo() { + if(successful.isEmpty()) { + return; + } + for(T recipe : successful) { + if(recipe != null) { + if(!list.add(recipe)) { + LogHelper.logError(String.format("Error restoring %s Recipe for %s", name, getRecipeInfo(recipe))); + } + } else { + LogHelper.logError(String.format("Error restoring %s Recipe: null object", name)); + } + } + } - @Override - public Object getOverrideKey() { - return null; - } - - /*** - * Returns the names of the recipes inside the list - * @param list which holds the recipes - * @return name or list of names describing the recipe - */ - private String getRecipeNames(LinkedList list) { - if(list.size() == 0) - return getRecipeInfo(); - - if(list.size() == 1) { - if(recipes.getFirst() instanceof ItemStack) - return ((ItemStack)list.getFirst()).getDisplayName(); - else if (recipes.getFirst() instanceof FluidStack) - return ((FluidStack)list.getFirst()).getFluid().getLocalizedName(); - else - return getRecipeInfo(); - } - - StringBuilder sb = new StringBuilder(); - - if(recipes.getFirst() instanceof ItemStack) { - for(ItemStack itemStack : (List)list) { - sb.append(itemStack.getDisplayName()).append(", "); - } - } else if (recipes.getFirst() instanceof FluidStack) { - for(FluidStack fluidStack : (List)list) { - sb.append(fluidStack.getFluid().getLocalizedName()).append(", "); - } - } else { - sb.append(getRecipeInfo()).append(", "); - } - - sb.setLength(sb.length() - 2); - return sb.toString(); - } -} + @Override + public String describe() { + return String.format("[ModTweaker2] Removing %d %s Recipe(s) for %s", this.recipes.size(), this.name, this.getRecipeInfo()); + } + + @Override + public String describeUndo() { + return String.format("[ModTweaker2] Restoring %d %s Recipe(s) for %s", this.recipes.size(), this.name, this.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 df82716..67bbfca 100644 --- a/src/main/java/modtweaker2/utils/BaseMapAddition.java +++ b/src/main/java/modtweaker2/utils/BaseMapAddition.java @@ -1,69 +1,77 @@ package modtweaker2.utils; -import minetweaker.IUndoableAction; -import net.minecraft.item.ItemStack; -import net.minecraftforge.fluids.FluidStack; - +import java.util.AbstractMap; +import java.util.HashMap; import java.util.Map; +import java.util.Map.Entry; -public abstract class BaseMapAddition implements IUndoableAction { - protected final Map map; - protected String description; - protected Object key; - protected Object recipe; +import modtweaker2.helpers.LogHelper; - public BaseMapAddition(Map map, Object key, Object recipe) { - this(null, map, key, recipe); - } +public abstract class BaseMapAddition extends BaseMapModification { + + protected final HashMap overwritten; + + protected BaseMapAddition(String name, Map map) { + super(name, map); + this.overwritten = new HashMap(); + } + + protected BaseMapAddition(String name, Map map, Map recipes) { + this(name, map); + this.recipes.putAll(recipes); + } - public BaseMapAddition(String description, Map map, Object key, Object recipe) { - this.map = map; - this.key = key; - this.recipe = recipe; - this.description = description; - } + @Override + public void apply() { + if(recipes.isEmpty()) + return; + + for(Entry entry : recipes.entrySet()) { + 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(key, oldValue); + } + + successful.put(key, value); + } + } - @Override - public void apply() { - map.put(key, recipe); - } + @Override + public void undo() { + if(successful.isEmpty() && overwritten.isEmpty()) + return; - @Override - public boolean canUndo() { - return map != null; - } + for(Entry entry : successful.entrySet()) { + K key = entry.getKey(); + V value = map.remove(key); - @Override - public void undo() { - map.remove(key); - } + if(value == null) { + LogHelper.logError(String.format("Error removing %s Recipe: null object", name)); + } + } + + for(Entry entry : overwritten.entrySet()) { + K key = entry.getKey(); + V value = entry.getValue(); + V oldValue = map.put(key, value); + + if(oldValue != null) { + LogHelper.logWarning(String.format("Overwritten %s Recipe which should not exist for %s", name, getRecipeInfo(new AbstractMap.SimpleEntry(entry.getKey(), value)))); + } + } + } - public String getRecipeInfo() { - return "Unknown Item"; - } + @Override + public String describe() { + return String.format("[ModTweaker2] Adding %d %s Recipe(s) for %s", recipes.size(), name, getRecipeInfo()); + } - @Override - public String describe() { - if (recipe instanceof ItemStack) - return "Adding " + description + " Recipe for :" + ((ItemStack) recipe).getDisplayName(); - else if (recipe instanceof FluidStack) - return "Adding " + description + " Recipe for :" + ((FluidStack) recipe).getFluid().getLocalizedName(); - else - return "Adding " + description + " Recipe for :" + getRecipeInfo(); - } - - @Override - public String describeUndo() { - if (recipe instanceof ItemStack) - return "Removing " + description + " Recipe for :" + ((ItemStack) recipe).getDisplayName(); - else if (recipe instanceof FluidStack) - return "Removing " + description + " Recipe for :" + ((FluidStack) recipe).getFluid().getLocalizedName(); - else - return "Removing " + description + " Recipe for :" + getRecipeInfo(); - } - - @Override - public Object getOverrideKey() { - return null; - } + @Override + public String describeUndo() { + return String.format("[ModTweaker2] 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 new file mode 100644 index 0000000..ac71c80 --- /dev/null +++ b/src/main/java/modtweaker2/utils/BaseMapModification.java @@ -0,0 +1,54 @@ +package modtweaker2.utils; + +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; + +public abstract class BaseMapModification extends BaseUndoable { + protected Map map; + protected final HashMap recipes; + protected final HashMap successful; + + protected BaseMapModification(String name, Map map) { + super(name); + this.map = map; + this.recipes = new HashMap(); + this.successful = new HashMap(); + } + + @Override + public boolean canUndo() { + return !recipes.isEmpty(); + } + + @Override + protected String getRecipeInfo() { + if(!recipes.isEmpty()) + { + StringBuilder sb = new StringBuilder(); + for(Entry recipe : recipes.entrySet()) { + if(recipe != null) { + sb.append(getRecipeInfo(recipe)).append(", "); + } + } + + if(sb.length() > 0) { + sb.setLength(sb.length() - 2); + } + + return sb.toString(); + } + + return super.getRecipeInfo(); + } + + /** + * This method must be overwritten by the extending classes. It should return + * the name of the key item, for which the recipe is for. For example for machines + * which produce new items, it should return the name of the ouput. For machines + * which are processing items (like a pulverizer) it should return the name of the + * the input. Another example would be the name of the enchantmant for a thaumcraft + * infusion recipe. + */ + protected abstract String getRecipeInfo(Entry recipe); +} diff --git a/src/main/java/modtweaker2/utils/BaseMapRemoval.java b/src/main/java/modtweaker2/utils/BaseMapRemoval.java index 0db79f7..dcdc9ce 100644 --- a/src/main/java/modtweaker2/utils/BaseMapRemoval.java +++ b/src/main/java/modtweaker2/utils/BaseMapRemoval.java @@ -1,73 +1,62 @@ package modtweaker2.utils; import java.util.Map; +import java.util.Map.Entry; -import minetweaker.IUndoableAction; -import net.minecraft.item.ItemStack; -import net.minecraftforge.fluids.FluidStack; +import modtweaker2.helpers.LogHelper; -public abstract class BaseMapRemoval implements IUndoableAction { - protected final Map map; - protected Object stack; - protected String description; - protected Object key; - protected Object recipe; +public abstract class BaseMapRemoval extends BaseMapModification { + + protected BaseMapRemoval(String name, Map map) { + super(name, map); + } + + protected BaseMapRemoval(String name, Map map, Map recipes) { + this(name, map); + + if(recipes != null) { + this.recipes.putAll(recipes); + } + } - public BaseMapRemoval(Object stack, Map map, Object key) { - this(null, map, key, stack); - } + @Override + public void apply() { + if(recipes.isEmpty()) + return; + + for(K key : recipes.keySet()) { + V oldValue = map.remove(key); + + if(oldValue != null) { + successful.put(key, oldValue); + } else { + LogHelper.logError(String.format("Error removing %s Recipe : null object", name)); + } + } + } + + @Override + public void undo() { + if(successful.isEmpty()) + return; + + for(Entry entry : successful.entrySet()) { + if(entry != 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))); + } + } + } + } + + @Override + public String describe() { + return String.format("[ModTweaker2] Removing %d %s Recipe(s) for %s", recipes.size(), name, getRecipeInfo()); + } - public BaseMapRemoval(String description, Map map, Object stack) { - this(description, map, null, stack); - } - - public BaseMapRemoval(String description, Map map, Object key, Object stack) { - this.stack = stack; - this.map = map; - this.key = key; - this.description = description; - } - - @Override - public void apply() { - recipe = map.get(key); - map.remove(key); - } - - @Override - public boolean canUndo() { - return map != null; - } - - @Override - public void undo() { - map.put(key, recipe); - } - - public String getRecipeInfo() { - return "Unknown Item"; - } - - @Override - public String describe() { - if (recipe instanceof ItemStack) - return "Removing " + description + " Recipe for : " + ((ItemStack) recipe).getDisplayName(); - else if (recipe instanceof FluidStack) - return "Removing " + description + " Recipe for : " + ((FluidStack) recipe).getFluid().getLocalizedName(); - else return "Removing " + description + " Recipe for :" + getRecipeInfo(); - } - - @Override - public String describeUndo() { - if (recipe instanceof ItemStack) - return "Restoring " + description + " Recipe for : " + ((ItemStack) recipe).getDisplayName(); - else if (recipe instanceof FluidStack) - return "Restoring " + description + " Recipe for : " + ((FluidStack) recipe).getFluid().getLocalizedName(); - else return "Restoring " + description + " Recipe for : " + getRecipeInfo(); - } - - @Override - public Object getOverrideKey() { - return null; - } + @Override + public String describeUndo() { + return String.format("[ModTweaker2] Restoring %d %s Recipe(s) for %s", recipes.size(), name, getRecipeInfo()); + } } diff --git a/src/main/java/modtweaker2/utils/BaseMultiModification.java b/src/main/java/modtweaker2/utils/BaseMultiModification.java new file mode 100644 index 0000000..c8fd478 --- /dev/null +++ b/src/main/java/modtweaker2/utils/BaseMultiModification.java @@ -0,0 +1,50 @@ +package modtweaker2.utils; + +import java.util.LinkedList; + +import minetweaker.IUndoableAction; + +public class BaseMultiModification extends BaseUndoable { + + protected final LinkedList actions; + + protected BaseMultiModification(String name) { + super(name); + this.actions = new LinkedList(); + } + + @Override + public void apply() { + for(IUndoableAction action : actions) { + action.apply(); + } + } + + @Override + public boolean canUndo() { + for(IUndoableAction action : actions) { + if(!action.canUndo()) + return false; + } + + return true; + } + + @Override + public String describe() { + return String.format("Applying %d actions for %s Recipe change", this.name, this.actions.size()); + } + + @Override + public String describeUndo() { + return String.format("Reverting %d actions for %s Recipe change", this.name, this.actions.size()); + } + + @Override + public void undo() { + for(IUndoableAction action : actions) { + action.undo(); + } + } + +} diff --git a/src/main/java/modtweaker2/utils/BaseMultipleListRemoval.java b/src/main/java/modtweaker2/utils/BaseMultipleListRemoval.java deleted file mode 100644 index 6ff17cb..0000000 --- a/src/main/java/modtweaker2/utils/BaseMultipleListRemoval.java +++ /dev/null @@ -1,83 +0,0 @@ -package modtweaker2.utils; - -import minetweaker.IUndoableAction; - -import java.util.ArrayList; -import java.util.List; - -public abstract class BaseMultipleListRemoval implements IUndoableAction { - protected final Position pos; - protected final String description; - protected final List list; - protected final Object search; - protected ArrayList recipes; - - public BaseMultipleListRemoval(String description, List list, Object search, Position pos) { - this.pos = pos; - this.description = description; - this.list = list; - this.search = search; - } - - //Return whether the items are equal or not - protected abstract boolean isEqual(Object recipe, Object search); - - @Override - public void apply() { - //Create a new list - recipes = new ArrayList(); - for (Object o : list) { - if (isEqual(o, search)) { - //If we want the last position, reset the array - if (pos == Position.LAST) { - recipes = new ArrayList(); - } - - recipes.add(o); - - //If we want the first position, exit the loop - if (pos == Position.FIRST) { - break; - } - } - } - - //Remove all the recipes that were considered valid for removal - for (Object o : recipes) { - list.remove(o); - } - } - - @Override - public boolean canUndo() { - return recipes != null; - } - - @Override - public void undo() { - for (Object o : recipes) { - list.add(o); - } - } - - public abstract String getRecipeInfo(); - - @Override - public String describe() { - return "Removing " + description + " Recipe for :" + getRecipeInfo(); - } - - @Override - public String describeUndo() { - return "Restoring " + description + " Recipe for :" + getRecipeInfo(); - } - - @Override - public Object getOverrideKey() { - return null; - } - - public static enum Position { - ALL, FIRST, LAST; - } -} diff --git a/src/main/java/modtweaker2/utils/BaseSetVar.java b/src/main/java/modtweaker2/utils/BaseSetVar.java deleted file mode 100644 index 7af454e..0000000 --- a/src/main/java/modtweaker2/utils/BaseSetVar.java +++ /dev/null @@ -1,57 +0,0 @@ -package modtweaker2.utils; - -import net.minecraft.item.ItemStack; -import minetweaker.IUndoableAction; -import modtweaker2.helpers.ReflectionHelper; - -public abstract class BaseSetVar implements IUndoableAction { - protected final String description; - protected final Class clazz; - protected final String field; - protected final int original; - protected final int newValue; - protected Object object; - - public BaseSetVar(String description, Class clazz, Object object, String field, int original, int newValue) { - this(description, clazz, field, original, newValue); - this.object = object; - } - - public BaseSetVar(String description, Class clazz, String field, int original, int newValue) { - this.description = description; - this.clazz = clazz; - this.field = field; - this.original = original; - this.newValue = newValue; - } - - @Override - public void apply() { - ReflectionHelper.setPrivateValue(clazz, object, field, newValue); - } - - @Override - public boolean canUndo() { - return true; - } - - @Override - public void undo() { - ReflectionHelper.setPrivateValue(clazz, object, field, original); - } - - @Override - public String describe() { - return "Setting " + description + " to " + newValue; - } - - @Override - public String describeUndo() { - return "Setting " + description + " to the default value of " + original; - } - - @Override - public Object getOverrideKey() { - return null; - } -} diff --git a/src/main/java/modtweaker2/utils/BaseUndoable.java b/src/main/java/modtweaker2/utils/BaseUndoable.java index e025434..a6cf6e1 100644 --- a/src/main/java/modtweaker2/utils/BaseUndoable.java +++ b/src/main/java/modtweaker2/utils/BaseUndoable.java @@ -3,13 +3,60 @@ package modtweaker2.utils; import minetweaker.IUndoableAction; public abstract class BaseUndoable implements IUndoableAction { + + // Holds the name of the mod / machine this action manipulates + protected final String name; + + // Basic indicator, if the action was successful and can be reverted + protected boolean success = false; + + protected BaseUndoable(String name) { + this.name = name; + } + + protected String getRecipeInfo() { + return "Unknown item"; + } + @Override public boolean canUndo() { - return true; + return success; } - + @Override - public Object getOverrideKey() { - return null; + public String describe() { + return String.format("[ModTweaker2] Altering %s Recipe(s) for %s", this.name, this.getRecipeInfo()); } -} + + @Override + public String describeUndo() { + return String.format("[ModTweaker2] Reverting %s Recipe(s) changes for %s", this.name, this.getRecipeInfo()); + } + + @Override + public boolean equals(Object obj) { + if (obj == null) + return false; + + if(!(obj instanceof BaseUndoable)) { + return false; + } + + BaseUndoable u = (BaseUndoable)obj; + + if(!name.equals(u.name)) + return false; + + return true; + } + + @Override + public int hashCode() { + return name.hashCode(); + } + + @Override + public Object getOverrideKey() { + return null; + } +} \ No newline at end of file diff --git a/src/main/java/modtweaker2/utils/TweakerPlugin.java b/src/main/java/modtweaker2/utils/TweakerPlugin.java index ce005f2..f839e1b 100644 --- a/src/main/java/modtweaker2/utils/TweakerPlugin.java +++ b/src/main/java/modtweaker2/utils/TweakerPlugin.java @@ -1,21 +1,19 @@ package modtweaker2.utils; import java.util.ArrayList; -import java.util.List; -import net.minecraft.item.ItemStack; import cpw.mods.fml.common.Loader; public class TweakerPlugin { - private static ArrayList isLoaded = new ArrayList(); + private static ArrayList isLoaded = new ArrayList(); - public static void register(String mod, Class clazz) { + public static void register(String mod, Class clazz) { if (Loader.isModLoaded(mod)) { load(mod, clazz); } } - public static void load(String mod, Class clazz) { + public static void load(String mod, Class clazz) { try { clazz.newInstance(); isLoaded.add(mod);