SimpleConfig: New way to working with configuration

fromgate

Administrator
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:

Java:
package ru.nukkit.regions.config;
 
import cn.nukkit.plugin.PluginBase;
import cn.nukkit.utils.SimpleConfig;
 
import java.util.List;
 
public class RegionsConfig extends SimpleConfig {
 
	@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) {
		super(plugin);
	}
}
Now you we must create object of this class (inside the main class of plugin)

Java:
	private RegionsConfig cfg;
 
	@Override
	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
		this.cfg.save(); // 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...):

Java:
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().:

Java:
cfg.intersectionsAllowed=true;
cfg.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:

Java:
	public RegionsConfig(PluginBase plugin) {
		super(plugin,"data.yml");
	}
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:

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