diff --git a/eclipseCleanUp.xml b/eclipseCleanUp.xml new file mode 100644 index 0000000000..114ecd4f0b --- /dev/null +++ b/eclipseCleanUp.xml @@ -0,0 +1,56 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/resources/assets/enderio/config/OreDictionaryPreferences_Core.xml b/resources/assets/enderio/config/OreDictionaryPreferences_Core.xml new file mode 100644 index 0000000000..733285c7e4 --- /dev/null +++ b/resources/assets/enderio/config/OreDictionaryPreferences_Core.xml @@ -0,0 +1,76 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/resources/assets/enderio/config/OreDictionaryPreferences_User.xml b/resources/assets/enderio/config/OreDictionaryPreferences_User.xml new file mode 100644 index 0000000000..f735f22b82 --- /dev/null +++ b/resources/assets/enderio/config/OreDictionaryPreferences_User.xml @@ -0,0 +1,5 @@ + + + \ No newline at end of file diff --git a/src/main/java/crazypants/enderio/EnderIO.java b/src/main/java/crazypants/enderio/EnderIO.java index 7bf42a9670..db352eba8e 100644 --- a/src/main/java/crazypants/enderio/EnderIO.java +++ b/src/main/java/crazypants/enderio/EnderIO.java @@ -1,9 +1,5 @@ package crazypants.enderio; -import static crazypants.enderio.EnderIO.MODID; -import static crazypants.enderio.EnderIO.MOD_NAME; -import static crazypants.enderio.EnderIO.VERSION; - import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; @@ -142,6 +138,7 @@ import crazypants.enderio.material.ItemMaterial; import crazypants.enderio.material.ItemPowderIngot; import crazypants.enderio.material.MaterialRecipes; +import crazypants.enderio.material.OreDictionaryPreferences; import crazypants.enderio.network.MessageTileNBT; import crazypants.enderio.network.PacketHandler; import crazypants.enderio.rail.BlockEnderRail; @@ -150,6 +147,9 @@ import crazypants.enderio.teleport.TeleportRecipes; import crazypants.enderio.teleport.TravelController; import crazypants.util.EntityUtil; +import static crazypants.enderio.EnderIO.MODID; +import static crazypants.enderio.EnderIO.MOD_NAME; +import static crazypants.enderio.EnderIO.VERSION; @Mod(modid = MODID, name = MOD_NAME, version = VERSION, dependencies = "required-after:Forge@10.13.0.1150,);after:MineFactoryReloaded", guiFactory = "crazypants.enderio.config.ConfigFactoryEIO") public class EnderIO { @@ -570,6 +570,9 @@ public void load(FMLInitializationEvent event) { @EventHandler public void postInit(FMLPostInitializationEvent event) { + //This must be loaded before parsing the recipes so we get the preferred outputs + OreDictionaryPreferences.loadConfig(); + //Regsiter the enchants Enchantments.getInstance(); diff --git a/src/main/java/crazypants/enderio/machine/recipe/RecipeConfigParser.java b/src/main/java/crazypants/enderio/machine/recipe/RecipeConfigParser.java index 6163ee1755..8b54ca815d 100644 --- a/src/main/java/crazypants/enderio/machine/recipe/RecipeConfigParser.java +++ b/src/main/java/crazypants/enderio/machine/recipe/RecipeConfigParser.java @@ -4,7 +4,6 @@ import java.io.File; import java.io.FileInputStream; import java.io.StringReader; -import java.util.ArrayList; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; @@ -31,6 +30,8 @@ import crazypants.enderio.machine.crusher.CrusherRecipeManager; import crazypants.enderio.machine.recipe.RecipeConfig.RecipeElement; import crazypants.enderio.machine.recipe.RecipeConfig.RecipeGroup; +import crazypants.enderio.material.OreDictionaryPreferences; +import crazypants.util.OreDictionaryHelper; public class RecipeConfigParser extends DefaultHandler { @@ -103,7 +104,7 @@ public static RecipeConfig parse(InputSource is, CustomTagHandler customHandler) private boolean inputTagOpen = false; private boolean debug = false; - + private boolean inCustomHandler = false; private CustomTagHandler customHandler = null; @@ -376,12 +377,16 @@ public static RecipeInput getItemStack(Attributes attributes) { int stackSize = getIntValue(AT_NUMBER, attributes, 1); String oreDict = getStringValue(AT_ORE_DICT, attributes, null); if(oreDict != null) { - ArrayList ores = OreDictionary.getOres(oreDict); - if(ores == null || ores.isEmpty() || ores.get(0) == null) { + if(!OreDictionaryHelper.isRegistered(oreDict)) { Log.debug(LP + "Could not find an entry in the ore dictionary for " + oreDict); return null; } - ItemStack stack = ores.get(0).copy(); + ItemStack stack = OreDictionaryPreferences.instance.getPreferred(oreDict); + if(stack == null) { + Log.debug(LP + "Could not find a prefered item in the ore dictionary for " + oreDict); + return null; + } + stack = stack.copy(); stack.stackSize = stackSize; return new OreDictionaryRecipeInput(stack, OreDictionary.getOreID(oreDict), getFloatValue(AT_MULTIPLIER, attributes, 1), getIntValue(AT_SLOT, attributes, -1)); diff --git a/src/main/java/crazypants/enderio/material/MaterialRecipes.java b/src/main/java/crazypants/enderio/material/MaterialRecipes.java index 0e93e66ddd..4a49f09440 100644 --- a/src/main/java/crazypants/enderio/material/MaterialRecipes.java +++ b/src/main/java/crazypants/enderio/material/MaterialRecipes.java @@ -2,6 +2,11 @@ import java.util.ArrayList; +import static crazypants.util.OreDictionaryHelper.INGOT_TIN; +import static crazypants.util.OreDictionaryHelper.hasCopper; +import static crazypants.util.OreDictionaryHelper.hasEnderPearlDust; +import static crazypants.util.OreDictionaryHelper.hasTin; + import net.minecraft.init.Blocks; import net.minecraft.init.Items; import net.minecraft.item.Item; @@ -15,11 +20,6 @@ import crazypants.util.OreDictionaryHelper; import static crazypants.enderio.EnderIO.itemBasicCapacitor; -import static crazypants.util.OreDictionaryHelper.INGOT_TIN; -import static crazypants.util.OreDictionaryHelper.getPreffered; -import static crazypants.util.OreDictionaryHelper.hasCopper; -import static crazypants.util.OreDictionaryHelper.hasEnderPearlDust; -import static crazypants.util.OreDictionaryHelper.hasTin; public class MaterialRecipes { @@ -193,10 +193,11 @@ public static void addRecipes() { public static void addOreDictionaryRecipes() { if(OreDictionaryHelper.hasCopper()) { FurnaceRecipes.smelting().func_151394_a(new ItemStack(EnderIO.itemPowderIngot, 1, PowderIngot.POWDER_COPPER.ordinal()), - getPreffered(OreDictionaryHelper.INGOT_COPPER), 0); + OreDictionaryPreferences.instance.getPreferred(OreDictionaryHelper.INGOT_COPPER), 0); } if(hasTin()) { - FurnaceRecipes.smelting().func_151394_a(new ItemStack(EnderIO.itemPowderIngot, 1, PowderIngot.POWDER_TIN.ordinal()), getPreffered(INGOT_TIN), 0); + FurnaceRecipes.smelting().func_151394_a(new ItemStack(EnderIO.itemPowderIngot, 1, PowderIngot.POWDER_TIN.ordinal()), + OreDictionaryPreferences.instance.getPreferred(INGOT_TIN), 0); } ItemStack capacitor = new ItemStack(EnderIO.itemBasicCapacitor, 1, 0); diff --git a/src/main/java/crazypants/enderio/material/OreDictionaryPreferenceParser.java b/src/main/java/crazypants/enderio/material/OreDictionaryPreferenceParser.java new file mode 100644 index 0000000000..605dbc3d97 --- /dev/null +++ b/src/main/java/crazypants/enderio/material/OreDictionaryPreferenceParser.java @@ -0,0 +1,143 @@ +package crazypants.enderio.material; + +import java.io.File; +import java.io.IOException; +import java.io.StringReader; + +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; + +import net.minecraft.item.ItemStack; +import net.minecraftforge.oredict.OreDictionary; + +import org.xml.sax.Attributes; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; +import org.xml.sax.XMLReader; +import org.xml.sax.helpers.DefaultHandler; + +import crazypants.enderio.Log; +import crazypants.enderio.config.Config; +import crazypants.enderio.machine.recipe.RecipeConfig; +import crazypants.enderio.machine.recipe.RecipeConfigParser; +import crazypants.enderio.machine.recipe.RecipeInput; + +public final class OreDictionaryPreferenceParser extends DefaultHandler { + + private static final String CORE_FILE_NAME = "OreDictionaryPreferences_Core.xml"; + private static final String CUSTOM_FILE_NAME = "OreDictionaryPreferences_User.xml"; + + public static void loadConfig() { + File coreFile = new File(Config.configDirectory, CORE_FILE_NAME); + + String defaultVals = null; + try { + defaultVals = RecipeConfig.readRecipes(coreFile, CORE_FILE_NAME, true); + } catch (IOException e) { + Log.error("Could not load painter lists " + coreFile + " from EnderIO jar: " + e.getMessage()); + e.printStackTrace(); + return; + } + if(!coreFile.exists()) { + Log.error("Could not load default lists from " + coreFile + " as the file does not exist."); + return; + } + + try { + parse(defaultVals); + } catch (Exception e) { + Log.error("Could not parse default lists from " + coreFile + ": " + e); + } + + File userFile = new File(Config.configDirectory, CUSTOM_FILE_NAME); + String userConfigStr = null; + try { + userConfigStr = RecipeConfig.readRecipes(userFile, CUSTOM_FILE_NAME, false); + if(userConfigStr == null || userConfigStr.trim().length() == 0) { + Log.error("Empty user config file: " + userFile.getAbsolutePath()); + } else { + parse(userConfigStr); + } + } catch (Exception e) { + Log.error("Could not load user defined painter lists from file: " + CUSTOM_FILE_NAME); + e.printStackTrace(); + } + } + + public static void parse(String str) throws Exception { + StringReader reader = new StringReader(str); + InputSource is = new InputSource(reader); + try { + parse(is); + } finally { + reader.close(); + } + + } + + private static void parse(InputSource is) throws Exception { + OreDictionaryPreferenceParser parser = new OreDictionaryPreferenceParser(OreDictionaryPreferences.instance); + + SAXParserFactory spf = SAXParserFactory.newInstance(); + spf.setNamespaceAware(true); + SAXParser saxParser = spf.newSAXParser(); + XMLReader xmlReader = saxParser.getXMLReader(); + xmlReader.setContentHandler(parser); + xmlReader.parse(is); + } + + public static final String ELEMENT_PREF = "preference"; + public static final String ELEMENT_STACK = "itemStack"; + + private static final String AT_ORE_DICT = "oreDictionary"; + public static final String AT_ITEM_META = "itemMeta"; + public static final String AT_ITEM_NAME = "itemName"; + public static final String AT_MOD_ID = "modID"; + + private OreDictionaryPreferences prefs; + private String oreDictName; + private ItemStack prefStack; + + private OreDictionaryPreferenceParser(OreDictionaryPreferences prefs) { + this.prefs = prefs; + } + + @Override + public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { + if(ELEMENT_PREF.equals(localName)) { + oreDictName = RecipeConfigParser.getStringValue(AT_ORE_DICT, attributes, null); + return; + } + if(ELEMENT_STACK.equals(localName) && oreDictName != null && prefStack == null) { + RecipeInput ri = RecipeConfigParser.getItemStack(attributes); + if(ri != null && ri.getInput() != null) { + prefStack = ri.getInput(); + } + return; + } + } + + @Override + public void endElement(String uri, String localName, String qName) throws SAXException { + if(ELEMENT_PREF.equals(localName)) { + if(oreDictName != null && prefStack != null) { + boolean matched = false; + int[] ids = OreDictionary.getOreIDs(prefStack); + if(ids != null) { + for (int i = 0; i < ids.length && !matched; i++) { + matched = oreDictName.equals(OreDictionary.getOreName(ids[i])); + } + } + if(matched) { + prefs.setPreference(oreDictName, prefStack); + } else { + Log.warn("OreDictionaryPreferenceParser: Attempted to register " + prefStack + " as the preffered output for " + oreDictName + + " but it is not registered in the OreDictionary as " + oreDictName); + } + } + oreDictName = null; + prefStack = null; + } + } + +} diff --git a/src/main/java/crazypants/enderio/material/OreDictionaryPreferences.java b/src/main/java/crazypants/enderio/material/OreDictionaryPreferences.java new file mode 100644 index 0000000000..2346f8a264 --- /dev/null +++ b/src/main/java/crazypants/enderio/material/OreDictionaryPreferences.java @@ -0,0 +1,42 @@ +package crazypants.enderio.material; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import crazypants.util.OreDictionaryHelper; + +import net.minecraft.item.ItemStack; + +public final class OreDictionaryPreferences { + + public static final OreDictionaryPreferences instance = new OreDictionaryPreferences(); + + public static void loadConfig() { + OreDictionaryPreferenceParser.loadConfig(); + } + + private Map preferences = new HashMap(); + + public void setPreference(String oreDictName, ItemStack stack) { + if(oreDictName == null || stack == null) { + return; + } + preferences.put(oreDictName, stack.copy()); + } + + public ItemStack getPreferred(String oreDictName) { + ItemStack result = null; + if(preferences.containsKey(oreDictName)) { + result = preferences.get(oreDictName); + } else { + List ores = OreDictionaryHelper.getOres(oreDictName); + if(!ores.isEmpty() && ores.get(0) != null) { + result = ores.get(0).copy(); + } + preferences.put(oreDictName, result); + } + return result; + } + +} diff --git a/src/main/java/crazypants/util/OreDictionaryHelper.java b/src/main/java/crazypants/util/OreDictionaryHelper.java index 7b18d6e9fb..5a92e78609 100644 --- a/src/main/java/crazypants/util/OreDictionaryHelper.java +++ b/src/main/java/crazypants/util/OreDictionaryHelper.java @@ -34,14 +34,6 @@ public static boolean hasEnderPearlDust() { return isRegistered(DUST_ENDERPEARL); } - public static ItemStack getPreffered(String oreDictName) { - List ores = getOres(oreDictName); - if(ores.isEmpty()) { - return null; - } - return ores.get(0); - } - private OreDictionaryHelper() { }