Reading / Saving Lists to Config (derp)

Discussion in 'Plugin Development' started by StaticE, Apr 10, 2013.

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

    StaticE

    Hey guys,

    I've been trying to work with lists lately, more specifically writing lists to a custom config file then reading them again later. This is what my config would look like (for example):
    Code:
    Players:
      Player1:
        items:    
          ID of some item (int)
          ID of another item..and so on
      Player2:
        items:    
          ID of some item(int)
          ID of another item...and so on
    So my first question is, when declaring the path to the list "items", would I do
    Code:
    getConfig().getStringList("Player1.items")
    ? ​

    Pretty much I just need some example code on which type of list to do, and how to write and read it from a config file.​

    Thanks! ​
     
  2. Offline

    SnipsRevival

    You should be using getConfig().getIntegerList("Players.Player1.items");
     
  3. Offline

    StaticE

    SnipsRevival Oh, that makes sense. So just to clarify, to write to the list I could use
    Code:
    getConfig().getIntegerList("Players.Player1.items").add(some integer here);
    And that will just add to the list, not override anything.

    And if I have multiple players under Player: then would I need to make a StringList of player?
     
  4. Offline

    SnipsRevival


    You can try doing that, but I don't think it works. The way I have always been adding to lists is by doing

    Code:
    ArrayList<Integer> list = (ArrayList<Integer>) getConfig().getIntegerList("Players.Player1.items")
     
    list.add(your int);
    getConfig().set("Players.Player1.items", list);
    saveConfig();
    
    This will definitely allow you to set and add to a list without overwriting anything.
     
  5. Offline

    StaticE

    SnipsRevival Ok, cool. Thanks! One more quick question lol. When I declare the ArrayList, instead of doing Players.Player1 could I just use
    Code:
    "Players." + player.getName()
    so I can actually get their real name? And if that path does not already exist, I am assuming it will be created?
     
  6. Offline

    SnipsRevival

    Yeah but make sure you also change the path of the list when you use set()
     
  7. Offline

    StaticE

    SnipsRevival Thanks:) I'm going to leave the post unsolved until I try this out, just in case :p
     
  8. Offline

    Reteckz

    To check if the path exists, just use:

    if (!(config.contains(path))){
    config.set(path, value);
    }
    get and other code stuff
     
  9. Offline

    StaticE

    Reteckz Yeah, I figured that out lol. Just curious, how would I check if a value in a list exists? I'm a huge noob when it comes to lists as you can tell, I really don't know why.
     
  10. Offline

    Reteckz

    Googled it :3

    Arrays.asList(...).contains(...)
     
  11. Offline

    SnipsRevival

    That shouldn't be necessary to check if the path already exists because if it doesn't exist, you will just be adding integers to an empty list and then setting that path as the list. If the path already exists, you would just be getting the existing integers and adding the new integer to that and then resetting the path to the new list of integers.
     
Thread Status:
Not open for further replies.

Share This Page