Saving ArrayLists in config

Discussion in 'Spigot Plugin Development' started by Zarlen, Apr 28, 2017.

  1. Hey!

    I'm developing a minigame and I putted all the players in an ArrayList.
    You know when I restart/reload the server or a player rejoins the ArrayLists will be emtpy again.

    How do I save it in the config? I'm experienced with coding but not with config files, I don't even know how to make a config file. Can someone help me to save the ArrayLists in the config? And how to create a config file.
     
  2. Have you even tried to google...


    Took me a minute...
    https://www.spigotmc.org/threads/how-to-save-an-arraylist-of-players-to-a-config.51031/
    https://bukkit.org/threads/how-to-save-arraylists-using-config-yml.254514/
    https://www.spigotmc.org/wiki/creating-a-config-file/
     
    • Agree Agree x 1
  3. We are not here to spoonfeed you.. Youtube, Google etc contains tons of tutorials, docs and stuff. Don't be a lazy guy and work for it..
     
  4. I don't want to be spoonfeeded, but I just need someone to explain.
     
    • Like Like x 1
  5. What don't you understand on using youtube, google and those links I provided... We don't talk in a baby language and make posts with over 500 words to explain something. Man up and work for it. Use google, youtube, read those docs. Try it.
     
  6. I did, but no one explains how to do this:
    Someone in the server gers putted in an ArrayList.
    And it gets saved instantly (at least before the server reloads/restarts or the player leaves).
     
  7. And like this format:

    {playername}: {arraylistname}
     
    • Optimistic Optimistic x 1
  8. Can you explain how to make it like
    {arraylistname} : {playername}
     
  9. Eehm, maybe do the path as:
    "Players."+ p.getuniculdid + "List";

    you might correct it :d i write it on my phone
     
  10. And where do I have to put config.set(path, stringlist);?
     
  11. On the part when you want to save your arraylist to your config
     
  12. Is it okay to create the onDisable function and put it in there? Because if the server reloads/restarts it gets trough the onDisable function
     
  13. It depends on what you're storing, but not usually a good idea to put them in onDisable() or onEnable().
     
  14. onEnable to store the players in the ArrayList from the config.
    onDisable to store the players in the config from the ArrayList
     
  15. Okay, can someone tell me how to set/save the config like this
    This is an example!
    testarraylist: playerone, playertwo, playerthree.
     
  16. That's an array.
    a List is saved to config like so:
    Code (Text):
    ListName:
    - player1
    - player2
    - player3
    You can simply use MemorySection#set(String, Object) (object being your List instance) function do do that; Spigot'll take care of the rest.

    So, as an example, say we have a List of 3 Strings: 'string1', 'string2' and 'string3':
    Code (Java):
    List<String> list = Arrays.asList("string1", "string2", "string3");
    Now, if we saved this to the config like so:
    Code (Java):
    config.set("MyList", list);
    .. our config would look like this:
    Code (Text):
    MyList:
    - string1
    - string2
    - string3
    Now, we can also add 'paths', and with Yaml, the separator is a dot. Say we want to have a List called 'MyList' located as follows;
    Code (Text):
    Examples:
      Lists:
        MyList:
        - string1
        - string2
        - string3
    Now, there are 2 'sections': 'Examples' and 'Lists' inside the Examples -section. Since the sections are separated by a dot, we can save our list right there as follows:
    Code (Java):
    config.set("Examples.Lists.MyList", list);
    Hope this helps :l
     
    • Useful Useful x 2
    • Winner Winner x 1