[SOLVED]How to save a HashMap<String,Location> in a File?

Discussion in 'Plugin Development' started by LukeSFT, Aug 18, 2012.

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

    LukeSFT

    How can I save a HashMap<String , Location> in a File??
    I tried it with:
    Code:
        public void saveRegions (HashMap<String , Location> regions ){
            try{
                ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream( /*Walls.regionsFolder + File.separator + */"regions.bin"));
                oos.writeObject(regions);
                System.out.println("Saving HashMap...");
                oos.flush();
                oos.close();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    but there was an Error: [SEVERE] java.io.NotSerializableException: org.bukkit.Location
     
  2. Offline

    Sagacious_Zed Bukkit Docs

    You cannot. Location is not serializable. You cannot straight up save a Location.
     
  3. Offline

    Deleted user

    Well you CAN.. but not like this.
     
  4. Offline

    Sagacious_Zed Bukkit Docs

    Then it is not serializing a Location object. It is possible to have a class with the key pieces of information.
     
  5. Offline

    LukeSFT

    I know that that what I did doesn't work and that there is a way to do this, but how can I do this??
     
  6. Offline

    JDigital1337

    Well, make a custom object that takes a location in its constructor and builds simple data type representations of the same data in the Location. Serialize that... then just make yourself a method that takes one of those and gives you a corresponding location object.
     
  7. Offline

    Firefly

    Like this: :D

    Show Spoiler

    Code:
    public class LocationData {
       
        private Location location;
       
        public LocationData(Location location) {
            this.location = location;
        }
       
        public String convertToString() {
            String locationData = location.getWorld().getName() + ", " + String.valueOf(location.getX()) + ", " + String.valueOf(location.getY()) + ", " + String.valueOf(location.getZ());
           
            return locationData;
        }
       
        public static LocationData convertFromString(String locationData) {
            locationData = locationData.replaceAll("\\s", "");
            String[] locationArray = locationData.split(",");
            String locationWorldName = locationArray[0];
            double locX = Double.parseDouble(locationArray[1]);
            double locY = Double.parseDouble(locationArray[2]);
            double locZ = Double.parseDouble(locationArray[3]);
           
            return new LocationData(new Location(Bukkit.getWorld(locationWorldName), locX, locY, locZ));
        }
       
        public Location getLocation() {
            return location;
        }
       
        public void setLocation(Location l) {
            this.location = l;
        }
     
    }
     
    LukeSFT likes this.
  8. Offline

    JDigital1337

    Helpful dev is helpful
     
  9. Offline

    Sagacious_Zed Bukkit Docs

  10. Offline

    LukeSFT

    Firefly: What is "locationData = locationData.replaceAll("\\s", "");" doing?
    why is the class LocationData static ?

    (I'm new in coding.)
     
  11. Offline

    LucasEmanuel

    You can use this object to serialize your location, then you can save the map with an ObjectStream, or you could modify the object to use a ConfigurationSerializable to be able to save it in your config file. :)
    Code:
    import java.io.Serializable;
     
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
     
    public class SerializedLocation implements Serializable {
        private static final long serialVersionUID = -9094035533656633605L;
       
        private final String WORLDNAME;
        private final double X;
        private final double Y;
        private final double Z;
        private final float YAW;
        private final float PITCH;
       
        public SerializedLocation(Location location) {
            this.WORLDNAME = location.getWorld().getName();
            this.X        = location.getX();
            this.Y        = location.getY();
            this.Z        = location.getZ();
            this.YAW      = location.getYaw();
            this.PITCH    = location.getPitch();
        }
       
        public Location deserialize() {
            return new Location(Bukkit.getWorld(this.WORLDNAME), this.X, this.Y, this.Z, this.YAW, this.PITCH);
        }
    }
     
  12. Offline

    Firefly

    LocationData isn't static, the convertFromString method is so you don't need the actual object to call the method. That other line just removes any blank spaces.
     
    LukeSFT likes this.
  13. Offline

    LukeSFT

    k
    I'll try it
     
  14. Offline

    LukeSFT

    but how can I save a HashMap<String , Location> with this??
     
  15. Offline

    Sagacious_Zed Bukkit Docs

    You take your HashMap<String, Location> and you construct a new HashMap<String, LocationData> and you save that in place of the original hashmap
     
  16. Offline

    LukeSFT

    but how can I do this?
    I mean the constructor uses a Location to construct an serializable Object, but how can I convert a HashMap with this constructor?
     
  17. Offline

    LukeSFT

    Does nobody knows how to do this??:(
     
  18. Offline

    LukeSFT

    I found a way to do this.
     
Thread Status:
Not open for further replies.

Share This Page