Solved Help saving a hashmap that contains a class!

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

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

    Gonmarte

    Hi,
    Im trying to save an hashmap on the config. The problem is that my hashmap is

    Code:
       public static HashMap<String, Clans> playersClan = new HashMap<>();
    
    My problem is that Clans (my class).

    So in the onDisable i have this
    Code:
      public void onDisable() {
     
         for(Entry<String, Clans> entry : Clans.playersClan.entrySet()){
         
             this.getConfig().set(entry.getKey(), entry.getValue());
     
         }
         saveConfig();
      }
    
    In the onEnable i have a problem... take a look´
    Code:
      public void onEnable() {
        for(String key : this.getConfig().getKeys(false))  {
    //My value is my class Clans, so how do i get it from config?? Its not a string and its not a int....
    Clans value = this.getConfig().
            Clans.playersClan.put(key, value);
     
        }
      }
    
    I was thinking about converting Clans to a string but i dont know if it is possible...
    Ty
     
    Last edited: Mar 21, 2016
  2. Offline

    mcdorli

    Instead of using a package name "Main.Main", use me.<yourusernamehere>.<yourprojectsname>.<yourmainclassname>
     
  3. Offline

    Gonmarte

    @mcdorli Nvm, i realised i was getting a problem, take a look at the main thread once i change it!
    Thank you!

    Bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Mar 22, 2016
  4. Offline

    I Al Istannen

    @Gonmarte
    You don't need to convert it to a String. You could implement ConfigurationSerializable and follow the javadoc, that says what you need. Then, somewhere in your code before you load the config (e.g. at the beginning of the onEnable()) you need to use ConfigurationSerialization#registerClass(Class). And make the class known to the ConfigurationSerialization. From now on, you can use the config.get and config.set normally. You will need to cast the result of Config.get, but it will be the object you saved.
     
    Gonmarte likes this.
  5. Offline

    Gonmarte

Thread Status:
Not open for further replies.

Share This Page