De-serializing a hashmap whitch contains a class?

Discussion in 'Plugin Development' started by Gonmarte, Mar 22, 2016.

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

    Gonmarte

    Hi guys,
    Im trying to de-seriazling my hashmap. @Zombie_Striker was helping but i still get errors so im going to share for all community. So im trying to save my Hashmap in onDisable() and loading it in onEnable();
    My hashmap:
    Code:
     public static HashMap<String, Clans> playersClan = new HashMap<>();
    So my onDisable() contains the serialize of my values on my hashmap (object of my class) and the save of the hashmap.
    Code:
      public void onDisable() {
         
         for(Entry<String, Clans> entry : Clans.playersClan.entrySet()){
             
             this.getConfig().set(entry.getKey(), entry.getValue().toString());
         
         }
         saveConfig();
      }
    
    In my onEnable im trying to load the hashmap, so once i serialised the object of my class to a tring i need to convert it again from a string to the class object (De-serialization) and is in this that im getting my error:
    Code:
      public void onEnable() {  
        for(String key : this.getConfig().getKeys(true))  {
           //While im trying to conver a String to my object Clans i get a red line underlining the code
            Clans value = this.getConfig().getString(key);
            Clans.playersClan.put(key, value);
         
        }
      }
    
    Thank you!
     
  2. Offline

    _loco_

    In your Clans object, make a static method that returns a Clans from a string. E.g:

    Code:
    public static Clans getClanFromString (String string)
    Then, just call that method to get the Clans from the string value in the config.
     
  3. Offline

    Zombie_Striker

    @Gonmarte
    As @_loco_, you need to create a method that turns a string into a Clans object, (in other words, a deserializer)

    Do this by using the above method, create a a new Clans object and set all of it's data equal to whatever is in the string.
     
  4. Offline

    Gonmarte

    @_loco_
    If i create a new Clans Object i will be creating a new clan...
     
    Last edited: Mar 23, 2016
  5. Offline

    Zombie_Striker

    @Gonmarte
    Every time you shut down your server, all of the clans are removed from memroy. When the server starts up again, that hashmap of clains is empty. What you need to do is recreate what clans existed before the server was turned off/ reloaded.
     
  6. Offline

    Gonmarte

    My constructor just add the the clan to my arraylist, the clan and the player are only put in the hasmap if a player creates a clan..
    @Zombie_Striker A friend told me that the method toString()...
    He also said that i should create a custom parser, so i will do some research on that
     
    Last edited: Mar 23, 2016
Thread Status:
Not open for further replies.

Share This Page