Solved Config always clearing itself after changing a value

Discussion in 'Plugin Development' started by IlluminatiGaming, Aug 19, 2015.

Thread Status:
Not open for further replies.
  1. Hello:),
    i have a big problem.
    I want that Users can enter World Names under "Worlds" in my config.
    Then, the plugin should generate a String list and a String under every world.
    That works, but if i edit something, my config will be empty.
    Code:
        public void loadconfig() {
            FileConfiguration cfg = this.getConfig();
            cfg.options().copyDefaults(false);
            List <String> worlds = getConfig().getStringList("Worlds");
            getConfig().set("Worlds", worlds);
            for (String s : worlds) {
                String bool = getConfig().getString("Worlds." + s + ".whitelist");
                if (bool == null) bool = "false";
                getConfig().set("Worlds." + s + ".whitelist", bool);
                try {
                    List<String> wp = getConfig().getStringList("Worlds." + s + ".whitelistedplayers");
                    if (wp.size() == 0) {
                        wp.add("TESTTEST");
                    }
                    getConfig().set("Worlds." + s +".whitelistedplayers", wp);
                }
                catch (Exception ex) {
                    System.out.println(ex.getMessage());
                    List <String> wp = new ArrayList();
                    wp.add("tezt");
                    getConfig().set("Worlds." + s + ".whitelistedplayers", wp);
                }
               
            }
           
            saveConfig();
        }
    I tried setting add Defaults true, but that doenst change a thing.
    So, i have no idea what to do know.
    In the config, i want it like this:
    Code:
    Worlds:
         - testletest
               - whitelist: 'true'
               - whitelistedplayers:
                   - IlluminatiGaming
                   - testplayer
    But when i add something to the whitelistedplayers i recieve
    Worlds[]
    Code:
    public void onEnable() {
            loadconfig();
            System.out.println("[WorldControl] Started & Active!");
            try {
                for (String s : getConfig().getConfigurationSection("Worlds").getKeys(false)) {
                    Bukkit.createWorld(new WorldCreator(s));
                }
                System.out.println("[WorldControl] Custom added Worlds loaded!");   
            }
            catch (NullPointerException ex) {
                ex.printStackTrace();
                System.out.println(ex.getMessage());
            }
        }
    //That is my onEnable, it is supposed to load all Worlds that are in the config. As getStringList didnt work, i used get ConfiguartionSection
    I hope that someone can help.
    Sincerely,
    IlluminatiGaming
     
  2. Offline

    Synapz

    @IlluminatiGaming by changing a value do you mean manually going into the config and adding something, then reloading?

    Also, you showed us how you wanted it to look... When you actually have it in the config (before it gets all removed) how did it look? Did you manually type it all out?

    BTW a list cant be set a value ex boolean
     
  3. Yes, i mean typing it in the Config and reloading then.
    Code:
    Worlds:
      Schloss:
        whitelist: 'false'
        whitelistedplayers:
        - TESTTEST
    
    When i add a world like "Schloss" it generates this in the config.
    But even if i change the value of the string whitelist, and then save&reload, my config will look like this:
    Code:
    Worlds:[]
     
  4. Offline

    FunIsDangerous

    I had the same problem. Turns out it was because I was reloading the server, not stopping and re-opening it.
     
  5. Offline

    Synapz

    @illumina1337
    Here is your issue:
    These two lines start off the issue and then none of the rest of the code is even ran
    Code:
            List <String> worlds = getConfig().getStringList("Worlds");
            getConfig().set("Worlds", worlds);
    The reason for that is because if you look, Schloss is not a List item its a value with a boolean value and list value stored under it. This applies to those two lines because there is no list called Worlds, when use call getConfig#getStringList() creates an empty list because Schloss isn't a list item. Then you set the section as this empty list making the [] go there.

    So how can you solve this?
    1) You can create a real Worlds list, then use those values to load the World. So this is how the config would look:
    Code:
    # This is the worlds list so you can just use
    # getConfig().getList("Worlds");
    # Loop through each world name and load its values
    Worlds
      - default
      - kitpvp
    
    # Theres are its values u loop
    # Example: for (String s : list) config.getBoolean("World." + s + ".boolean")
    
    World:
      default:
        whitelisted: false
        whitelistedplayers
            - bob
    Or you can create one section named World, and instead of assigning a name (because when you reload/restart the server forgets everything and has no idea what the world names are) you can assign a number. For example
    Code:
    World:
      1:
        name: default
        whitelist: false
        whitelistplayers
          - bob
    This works because you can create a for loop looping through like 200 numbers and then loading all the values into a world
     
    Last edited: Aug 19, 2015
    IlluminatiGaming likes this.
  6. Thanks,
    i will try changing my code when i'm at my Computer again.
    Thank you very much, i think I did misunderstood config paths a little bit.
    Yup, tried out & used method number 1.
    You saved me :D, thank you very much!
     
    Last edited: Aug 19, 2015
    Synapz likes this.
Thread Status:
Not open for further replies.

Share This Page