Location serialized..

Discussion in 'Plugin Development' started by Deleted user, Oct 16, 2012.

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

    Deleted user

    So basically I want users to be able to save a 'home' and so I have this Hashmap<String, Location> and just place the players name and their location into the hashmap and then save it to a file read it etc however Location isn't serialized so that wont work :/ What's a way around doing this? Like instead of location make it a string but then how do I covert a string to a location?
     
  2. Offline

    gcflames5

    Here is an entire class that will solve your problem, I am in a good mood today. :)

    Code:
    package me.gcflames5.plugin;
     
    import java.io.Serializable;
    import java.util.HashMap;
    import java.util.Map;
     
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.Server;
    import org.bukkit.World;
     
    /**
    * NOTE TO SELF - DO NOT CHANGE ANYTHING IN THIS CLASS!
    *
    * [USER=90649340]gcflames5[/USER]
    *
    */
     
    public final class SerializableLocation implements Serializable {
     
        private static final long serialVersionUID = 7498864812883577904L;
        private final String world;
        private final String uuid;
        private final double x, y, z;
        private final float yaw, pitch;
        private transient Location loc;
     
        public SerializableLocation(Location l) {
            this.world = l.getWorld().getName();
            this.uuid = l.getWorld().getUID().toString();
            this.x = l.getX();
            this.y = l.getY();
            this.z = l.getZ();
            this.yaw = l.getYaw();
            this.pitch = l.getPitch();
        }
     
        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;
        }
        public static Location returnBlockLocation(SerializableLocation l) {
            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);
            return location;
        }
     
        public SerializableLocation(Map<String, Object> map) {
            this.world = (String) map.get("world");
            this.uuid = (String) map.get("uuid");
            this.x = (Double) map.get("x");
            this.y = (Double) map.get("y");
            this.z = (Double) map.get("z");
            this.yaw = ((Float) map.get("yaw")).floatValue();
            this.pitch = ((Float) map.get("pitch")).floatValue();
        }
     
        public final Map<String, Object> serialize() {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("world", this.world);
            map.put("uuid", this.uuid);
            map.put("x", this.x);
            map.put("y", this.y);
            map.put("z", this.z);
            map.put("yaw", this.yaw);
            map.put("pitch", this.pitch);
            return map;
        }
     
        public final Location getLocation(Server server) {
            if (loc == null) {
                World world = server.getWorld(this.uuid);
                if (world == null) {
                    world = server.getWorld(this.world);
                }
                loc = new Location(world, x, y, z, yaw, pitch);
            }
            return loc;
        }
    }
     
    1 person likes this.
  3. Offline

    Deleted user


    Jesus, okay uhm so how do I use this to put it in a Hashmap and yeah?
     
  4. Offline

    Sagacious_Zed Bukkit Docs

    This would be the value class for the hash map. Instead of your Map<String, Location> this would be in place of the location.
     
  5. Offline

    devilquak

    gcflames5
    Sagacious_Zed

    How on earth do you use it after that though? I can't convert anything to a serializeable location...

    Code:
    public HashMap<SerializableLocation, String> locations = new HashMap <SerializableLocation, String>();
     
    locations.put(<What goes here???>, string);
     
  6. Offline

    Sagacious_Zed Bukkit Docs

    a
    What goes there is either a new Serializable location, or a variable pointing to an existing one.
     
    devilquak likes this.
  7. Offline

    devilquak

    Thank you.
     
  8. Offline

    bobacadodl

    I'm making an plugin with a ton of useful classes and methods like this. Mind if I add this to it?
     
    gcflames5 likes this.
  9. Offline

    Aikar

    Theres no reason to require a Server parameter on getLocation, just use Bukkit.getServer();
     
  10. Offline

    fireblast709

    Why do people want to serialize everything instead of just using a String representation of the Location? And why the hell does it need to store the World UUID?
     
  11. Offline

    tommycake50

    lol yeah plus a string representation i easier xD.
     
  12. Offline

    gcflames5

    Umm thanks for the criticism (I guess)... was just trying to help...

    PS At least I gave him something that works
     
  13. Offline

    fireblast709

    Well if you want to bitch about it ;3
    Using a String representation:
    Code:java
    1. public String locToString(Location l)
    2. {
    3. return new StringBuilder().append(l.getWorld().getName()).append(",")
    4. .append(l.getX()).append(",")
    5. .append(l.getY()).append(",")
    6. .append(l.getZ()).append(",")
    7. .append(l.getYaw()).append(",")
    8. .append(l.getPitch()).toString()
    9. }
    10.  
    11. public Location stringToLoc(String s)
    12. {
    13. try
    14. {
    15. String[] parts = s.split(",", 6);
    16. if(parts.length != 6)
    17. {
    18. throw new IllegalArgumentException("Invalid location. It did not contain all the parts");
    19. }
    20. World w = Bukkit.getWorld(parts[0]);
    21. double x = Double.parseDouble(parts[1]);
    22. double y = Double.parseDouble(parts[2]);
    23. double z = Double.parseDouble(parts[3]);
    24. float yaw = Float.parseFloat(parts[4]);
    25. float pitch = Float.parseFloat(parts[5]);
    26. if(w == null)
    27. {
    28. throw new IllegalStateException("World cannot be null");
    29. }
    30. return new Location(w,x,y,z,yaw,pitch);
    31. }
    32. catch(Exception ex)
    33. {
    34. return null;
    35. }
    36. }

    A more user friendly method using ConfigurationSection:
    Code:java
    1. public void saveLocation(Location l, String path)
    2. {
    3. ConfigurationSection loc = getConfig().getConfigurationSection(path);
    4. if(loc == null)
    5. {
    6. loc = getConfig().createSection(path);
    7. }
    8. loc.set("world", l.getWorld().getName());
    9. loc.set("x", l.getX());
    10. loc.set("y", l.getY());
    11. loc.set("z", l.getZ());
    12. loc.set("yaw", l.getYaw());
    13. loc.set("pitch", l.getPitch());
    14. saveConfig();
    15. }
    16.  
    17. public Location loadLocation(String path)
    18. {
    19. try
    20. {
    21. ConfigurationSection loc = getConfig().getConfigurationSecion(path);
    22. World w = Bukkit.getWorld(loc.getString("world", ""));
    23. double x = loc.getDouble("x");
    24. double y = loc.getDouble("y");
    25. double z = loc.getDouble("z");
    26. float yaw = (float)loc.getDouble("yaw");
    27. float pitch = (float)loc.getDouble("pitch");
    28. if(w == null)
    29. {
    30. throw new IllegalStateException("World was not found");
    31. }
    32. return new Location(w,x,y,z,yaw,pitch);
    33. }
    34. catch(Exception ex)
    35. {
    36. return null;
    37. }
    38.  
    39. }
     
    GetGoodKid likes this.
  14. Offline

    GetGoodKid

    I think I'm in love with you...
     
Thread Status:
Not open for further replies.

Share This Page