Serialize/Deserialize Location problem

Discussion in 'Plugin Development' started by BagduFagdu, Jun 3, 2015.

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

    BagduFagdu

    So, I have a plugin that saves a Location and a String in a HashMap, serializes it using OIS and deserializes it using OOS. So far, the plugin gives no error and stores the data, but it doesn't manage to serialize/deserialize the Location back to the original hashmap. This is the current code:

    Code:
        
        private static final File savedFolder = newFile("plugins/Plugin");
        private static final File savedFile = newFile("plugins/Plugin/HashMap");
        public static HashMap<Location, String> map = new HashMap<Location, String>();
        public static HashMap<String, String> storage = new HashMap<String, String>();
    
        public static void save() throws Exception {
            if(!savedFolder.exists()) {
                savedFolder.mkdir();
            }
            if(!savedFile.exists()) {
                savedFile.createNewFile();
            }    
            for(Location keys : map.keySet()) {
                String loc = locationToString(keys);
                storage.put(loc, map.get(keys));
            }
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(savedFile));
            oos.writeObject(storage);
            oos.close();
        }
    
        public static void load() throws Exception {
            if(!savedFile.exists()) {
                return;
            }
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream(savedFile));
            Object reader = ois.readObject();
            if(!reader.equals(null) && reader instanceof HashMap) {
                for(String keys : storage.keySet()) {
                    Location loc = stringToLocation(keys);
                    map.put(loc, storage.get(keys));
                }
            }
            ois.close();
        }
    
        public static String locationToString(Location loc) {
            StringBuilder sb = new StringBuilder();
            return sb.append(loc.getWorld().getName()).append(",").append(loc.getX()).append(",")
                    .append(loc.getY()).append(",").append(loc.getZ()).append(",").append(loc.getYaw()).append(",").append(loc.getPitch()).toString();
        }
    
        public static Location stringToLocation(String loc) {
             String[] str = loc.split(",");
             World world = Bukkit.getServer().getWorld(str[0]);  
             double x = Double.parseDouble(str[1]);
             double y = Double.parseDouble(str[2]);
             double z = Double.parseDouble(str[3]);
             float yaw = Float.parseFloat(str[4]);
             float pitch = Float.parseFloat(str[5]);
             return new Location(world, x, y, z, yaw, pitch);
        }
     
  2. Offline

    mine-care

    So you wana save the hashmap to a file or the location to the hashmap?
     
  3. Offline

    drew6017

    You are making this way to hard. Bukkit allows for location storing in yml's.
    Code:
    // For saving
            YamlConfiguration var = new YamlConfiguration();
            var.set("key", location);
            var.save(file);
    
    // For loading
            YamlConfiguration var = new YamlConfiguration();
            var.load(file);
            Location location = (Location)var.get("key");
     
  4. Offline

    mine-care

    @drew6017 i think there is a reason for chosing serialization like no human interfeerance or something :/ Well it is up to the op to tell us.
     
  5. Offline

    BagduFagdu

    @Mini-care @drew6017 I would to save the "map" HashMap, but Location isn't serializable. So I try to convert it into a string, I save the serialized location into a "storage" hashmap. When the plugin goes back online it deserializes it and puts it into the "map" HashMap, which is the one used for plugin use. But it doesn't seem to be working as I wish.
     
Thread Status:
Not open for further replies.

Share This Page