diff options
author | Jofkos <JofkosDE@gmail.com> | 2014-12-26 22:43:59 +0100 |
---|---|---|
committer | md_5 <git@md-5.net> | 2015-01-07 09:49:16 +1100 |
commit | b4e9811235ed29c0e8a3ac6dc776da15e87abdcb (patch) | |
tree | 7de7f3be5725664a5116148edd7c0a14ea92ba00 | |
parent | dd0fec4187a081e6ed362c51d786b33fa9ffde55 (diff) | |
download | bukkit-b4e9811235ed29c0e8a3ac6dc776da15e87abdcb.tar bukkit-b4e9811235ed29c0e8a3ac6dc776da15e87abdcb.tar.gz bukkit-b4e9811235ed29c0e8a3ac6dc776da15e87abdcb.tar.lz bukkit-b4e9811235ed29c0e8a3ac6dc776da15e87abdcb.tar.xz bukkit-b4e9811235ed29c0e8a3ac6dc776da15e87abdcb.zip |
Make Location ConfigurationSerializable.
-rw-r--r-- | src/main/java/org/bukkit/Location.java | 38 | ||||
-rw-r--r-- | src/main/java/org/bukkit/configuration/serialization/ConfigurationSerialization.java | 2 |
2 files changed, 39 insertions, 1 deletions
diff --git a/src/main/java/org/bukkit/Location.java b/src/main/java/org/bukkit/Location.java index 37fecc10..2a6b3942 100644 --- a/src/main/java/org/bukkit/Location.java +++ b/src/main/java/org/bukkit/Location.java @@ -1,13 +1,17 @@ package org.bukkit; +import java.util.HashMap; +import java.util.Map; + import org.bukkit.block.Block; +import org.bukkit.configuration.serialization.ConfigurationSerializable; import org.bukkit.util.NumberConversions; import org.bukkit.util.Vector; /** * Represents a 3-dimensional position in a world */ -public class Location implements Cloneable { +public class Location implements Cloneable, ConfigurationSerializable { private World world; private double x; private double y; @@ -560,4 +564,36 @@ public class Location implements Cloneable { public static int locToBlock(double loc) { return NumberConversions.floor(loc); } + + @Utility + public Map<String, Object> serialize() { + Map<String, Object> data = new HashMap<String, Object>(); + data.put("world", this.world.getName()); + + data.put("x", this.x); + data.put("y", this.y); + data.put("z", this.z); + + data.put("yaw", this.yaw); + data.put("pitch", this.pitch); + + return data; + } + + /** + * Required method for deserialization + * + * @param args map to deserialize + * @return deserialized location + * @throws IllegalArgumentException if the world don't exists + * @see ConfigurationSerializable + */ + public static Location deserialize(Map<String, Object> args) { + World world = Bukkit.getWorld((String) args.get("world")); + if (world == null) { + throw new IllegalArgumentException("unknown world"); + } + + return new Location(world, NumberConversions.toDouble(args.get("x")), NumberConversions.toDouble(args.get("y")), NumberConversions.toDouble(args.get("z")), NumberConversions.toFloat(args.get("yaw")), NumberConversions.toFloat(args.get("pitch"))); + } } diff --git a/src/main/java/org/bukkit/configuration/serialization/ConfigurationSerialization.java b/src/main/java/org/bukkit/configuration/serialization/ConfigurationSerialization.java index 375297c6..e5ddef25 100644 --- a/src/main/java/org/bukkit/configuration/serialization/ConfigurationSerialization.java +++ b/src/main/java/org/bukkit/configuration/serialization/ConfigurationSerialization.java @@ -12,6 +12,7 @@ import java.util.logging.Logger; import org.apache.commons.lang.Validate; import org.bukkit.Color; import org.bukkit.FireworkEffect; +import org.bukkit.Location; import org.bukkit.block.banner.Pattern; import org.bukkit.configuration.Configuration; import org.bukkit.inventory.ItemStack; @@ -35,6 +36,7 @@ public class ConfigurationSerialization { registerClass(PotionEffect.class); registerClass(FireworkEffect.class); registerClass(Pattern.class); + registerClass(Location.class); } protected ConfigurationSerialization(Class<? extends ConfigurationSerializable> clazz) { |