Skip to content

Using the built in ConfigLoader

Selena Rose Hammond edited this page Dec 1, 2023 · 2 revisions

The config loader

The config loader was originally uploaded to Spigot by uksspy and has been edited by me for a better experience. You can use the ConfigLoader to create a JSON config file from a Java class, and populate the class variables from the JSON to load any changes.

First you should create an enum to store the mapped classes in memory

Though it is not required and There is defiantly other ways to do this, I have found this to be the best method for myself

Example of the Configs enum

public enum Configs {

    LANG(Lang.class, "config", "Lang.json"),
    CONFIG(Config.class, "", "Config.json");


    private final File file;

    private final Class<?> clazz;

    <T> Configs(Class<T> clazz, String parent, String path) {
        this.clazz = clazz;
        file = FileManager.file(parent, path);
    }


    // Used for getting the populated config class
    @SuppressWarnings("unchecked")
    public <T> T getConfig() {
        return (T) FileManager.loadFile(clazz, file);
    }
}

Then an example for the Lang

public class Lang {

    public String NO_PERMISSION = "&cInvalid permissions",
            NOT_ENOUGH_ARGUMENTS = "&cNot enough arguments";
    public String Title = "<#A5A5A5>Enchanted book: %enchant_rarity_color%%enchant_name% %enchant_level%";

    public List<String> Lore = new ArrayList<String>() {{
       add("<#A5A5A5>A mysterious book that holds many powers");
       add("");
       add("<#A5A5A5>About:");
       add("<#A5A5A5>Tier: %enchant_rarity_color%%enchant_tier_name%");
       add("<#A5A5A5>Max level: %enchant_rarity_color%%enchant_max_level%");
       add("<#A5A5A5>Enchants: %enchant_rarity_color%%enchant_can_enchant%");
       add("<#A5A5A5>Stackable: %enchant_stackable%");
       add("");
       add("<#A5A5A5>Description:");
       add("<#A5A5A5>%enchant_description%");
    }};


    public static Lang get() {
        return Configs.LANG.getConfig();
    }
}

The generated outcome once it has been converted should look something like this

{
  "NO_PERMISSION": "&cInvalid permissions",
  "NOT_ENOUGH_ARGUMENTS": "&cNot enough arguments",
  "Title": "<#A5A5A5>Enchanted book: %enchant_rarity_color%%enchant_name% %enchant_level%",
  "Lore": [
    "<#A5A5A5>A mysterious book that holds many powers",
    "",
    "<#A5A5A5>About:",
    "<#A5A5A5>Tier: %enchant_rarity_color%%enchant_tier_name%",
    "<#A5A5A5>Max level: %enchant_rarity_color%%enchant_max_level%",
    "<#A5A5A5>Enchants: %enchant_rarity_color%%enchant_can_enchant%",
    "<#A5A5A5>Stackable: %enchant_stackable%",
    "",
    "<#A5A5A5>Description:",
    "<#A5A5A5>About: %enchant_description%"
  ]
}

Loading & Saving the config

If you are using the Enum method, you will want to place the following inside your onEnable method EnumSet.allOf(Configs.class).forEach(Configs::getConfig); This will loop through each of the Configs in the enum and load them