1.14.4 How can I load a saved hashmap with a class inside it from a config?

Discussion in 'Spigot Plugin Development' started by RoboBoy99, Feb 7, 2020.

  1. How can I load the hashMap?
    Note: The saving those work
    HashMap and List:
    Code (Text):
    public HashMap<String, ClanManager> Clans = new HashMap<String, ClanManager>();

    List<String> ClansData = new ArrayList<String>();

    Saving the HashMap:
    Code (Text):
            for (Entry<String, ClanManager> clanName : Clan.Clans.entrySet())
            {
                String data = clanName.toString();
                ClansData.add(data);
            }    
     
  2. What is your question? (Please explain more than in the title)
     
  3. How can I load the hashmap from the config?
     
  4. Loop through the keys in the config, insert the key (as the key) to the hashmap and the value in the config (as the value) to the hashmap
     
  5. #5 RoboBoy99, Feb 7, 2020
    Last edited: Feb 7, 2020
  6. Code (Text):

    for (String key : config.getConfigurationSection("clans").getKeys(false))
     
     
  7. I'm using this method
    But when I use your's or mine
    I still need the class in it...

    Code (Text):
    for(String Clans : getConfig().getStringList("Clans.yml"))
    {

    }
     
  8. You're using .getStringList() wrong. You don't put the file name in the arguments, you put the name of the list.

    Code (Java):
    config.getStringList("Example Path");
    Code (Text):
    Example Path:
          -  Value 1
          - Value 2
          - Value 3
     
  9. @C10sp The path I can fix but I still don't understand how I will add the values from the config to the current hashMap
     
  10. Code (Java):

    // your map
    Map<UUID, GamePlayer> dataMap = new HashMap<>();

    // a GSON instance
    final Gson gson = new Gson();

    // save
    getConfig().set("my.map", gson.toJson(dataMap));

    // load
    Map<UUID, GamePlayer> loadedData = gson.fromJson(getConfig().getString("my.map"), HashMap.class);
     
    Not getting simpler than this.

    Explanation: uses GSON (included JSON library from google in Spigot) to serialize the map and it content's and then read and map them back.
     
  11. in the load, Its not letting me to get my hashMap (its located in other class)
     
    #11 RoboBoy99, Feb 7, 2020
    Last edited: Feb 7, 2020
  12. Sounds like poor project setup or code style
     
  13. nvm I just placed it in the wrong place by accident
     
  14. And it's still not working..
     
  15. Please provide some actual useful information, like, what you tried and what isn't working.
    If you want us to look with you, for you, please at least give us the chance.