Configs

HCore allows you to easily work with configs using JSON and YAML.

Starting

For starting, you need to create config class.

Example config's class:

package your.package;

@ConfigFile(
    plugin = YourPlugin.class,
    type = ConfigurationType.JSON, // You need to provide your config type: JSON or YAML
    resource = "config.json",
    path = "plugins/yourPlugin/config.json"
)
public final class YourConfig {
    @ConfigValue(
        path = "welcome.message"
    ) /*
    If config type is json it will look like this:
    {
        welcome: {
            message: "Welcome to the server!"
        }
    }
    Yaml:
    welcome:
        message: "Welcome to the server!"
    */
    public String welcomeMessage = "Welcome to the server!";
}

Then, you need to register config to HCore.

How to:

private ConfigContainer<YourConfig> yourContainer;

// Your plugin's onEnable method
@Override
public void onEnable() {
    yourContainer = HCore.loadConfig(new YourConfig());
}

Now, you will able to get this config and get values from config's class. Example:

@EventHandler
public void join(PlayerJoinEvent event) {
    Player player = event.getPlayer();
    player.sendMessage(yourContainer.getConfig().welcomeMessage);
}

ConfigContainer#save()

Saves your config to file.

ConfigContainer#update()

Updates your config.

ConfigContainer#getConfig()

Returns your config's class. You can work with it and store values from it.

ConfigContainer#setConfig()

You can update config to new and save it.

Example:

public void doStuff() {
    YourConfig config = new YourConfig();
    config.welcomeMessage = "Welcome from new config!"; // Config will update to new with default values and provided welcome message
    yourContainer.setConfig(config);
}

ConfigContainer#getFile()

Returns config's file.

Last updated