Using the Config

Reading the config

To read the config you first call

Config.ConfigLoader.loadConfig(ExampleConfig.class);

Then to read data you can call

System.out.println("Max Health: " + ExampleConfig.maxHealth);

All together you get

package dev.xdpxi.examplemod;

import dev.xdpxi.examplemod.config.ExampleConfig;
import dev.xdpxi.xdlib.api.Config;
import net.fabricmc.api.ModInitializer;

public class ExampleMod implements ModInitializer {
    public static final String MOD_ID = "examplemod";

    @Override
    public void onInitialize() {
        try {
            // Load the configuration
            Config.ConfigLoader.loadConfig(ExampleConfig.class);
            System.out.println("Configuration loaded!");

            // Access config values
            System.out.println("Max Health: " + ExampleConfig.maxHealth);
            System.out.println("Enable Feature: " + ExampleConfig.enableFeature);
            System.out.println("Welcome Message: " + ExampleConfig.welcomeMessage);

        } catch (Exception e) {
            System.err.println("Failed to load configuration!");
            e.printStackTrace();
        }

        // Example usage of configuration values
        if (ExampleConfig.enableFeature) {
            System.out.println("Feature is enabled!");
        }
    }
}

Writing to the config

To write to the config you can call

ExampleConfig.maxHealth = 40;
ExampleConfig.enableFeature = false;

To save it you call

Config.ConfigLoader.saveConfig(ExampleConfig.class, "config/examplemod/example.json");

Last updated