Solved Writing and Getting Lists from Config

Discussion in 'Plugin Development' started by MasterDoctor, Oct 31, 2015.

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

    MasterDoctor

    I have this code:
    Code:
    if (plugin.getConfig().getList("chest-data.locations") == null) {
                  
                    // List doesn't exist!
                    //player.sendMessage("List nonexistant code executed!");
                    List<String> locationList = new ArrayList<String>();
                    locationList.add(chestLocation);
                    plugin.getConfig().set("chest-data.locations", locationList);
                    plugin.saveConfig();
                } else {
                    // List exists!
                    //player.sendMessage("List exists code executed!");
                    @SuppressWarnings("unchecked")
                    List<String> locationList = (List<String>) plugin.getConfig().getList("chest-data.locations");
                    locationList.add(chestLocation);
                    plugin.getConfig().set("chest-data.locations", locationList);
                    plugin.saveConfig();
    }
    which is in an event listener for block place. It checks if the block is a chest, defines the variables and runs this code.
    However, whenever I run this code it always runs the Line doesn't exist code therefore when I go into the config it has overwritten the previous entry there. I have also tried stopping and restarting the server, it still overwrites.

    EDIT: Tidying up some code.
     
  2. Offline

    bowlerguy66

    What about .getStringList()?
     
  3. Offline

    Zombie_Striker

    use .contains()
    Because you are creating a NEW arraylist each time, it overrides the last one. You would need to change New ArrayList to Config.getList().
     
  4. Offline

    MasterDoctor

    But the NEW array list part of code is only meant to run if the list was NULL

    So I should read&write the list as strings?
    OK, I will give that a shot later on this evening.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 31, 2015
  5. Offline

    teej107

    You are adding and getting Strings from the List right? You even create a List with the String as the type. You don't need to do a null check with #getStringList() because it will return an empty List if the path isn't found as specified in the documentation.
     
  6. Offline

    MasterDoctor

    Ohh! Thanks!

    Hmm, I tried this but I got a NullPointerException...

    EDIT: Never mind, I saw what I was doing wrong!
    For anyone who finds this useful; here is my amended code - I was getting a list because I previously stored a location object not a string, But it is easier to do this with a String List [So save and load a string list].
    EDIT #2: Cleaned code formatting

    Amended Code (open)

    Code:
               List<String> locationList = plugin.getConfig().getStringList("chest-data.locations");
                locationList.add(chestLocation);
                plugin.getConfig().set("chest-data.locations", locationList);
                plugin.saveConfig();


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited: Oct 31, 2015
Thread Status:
Not open for further replies.

Share This Page