SimpleConfig: New way to working with configuration

Discussion in 'Plugins for Nukkit' started by fromgate, 11/2/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

    Likes Received:
    SimpleConfig: New way to working with configuration
    Recently, inspired by idea of the Yamler plugin, I wrote a little class to use in the plugin Regions (it still under construction). This class (or rather its very early version) was published on the forum (russian).

    After some modification I found that it's a really useful and suggested to add this class into Nukkit. It was added and now you can create config using this class.
    You will find that SimpleConfig will allow to create configuration easily and of cause you can keep using Config class - nothing was broken.

    Ok, now I tell you...

    How to use SimpleConfig class
    First you must create your own configuration class that extends the SimepleConfig:

    package ru.nukkit.regions.config;
    import cn.nukkit.plugin.PluginBase;
    import cn.nukkit.utils.SimpleConfig;
    import java.util.List;
    public class RegionsConfig extends SimpleConfig {
    [pre][code]@Path (value = "claim.max-regions-per-player")
    public int maxRegionPerPlayer=5;
    @Path (value = "claim.max-claim-volume")
    public int maxClaimVolume=10000;
    @Path (value = "claim.claim-only-existing-regions")
    public boolean claimOnlyExisting = false;
    @Path (value = "claim.allow-to-claim-another-regions")
    public boolean intersectionsAllowed = false;
    public RegionsConfig(PluginBase plugin) {

    Now you we must create object of this class (inside the main class of plugin)

     private RegionsConfig cfg;
    public void onEnable(){
        // Here yous can perform this.saveResource("config.yml"); if your jar contains pre-defined config file
        this.cfg = new RegionsConfig(this); // creating config-object
        this.cfg.load(); // loading configuration from file; // saving configuration to file
        // Do anything that you want to do in onEnable() method ;)
    After loading - cfg.load() all settings from file will be assigned to fields of config object. And if you need get values you just need to use fields of the object cfg (You don't need to use getString, getBoolean, getInt...):

    player.sendMessage ("You cannot claim more than "+ cfg.maxClaimVolume + " blocks!");
    If you need to change value of config variable, you just need to assign the value to corresponding field and call method save().:

    That's all!

    Well, just in case a few notes:

    Note 1. Using another file name
    If you going to create config related to another file, you must define file name in constructor:

    public RegionsConfig(PluginBase plugin) {
    Now instead of config.yml configuration will saved to file "data.yml" (and loaded from it), located in plugins folder.

    Note 2. Few words about Path annotation
    Annotation Path defines the path that will be use to store fields of config class. When annotation Path is skipped path will be equal to field's name (but symbol "_" will be replaced with ".").

    So this two codes will provide same output in config file:

     @Path (value = "claim.volume")
    public int maxClaimVolume=10000;
     public int claim_volume=10000;

Share This Page