@@ -21,7 +21,7 @@ public class BlockIterator implements Iterator<Block> {
|
||||
private final World world;
|
||||
private final int maxDistance;
|
||||
|
||||
private static final int gridSize = 1<<24;
|
||||
private static final int gridSize = 1 << 24;
|
||||
|
||||
private boolean end = false;
|
||||
|
||||
@@ -56,74 +56,74 @@ public class BlockIterator implements Iterator<Block> {
|
||||
this.maxDistance = maxDistance;
|
||||
|
||||
Vector startClone = start.clone();
|
||||
startClone.setY(startClone.getY( )+ yOffset);
|
||||
|
||||
startClone.setY(startClone.getY() + yOffset);
|
||||
|
||||
currentDistance = 0;
|
||||
|
||||
double mainDirection = 0;
|
||||
double mainDirection = 0;
|
||||
double secondDirection = 0;
|
||||
double thirdDirection = 0;
|
||||
double thirdDirection = 0;
|
||||
|
||||
double mainPosition = 0;
|
||||
double secondPosition = 0;
|
||||
double thirdPosition = 0;
|
||||
double mainPosition = 0;
|
||||
double secondPosition = 0;
|
||||
double thirdPosition = 0;
|
||||
|
||||
Block startBlock = world.getBlockAt((int)Math.floor(startClone.getX()), (int)Math.floor(startClone.getY()), (int)Math.floor(startClone.getZ()));
|
||||
Block startBlock = world.getBlockAt((int) Math.floor(startClone.getX()), (int) Math.floor(startClone.getY()), (int) Math.floor(startClone.getZ()));
|
||||
|
||||
if (getXLength(direction) > mainDirection) {
|
||||
mainFace = getXFace(direction);
|
||||
mainDirection = getXLength(direction);
|
||||
mainPosition = getXPosition(direction, startClone, startBlock);
|
||||
mainFace = getXFace(direction);
|
||||
mainDirection = getXLength(direction);
|
||||
mainPosition = getXPosition(direction, startClone, startBlock);
|
||||
|
||||
secondFace = getYFace(direction);
|
||||
secondFace = getYFace(direction);
|
||||
secondDirection = getYLength(direction);
|
||||
secondPosition = getYPosition(direction, startClone, startBlock);
|
||||
secondPosition = getYPosition(direction, startClone, startBlock);
|
||||
|
||||
thirdFace = getZFace(direction);
|
||||
thirdDirection = getZLength(direction);
|
||||
thirdPosition = getZPosition(direction, startClone, startBlock);
|
||||
thirdFace = getZFace(direction);
|
||||
thirdDirection = getZLength(direction);
|
||||
thirdPosition = getZPosition(direction, startClone, startBlock);
|
||||
}
|
||||
if (getYLength(direction) > mainDirection) {
|
||||
mainFace = getYFace(direction);
|
||||
mainDirection = getYLength(direction);
|
||||
mainPosition = getYPosition(direction, startClone, startBlock);
|
||||
mainFace = getYFace(direction);
|
||||
mainDirection = getYLength(direction);
|
||||
mainPosition = getYPosition(direction, startClone, startBlock);
|
||||
|
||||
secondFace = getZFace(direction);
|
||||
secondFace = getZFace(direction);
|
||||
secondDirection = getZLength(direction);
|
||||
secondPosition = getZPosition(direction, startClone, startBlock);
|
||||
secondPosition = getZPosition(direction, startClone, startBlock);
|
||||
|
||||
thirdFace = getXFace(direction);
|
||||
thirdDirection = getXLength(direction);
|
||||
thirdPosition = getXPosition(direction, startClone, startBlock);
|
||||
thirdFace = getXFace(direction);
|
||||
thirdDirection = getXLength(direction);
|
||||
thirdPosition = getXPosition(direction, startClone, startBlock);
|
||||
}
|
||||
if (getZLength(direction) > mainDirection) {
|
||||
mainFace = getZFace(direction);
|
||||
mainDirection = getZLength(direction);
|
||||
mainPosition = getZPosition(direction, startClone, startBlock);
|
||||
mainFace = getZFace(direction);
|
||||
mainDirection = getZLength(direction);
|
||||
mainPosition = getZPosition(direction, startClone, startBlock);
|
||||
|
||||
secondFace = getXFace(direction);
|
||||
secondFace = getXFace(direction);
|
||||
secondDirection = getXLength(direction);
|
||||
secondPosition = getXPosition(direction, startClone, startBlock);
|
||||
secondPosition = getXPosition(direction, startClone, startBlock);
|
||||
|
||||
thirdFace = getYFace(direction);
|
||||
thirdDirection = getYLength(direction);
|
||||
thirdPosition = getYPosition(direction, startClone, startBlock);
|
||||
thirdFace = getYFace(direction);
|
||||
thirdDirection = getYLength(direction);
|
||||
thirdPosition = getYPosition(direction, startClone, startBlock);
|
||||
}
|
||||
|
||||
// trace line backwards to find intercept with plane perpendicular to the main axis
|
||||
|
||||
double d = mainPosition/mainDirection; // how far to hit face behind
|
||||
double secondd = secondPosition - secondDirection*d;
|
||||
double thirdd = thirdPosition - thirdDirection*d;
|
||||
|
||||
secondError = (int)(Math.floor(secondd*gridSize));
|
||||
secondStep = (int)(Math.round(secondDirection/mainDirection*gridSize));
|
||||
thirdError = (int)(Math.floor(thirdd*gridSize));
|
||||
thirdStep = (int)(Math.round(thirdDirection/mainDirection*gridSize));
|
||||
double d = mainPosition / mainDirection; // how far to hit face behind
|
||||
double secondd = secondPosition - secondDirection * d;
|
||||
double thirdd = thirdPosition - thirdDirection * d;
|
||||
|
||||
// Guarantee that the ray will pass though the start block.
|
||||
// It is possible that it would miss due to rounding
|
||||
// This should only move the ray by 1 grid position
|
||||
secondError = (int) (Math.floor(secondd * gridSize));
|
||||
secondStep = (int) (Math.round(secondDirection / mainDirection * gridSize));
|
||||
thirdError = (int) (Math.floor(thirdd * gridSize));
|
||||
thirdStep = (int) (Math.round(thirdDirection / mainDirection * gridSize));
|
||||
|
||||
if (secondError + secondStep <= 0) {
|
||||
secondError = -secondStep + 1;
|
||||
@@ -134,6 +134,7 @@ public class BlockIterator implements Iterator<Block> {
|
||||
}
|
||||
|
||||
Block lastBlock;
|
||||
|
||||
lastBlock = startBlock.getFace(reverseFace(mainFace));
|
||||
|
||||
if (secondError < 0) {
|
||||
@@ -157,7 +158,7 @@ public class BlockIterator implements Iterator<Block> {
|
||||
|
||||
boolean startBlockFound = false;
|
||||
|
||||
for (int cnt=currentBlock; cnt>=0; cnt--) {
|
||||
for (int cnt = currentBlock; cnt >= 0; cnt--) {
|
||||
if (blockEquals(blockQueue[cnt], startBlock)) {
|
||||
currentBlock = cnt;
|
||||
startBlockFound = true;
|
||||
@@ -170,7 +171,7 @@ public class BlockIterator implements Iterator<Block> {
|
||||
}
|
||||
|
||||
// Calculate the number of planes passed to give max distance
|
||||
maxDistanceInt = (int)Math.round(maxDistance/(Math.sqrt(mainDirection*mainDirection + secondDirection*secondDirection + thirdDirection*thirdDirection)/mainDirection));
|
||||
maxDistanceInt = (int) Math.round(maxDistance / (Math.sqrt(mainDirection * mainDirection + secondDirection * secondDirection + thirdDirection * thirdDirection) / mainDirection));
|
||||
|
||||
}
|
||||
|
||||
@@ -179,14 +180,27 @@ public class BlockIterator implements Iterator<Block> {
|
||||
}
|
||||
|
||||
private BlockFace reverseFace(BlockFace face) {
|
||||
switch(face) {
|
||||
case UP: return BlockFace.DOWN;
|
||||
case DOWN: return BlockFace.UP;
|
||||
case NORTH: return BlockFace.SOUTH;
|
||||
case SOUTH: return BlockFace.NORTH;
|
||||
case EAST: return BlockFace.WEST;
|
||||
case WEST: return BlockFace.EAST;
|
||||
default: return null;
|
||||
switch (face) {
|
||||
case UP:
|
||||
return BlockFace.DOWN;
|
||||
|
||||
case DOWN:
|
||||
return BlockFace.UP;
|
||||
|
||||
case NORTH:
|
||||
return BlockFace.SOUTH;
|
||||
|
||||
case SOUTH:
|
||||
return BlockFace.NORTH;
|
||||
|
||||
case EAST:
|
||||
return BlockFace.WEST;
|
||||
|
||||
case WEST:
|
||||
return BlockFace.EAST;
|
||||
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -215,7 +229,7 @@ public class BlockIterator implements Iterator<Block> {
|
||||
}
|
||||
|
||||
private double getPosition(double direction, double position, int blockPosition) {
|
||||
return direction > 0 ? (position-blockPosition) : (blockPosition + 1 - position);
|
||||
return direction > 0 ? (position - blockPosition) : (blockPosition + 1 - position);
|
||||
}
|
||||
|
||||
private double getXPosition(Vector direction, Vector position, Block block) {
|
||||
@@ -278,7 +292,6 @@ public class BlockIterator implements Iterator<Block> {
|
||||
this(entity.getLocation(), entity.getEyeHeight(), maxDistance);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Constructs the BlockIterator.
|
||||
*
|
||||
@@ -300,7 +313,7 @@ public class BlockIterator implements Iterator<Block> {
|
||||
return currentBlock != -1;
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* Returns the next Block in the trace
|
||||
*
|
||||
* @return the next Block in the trace
|
||||
@@ -311,7 +324,7 @@ public class BlockIterator implements Iterator<Block> {
|
||||
if (currentBlock <= -1) {
|
||||
throw new NoSuchElementException();
|
||||
} else {
|
||||
return blockQueue[currentBlock--];
|
||||
return blockQueue[currentBlock--];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -338,7 +351,7 @@ public class BlockIterator implements Iterator<Block> {
|
||||
|
||||
if (secondError > 0 && thirdError > 0) {
|
||||
blockQueue[2] = blockQueue[0].getFace(mainFace);
|
||||
if (((long)secondStep) * ((long)thirdError) < ((long)thirdStep) * ((long)secondError)) {
|
||||
if (((long) secondStep) * ((long) thirdError) < ((long) thirdStep) * ((long) secondError)) {
|
||||
blockQueue[1] = blockQueue[2].getFace(secondFace);
|
||||
blockQueue[0] = blockQueue[1].getFace(thirdFace);
|
||||
} else {
|
||||
|
||||
@@ -5,10 +5,11 @@ package org.bukkit.util;
|
||||
* BlockVector in WorldEdit. BlockVectors can be used in hash sets and
|
||||
* hash maps. Be aware that BlockVectors are mutable, but it is important
|
||||
* that BlockVectors are never changed once put into a hash set or hash map.
|
||||
*
|
||||
*
|
||||
* @author sk89q
|
||||
*/
|
||||
public class BlockVector extends Vector {
|
||||
|
||||
/**
|
||||
* Construct the vector with all components as 0.
|
||||
*/
|
||||
@@ -65,7 +66,7 @@ public class BlockVector extends Vector {
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Checks if another object is equivalent.
|
||||
*
|
||||
@@ -77,9 +78,9 @@ public class BlockVector extends Vector {
|
||||
if (!(obj instanceof BlockVector)) {
|
||||
return false;
|
||||
}
|
||||
BlockVector other = (BlockVector)obj;
|
||||
return (int)other.getX() == (int)this.x && (int)other.getY() == (int)this.y
|
||||
&& (int)other.getZ() == (int)this.z;
|
||||
BlockVector other = (BlockVector) obj;
|
||||
|
||||
return (int) other.getX() == (int) this.x && (int) other.getY() == (int) this.y && (int) other.getZ() == (int) this.z;
|
||||
|
||||
}
|
||||
|
||||
@@ -90,9 +91,7 @@ public class BlockVector extends Vector {
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return (Integer.valueOf((int)x).hashCode() >> 13) ^
|
||||
(Integer.valueOf((int)y).hashCode() >> 7) ^
|
||||
Integer.valueOf((int)z).hashCode();
|
||||
return (Integer.valueOf((int) x).hashCode() >> 13) ^ (Integer.valueOf((int) y).hashCode() >> 7) ^ Integer.valueOf((int) z).hashCode();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -102,7 +101,8 @@ public class BlockVector extends Vector {
|
||||
*/
|
||||
@Override
|
||||
public BlockVector clone() {
|
||||
BlockVector v = (BlockVector)super.clone();
|
||||
BlockVector v = (BlockVector) super.clone();
|
||||
|
||||
v.x = x;
|
||||
v.y = y;
|
||||
v.z = z;
|
||||
|
||||
@@ -36,7 +36,7 @@ public class FileUtil {
|
||||
long size = in.size();
|
||||
|
||||
while (pos < size) {
|
||||
pos += in.transferTo(pos, 10*1024*1024, out);
|
||||
pos += in.transferTo(pos, 10 * 1024 * 1024, out);
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
return false;
|
||||
|
||||
@@ -8,8 +8,9 @@ public class Java15Compat {
|
||||
if (original.length >= start && 0 <= start) {
|
||||
if (start <= end) {
|
||||
int length = end - start;
|
||||
int copyLength = Math.min( length, original.length - start);
|
||||
int copyLength = Math.min(length, original.length - start);
|
||||
T[] copy = (T[]) Array.newInstance(original.getClass().getComponentType(), length);
|
||||
|
||||
System.arraycopy(original, start, copy, 0, copyLength);
|
||||
return copy;
|
||||
}
|
||||
|
||||
@@ -171,8 +171,7 @@ public class Vector implements Cloneable {
|
||||
* @return the distance
|
||||
*/
|
||||
public double distance(Vector o) {
|
||||
return Math.sqrt(Math.pow(x - o.x, 2) + Math.pow(y - o.y, 2)
|
||||
+ Math.pow(z - o.z, 2));
|
||||
return Math.sqrt(Math.pow(x - o.x, 2) + Math.pow(y - o.y, 2) + Math.pow(z - o.z, 2));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -181,8 +180,7 @@ public class Vector implements Cloneable {
|
||||
* @return the distance
|
||||
*/
|
||||
public double distanceSquared(Vector o) {
|
||||
return Math.pow(x - o.x, 2) + Math.pow(y - o.y, 2)
|
||||
+ Math.pow(z - o.z, 2);
|
||||
return Math.pow(x - o.x, 2) + Math.pow(y - o.y, 2) + Math.pow(z - o.z, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -193,7 +191,8 @@ public class Vector implements Cloneable {
|
||||
*/
|
||||
public float angle(Vector other) {
|
||||
double dot = dot(other) / (length() * other.length());
|
||||
return (float)Math.acos(dot);
|
||||
|
||||
return (float) Math.acos(dot);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -287,6 +286,7 @@ public class Vector implements Cloneable {
|
||||
double newX = y * o.z - o.y * z;
|
||||
double newY = z * o.x - o.z * x;
|
||||
double newZ = x * o.y - o.x * y;
|
||||
|
||||
x = newX;
|
||||
y = newY;
|
||||
z = newZ;
|
||||
@@ -330,9 +330,7 @@ public class Vector implements Cloneable {
|
||||
* @return whether this vector is in the AABB
|
||||
*/
|
||||
public boolean isInAABB(Vector min, Vector max) {
|
||||
return x >= min.x && x <= max.x
|
||||
&& y >= min.y && y <= max.y
|
||||
&& z >= min.z && z <= max.z;
|
||||
return x >= min.x && x <= max.x && y >= min.y && y <= max.y && z >= min.z && z <= max.z;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -343,10 +341,7 @@ public class Vector implements Cloneable {
|
||||
* @return whether this vector is in the sphere
|
||||
*/
|
||||
public boolean isInSphere(Vector origin, double radius) {
|
||||
return (Math.pow(origin.x - x, 2)
|
||||
+ Math.pow(origin.y - y, 2)
|
||||
+ Math.pow(origin.z - z, 2))
|
||||
<= Math.pow(radius, 2);
|
||||
return (Math.pow(origin.x - x, 2) + Math.pow(origin.y - y, 2) + Math.pow(origin.z - z, 2)) <= Math.pow(radius, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -365,7 +360,7 @@ public class Vector implements Cloneable {
|
||||
* @return block X
|
||||
*/
|
||||
public int getBlockX() {
|
||||
return (int)Math.floor(x);
|
||||
return (int) Math.floor(x);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -384,7 +379,7 @@ public class Vector implements Cloneable {
|
||||
* @return block y
|
||||
*/
|
||||
public int getBlockY() {
|
||||
return (int)Math.floor(y);
|
||||
return (int) Math.floor(y);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -403,7 +398,7 @@ public class Vector implements Cloneable {
|
||||
* @return block z
|
||||
*/
|
||||
public int getBlockZ() {
|
||||
return (int)Math.floor(z);
|
||||
return (int) Math.floor(z);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -518,11 +513,9 @@ public class Vector implements Cloneable {
|
||||
return false;
|
||||
}
|
||||
|
||||
Vector other = (Vector)obj;
|
||||
Vector other = (Vector) obj;
|
||||
|
||||
return Math.abs(x - other.x) < epsilon
|
||||
&& Math.abs(y - other.y) < epsilon
|
||||
&& Math.abs(z - other.z) < epsilon && (this.getClass().equals(obj.getClass()));
|
||||
return Math.abs(x - other.x) < epsilon && Math.abs(y - other.y) < epsilon && Math.abs(z - other.z) < epsilon && (this.getClass().equals(obj.getClass()));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -533,6 +526,7 @@ public class Vector implements Cloneable {
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int hash = 7;
|
||||
|
||||
hash = 79 * hash + (int) (Double.doubleToLongBits(this.x) ^ (Double.doubleToLongBits(this.x) >>> 32));
|
||||
hash = 79 * hash + (int) (Double.doubleToLongBits(this.y) ^ (Double.doubleToLongBits(this.y) >>> 32));
|
||||
hash = 79 * hash + (int) (Double.doubleToLongBits(this.z) ^ (Double.doubleToLongBits(this.z) >>> 32));
|
||||
@@ -547,11 +541,12 @@ public class Vector implements Cloneable {
|
||||
@Override
|
||||
public Vector clone() {
|
||||
try {
|
||||
Vector v = (Vector)super.clone();
|
||||
Vector v = (Vector) super.clone();
|
||||
|
||||
v.x = x;
|
||||
v.y = y;
|
||||
v.z = z;
|
||||
return v;
|
||||
return v;
|
||||
} catch (CloneNotSupportedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@@ -586,10 +581,10 @@ public class Vector implements Cloneable {
|
||||
public Location toLocation(World world, float yaw, float pitch) {
|
||||
return new Location(world, x, y, z, yaw, pitch);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the block vector of this vector.
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public BlockVector toBlockVector() {
|
||||
@@ -613,10 +608,7 @@ public class Vector implements Cloneable {
|
||||
* @return minimum
|
||||
*/
|
||||
public static Vector getMinimum(Vector v1, Vector v2) {
|
||||
return new Vector(
|
||||
Math.min(v1.x, v2.x),
|
||||
Math.min(v1.y, v2.y),
|
||||
Math.min(v1.z, v2.z));
|
||||
return new Vector(Math.min(v1.x, v2.x), Math.min(v1.y, v2.y), Math.min(v1.z, v2.z));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -627,10 +619,7 @@ public class Vector implements Cloneable {
|
||||
* @return maximum
|
||||
*/
|
||||
public static Vector getMaximum(Vector v1, Vector v2) {
|
||||
return new Vector(
|
||||
Math.max(v1.x, v2.x),
|
||||
Math.max(v1.y, v2.y),
|
||||
Math.max(v1.z, v2.z));
|
||||
return new Vector(Math.max(v1.x, v2.x), Math.max(v1.y, v2.y), Math.max(v1.z, v2.z));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -640,9 +629,6 @@ public class Vector implements Cloneable {
|
||||
* @return
|
||||
*/
|
||||
public static Vector getRandom() {
|
||||
return new Vector(
|
||||
random.nextDouble(),
|
||||
random.nextDouble(),
|
||||
random.nextDouble());
|
||||
return new Vector(random.nextDouble(), random.nextDouble(), random.nextDouble());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,10 +18,10 @@ import org.yaml.snakeyaml.representer.Representer;
|
||||
* a file and call its load() method. For specifying node paths in the
|
||||
* various get*() methods, they support SK's path notation, allowing you to
|
||||
* select child nodes by delimiting node names with periods.
|
||||
*
|
||||
*
|
||||
* <p>
|
||||
* For example, given the following configuration file:</p>
|
||||
*
|
||||
*
|
||||
* <pre>members:
|
||||
* - Hollie
|
||||
* - Jason
|
||||
@@ -36,38 +36,39 @@ import org.yaml.snakeyaml.representer.Representer;
|
||||
* cool: false
|
||||
* eats:
|
||||
* babies: true</pre>
|
||||
*
|
||||
*
|
||||
* <p>Calling code could access sturmeh's baby eating state by using
|
||||
* <code>getBoolean("sturmeh.eats.babies", false)</code>. For lists, there are
|
||||
* methods such as <code>getStringList</code> that will return a type safe list.
|
||||
*
|
||||
*
|
||||
* <p>This class is currently incomplete. It is not yet possible to get a node.
|
||||
* </p>
|
||||
*
|
||||
*
|
||||
* @author sk89q
|
||||
*/
|
||||
public class Configuration extends ConfigurationNode {
|
||||
private Yaml yaml;
|
||||
private File file;
|
||||
|
||||
|
||||
public Configuration(File file) {
|
||||
super(new HashMap<String, Object>());
|
||||
|
||||
|
||||
DumperOptions options = new DumperOptions();
|
||||
|
||||
options.setIndent(4);
|
||||
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
|
||||
|
||||
yaml = new Yaml(new SafeConstructor(), new Representer(), options);
|
||||
|
||||
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Loads the configuration file. All errors are thrown away.
|
||||
*/
|
||||
public void load() {
|
||||
public void load() {
|
||||
FileInputStream stream = null;
|
||||
|
||||
|
||||
try {
|
||||
stream = new FileInputStream(file);
|
||||
read(yaml.load(new UnicodeReader(stream)));
|
||||
@@ -80,56 +81,54 @@ public class Configuration extends ConfigurationNode {
|
||||
if (stream != null) {
|
||||
stream.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
}
|
||||
} catch (IOException e) {}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Saves the configuration to disk. All errors are clobbered.
|
||||
*
|
||||
* @return true if it was successful
|
||||
*
|
||||
* @return true if it was successful
|
||||
*/
|
||||
public boolean save() {
|
||||
FileOutputStream stream = null;
|
||||
|
||||
|
||||
File parent = file.getParentFile();
|
||||
|
||||
if (parent != null) {
|
||||
parent.mkdirs();
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
stream = new FileOutputStream(file);
|
||||
yaml.dump(root, new OutputStreamWriter(stream, "UTF-8"));
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
} finally {
|
||||
} catch (IOException e) {} finally {
|
||||
try {
|
||||
if (stream != null) {
|
||||
stream.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
}
|
||||
} catch (IOException e) {}
|
||||
}
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void read(Object input) throws ConfigurationException {
|
||||
try {
|
||||
if ( null == input ) {
|
||||
if (null == input) {
|
||||
root = new HashMap<String, Object>();
|
||||
} else {
|
||||
root = (Map<String, Object>)input;
|
||||
root = (Map<String, Object>) input;
|
||||
}
|
||||
} catch (ClassCastException e) {
|
||||
throw new ConfigurationException("Root document must be an key-value structure");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This method returns an empty ConfigurationNode for using as a
|
||||
* This method returns an empty ConfigurationNode for using as a
|
||||
* default in methods that select a node from a node list.
|
||||
* @return
|
||||
*/
|
||||
|
||||
@@ -2,16 +2,16 @@ package org.bukkit.util.config;
|
||||
|
||||
/**
|
||||
* Configuration exception.
|
||||
*
|
||||
*
|
||||
* @author sk89q
|
||||
*/
|
||||
public class ConfigurationException extends Exception {
|
||||
private static final long serialVersionUID = -2442886939908724203L;
|
||||
|
||||
|
||||
public ConfigurationException() {
|
||||
super();
|
||||
}
|
||||
|
||||
|
||||
public ConfigurationException(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
|
||||
@@ -7,23 +7,23 @@ import java.util.Map;
|
||||
|
||||
/**
|
||||
* Represents a configuration node.
|
||||
*
|
||||
*
|
||||
* @author sk89q
|
||||
*/
|
||||
public class ConfigurationNode {
|
||||
protected Map<String, Object> root;
|
||||
|
||||
|
||||
protected ConfigurationNode(Map<String, Object> root) {
|
||||
this.root = root;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets a property at a location. This will either return an Object
|
||||
* or null, with null meaning that no configuration value exists at
|
||||
* that location. This could potentially return a default value (not yet
|
||||
* implemented) as defined by a plugin, if this is a plugin-tied
|
||||
* configuration.
|
||||
*
|
||||
*
|
||||
* @param path path to node (dot notation)
|
||||
* @return object or null
|
||||
*/
|
||||
@@ -31,40 +31,41 @@ public class ConfigurationNode {
|
||||
public Object getProperty(String path) {
|
||||
if (!path.contains(".")) {
|
||||
Object val = root.get(path);
|
||||
|
||||
if (val == null) {
|
||||
return null;
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
|
||||
String[] parts = path.split("\\.");
|
||||
Map<String, Object> node = root;
|
||||
|
||||
|
||||
for (int i = 0; i < parts.length; i++) {
|
||||
Object o = node.get(parts[i]);
|
||||
|
||||
|
||||
if (o == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
if (i == parts.length - 1) {
|
||||
return o;
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
node = (Map<String, Object>)o;
|
||||
node = (Map<String, Object>) o;
|
||||
} catch (ClassCastException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set the property at a location. This will override existing
|
||||
* configuration data to have it conform to key/value mappings.
|
||||
*
|
||||
*
|
||||
* @param path
|
||||
* @param value
|
||||
*/
|
||||
@@ -74,26 +75,26 @@ public class ConfigurationNode {
|
||||
root.put(path, value);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
String[] parts = path.split("\\.");
|
||||
Map<String, Object> node = root;
|
||||
|
||||
|
||||
for (int i = 0; i < parts.length; i++) {
|
||||
Object o = node.get(parts[i]);
|
||||
|
||||
|
||||
// Found our target!
|
||||
if (i == parts.length - 1) {
|
||||
node.put(parts[i], value);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (o == null || !(o instanceof Map)) {
|
||||
// This will override existing configuration data!
|
||||
o = new HashMap<String, Object>();
|
||||
node.put(parts[i], o);
|
||||
}
|
||||
|
||||
node = (Map<String, Object>)o;
|
||||
|
||||
node = (Map<String, Object>) o;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,12 +103,13 @@ public class ConfigurationNode {
|
||||
* or null, with null meaning that no configuration value exists at
|
||||
* that location. If the object at the particular location is not actually
|
||||
* a string, it will be converted to its string representation.
|
||||
*
|
||||
*
|
||||
* @param path path to node (dot notation)
|
||||
* @return string or null
|
||||
*/
|
||||
public String getString(String path) {
|
||||
Object o = getProperty(path);
|
||||
|
||||
if (o == null) {
|
||||
return null;
|
||||
}
|
||||
@@ -118,13 +120,14 @@ public class ConfigurationNode {
|
||||
* Gets a string at a location. This will either return an String
|
||||
* or the default value. If the object at the particular location is not
|
||||
* actually a string, it will be converted to its string representation.
|
||||
*
|
||||
*
|
||||
* @param path path to node (dot notation)
|
||||
* @param def default value
|
||||
* @return string or default
|
||||
*/
|
||||
public String getString(String path, String def) {
|
||||
String o = getString(path);
|
||||
|
||||
if (o == null) {
|
||||
setProperty(path, def);
|
||||
return def;
|
||||
@@ -137,13 +140,14 @@ public class ConfigurationNode {
|
||||
* or the default value. If the object at the particular location is not
|
||||
* actually a integer, the default value will be returned. However, other
|
||||
* number types will be casted to an integer.
|
||||
*
|
||||
*
|
||||
* @param path path to node (dot notation)
|
||||
* @param def default value
|
||||
* @return int or default
|
||||
*/
|
||||
public int getInt(String path, int def) {
|
||||
Integer o = castInt(getProperty(path));
|
||||
|
||||
if (o == null) {
|
||||
setProperty(path, def);
|
||||
return def;
|
||||
@@ -157,13 +161,14 @@ public class ConfigurationNode {
|
||||
* or the default value. If the object at the particular location is not
|
||||
* actually a double, the default value will be returned. However, other
|
||||
* number types will be casted to an double.
|
||||
*
|
||||
*
|
||||
* @param path path to node (dot notation)
|
||||
* @param def default value
|
||||
* @return double or default
|
||||
*/
|
||||
public double getDouble(String path, double def) {
|
||||
Double o = castDouble(getProperty(path));
|
||||
|
||||
if (o == null) {
|
||||
setProperty(path, def);
|
||||
return def;
|
||||
@@ -176,13 +181,14 @@ public class ConfigurationNode {
|
||||
* Gets a boolean at a location. This will either return an boolean
|
||||
* or the default value. If the object at the particular location is not
|
||||
* actually a boolean, the default value will be returned.
|
||||
*
|
||||
*
|
||||
* @param path path to node (dot notation)
|
||||
* @param def default value
|
||||
* @return boolean or default
|
||||
*/
|
||||
public boolean getBoolean(String path, boolean def) {
|
||||
Boolean o = castBoolean(getProperty(path));
|
||||
|
||||
if (o == null) {
|
||||
setProperty(path, def);
|
||||
return def;
|
||||
@@ -190,22 +196,25 @@ public class ConfigurationNode {
|
||||
return o;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get a list of keys at a location. If the map at the particular location
|
||||
* does not exist or it is not a map, null will be returned.
|
||||
*
|
||||
*
|
||||
* @param path path to node (dot notation)
|
||||
* @return list of keys
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<String> getKeys(String path) {
|
||||
if (path == null) return new ArrayList<String>(root.keySet());
|
||||
if (path == null) {
|
||||
return new ArrayList<String>(root.keySet());
|
||||
}
|
||||
Object o = getProperty(path);
|
||||
|
||||
if (o == null) {
|
||||
return null;
|
||||
} else if (o instanceof Map) {
|
||||
return new ArrayList<String>(((Map<String,Object>)o).keySet());
|
||||
return new ArrayList<String>(((Map<String, Object>) o).keySet());
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
@@ -214,22 +223,23 @@ public class ConfigurationNode {
|
||||
/**
|
||||
* Gets a list of objects at a location. If the list is not defined,
|
||||
* null will be returned. The node must be an actual list.
|
||||
*
|
||||
*
|
||||
* @param path path to node (dot notation)
|
||||
* @return boolean or default
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<Object> getList(String path) {
|
||||
Object o = getProperty(path);
|
||||
|
||||
if (o == null) {
|
||||
return null;
|
||||
} else if (o instanceof List) {
|
||||
return (List<Object>)o;
|
||||
return (List<Object>) o;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets a list of strings. Non-valid entries will not be in the list.
|
||||
* There will be no null slots. If the list is not defined, the
|
||||
@@ -237,120 +247,131 @@ public class ConfigurationNode {
|
||||
* and an empty list will be returned instead. If an item in the list
|
||||
* is not a string, it will be converted to a string. The node must be
|
||||
* an actual list and not just a string.
|
||||
*
|
||||
*
|
||||
* @param path path to node (dot notation)
|
||||
* @param def default value or null for an empty list as default
|
||||
* @return list of strings
|
||||
*/
|
||||
public List<String> getStringList(String path, List<String> def) {
|
||||
List<Object> raw = getList(path);
|
||||
|
||||
if (raw == null) {
|
||||
return def != null ? def : new ArrayList<String>();
|
||||
}
|
||||
|
||||
List<String> list = new ArrayList<String>();
|
||||
|
||||
for (Object o : raw) {
|
||||
if (o == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
list.add(o.toString());
|
||||
}
|
||||
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets a list of integers. Non-valid entries will not be in the list.
|
||||
* There will be no null slots. If the list is not defined, the
|
||||
* default will be returned. 'null' can be passed for the default
|
||||
* and an empty list will be returned instead. The node must be
|
||||
* an actual list and not just an integer.
|
||||
*
|
||||
*
|
||||
* @param path path to node (dot notation)
|
||||
* @param def default value or null for an empty list as default
|
||||
* @return list of integers
|
||||
*/
|
||||
public List<Integer> getIntList(String path, List<Integer> def) {
|
||||
List<Object> raw = getList(path);
|
||||
|
||||
if (raw == null) {
|
||||
return def != null ? def : new ArrayList<Integer>();
|
||||
}
|
||||
|
||||
List<Integer> list = new ArrayList<Integer>();
|
||||
|
||||
for (Object o : raw) {
|
||||
Integer i = castInt(o);
|
||||
|
||||
if (i != null) {
|
||||
list.add(i);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets a list of doubles. Non-valid entries will not be in the list.
|
||||
* There will be no null slots. If the list is not defined, the
|
||||
* default will be returned. 'null' can be passed for the default
|
||||
* and an empty list will be returned instead. The node must be
|
||||
* an actual list and cannot be just a double.
|
||||
*
|
||||
*
|
||||
* @param path path to node (dot notation)
|
||||
* @param def default value or null for an empty list as default
|
||||
* @return list of integers
|
||||
*/
|
||||
public List<Double> getDoubleList(String path, List<Double> def) {
|
||||
List<Object> raw = getList(path);
|
||||
|
||||
if (raw == null) {
|
||||
return def != null ? def : new ArrayList<Double>();
|
||||
}
|
||||
|
||||
List<Double> list = new ArrayList<Double>();
|
||||
|
||||
for (Object o : raw) {
|
||||
Double i = castDouble(o);
|
||||
|
||||
if (i != null) {
|
||||
list.add(i);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets a list of booleans. Non-valid entries will not be in the list.
|
||||
* There will be no null slots. If the list is not defined, the
|
||||
* default will be returned. 'null' can be passed for the default
|
||||
* and an empty list will be returned instead. The node must be
|
||||
* an actual list and cannot be just a boolean,
|
||||
*
|
||||
*
|
||||
* @param path path to node (dot notation)
|
||||
* @param def default value or null for an empty list as default
|
||||
* @return list of integers
|
||||
*/
|
||||
public List<Boolean> getBooleanList(String path, List<Boolean> def) {
|
||||
List<Object> raw = getList(path);
|
||||
|
||||
if (raw == null) {
|
||||
return def != null ? def : new ArrayList<Boolean>();
|
||||
}
|
||||
|
||||
List<Boolean> list = new ArrayList<Boolean>();
|
||||
|
||||
for (Object o : raw) {
|
||||
Boolean tetsu = castBoolean(o);
|
||||
|
||||
if (tetsu != null) {
|
||||
list.add(tetsu);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets a list of nodes. Non-valid entries will not be in the list.
|
||||
* There will be no null slots. If the list is not defined, the
|
||||
* default will be returned. 'null' can be passed for the default
|
||||
* and an empty list will be returned instead. The node must be
|
||||
* an actual node and cannot be just a boolean,
|
||||
*
|
||||
*
|
||||
* @param path path to node (dot notation)
|
||||
* @param def default value or null for an empty list as default
|
||||
* @return list of integers
|
||||
@@ -358,70 +379,72 @@ public class ConfigurationNode {
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<ConfigurationNode> getNodeList(String path, List<ConfigurationNode> def) {
|
||||
List<Object> raw = getList(path);
|
||||
|
||||
if (raw == null) {
|
||||
return def != null ? def : new ArrayList<ConfigurationNode>();
|
||||
}
|
||||
|
||||
List<ConfigurationNode> list = new ArrayList<ConfigurationNode>();
|
||||
|
||||
for (Object o : raw) {
|
||||
if (o instanceof Map) {
|
||||
list.add(new ConfigurationNode((Map<String, Object>)o));
|
||||
list.add(new ConfigurationNode((Map<String, Object>) o));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get a configuration node at a path. If the node doesn't exist or the
|
||||
* path does not lead to a node, null will be returned. A node has
|
||||
* key/value mappings.
|
||||
*
|
||||
*
|
||||
* @param path
|
||||
* @return node or null
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public ConfigurationNode getNode(String path) {
|
||||
Object raw = getProperty(path);
|
||||
|
||||
if (raw instanceof Map) {
|
||||
return new ConfigurationNode((Map<String, Object>)raw);
|
||||
return new ConfigurationNode((Map<String, Object>) raw);
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get a list of nodes at a location. If the map at the particular location
|
||||
* does not exist or it is not a map, null will be returned.
|
||||
*
|
||||
*
|
||||
* @param path path to node (dot notation)
|
||||
* @return map of nodes
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public Map<String, ConfigurationNode> getNodes(String path) {
|
||||
Object o = getProperty(path);
|
||||
|
||||
if (o == null) {
|
||||
return null;
|
||||
} else if (o instanceof Map) {
|
||||
Map<String, ConfigurationNode> nodes =
|
||||
new HashMap<String, ConfigurationNode>();
|
||||
|
||||
for (Map.Entry<String, Object> entry : ((Map<String, Object>)o).entrySet()) {
|
||||
Map<String, ConfigurationNode> nodes = new HashMap<String, ConfigurationNode>();
|
||||
|
||||
for (Map.Entry<String, Object> entry : ((Map<String, Object>) o).entrySet()) {
|
||||
if (entry.getValue() instanceof Map) {
|
||||
nodes.put(entry.getKey(),
|
||||
new ConfigurationNode((Map<String, Object>) entry.getValue()));
|
||||
nodes.put(entry.getKey(), new ConfigurationNode((Map<String, Object>) entry.getValue()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return nodes;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Casts a value to an integer. May return null.
|
||||
*
|
||||
*
|
||||
* @param o
|
||||
* @return
|
||||
*/
|
||||
@@ -429,23 +452,23 @@ public class ConfigurationNode {
|
||||
if (o == null) {
|
||||
return null;
|
||||
} else if (o instanceof Byte) {
|
||||
return (int)(Byte)o;
|
||||
return (int) (Byte) o;
|
||||
} else if (o instanceof Integer) {
|
||||
return (Integer)o;
|
||||
return (Integer) o;
|
||||
} else if (o instanceof Double) {
|
||||
return (int)(double)(Double)o;
|
||||
return (int) (double) (Double) o;
|
||||
} else if (o instanceof Float) {
|
||||
return (int)(float)(Float)o;
|
||||
return (int) (float) (Float) o;
|
||||
} else if (o instanceof Long) {
|
||||
return (int)(long)(Long)o;
|
||||
return (int) (long) (Long) o;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Casts a value to a double. May return null.
|
||||
*
|
||||
*
|
||||
* @param o
|
||||
* @return
|
||||
*/
|
||||
@@ -453,23 +476,23 @@ public class ConfigurationNode {
|
||||
if (o == null) {
|
||||
return null;
|
||||
} else if (o instanceof Float) {
|
||||
return (double)(Float)o;
|
||||
return (double) (Float) o;
|
||||
} else if (o instanceof Double) {
|
||||
return (Double)o;
|
||||
return (Double) o;
|
||||
} else if (o instanceof Byte) {
|
||||
return (double)(Byte)o;
|
||||
return (double) (Byte) o;
|
||||
} else if (o instanceof Integer) {
|
||||
return (double)(Integer)o;
|
||||
return (double) (Integer) o;
|
||||
} else if (o instanceof Long) {
|
||||
return (double)(Long)o;
|
||||
return (double) (Long) o;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Casts a value to a boolean. May return null.
|
||||
*
|
||||
*
|
||||
* @param o
|
||||
* @return
|
||||
*/
|
||||
@@ -477,16 +500,16 @@ public class ConfigurationNode {
|
||||
if (o == null) {
|
||||
return null;
|
||||
} else if (o instanceof Boolean) {
|
||||
return (Boolean)o;
|
||||
return (Boolean) o;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Remove the property at a location. This will override existing
|
||||
* configuration data to have it conform to key/value mappings.
|
||||
*
|
||||
*
|
||||
* @param path
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
@@ -495,20 +518,20 @@ public class ConfigurationNode {
|
||||
root.remove(path);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
String[] parts = path.split("\\.");
|
||||
Map<String, Object> node = root;
|
||||
|
||||
|
||||
for (int i = 0; i < parts.length; i++) {
|
||||
Object o = node.get(parts[i]);
|
||||
|
||||
|
||||
// Found our target!
|
||||
if (i == parts.length - 1) {
|
||||
node.remove(parts[i]);
|
||||
return;
|
||||
}
|
||||
|
||||
node = (Map<String, Object>)o;
|
||||
|
||||
node = (Map<String, Object>) o;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user