From e7ba871adeeeee8f67c0a543e8d15635c40d9525 Mon Sep 17 00:00:00 2001 From: Progwml6 Date: Sun, 7 Dec 2014 02:29:12 -0500 Subject: [PATCH] get rid of LogIdentifier(FML has this now), add in optional recipe duplication detection --- src/mantle/Mantle.java | 15 +- src/mantle/debug/LogIdentifier.java | 56 ----- src/mantle/lib/CoreConfig.java | 8 +- src/mantle/utils/CraftingHandler.java | 329 ++++++++++++++++++++++++++ 4 files changed, 341 insertions(+), 67 deletions(-) delete mode 100644 src/mantle/debug/LogIdentifier.java create mode 100644 src/mantle/utils/CraftingHandler.java diff --git a/src/mantle/Mantle.java b/src/mantle/Mantle.java index 91509a0..7233f11 100644 --- a/src/mantle/Mantle.java +++ b/src/mantle/Mantle.java @@ -4,16 +4,16 @@ import static mantle.lib.CoreRepo.logger; import static mantle.lib.CoreRepo.modId; import static mantle.lib.CoreRepo.modName; import static mantle.lib.CoreRepo.modVersion; + +import cpw.mods.fml.common.event.FMLServerStartedEvent; import mantle.books.BookData; import mantle.books.BookDataStore; import mantle.common.IDDumps; import mantle.common.MProxyCommon; -import mantle.debug.LogIdentifier; import mantle.items.Manual; import mantle.lib.CoreConfig; import mantle.lib.CoreRepo; import mantle.lib.environment.EnvironmentChecks; -import mantle.module.ModuleController; import cpw.mods.fml.common.Mod; import cpw.mods.fml.common.Mod.EventHandler; import cpw.mods.fml.common.Mod.Instance; @@ -23,6 +23,7 @@ import cpw.mods.fml.common.event.FMLPostInitializationEvent; import cpw.mods.fml.common.event.FMLPreInitializationEvent; import cpw.mods.fml.common.network.NetworkRegistry; import cpw.mods.fml.common.registry.GameRegistry; +import mantle.utils.CraftingHandler; import java.io.File; @@ -44,7 +45,6 @@ public class Mantle public static MProxyCommon proxy; public static Manual mantleBook; - public static LogIdentifier logID; /** * Constructor * @@ -69,10 +69,6 @@ public class Mantle logger.info("Entering preinitialization phase."); CoreConfig.loadConfiguration(evt.getModConfigurationDirectory()); - if (CoreConfig.identifyLogs) { - logID = new LogIdentifier(); - logID.preinit(); - } NetworkRegistry.INSTANCE.registerGuiHandler(instance, proxy); mantleBook = (Manual) new Manual().setUnlocalizedName("mantle.manual"); @@ -115,5 +111,10 @@ public class Mantle CoreConfig.loadBookLocations(); IDDumps.dump(); } + @EventHandler + public void serverStart(FMLServerStartedEvent evt) + { + CraftingHandler.logConflicts(); + } } diff --git a/src/mantle/debug/LogIdentifier.java b/src/mantle/debug/LogIdentifier.java deleted file mode 100644 index 8a73b01..0000000 --- a/src/mantle/debug/LogIdentifier.java +++ /dev/null @@ -1,56 +0,0 @@ -package mantle.debug; - -import java.io.PrintStream; -import static mantle.lib.CoreRepo.logger; - -import cpw.mods.fml.common.Loader; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - -public class LogIdentifier -{ - private Logger outLogger = LogManager.getLogger("Mantle-STDOUT"); - private Logger errLogger = LogManager.getLogger("Mantle-STDERR"); - - public void preinit () - { - if(!Loader.isModLoaded("Artemis"))//this is completely useless if artemis is installed - { - logger.info("Inserting TracingPrintStream..."); - System.setOut(new TracingPrintStream(outLogger, "STDOUT", System.out)); - System.setErr(new TracingPrintStream(errLogger, "STDERR", System.err)); - logger.info("TracingPrintStream inserted on STDOUT/STDERR. These will now be redirected to the Mantle-STDOUT/Mantle-STDERR loggers."); - System.out.println("Mantle redirect test-out"); - System.err.println("Mantle redirect test-err");//intended behavior to be able to ensure the integrity of the redirector - //logger.info("Forcing Java.util.Logger logging into log4j2"); - //JavaLoggingRedirector.activate(); - // logger.info("Successfully redirected java.util.logging"); - } - } - -} - -class TracingPrintStream extends PrintStream -{ - static Logger log; - - public TracingPrintStream(Logger l, String Stream, PrintStream original) - { - super(original); - log = l; - } - - @Override - public void println (String x) - { - x = (x == null ? " " : x); - StackTraceElement[] stack = Thread.currentThread().getStackTrace(); - // stack(2) is calling object. - String name = stack[2].getClassName(); - if (name != null) - log.info("[" + name + "]: " + x); - else - log.info("[null|" + Thread.currentThread().getName() + "]: " + x); - } - -} diff --git a/src/mantle/lib/CoreConfig.java b/src/mantle/lib/CoreConfig.java index 17dc9ba..71fbe82 100644 --- a/src/mantle/lib/CoreConfig.java +++ b/src/mantle/lib/CoreConfig.java @@ -53,12 +53,12 @@ public class CoreConfig dumpBiomeIDs = config.get("DebugHelpers", "Dump BIOME ID's in log", dumpBiomeIDs).getBoolean(dumpBiomeIDs); dumpPotionIDs = config.get("DebugHelpers", "Dump POTION ID's in log", dumpPotionIDs).getBoolean(dumpPotionIDs); dumpEnchantIDs = config.get("DebugHelpers", "Dump ENCHANT ID's in log", dumpEnchantIDs).getBoolean(dumpEnchantIDs); + dumpRecipeConflicts = config.get("DebugHelpers", "Dump Recipe Conflicts in log when server starts",dumpRecipeConflicts).getBoolean(dumpRecipeConflicts); - identifyLogs = config.get("DebugHelpers", "Force all unidentified logging entries (STDOUT/STDERR) through log4j2", identifyLogs).getBoolean(identifyLogs); - //check for debugging overrides in system environment checkSysOverrides(); - config.save(); + if(config.hasChanged()) + config.save(); logger.info("Configuration load completed."); } @@ -98,7 +98,7 @@ public class CoreConfig public static boolean dumpBiomeIDs = false; public static boolean dumpPotionIDs = false; public static boolean dumpEnchantIDs = false; - public static boolean identifyLogs = false; + public static boolean dumpRecipeConflicts = false; } diff --git a/src/mantle/utils/CraftingHandler.java b/src/mantle/utils/CraftingHandler.java new file mode 100644 index 0000000..bc622f5 --- /dev/null +++ b/src/mantle/utils/CraftingHandler.java @@ -0,0 +1,329 @@ +package mantle.utils; + +import static mantle.lib.CoreRepo.logger; + +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import cpw.mods.fml.common.registry.GameRegistry; +import mantle.lib.CoreConfig; +import net.minecraft.item.ItemStack; +import net.minecraft.item.crafting.CraftingManager; +import net.minecraft.item.crafting.IRecipe; +import net.minecraft.item.crafting.ShapedRecipes; +import net.minecraft.item.crafting.ShapelessRecipes; +import net.minecraftforge.oredict.OreDictionary; +import net.minecraftforge.oredict.ShapedOreRecipe; +import net.minecraftforge.oredict.ShapelessOreRecipe; + +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +public class CraftingHandler { + private static boolean ia1NG; + + public static void logConflicts () { + + //GameRegistry.addShapelessRecipe(new ItemStack(Items.diamond_hoe), Blocks.planks, Items.stick, Items.chainmail_boots); + //GameRegistry.addShapelessRecipe(new ItemStack(Items.apple), Blocks.planks, Items.stick, Items.chainmail_boots); + if (CoreConfig.dumpRecipeConflicts) { + for (Map.Entry> me : getPotentialConflicts().entrySet()) { + String s = ""; + for (IRecipe i : me.getValue()) { + s += print(i); + } + logger.error("found conflict: \n" + s); + } + } + } + + public static Map> getPotentialConflicts () { + logger.info("dumping recipe conflicts to log this can take a while"); + Map> conflicts = Maps.newHashMap(); + CraftingManager inst = CraftingManager.getInstance(); + List recipes = inst.getRecipeList(); + for (IRecipe i : recipes) { + for (int r = 0; r < recipes.size(); r++) { + IRecipe rc = recipes.get(r); + if (i != null && rc != null) { + boolean comp = compare(i, rc); + if (comp && !areItemStacksEqual(i.getRecipeOutput(), rc.getRecipeOutput())) { + if (conflicts.containsKey(i)) { + List l = conflicts.get(i); + conflicts.remove(i); + if (!l.contains(r)) { + l.add(recipes.get(r)); + } + conflicts.put(i, l); + } else if (conflicts.containsKey(rc)) { + List l = conflicts.get(rc); + conflicts.remove(rc); + if (!l.contains(i)) { + l.add(i); + } + conflicts.put(rc, l); + } else { + List l = Lists.newArrayList(i, rc); + conflicts.put(i, l); + } + if (ia1NG) { + ia1NG = false; + break; + } + } + } + } + } + return conflicts; + } + + private static boolean compare (IRecipe i1, IRecipe i2) { + if (areItemStacksEqual(i1.getRecipeOutput(), i2.getRecipeOutput())) { + return false; + } + boolean is1l = false; + boolean is2l = false; + List il1 = Lists.newArrayList(); + List il2 = Lists.newArrayList(); + ItemStack[] ia1 = null; + boolean invalid = false; + ItemStack[] ia2 = null; + if (i1 instanceof ShapedRecipes) { + ia1 = ((ShapedRecipes) i1).recipeItems.clone(); + } else if (i1 instanceof ShapelessRecipes) { + il1 = new ArrayList(((ShapelessRecipes) i1).recipeItems); + //logger.error("SL Recipe: \n" + print(i1)); + is1l = true; + } else if (i1 instanceof ShapedOreRecipe) { + Object[] o = ((ShapedOreRecipe) i1).getInput(); + ia1 = new ItemStack[o.length]; + for (int in = 0; in < o.length; in++) { + if (o[in] != null && o[in] instanceof ItemStack) { + ia1[in] = (ItemStack) o[in]; + } else if (o[in] != null) { + ia1[in] = ((ArrayList) o[in]).get(0); + } else { + ia1[in] = null; + } + } + } else if (i1 instanceof ShapelessOreRecipe) { + il1 = new ArrayList(); + for (Object o : ((ShapelessOreRecipe) i1).getInput()) { + if (o != null && o instanceof ItemStack) { + ItemStack is = (ItemStack) o; + il1.add(is); + } else if (o != null) { + ItemStack is = ((ArrayList) o).get(0); + il1.add(is); + } else { + il1.add(null); + } + } + is1l = true; + } else { + ia1NG = true; + invalid = true; + } + if (i2 instanceof ShapedRecipes) { + ia2 = ((ShapedRecipes) i2).recipeItems.clone(); + } else if (i2 instanceof ShapelessRecipes) { + il2 = new ArrayList(((ShapelessRecipes) i2).recipeItems); + is2l = true; + } else if (i2 instanceof ShapedOreRecipe) { + Object[] o = ((ShapedOreRecipe) i2).getInput(); + ia2 = new ItemStack[o.length]; + for (int in = 0; in < o.length; in++) { + if (o.length == 6) { + print(i2); + } + if (o[in] != null && o[in] instanceof ItemStack) { + ia2[in] = (ItemStack) o[in]; + } else if (o[in] != null) { + ItemStack is = ((ArrayList) o[in]).get(0); + ia2[in] = is; + } else { + ia2[in] = null; + } + } + } else if (i2 instanceof ShapelessOreRecipe) { + il2 = new ArrayList(); + for (Object o : ((ShapelessOreRecipe) i2).getInput()) { + if (o != null && o instanceof ItemStack) { + ItemStack is = (ItemStack) o; + il2.add(is); + } else if (o != null) { + ItemStack is = ((ArrayList) o).get(0); + il2.add(is); + } else { + il2.add(null); + } + } + is2l = true; + } else { + invalid = true; + } + if (!invalid) { + if (!is1l && !is2l) { + if (ia1 == null) { + logger.error("ia1 null"); + } + if (ia2 == null) { + logger.error("ia2 null"); + } + if (ia1.length != ia2.length) { + return false; + } + for (int i = 0; i < ia1.length; i++) { + if (ia1[i] != null && ia2[i] != null) { + if (!areItemStacksEqual(ia1[i], ia2[i])) { + return false; + } + } else { + return false; + } + } + } else if (is1l && is2l) { + if (il1.size() == il2.size()) { + boolean has = false; + for (int i = 0; i < il1.size(); i++) { + ItemStack ti1 = il1.get(i); + for (int j = 0; j < il2.size(); j++) { + if (areItemStacksEqual(ti1, il2.get(j))) { + il2.remove(j); + has = true; + break; + } + } + } + if (!has) { + return false; + } + if (il2.size() != 0) { + return false; + } + } else { + return false; + } + } else { + List stacks = is1l ? il1 : il2; + ItemStack[] stcks = is2l ? ia1 : ia2; + for (ItemStack i : stcks) { + if (!stacks.contains(i)) { + return false; + } else { + stacks.remove(i); + } + } + } + } else { + return false; + } + if (i1 instanceof ShapedOreRecipe && i2 instanceof ShapedOreRecipe) { + int i1w, i2w, i1h, i2h; + try { + Class cl = ShapedOreRecipe.class; + Field w = cl.getDeclaredField("width"); + w.setAccessible(true); + Field h = cl.getDeclaredField("height"); + h.setAccessible(true); + i1w = (Integer) w.get((ShapedOreRecipe) i1); + i2w = (Integer) w.get((ShapedOreRecipe) i2); + i1h = (Integer) h.get((ShapedOreRecipe) i1); + i2h = (Integer) h.get((ShapedOreRecipe) i2); + if (i1w != i2w || i1h != i2h) { + return false; + } + } catch (Exception e) { + logger.error(e.getMessage(), e); + } + } + //logger.error("CONFLICT SET: \n" + print(i1) + print(i2)); + return true; + } + + private static String print (IRecipe i1) { + String out = ""; + if (i1 instanceof ShapedRecipes) { + out += "SHAPED " + toStr(i1.getRecipeOutput()) + "\n"; + for (ItemStack i : ((ShapedRecipes) i1).recipeItems) { + if (i != null && i.getItem() != null) { + out += toStr((ItemStack) i) + "\n"; + } else { + out += "null \n"; + } + } + + } else if (i1 instanceof ShapelessRecipes) { + out += "SHAPELESS " + toStr(i1.getRecipeOutput()) + "\n"; + for (Object i : ((ShapelessRecipes) i1).recipeItems) { + if ((ItemStack) i != null && ((ItemStack) i).getItem() != null) { + out += toStr((ItemStack) i) + "\n"; + } else { + out += "null \n"; + } + } + } else if (i1 instanceof ShapedOreRecipe) { + out += "SHAPED ORE " + toStr(i1.getRecipeOutput()) + "\n"; + Object[] o = ((ShapedOreRecipe) i1).getInput(); + ItemStack[] ia1 = new ItemStack[o.length]; + for (int in = 0; in < o.length; in++) { + if (o[in] != null && o[in] instanceof ItemStack) { + ia1[in] = (ItemStack) o[in]; + } else if (o[in] != null) { + ia1[in] = ((ArrayList) o[in]).get(0); + } else { + ia1[in] = null; + } + } + + for (Object i : ia1) { + if (i != null && ((ItemStack) i).getItem() != null) { + out += toStr((ItemStack) i) + "\n"; + } else { + out += "null \n"; + } + } + } else if (i1 instanceof ShapelessOreRecipe) { + out += "SHAPELESS ORE " + toStr(i1.getRecipeOutput()) + "\n"; + ItemStack i; + for (Object ob : ((ShapelessOreRecipe) i1).getInput()) { + if (ob instanceof ItemStack) { + i = (ItemStack) ob; + } else { + i = ((ArrayList) ob).get(0); + } + if ((ItemStack) i != null && ((ItemStack) i).getItem() != null) { + out += toStr((ItemStack) i) + "\n"; + } else { + out += "null \n"; + } + } + } + return out; + } + + private static String toStr (ItemStack is) { + return GameRegistry.findUniqueIdentifierFor(((ItemStack) is).getItem()) + " " + ((ItemStack) is).toString(); + } + + public static boolean areItemStacksEqual (ItemStack a, ItemStack b) { + if ((a == null || b == null) && !(a == null && b == null)) { + return false; + } + if ((a == null && b == null)) { + return true; + } + return a.stackSize != b.stackSize + ? false + : (a.getItem() != b.getItem() + ? false + : (compdamage(a, b) + ? false + : (a.stackTagCompound == null && b.stackTagCompound != null ? false : a.stackTagCompound == null || a.stackTagCompound.equals(b.stackTagCompound)))); + } + + public static boolean compdamage (ItemStack a, ItemStack b) { + return a.getItemDamage() != b.getItemDamage() || a.getItemDamage() == OreDictionary.WILDCARD_VALUE || b.getItemDamage() == OreDictionary.WILDCARD_VALUE; + } +} \ No newline at end of file