Skip to content

Config Files

Jacky2611 edited this page Dec 28, 2018 · 2 revisions

Cotton comes with a simple Config File Loader. To use it in your mod you simply have to create a POJO containing all your config variables and then load it in your Mod's init method.

Creating a config POJO

For this example, we want to create a config file for a mod called Foo so that users can change the number of times bar will be printed to the console. First we create a simple POJO that has all the variables we want:

public class FooConfig {
    public int barAmount = 121;
}

Now we also must tell the config loader what name this config file will have. To do this let's add the ConfigFile annotation:

@ConfigFile(name="FooConfigFile")
public class FooConfig {
    public int barAmount = 121;
}

This config file will be saved as FooConfigFile.conf.

Adding comments

While in smaller config files it might still be obvious what each option means bigger mods might want to add comments to their config files. To accommodate this we are using the jankson json parser. This allows us to support both single line and multi line comments. The easiest way to add a comment to a property is to use the comment annotation:

@ConfigFile(name="FooConfigFile")
public class FooConfig {
    @Comment(value="Changes how often bar will be printed")
    public int barAmount = 121;
}

Loading it in your mod's init method

All you have to do in order to load a config file is to use ConfigManager.loadConfig(). In practice this looks something like this:

public class ExampleMod implements ModInitializer {
    public static FooConfig config;
    @Override
    public void onInitialize() {
        config = ConfigManager.loadConfig(FooConfig.class)
    }
}

This will attempt to load the config file and create it if it doesn't already exist.