ConfigSection - advansed way to use Config

Discussion in 'Nukkit discussions and help' started by fromgate, 28/4/16.

  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
Dismiss Notice
We welcome you on our site. This site is devoted to the Nukkit project and all that is connected with him. Here you can communicate, download plugins, also many other things get acquainted! Register the account right now :3
  1. fromgate

    fromgate Administrator

    Messages:
    665
    Likes Received:
    186
    Yesterday I made pull-request with Config class modifications:




    Firstly, a separate ConfigSection class was added. This class inherits the LinkedHashMap class and now it is base of Config class (previously LinkedHashMap used to store data in Config).



    Second, was added few constructors and methods, which allows to use Config without linking to the pre-defined file. Now you don't need to define file if you working with autonomous config objects or if you going to fill config with data stored in jar file.

    Updated Config is fully compatible with previous verisions of Config and SimpleConfig class, so your current plugins will not requires modification.

    Main goal of modifications - make working with configuration contained multiple objects easier.

    For example you have configuration, that contains sections "players" with sub-sections, named according to player names" — players.<имяИгрока>.mana

    Code:
    players:
    fromgate:
    mana: 100
    bob:
    mana: 10
    john:
    mana: 30
    You can directly access to mana value of know player — getConfig().getInt ("players.fromgate.mana");

    But what if you need to get all player names? Or access to all values in loop?

    Code:
     public void readPlayersMana(){
    ConfigSection section = this.getConfig().getSections("players");
    
    [pre][code]    //Get all player names (subkeys of "players" section):
        Set&lt;String&gt; names = section.getKeys(false); 
    
        // Now you can loop through all player names
        names.forEach(player -&gt;{
            int mana = ((ConfigSection) section.get(player)).getInt("mana");
            this.getLogger().info(player+"'s mana is "+mana);
        });
    
        // Or you can loop through all player-sections
    
        section.entrySet().forEach(entry -&gt;{
            int mana = ((ConfigSection) entry.getValue()).getInt("mana");
            this.getLogger().info(entry.getKey()+"'s mana is "+mana);
        });
    }
    [/code][/pre]
    ConfigSection contains all required methods, such as "set", "isSection", "isInt", "getInt", "getString", etc.
     
    Pub4Game likes this.
  2. Pub4Game

    Pub4Game Модератор всея Nukkit.ru Moderator

    Messages:
    62
    Likes Received:
    31
    Minecraft:
    Pub4Game
    Очень и очень полезная штука, спасибо!
     

Share This Page