1.14.4 How can I save and load a hashmap with a class in it?

Discussion in 'Spigot Plugin Development' started by ImJustHere, Feb 10, 2020.

  1. How would I save and load a HashMap with a class in it with the values in the ClanManager class

    Code (Text):
    Class Clan:

    public static HashMap<String, ClanManager> Clans = new HashMap<String, ClanManager>();

    Class ClanManager:
    public ClanManager (Player owner, String name, ChatColor color, int Size) {
            this.clanName = name;
            this.color = color;
            this.Size = Size;
            members.put(owner.getUniqueId(), "Owner");
  2. depends on the storage provider... Are you planning to use JSON, YML, NBT, XML, FML, custom?

    (one of these is not like the others)
  3. I'm planning to use json
  4. Make sure that ClanManager is serializable and then you’re good to go
    • Agree Agree x 1
  5. if you use GSON, it shouldn't be a problem. If the only stored properties are size, color, and clanName, the class should be serializable (as @Maxx_Qc mentioned).
  6. I don't really understand how I will serialize the clanManager it and then deserialize it to a config because I never did that before
  7. Really not different from any other serializing:
    Code (Java):
    final Map<String, CustomClass> map = new HashMap<>();

    map.put("first", new CustomClass("id", 42, CustomEnum.C));
    map.put("second", new CustomClass("id2", 42, CustomEnum.A));

    Gson gson = new GsonBuilder()
        .setPrettyPrinting()    // not required, makes the json look nice

     final String json = gson.toJson(map, Map.class);

    // prints
    // {
    //  "first": {
    //    "id": "id",
    //    "fortyTwo": 42,
    //    "customEnum": "C"
    //  },
    //  "second": {
    //    "id": "id2",
    //    "fortyTwo": 42,
    //    "customEnum": "A"
    //  }

    final Map<String,CustomClass> regainedMap = gson.fromJson(json, Map.class);

    // prints
    // {first={id=id, fortyTwo=42.0, customEnum=C}, second={id=id2, fortyTwo=42.0, customEnum=A}}
  8. @Schottky it is saving it but when I try to load it's not working
    Code (Text):
    The HashMap (Located in a different class) :
    public final static Map<String, ClanManager> Clans = new HashMap<>();

    ClansConfig.set("Clans", gson.toJson(Clan.Clans));

    final Map<UUID, ClanManager> loadedData = gson.fromJson(ClansConfig.getString("Clans"), Map.class);
    #8 ImJustHere, Feb 12, 2020
    Last edited: Feb 12, 2020
  9. Exact error please.
    Use Map as Type parameter (gson.fromJson(..., Map.class)), and Cast it to Map
  10. I don't get any errors but when I try to do any of my plugin commands I get no response and I get this msg in console playerName issued server command: <commandName>
  11. Whats the command code
  12. The code is working fine but when I load the HashMap everything is stoping to work
  13. You're switching from string to UUID, of course it's not working....
  14. I changed that and I still have the same result
  16. I didn't really understand how I will get the name from it

    Code (Text):
    final Map<String, ClanManager> loadedData = gson.fromJson(ClansConfig.getString("Clans"), Map.class);
            Clan.Clans.put("?" ,(ClanManager) loadedData);
  17. Code (Java):
    Clan.Clans = gson.fromJson(...);
  18. @Schottky I'm getting an internal error now
  19. Please post the error
    Don‘t make the Map final and don't use a HashMap as Type, instead, use Map
  20. The map is not final and i didn't use the HashMap as a type