Added ability to specify preferences for OreDictionary outputs from the SAG Mill

This commit is contained in:
CrazyPants committed 2014-12-09 17:51:15 +08:00
1 parent 57191de651
commit 85efebded5
9 files changed
+347 -24

No files matched your search

@@ -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.ItemMachinePart;
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.ItemTravelStaff;
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:[email protected],);after:MineFactoryReloaded", guiFactory = "crazypants.enderio.config.ConfigFactoryEIO")
public class EnderIO {
@@ -570,6 +570,9 @@ public class EnderIO {
@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();
@@ -4,7 +4,6 @@ import java.io.BufferedInputStream;
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.Log;
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 class RecipeConfigParser extends DefaultHandler {
private boolean inputTagOpen = false;
private boolean debug = false;
private boolean inCustomHandler = false;
private CustomTagHandler customHandler = null;
@@ -376,12 +377,16 @@ public class RecipeConfigParser extends DefaultHandler {
int stackSize = getIntValue(AT_NUMBER, attributes, 1);
String oreDict = getStringValue(AT_ORE_DICT, attributes, null);
if(oreDict != null) {
ArrayList<ItemStack> 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));
@@ -2,6 +2,11 @@ package crazypants.enderio.material;
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.enderio.config.Config;
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 class MaterialRecipes {
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);
@@ -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;
}
}
}
@@ -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<String, ItemStack> preferences = new HashMap<String, ItemStack>();
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<ItemStack> ores = OreDictionaryHelper.getOres(oreDictName);
if(!ores.isEmpty() && ores.get(0) != null) {
result = ores.get(0).copy();
}
preferences.put(oreDictName, result);
}
return result;
}
}
@@ -34,14 +34,6 @@ public final class OreDictionaryHelper {
return isRegistered(DUST_ENDERPEARL);
}
public static ItemStack getPreffered(String oreDictName) {
List<ItemStack> ores = getOres(oreDictName);
if(ores.isEmpty()) {
return null;
}
return ores.get(0);
}
private OreDictionaryHelper() {
}