Added basic loading

Need to create test cases
This commit is contained in:
Francesco Macagno 2015-08-15 21:44:34 -07:00
parent 700014dad4
commit 708fde795f
2 changed files with 107 additions and 76 deletions

View file

@ -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() {

View file

@ -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<Star> stars = new ArrayList<Star>();
private static ArrayList<Planetoid> moons = new ArrayList<Planetoid>();
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);
}
}
}
}