1.15.2 Save and load arrays to config

Discussion in 'Spigot Plugin Development' started by gordanb740, Aug 1, 2020 at 9:12 PM.

  1. So I added a feature that can be enabled/disabled with a simple command. Right now when you enable/disable the feature, a boolean changes from true to false, like this:

    Code (Java):
    public static Map<UUID, Boolean> mModeDisabled = new HashMap<UUID, Boolean>();
    However, I need to save the player's choice in a config to that it doesn't reset on every server restart. To decrease amount of writes to the config, it should only save whenever the server shuts down, but I'm not sure how to save/load a Map to the config. What would be the easiest way to do this? Maybe a boolean isn't even necessary?
     
  2. Store it in a Set instead of a map and save it as a string-list
     
  3. Why would a Set be better than Map? And I know it should be saved as a string list but I just don't know how to add the boolean to it. Would this be the best way to do it?

    Code (Text):
    Userdata:
      (UUID):
        enabled: true
    If so, how do I add the UUID and the boolean from the Map or Set?
     
  4. well with a set, the boolean can be simplified to Set#contains(Object). All UUID's stored in the config have the boolean set to true, all that don't have the boolean set to false
     
  5. Ok I tried this in the OnEnable() but it didn't work:

    Code (Java):
    this.getConfig().getConfigurationSection("Userdata").getKeys(false).forEach(key ->{
                mModeDisabled.put(key, this.getConfig().getStringList("Userdata." + key));
            });
    I'm trying to get the UUID.toString() as well as the boolean from the config but it's not working, any idea why?
     
  6. What exactly is not working? What I‘m suggesting is this:
    Code (Java):
    Set<UUID> set = ...; // some uuids in here
    void save() {
        final List<String> uuids = new ArrayList<>();
        set.forEach(uuid -> uuids.add(uuid.toString());
        config.set(„userdata“, uuids);
    }
    void load() {
        final List<String> uuids = config.getStringList(„userdata“);
        uuids.forEach(uuid -> set.add(UUID.fromString(uuid));
    }