Refactoring of the LocationUtil class.

This commit is contained in:
Tobias Ottenweller 2013-06-03 16:09:26 +02:00
parent 7119c185e7
commit b18df58870

View File

@ -13,14 +13,18 @@ import de.craftinc.gates.Plugin;
*/
public class LocationUtil
{
protected static String worldKey = "world";
protected static String xKey = "x";
protected static String yKey = "y";
protected static String zKey = "z";
protected final static String worldKey = "world";
protected final static String xKey = "x";
protected final static String yKey = "y";
protected final static String zKey = "z";
protected static World getWorld(String name) throws Exception
protected static World getWorld(final String name) throws Exception
{
if (name == null) {
throw new IllegalArgumentException("The name of the world must not be 'null");
}
World world = Plugin.getPlugin().getServer().getWorld(name);
if (world == null) {
@ -31,7 +35,13 @@ public class LocationUtil
}
public static Map<String, Object> serializeLocation(Location l)
/**
* Serializes a location. Helps storing locations inside yaml files.
*
* @param l The location to serialize. Supplying 'null' is ok..
* @return A Map object ready for storing inside a yaml file. Will return 'null' if 'l' is null.
*/
public static Map<String, Object> serializeLocation(final Location l)
{
if (l == null) {
return null;
@ -48,7 +58,14 @@ public class LocationUtil
}
public static Location deserializeLocation(Map<String, Object> map) throws Exception
/**
*
* @param map A map generated with the 'serializeLocation' method. Supplying 'null' is ok.
* @return A deserialized location. This method will return 'null' if 'map' is null!
* @throws Exception This method will throw an exception if the world of the supplied serialized location
* does not exist or if 'map' does not contain all necessary keys!
*/
public static Location deserializeLocation(final Map<String, Object> map) throws Exception
{
if (map == null) {
return null;
@ -56,30 +73,14 @@ public class LocationUtil
World w = getWorld((String)map.get(worldKey));
Number x = (Number)map.get(xKey);
Number y = (Number)map.get(yKey);
Number z = (Number)map.get(zKey);
// verbose loading of coordinates (they might be Double or Integer)
Object objX = map.get(xKey);
Object objY = map.get(yKey);
Object objZ = map.get(zKey);
if (x == null || y == null || z == null) {
throw new IllegalArgumentException("Supplied map is invalid x, y or z coordinate was not supplied");
}
double x,y,z;
if (objX instanceof Integer)
x = (double)(Integer)objX;
else
x = (Double)objX;
if (objY instanceof Integer)
y = (double)(Integer)objY;
else
y = (Double)objY;
if (objZ instanceof Integer)
z = (double)(Integer)objZ;
else
z = (Double)objZ;
return new Location(w, x, y, z);
return new Location(w, x.doubleValue(), y.doubleValue(), z.doubleValue());
}
}