[SOLVED] Serializable Locations.

Discussion in 'Plugin Development' started by ThatBox, Feb 9, 2012.

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

    ThatBox

    I know that they aren't serializable but what can I use to replace them ?
     
  2. Offline

    Sagacious_Zed Bukkit Docs

    You can replace them with either your primitives or your own ConfigurationSerializable proxy for location.
     
    ThatBox likes this.
  3. Is saving only the x, y ,z and the world in a file an option?
    Then you could create a location again from those values....
     
  4. I use my own class for this.
    Code:
    public class TinyLocation implements ConfigurationSerializable {
     
        private final static long serialVersionUID = 1L;
        private String world;
        private double x;
        private double y;
        private double z;
     
        public TinyLocation(World world, Double x, Double y, Double z) {
            this.world = world.getName();
            this.x = x;
            this.y = y;
            this.z = z;
        }
     
        public TinyLocation(Location location) {
            this.world = location.getWorld().getName();
            this.x = location.getX();
            this.y = location.getY();
            this.z = location.getZ();
        }
        
        public TinyLocation(ConfigurationSection map) {
            this.world = map.getString("world");
            this.x = map.getDouble("x");
            this.y = map.getDouble("y");
            this.z = map.getDouble("z");                
        }
     
        public Location toLocation() {
            Location l = new Location(Bukkit.getWorld(world), x, y, z);
            return l;
        }
     
        @Override
        public Map<String, Object> serialize() {
            Map<String, Object> map = new HashMap<String, Object>();
     
            map.put("world", world);
            map.put("x", x);
            map.put("y", y);
            map.put("z", z);
     
            return map;
        }
     
        public static TinyLocation deserialize(Map<String, Object> map) {
            Configuration conf = new MemoryConfiguration();
            for (Entry<String, Object> e : map.entrySet()) {
                conf.set(e.getKey(), e.getValue());
            }
            return new TinyLocation(conf);
        }
    }
    
     
  5. Offline

    ThatBox

    Maybe I will. But will the servers owners have to put sync in their plugin folder as well?
    EDIT: Seems like they do :p
    EDIT: How would make the object a HashMap.

    HashMap<String, Location> Is what i'm trying to do. Btw too many hashmaps just means more lag ;p

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 23, 2016
  6. You want to use a Sync Location. This can be easily created by just passing a Bukkit Location into the constructor like so.

    Code:java
    1.  
    2. Location bukkitLoc;
    3. SyncLocation syncLoc = new SyncLocation(bukkitLoc);
    4.  
     
  7. Offline

    ThatBox

    Edit: solved
     
Thread Status:
Not open for further replies.

Share This Page