Skip to content

Using the ConfigurationAPI

ancap-kun edited this page Feb 13, 2023 · 2 revisions

When developing any kind of major plugin, you have to work with configs. You can't hardcode anything and everything.

In vanilla bukkit, this looks pretty depressing - by pulling values directly from ConfigurationSection. It's long and inconvenient.

AncapFramework introduces a radical new way of working with frameworks - AnnotationConfig. The idea is that you simply enter variables into a class, and by the time you use an object of that class, they will magically fill in.

AnnotationConfig reads the declared variables in the class and populates them by reading the values from the config. To get AnnotationConfig to read your config and fill the variables is as follows.

First of all, you need to create the config itself. This is what the creation of the class looks like:

public class PollutedConfig {
    
    public static String myString;

    @Configure(path = "custom.path")
    public static String otherString;

    @GenericType(Double.class);
    public static List<Double> list;

}

Let's break down what the annotations mean. @Configure defines your own path to your variable. If you don't have one, AnnotationConfig will choose a path based on the variable name (for example, the variable name myString will turn into the path my.string). GenericType allows you to specify how the field is typed. That is, for List the type must be String, for List the type is Double, and so on. This is necessary because in Java you can't get the generic type in runtime. Cases with types that have more than 1 generic type are not provided (for now) - except for Map, which always has a key as a string, so you only need to specify the map value type in GenericType.

You can load data into the class as follows:

AnnotationConfig.load(YourConfig.class, section);

where YourConfig is your config and section is the configuration section from which you want to take data.

Clone this wiki locally