Skip to content

Configuration

Amaury Carrade edited this page May 16, 2016 · 15 revisions

This component simplifies the management of commands with sub-commands and aliases.

Documentation JavaDoc
Loader class
(for loadComponents)
See below

The concept

The old way to manage configuration is pretty annoying. You have to access the main plugin class everytime, the data types are limited, the configuration paths are hard-coded...

Using zLib, you transform this...

final Achievement achievement;
try
{
    achievement = Achievement.valueOf(MainPluginClass.getInstance().getConfig().getString("my_section.achievement", "OPEN_INVENTORY");
}
catch (IllegalArgumentException e)
{
    achievement = Achievement.OPEN_INVENTORY;
    MainPluginClass.getInstance().getLogger().warning("Invalid achievement set in config; using default value.");
}

...into this:

final Achievement achievement = Config.MY_SECTION.ACHIEVEMENT.get();

Better, eh?

Writing the configuration class

Behind the scenes, there is a class storing the configuration scheme.
Here is the only small inconvenient of this configuration management: you have to duplicate the config in both the config.yml file and this class. But it's a small task, plus a tool exist to generate the configuration class from the config.yml (we'll talk about this later).

You'll have to write a class extending the Configuration class. Let's call this class Config.

import fr.zcraft.zlib.components.configuration.Configuration;

public final class Config extends Configuration
{

}

Then you add the configuration entries. The idea is to add a public and static field per entry, the data type being a ConfigurationItem<?>.

import fr.zcraft.zlib.components.configuration.Configuration;
import fr.zcraft.zlib.components.configuration.ConfigurationItem;

// I highly recommend to import this statically
import static fr.zcraft.zlib.components.configuration.ConfigurationItem.item;

public final class Config extends Configuration
{
    public static ConfigurationItem<String> MY_CONFIG = item("my_config", "default value");
}
Clone this wiki locally