How to Save Locations as Strings

Discussion in 'Plugin Development' started by Hertz, Oct 5, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    Hertz

    how can you save different locations as string kinda like warps.
     
  2. Offline

    Tirelessly

    Make a parser...
     
  3. Offline

    Hertz

    how
     
  4. Offline

    CorrieKay

    does have to be a STRING? or can it be stored as an array list of strings?
    (tip: both can be stored in a single key in a file configuration)

    This turns a location into a list of strings, and this turns that list of strings back into a location.
     
  5. Offline

    Jnorr44

    Code:
    /**
    * The class that allows location to be serialized, and be used
    * in a file. This class should not be changed, otherwise it
    * will not work for older files.
    *
    * @author Jnorr44
    */
    public final class SerializableLocation implements Serializable {
        private static final long serialVersionUID = 7661607750632441897L;
     
        /**
        * Gets the org.bukkit.Location back from any SerializableLocation.
        *
        * @param l The SerializableLocation to be returned as a Location
        * @return An instance of Location
        */
        public static Location returnLocation(SerializableLocation l) {
            float pitch = l.pitch;
            float yaw = l.yaw;
            double x = l.x;
            double y = l.y;
            double z = l.z;
            World world = Bukkit.getWorld(l.world);
            Location location = new Location(world, x, y, z, yaw, pitch);
            return location;
        }
     
        private transient Location loc;
        private final String uuid;
        private final String world;
        private final double x, y, z;
        private final float yaw, pitch;
     
        /**
        * Creates a new SerializableLocation instance of any org.bukkit.Location.
        *
        * @param l The location to be serialized
        */
        public SerializableLocation(Location l) {
            world = l.getWorld().getName();
            uuid = l.getWorld().getUID().toString();
            x = l.getX();
            y = l.getY();
            z = l.getZ();
            yaw = l.getYaw();
            pitch = l.getPitch();
        }
     
        /**
        * Creates a new SerializableLocation instance of any org.bukkit.Location as a map.
        *
        * @param map The map to be made into a SerializableLocation
        */
        public SerializableLocation(Map<String, Object> map) {
            world = (String) map.get("world");
            uuid = (String) map.get("uuid");
            x = (Double) map.get("x");
            y = (Double) map.get("y");
            z = (Double) map.get("z");
            yaw = ((Float) map.get("yaw")).floatValue();
            pitch = ((Float) map.get("pitch")).floatValue();
        }
     
        /**
        * Gets the SerializableLocation as an org.bukkit.Location for the defined server.
        *
        * @param server The server to get the location from
        * @return A Location from this SerializableLocation, that is located on the server
        */
        public final Location getLocation(Server server) {
            if (loc == null) {
                World world = server.getWorld(uuid);
                if (world == null)
                    world = server.getWorld(this.world);
                loc = new Location(world, x, y, z, yaw, pitch);
            }
            return loc;
        }
     
        /**
        * Returns a map of the SerializableLocation.
        *
        * @return A map where the key is the type of argument, and the value is the argument
        */
        public final Map<String, Object> serialize() {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("world", world);
            map.put("uuid", uuid);
            map.put("x", x);
            map.put("y", y);
            map.put("z", z);
            map.put("yaw", yaw);
            map.put("pitch", pitch);
            return map;
        }
    }
    
    problem solved.
     
Thread Status:
Not open for further replies.

Share This Page