Added new package and finished Orb implementation

Will still eventually need generate implementation, but not soon
This commit is contained in:
Francesco Macagno 2015-08-09 17:00:56 -07:00
parent 372a98d620
commit 65aebd3b58
2 changed files with 92 additions and 47 deletions

View file

@ -1,7 +1,7 @@
/**
*
*/
package cr0s.warpdrive.conf;
package cr0s.warpdrive.conf.structures;
import net.minecraft.world.gen.feature.WorldGenerator;

View file

@ -1,5 +1,6 @@
package cr0s.warpdrive.conf;
package cr0s.warpdrive.conf.structures;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Random;
@ -12,146 +13,190 @@ import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import cr0s.warpdrive.WarpDrive;
import cr0s.warpdrive.conf.InvalidXmlException;
import cr0s.warpdrive.conf.XmlRepresentable;
public abstract class Orb extends DeployableStructure implements XmlRepresentable {
private OrbShell[] shellRelative;
private ArrayList<OrbShell> shells;
public Orb(int diameter) {
super(diameter, diameter, diameter);
}
}
@Override
public void loadFromXmlElement(Element e) {
// TODO Auto-generated method stub
public void loadFromXmlElement(Element e) throws InvalidXmlException {
NodeList shells = e.getElementsByTagName("shell");
for (int i = 0; i < shells.getLength(); i++) {
Element tmp = (Element) shells.item(i);
OrbShell shell = new OrbShell();
shell.loadFromXmlElement(tmp);
setShell(shell.getRadiusInner(), shell.getRadiusOuter(), shell);
}
}
@Override
public void saveToXmlElement(Element e, Document d) {
// TODO Auto-generated method stub
for (OrbShell shell : shells) {
Element tmp = d.createElement("shell");
shell.saveToXmlElement(tmp, d);
e.appendChild(tmp);
}
}
@Override
public boolean generate(World p_76484_1_, Random p_76484_2_, int p_76484_3_, int p_76484_4_, int p_76484_5_) {
// TODO Auto-generated method stub
return false;
}
public void setShell(int inner, int outer, OrbShell shell) {
for (; inner <= outer; inner++)
shellRelative[inner] = shell;
}
public OrbShell getShellForRadius(int r) {
return shellRelative[r];
}
public Block getBlockForRadius(Random rand, int r) {
return shellRelative[r].getNextBlock(rand);
}
public class OrbShell implements XmlRepresentable {
private int radiusInner, radiusOuter;
private HashMap<String, Integer> compositionBlocks;
private Block[] blockWeights;
private String name;
private int totalWeight;
public OrbShell() {
compositionBlocks = new HashMap<String, Integer>();
totalWeight = 1;
blockWeights = new Block[0];
}
/**
* Gets a block randomly chosen according to the setup of this OrbShell
*
* @param r
* The Random to use
* @return A Non-null block instance
*/
public Block getNextBlock(Random r) {
return blockWeights[r.nextInt(totalWeight)];
}
public int getRadiusInner() {
return radiusInner;
}
public void setRadiusInner(int radiusInner) {
this.radiusInner = radiusInner;
}
public int getRadiusOuter() {
return radiusOuter;
}
public void setRadiusOuter(int radiusOuter) {
this.radiusOuter = radiusOuter;
}
@Override
public void loadFromXmlElement(Element e) throws InvalidXmlException {
WarpDrive.logger.info("Loading shell " + e.getAttribute("name"));
name = e.getAttribute("name");
totalWeight = 0;
try {
radiusInner = Integer.parseInt(e.getAttribute("radiusInner"));
radiusOuter = Integer.parseInt(e.getAttribute("radiusOuter"));
if (radiusInner < 1 || radiusInner > radiusOuter)
throw new InvalidXmlException("Orb creation arguments are incorrect!");
NodeList compBlocks = e.getElementsByTagName("CompositionBlock");
for (int i = 0; i < compBlocks.getLength(); i++) {
Element tmp = (Element) compBlocks.item(i);
if (!tmp.hasAttribute("weight"))
throw new InvalidXmlException("Shell is missing weight at place " + i + "!");
if (!tmp.hasAttribute("block"))
throw new InvalidXmlException("Shell is missing block at place " + i + "!");
int tmpWeight = Integer.parseInt(tmp.getAttribute("weight"));
if (tmpWeight < 1)
throw new InvalidXmlException("Weight is less than 1 at place " + i + "!");
String tmpBlock = tmp.getAttribute("block");
if (Block.getBlockFromName(tmpBlock) == null)
throw new InvalidXmlException("Shell has unknown block at place " + i + "!");
totalWeight += tmpWeight;
compositionBlocks.put(tmpBlock, tmpWeight);
}
int index = 0;
blockWeights = new Block[totalWeight];
for (Entry<String, Integer> compBlock : compositionBlocks.entrySet()) {
Block bl = Block.getBlockFromName(compBlock.getKey());
for (int i = 0; i < compBlock.getValue(); i++) {
blockWeights[index++] = bl;
}
}
} catch (NumberFormatException ex) {
throw new InvalidXmlException("Invalid integer in shell " + name + "!");
}
if (totalWeight < 1)
throw new InvalidXmlException("At least one kind of block must be defined!");
}
@Override
public void saveToXmlElement(Element e, Document d) {
WarpDrive.logger.info("Saving shell " + e.getAttribute("name"));
e.setAttribute("name", name);
e.setAttribute("radiusInner", "" + radiusInner);
e.setAttribute("radiusOuter", "" + radiusOuter);
for (Entry<String, Integer> compBlock : compositionBlocks.entrySet()) {
Element child = d.createElement("CompositionBlock");
child.setAttribute("weight", compBlock.getValue() + "");
child.setAttribute("block", compBlock.getKey());
e.appendChild(child);
}
}
}
}