From b4e9811235ed29c0e8a3ac6dc776da15e87abdcb Mon Sep 17 00:00:00 2001 From: Jofkos Date: Fri, 26 Dec 2014 22:43:59 +0100 Subject: Make Location ConfigurationSerializable. --- src/main/java/org/bukkit/Location.java | 38 +++++++++++++++++++++- .../serialization/ConfigurationSerialization.java | 2 ++ 2 files changed, 39 insertions(+), 1 deletion(-) 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 serialize() { + Map data = new HashMap(); + 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 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 clazz) { -- cgit v1.2.3