From 834f4f8b9533750d6cf929e9c17b929d1c4d138c Mon Sep 17 00:00:00 2001 From: Francesco Macagno Date: Mon, 17 Aug 2015 23:06:20 -0700 Subject: [PATCH] Changed OreManager to FillerManager Also added new categories to match --- .../{OreManager.java => FillerManager.java} | 157 +++++++++--------- .../cr0s/warpdrive/conf/WarpDriveConfig.java | 2 +- .../cr0s/warpdrive/conf/structures/Orb.java | 11 +- 3 files changed, 83 insertions(+), 87 deletions(-) rename src/main/java/cr0s/warpdrive/conf/{OreManager.java => FillerManager.java} (69%) diff --git a/src/main/java/cr0s/warpdrive/conf/OreManager.java b/src/main/java/cr0s/warpdrive/conf/FillerManager.java similarity index 69% rename from src/main/java/cr0s/warpdrive/conf/OreManager.java rename to src/main/java/cr0s/warpdrive/conf/FillerManager.java index 25667d36..e71a75a2 100644 --- a/src/main/java/cr0s/warpdrive/conf/OreManager.java +++ b/src/main/java/cr0s/warpdrive/conf/FillerManager.java @@ -16,168 +16,165 @@ import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; -import cpw.mods.fml.common.Loader; import cr0s.warpdrive.WarpDrive; -public class OreManager { - - - public enum OreSpawnCategory { - RARE, COMMON; - +public class FillerManager { + + + public enum FillerCategory { + RARE_ORES("rareOres"), COMMON_ORES("commonOres"), UNCOMMON_ORES("uncommonOres"), OVERWORLD("overworld"), NETHER("nether"), END("end"); - private TreeMap ores; + + private TreeMap fillerBlocks; public int totWeight; - - private OreSpawnCategory() { - ores = new TreeMap(new BlockComparator()); + private String name; + + private FillerCategory(String name) { + this.name = name; + fillerBlocks = new TreeMap(new BlockComparator()); } + + public void addFillerBlock(Block b, int weight) { - public void addOre(Block b, int weight) { - if (b == null || weight < 1) throw new IllegalArgumentException(); - - ores.put(b, weight); + + fillerBlocks.put(b, weight); totWeight += weight; } - - public Set> getOres() { - return ores.entrySet(); - } - - public Integer getOreWeight(Block b) { - return ores.get(b); + + public Set> getBlocks() { + return fillerBlocks.entrySet(); } + public Integer getBlockWeight(Block b) { + return fillerBlocks.get(b); + } + public int getTotalWeight() { return totWeight; } - - public static OreSpawnCategory getOreSpawnValue(String s) { - return OreSpawnCategory.valueOf(s.trim().toUpperCase()); + + public static FillerCategory getOreSpawnValue(String s) { + for (FillerCategory cat : FillerCategory.values()) + if (cat.name.equals(s)) + return cat; + return null; } - - } - - public static void loadOres(String oreConfDirectory) { - - loadOres(new File(oreConfDirectory)); } + public static void loadOres(String oreConfDirectory) { + + loadOres(new File(oreConfDirectory)); + + } + public static void loadOres(File dir) { - + String s = "These are the ores names that have been registered, and so may be used to create structures: "; - + for (String ore : OreDictionary.getOreNames()) { s = s + "\n" + ore; } - + WarpDrive.logger.info(s); - + dir.mkdir(); - + if (!dir.isDirectory()) throw new IllegalArgumentException("File path " + dir.getPath() + " must be a drectory!"); - - File[] files = dir.listFiles(new FilenameFilter() { + File[] files = dir.listFiles(new FilenameFilter() { + @Override public boolean accept(File dir, String name) { - return name.startsWith("ores") && name.endsWith(".xml"); + return name.startsWith("filler") && name.endsWith(".xml"); } - + }); - + for(File f : files) { - + try { - - WarpDrive.logger.info("Loading ore data file " + f.getPath()); - + + WarpDrive.logger.info("Loading filler data file " + f.getPath()); + loadXmlOreFile(f); - - WarpDrive.logger.info("Finished loading ore data file " + f.getPath()); - + + WarpDrive.logger.info("Finished loading filler data file " + f.getPath()); + } catch (Exception e) { WarpDrive.logger.error("Error loading file " + f.getPath() + ": " + e.getMessage()); e.printStackTrace(); } - + } + - } - + private static void loadXmlOreFile(File f) throws InvalidXmlException, SAXException, IOException { - + Document base = WarpDriveConfig.getXmlDocumentBuilder().parse(f); - + NodeList modReqList = base.getElementsByTagName("ModRequirements"); for (int i = 0; i < modReqList.getLength(); i++) { - NodeList mods = ((Element) modReqList.item(i)).getElementsByTagName("mod"); - for (int j = 0; j < mods.getLength(); j++) { - Element mod = (Element) mods.item(j); + String res = ModRequirementChecker.getMissingMods((Element) modReqList.item(i)); + + if (!res.isEmpty()) { - String name = mod.getTextContent(); - if (name.isEmpty()) - throw new InvalidXmlException("A mod requirement at " + i + ":" + j + " is empty!"); - - if (!Loader.isModLoaded(name)) { - WarpDrive.logger.info("Skippping ore data file " + f.getPath() + " because the mod " + name + " is not loaded"); - } - + WarpDrive.logger.info("Skippping filler data file " + f.getPath() + " because the mods " + res + " are not loaded"); } } - + NodeList ores = base.getElementsByTagName("ore"); for (int i = 0; i < ores.getLength(); i++) { - + Element ore = (Element) ores.item(i); - + String oreName = ore.getAttribute("name"); if (oreName.isEmpty()) throw new InvalidXmlException("Ore " + i + "does not contain a name!"); - + String oreWeight = ore.getAttribute("weight"); if (oreWeight.isEmpty()) throw new InvalidXmlException("Ore " + i + " does not contain a weight!"); - + String category = ore.getAttribute("category"); if (category.isEmpty()) throw new InvalidXmlException("Ore " + i + " does not contain a category!"); - + int weight; try { weight = Integer.parseInt(oreWeight); } catch (NumberFormatException e) { throw new InvalidXmlException("Ore weight at " + i + " is invalid!"); } - + if(weight < 1) throw new InvalidXmlException("Ore weight at " + i + " is too low!"); - - OreSpawnCategory cat = OreSpawnCategory.getOreSpawnValue(category); + + FillerCategory cat = FillerCategory.getOreSpawnValue(category); if(cat == null) throw new InvalidXmlException(); - + Block b = Block.getBlockFromName(oreName); if(b == null) throw new InvalidXmlException("Ore name at " + i + " is not a valid block!"); - + //Everything is fine - cat.addOre(b, weight); + cat.addFillerBlock(b, weight); } } - + public static class BlockComparator implements Comparator { - + @Override public int compare(Block arg0, Block arg1) { return arg0.getUnlocalizedName().compareTo(arg1.getUnlocalizedName()); } - + } - + } diff --git a/src/main/java/cr0s/warpdrive/conf/WarpDriveConfig.java b/src/main/java/cr0s/warpdrive/conf/WarpDriveConfig.java index bdac6050..b4b8f12b 100644 --- a/src/main/java/cr0s/warpdrive/conf/WarpDriveConfig.java +++ b/src/main/java/cr0s/warpdrive/conf/WarpDriveConfig.java @@ -891,7 +891,7 @@ public class WarpDriveConfig { - OreManager.loadOres(baseConfFolder); + FillerManager.loadOres(baseConfFolder); StructureManager.loadStructures(baseConfFolder); } diff --git a/src/main/java/cr0s/warpdrive/conf/structures/Orb.java b/src/main/java/cr0s/warpdrive/conf/structures/Orb.java index 195e1092..e2405a7d 100644 --- a/src/main/java/cr0s/warpdrive/conf/structures/Orb.java +++ b/src/main/java/cr0s/warpdrive/conf/structures/Orb.java @@ -14,7 +14,7 @@ import org.w3c.dom.NodeList; import cr0s.warpdrive.WarpDrive; import cr0s.warpdrive.conf.InvalidXmlException; -import cr0s.warpdrive.conf.OreManager.OreSpawnCategory; +import cr0s.warpdrive.conf.FillerManager.FillerCategory; import cr0s.warpdrive.conf.XmlRepresentable; import cr0s.warpdrive.world.EntitySphereGen; @@ -195,7 +195,7 @@ public abstract class Orb extends DeployableStructure implements XmlRepresentabl int weight = Integer.parseInt(cat.getAttribute("weight")); String oreSpawnCategory = cat.getAttribute("category"); - if (OreSpawnCategory.getOreSpawnValue(oreSpawnCategory) == null) + if (FillerCategory.getOreSpawnValue(oreSpawnCategory) == null) throw new InvalidXmlException("Shell has an invalid ores category!"); if (weight < 1) @@ -217,15 +217,14 @@ public abstract class Orb extends DeployableStructure implements XmlRepresentabl Block bl = Block.getBlockFromName(compBlock.getKey()); - for (int i = 0; i < compBlock.getValue(); i++) { + for (int i = 0; i < compBlock.getValue(); i++) blockWeights[index++] = bl; - } } for(Entry category : categoryTemp.entrySet()) { - OreSpawnCategory value = OreSpawnCategory.getOreSpawnValue(category.getKey()); - for (Entry compBlock : value.getOres()) { + FillerCategory value = FillerCategory.getOreSpawnValue(category.getKey()); + for (Entry compBlock : value.getBlocks()) { int reduced = category.getValue() * compBlock.getValue() / value.getTotalWeight(); for (int i = 0; i < reduced; i++) {