Solved Configuration files: Add/Remove from a list

Discussion in 'Plugin Development' started by NewDevOnTheBlock, Apr 15, 2015.

Thread Status:
Not open for further replies.
  1. Hello,

    So basically I'm running into an issue. The way I create a config file is just by putting 'saveDefaultConfig();' in my onEnable. That may not be the best way to create the config file, but it's just what I've found so far.

    So I initialized a List like this:
    Code:
    List<String> test = getConfig().getStringList("Players");
    Checked if the player was in the List:
    Code:
    test.contains(player.getName())
    Added players from List:
    Code:
    test.add(player.getName().toString());
    Removed players from List:
    Code:
    test.remove(player.getName().toString());
    For reasons I do not know... This keeps the players in the list even after they logout, but my ArrayList was not.
    So, no where in my code do I store a players name to a config file. Here is what my config file looks like:
    Code:
    Players: []
    Here is what I am looking for. Even though my code works, and if the player logs out, and then back in it remembers him. I would still like to know out of curiosity, how to add/remove a player to my 'Players: []' list and have it look like...
    Code:
    Players:
        - Name1
        - Name2
        - Name3
    I looked for hours on an answer for this, but could find nothing that worked. If you have a good (up-to-date) tutorial on making configuration files feel free to leave a link!

    Thank you
     
  2. Offline

    Konato_K

  3. Invisible

    nverdier

    @NewDevOnTheBlock Are you saving the list back to the config after you remove a player?

    EDIT: Ninja'D ;3
     
  4. Thank you guys for getting back to me

    @Konato_K In my various test before coming to these forums I did try that.
    Code:
    getConfig().getStringList("Players").add(player.getName().toString());
    saveConfig();
    This was one method I tried, and personally in theory it seems like it should work? .getStringList("Players") is going to return a list of everyone inside of the "Players" key, and then I'm just adding another player name to the list, right?

    To answer your question, I do not save the config anywhere in my code as of right now. I tried before, but it still did not work. What I have now probably doesn't even need a config file, and that's why I don't know how it's working, but it is.
     
  5. Invisible

    nverdier

    @NewDevOnTheBlock When you use #getStringList() it returns a List<String>, but you have to set "Players" to the List after you edit it.
     
  6. Alright so I read through that entire (http://wiki.bukkit.org/Configuration_API_Reference), and I wish I did 5 hours ago when I came to this problem. I have now resolved the issue, and here is what I had to do.

    onCommand
    Code:
    test.add(player.getName().toString()); //This adds the player to the list
    this.getConfig().set("Players", test); //This writes it in the configuration file
    saveConfig(); //Saves the configuration file
    Event
    Code:
    test.remove(player.getName().toString()); //Removed the player from the list
    this.getConfig().set("Players", test); //Write to the config the new list
    saveConfig(); //Save the config file
    It is finally working! Thank you for linking me that Configuration API Reference @Konato_K . Today was a good learning day...
     
    Konato_K likes this.
  7. Invisible

    nverdier

    @NewDevOnTheBlock Glad you actually decided to read it :D You'd be surprised how many people don't ;3
     
    Konato_K likes this.
  8. Offline

    Evaluations

Thread Status:
Not open for further replies.

Share This Page