Solved Making new sections in config

Discussion in 'Plugin Development' started by Brixishuge, Apr 10, 2014.

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


    Hello, I haven't been working with plugins for a long time, since I'm python programmer I never actually used Bukkit's configs, instead of them, I was writing my own ones (stupid me). But now I see that it has few bugs and I can't fix them without 2-3-4 hours of coding, which is maybe waste of time, since it will be done faster using bukkits config.

    I made config, loaded it and everything, but I'm just wondering how can I make config like this:

        points: 45
        level: 4
        points: 5
        level: 2
    Default values don't need to be set by that function since I believe I can set them using this (that's what I tried and it works):

    section = pyplugin.getCfg().getConfigurationSection("main")
    all = "nickname.level"
    all2 = "nickname.points"
    section.set(all, math)
    P.S. Maybe this code looks weird to you, but I'm python coder, I understand java so I accept java explanations, so feel free to type code in java :)

    Thanks in advance.
  2. Offline


    1. getConfig().set("main.Brixishuge.points", 45);
    2. getConfig().set("main.Brixishuge.level", 4);
    3. getConfig().set("main.Brixishuge1.points", 5);
    4. getConfig().set("main.Brixishuge1.level", 2);

    The "." makes a new section.
  3. Offline


    yupie_123 Isn't that just for updating section that already exists? I want to make new one. To sum up, I want to make new player-info config "box" in format given up there everytime something happens (doesn't matter, it's event)
  4. Offline


    Hmm it should work, at least for me it does ;)
  5. Offline


    AdamQpzm yupie_123 Yes, it creates it for points and levels, but not for nickname, since I don't have second argument and I don't need it...

                  section.set("main.nickname.level", 1)
                  section.set("main.nickname.points", 0)
    That's the problem... How do I do that?


    Fixed it using section.createSection... Solved
  6. Offline


    I think this is what you want?
    1. getConfig().set("main." + player.getName() + ".level", 1 );
    2. saveConfig();

    This works perfectly for me, maybe if you post more of your code we could help more.
    But I just tested this and it works fine.
  7. Offline


  8. Offline


    There is also a long hand form which is nice in that it doesn't uses string literals except the key itself and once it's created you have a ConfigurationSection, so if your using "main." and you misspell it, that's a bug.. if you forget the "." that's a bug.

    So you could do this:
    1. String configPath = "main.nickname";
    2. ConfigurationSection nickConfig = getConfig.isConfigurationSection(configPath ) ? getConfig.getConfigurationSection(configPath ) : getConfig.createSection(configPath );

    Once you have the ConfigurationSection you have a direct link so you don't have to repeat yourself, resulting in less chance for typo bugs.

    1. nickConfig.set("level", 1);
    2. nickConfig.set("points", 0);

    ConfigurationSections can't have values. Create a name key. Using my above example do

    1. nickConfig.set("nickname", "Alshain01");

    or more appropriately, use the nickname AS the key.

    1. String nickname = "Alshain01"
    2. String configPath = "main." + nickname;
    3. ConfigurationSection nickConfig = getConfig().isConfigurationSection(configPath) ? getConfig().getConfigurationSection(configPath) : getConfig().createSection(configPath);
    4. nickConfig.set("level", 1);
    5. nickConfig.set("points", 0);

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: Jun 7, 2016
  9. Offline


    Alshain01 But It will produce nickname: Alshain01 and that's not what I want.
    I want it in format specified in thread... Nevermind, I solved it...
  10. Offline


    sorry read my edit.
    Brixishuge likes this.
  11. Offline


    Alshain01 Thanks for your effort. But I already did it on much easier way using createSection method...
Thread Status:
Not open for further replies.

Share This Page