From 708fde795f5a95fabc60f36e09a1439c1380500a Mon Sep 17 00:00:00 2001 From: Francesco Macagno Date: Sat, 15 Aug 2015 21:44:34 -0700 Subject: [PATCH] Added basic loading Need to create test cases --- .../cr0s/warpdrive/conf/WarpDriveConfig.java | 74 +----------- .../conf/structures/StructureManager.java | 109 +++++++++++++++++- 2 files changed, 107 insertions(+), 76 deletions(-) diff --git a/src/main/java/cr0s/warpdrive/conf/WarpDriveConfig.java b/src/main/java/cr0s/warpdrive/conf/WarpDriveConfig.java index 16bb2f17..3a87e664 100644 --- a/src/main/java/cr0s/warpdrive/conf/WarpDriveConfig.java +++ b/src/main/java/cr0s/warpdrive/conf/WarpDriveConfig.java @@ -2,7 +2,6 @@ package cr0s.warpdrive.conf; import java.lang.reflect.Method; import java.util.ArrayList; -import java.util.Random; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; @@ -20,6 +19,7 @@ import net.minecraftforge.oredict.OreDictionary; import cpw.mods.fml.common.Loader; import cpw.mods.fml.common.registry.GameRegistry; import cr0s.warpdrive.WarpDrive; +import cr0s.warpdrive.conf.structures.StructureManager; import cr0s.warpdrive.data.TransitionPlane; // FIXME import dan200.computercraft.ComputerCraft; @@ -800,77 +800,9 @@ public class WarpDriveConfig { } } - public static Block getDefaultSurfaceBlock(Random random, boolean corrupted, boolean isMoon) { - if (isMoon) { - if (isIndustrialCraft2loaded && random.nextInt(10) == 1) - return getModBlock("IC2", "blockBasalt"); - else if (isAppliedEnergistics2Loaded && random.nextInt(10) == 1) - return getModBlock("appliedenergistics2", "tile.BlockSkyStone"); - else if (random.nextInt(5) == 1) { - return Blocks.netherrack; - } else if (random.nextInt(15) == 1) { - return Blocks.end_stone; - } - } else { - if (isIndustrialCraft2loaded && random.nextInt(10) == 1) - return getModBlock("IC2", "blockBasalt"); - else if (isAppliedEnergistics2Loaded && random.nextInt(10) == 1) - return getModBlock("appliedenergistics2", "tile.BlockSkyStone"); - else if (random.nextInt(6) == 1) { - return Blocks.netherrack; - } else if (random.nextInt(50) == 1) { - return Blocks.end_stone; - } - } - if (corrupted && random.nextBoolean()) { - return Blocks.cobblestone; - } - return Blocks.stone; - } - - public static Block getRandomSurfaceBlock(Random random, Block def, boolean bedrock) { - if (bedrock && (random.nextInt(1000) == 1)) { - return Blocks.bedrock; - } else if (def.isAssociatedBlock(Blocks.end_stone)) { - return getRandomEndBlock(random, def); - } else if (def.isAssociatedBlock(Blocks.netherrack)) { - return getRandomNetherBlock(random, def); - } - return getRandomOverworldBlock(random, def); - } - - public static Block getRandomOverworldBlock(Random random, Block def) { - if (random.nextInt(25) == 5) { - return commonWorldGenOres.get(random.nextInt(commonWorldGenOres.size())); - } else if (random.nextInt(250) == 1) { - return Blocks.diamond_ore; - } else if (isIndustrialCraft2loaded && (random.nextInt(10000) == 42)) { - return WarpDrive.blockIridium; - } - return def; - } - - public static Block getRandomNetherBlock(Random random, Block def) { - if (isIndustrialCraft2loaded && (random.nextInt(10000) == 42)) { - return WarpDrive.blockIridium; - } else if (random.nextInt(25) == 1) { - return Blocks.quartz_ore; - } else if ((!isNetherOresLoaded) && (random.nextInt(100) == 13)) - return commonWorldGenOres.get(random.nextInt(commonWorldGenOres.size())); - return def; - } - - public static Block getRandomEndBlock(Random random, Block def) { - if (isIndustrialCraft2loaded && random.nextInt(10000) == 42) { - return WarpDrive.blockIridium; - } else if (random.nextInt(200) == 13) { - return commonWorldGenOres.get(random.nextInt(commonWorldGenOres.size())); - } - return def; - } - public static void loadWorldGen() { - + OreManager.loadOres("config/warpdrive/"); + StructureManager.loadStructures("config/warpdrive/"); } public static DocumentBuilder getXmlDocumentBuilder() { diff --git a/src/main/java/cr0s/warpdrive/conf/structures/StructureManager.java b/src/main/java/cr0s/warpdrive/conf/structures/StructureManager.java index ce9035d8..c3af0b43 100644 --- a/src/main/java/cr0s/warpdrive/conf/structures/StructureManager.java +++ b/src/main/java/cr0s/warpdrive/conf/structures/StructureManager.java @@ -1,15 +1,114 @@ package cr0s.warpdrive.conf.structures; import java.io.File; +import java.io.FilenameFilter; +import java.io.IOException; +import java.util.ArrayList; + +import org.w3c.dom.Document; +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; +import cr0s.warpdrive.conf.InvalidXmlException; +import cr0s.warpdrive.conf.WarpDriveConfig; + public class StructureManager { - - public StructureManager() { + + private static ArrayList stars = new ArrayList(); + private static ArrayList moons = new ArrayList(); + + public static void loadStructures(String structureConfDir) { + loadStructures(new File(structureConfDir)); + + } + + public static void loadStructures(File dir) { + dir.mkdir(); + + if (!dir.isDirectory()) + throw new IllegalArgumentException("File path " + dir.getPath() + " must be a drectory!"); + + File[] files = dir.listFiles(new FilenameFilter() { + + @Override + public boolean accept(File dir, String name) { + return name.startsWith("structure") && name.endsWith(".xml"); + } + + }); + + for (File f : files) { + + try { + + WarpDrive.logger.info("Loading structure data file " + f.getPath()); + + loadXmlStructureFile(f); + + WarpDrive.logger.info("Finished loading structure data file " + f.getPath()); + + } catch (Exception e) { + WarpDrive.logger.error("Error loading file " + f.getPath() + ": " + e.getMessage()); + e.printStackTrace(); + } + + } } - public void loadStructures(File f) { - //TODO: + private static void loadXmlStructureFile(File f) throws SAXException, IOException, InvalidXmlException { + 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); + if (!mod.hasAttribute("name")) + throw new InvalidXmlException("A mod requirement at " + i + ":" + j + " is missing the name attribute!"); + + String name = mod.getAttribute("name"); + if (!Loader.isModLoaded(name)) { + WarpDrive.logger.info("Skippping structure data file " + f.getPath() + " because the mod " + name + " is not loaded"); + } + + } + } + + NodeList structures = base.getElementsByTagName("structure"); + for (int i = 0; i < structures.getLength(); i++) { + + Element struct = (Element) structures.item(i); + + String type = struct.getAttribute("type"); + String name = struct.getAttribute("name"); + + if(type.isEmpty()) + throw new InvalidXmlException("Structure must have a type!"); + + int radius; + try { + radius = Integer.parseInt(struct.getAttribute("radius")); + } catch(NumberFormatException e) { + throw new InvalidXmlException("Structure radius is invalid!"); + } + + if(type.equalsIgnoreCase("star")) { + Star s = new Star(radius); + s.loadFromXmlElement(struct); + stars.add(s); + } else if(type.equalsIgnoreCase("moon")) { + Planetoid pl = new Planetoid(radius); + pl.loadFromXmlElement(struct); + + } + + } + } - + }