Solved Clearing stringLists

Discussion in 'Plugin Development' started by Anrza, Jul 26, 2014.

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

    Anrza

    I have this code in my onDisable:
    Code:java
    1. List<String> worldSpawns = getConfig().getStringList("worldSpawns");
    2. for (String string : worldMap.keySet()) {
    3. worldSpawns.add(string + ":" + worldMap.get(string));
    4. }
    5. getConfig().set("worldSpawns", worldSpawns);
    6. saveConfig();

    It saves the hashmap worldSpawns on onDisable, and that works fine. However, it adds a new, identical one every time, so it's stacks up to this:
    - string:world
    - string:world
    - string:world
    - string:world
    et.c., but as you might see, that's kinda unwanted.
    How do I fix this? I've tried to getConfig().getStringList("worldSpawns").clear(), but it doesn't work, and I've tried to remove the values one by one, getConfig().getStringList("worldSpawns".remove(), but neither works.
     
  2. Offline

    Dragonphase

    Anrza

    Have you tried:

    Code:java
    1. List<String> worldSpawns = new ArrayList<String>();
     
  3. Offline

    Anrza

    No... I don't see how that would help...
     
  4. Offline

    Dragonphase

    Anrza

    You said you've tried to use getConfig().getStringList("worldSpawns").clear(), which tells me you want to clear the list before applying changes. That code will not actually clear the list inside the config. Your current code gets the current list from the config and adds additional strings to it. If you initialize worldSpawns as a new list, it'll be empty and you can then add your strings.
     
  5. Offline

    Anrza

    So I did:
    Code:java
    1. List<String> worldSpawns = new ArrayList<String>(getConfig().getStringList("worldSpawns"));

    Still having the same problem.
     
  6. Offline

    Dragonphase

    Anrza

    ...remove the getConfig().getStringList("worldSpawns") part from it.
     
  7. Offline

    k9rosie

    Code:java
    1. List<String> worldSpawns = new ArrayList<String>();
    2.  
    3. getConfig.set("worldSpawns", worldSpawns);
    4. saveConfig();


    ?
     
  8. Offline

    Dragonphase

    k9rosie

    That's not what he's doing. He's saving the worldMap HashMap to the config, however it adds a duplicate every time he unloads the plugin, reloads or shuts down the server. The only thing I can see that could be causing that with the information he provided is the first line, which retrieves the current worldSpawns from the config.

    Anrza

    Common logic would be that you're loading the list from the config in your onEnable method, and saving its contents to the worldMap HashMap. If that's so, there is no need to load the String List again in your onDisable - just create a new list and add the contents of your map.
     
  9. Offline

    Anrza

    Dragonphase
    Oh. You're a genius. Sorry, I'm kinda tired.
    I get what it does. Thank you very much.
     
    Dragonphase likes this.
  10. Offline

    Dragonphase

    Anrza

    It's cool :) Happy to help!
     
Thread Status:
Not open for further replies.

Share This Page