Solved Help with adding Strings to a list in a config

Discussion in 'Plugin Development' started by jojohnson1, Nov 10, 2012.

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

    jojohnson1

    Hey Guys,
    I'm writing a plugin such as Multiverse, and the thing i'm struggling with is the adding of a new world's name to a list in a config file. This is my code:

    Code:java
    1.  
    Code:java
    1. package net.jojohnson1.mineworld.config;
    2.  
    3. import java.io.BufferedWriter;
    4. import java.io.File;
    5. import java.io.FileWriter;
    6. import org.bukkit.configuration.file.YamlConfiguration;
    7.  
    8. public class MainConfig {
    9.  
    10. private static File configFile = new File("plugins/MineWorld/config.yml");
    11. private static YamlConfiguration config = new YamlConfiguration();
    12.  
    13. public static void createConfig() {
    14.  
    15. if (!(configFile.exists())) {
    16. try {
    17.  
    18. configFile.getParentFile().mkdirs();
    19. configFile.createNewFile();
    20.  
    21. FileWriter fstream = new FileWriter(configFile);
    22. BufferedWriter out = new BufferedWriter(fstream);
    23. out.write("###############################################"); out.newLine();
    24. out.write("## Config ##"); out.newLine();
    25. out.write("###############################################"); out.newLine();
    26. out.newLine();
    27. out.close();
    28.  
    29. }catch(Exception e) {
    30. e.printStackTrace();
    31. }
    32. }
    33.  
    34. }
    35.  
    36. public static void reloadWorldConfig() {
    37. }
    38.  
    39. public static void addWorld(String worldName) {
    40.  
    41. }
    42.  
    43. }
    44. [SIZE=3][/SIZE]


    What i want to do is that when a world is created (no problem with that) the Name of the World should end up in a list with all names of all worlds created with my plugin so that i can load them automatically. So, i want to have a list like that:

    PHP:
    worldsmineworldpvphungergames
    and if you add a world the addWorld() method is called and a new Name should appear in the list.




    As i always have problems with configs, i would be really thankful if anyone could help me.

    Kind regards
    jojohnson1
     
  2. Offline

    gomeow

    What I do is get the whole list, add something, then put in back (replacing the current list)
     
  3. Offline

    jojohnson1

    Yeah, but how? I tried several things like List<String> or String[] or ArrayList<Sntrig> but none of these ways worked...
    Normally, i can get along wih plugin coding, but could you give me an example how it would work?
     
  4. Offline

    gomeow

    Code:
    ArrayList<String> list = this.getConfig().getStringList("path");
    list.add("whatever");
    this.getConfig().set("path", list);
    saveConfig();
     
    gvlfm78 likes this.
  5. Offline

    jojohnson1

    Wow thankyou, im gonna try this way as soon possible! Thanks for your quick help!
     
  6. Offline

    gomeow

    And why all the difficult code for a config.yml? Bukkit provides an API for that
     
  7. Offline

    jojohnson1

    Good question :D i need my config stuff anyway, because i want to create like a server.properties for each world, so its not just one file im talking of, but for each world a new one. As im creating a whole API for my Plugin, im also creating my own config api. As well, i can now add comments above the single booleans or strings like "this sets your default save interval" and stuff...
     
    Code0 likes this.
Thread Status:
Not open for further replies.

Share This Page