Config values won't update when changed

Hello! I have a config file for my plugin and all the default values are loaded from the default config correctly but when someone changes the values in the config, they aren’t changed in the code.
Here is the code:

@Inject
@DefaultConfig(sharedRoot = false)
private ConfigurationLoader<CommentedConfigurationNode> configLoader;

@Inject
@ConfigDir(sharedRoot = false)
private Path privateConfigDir;

@Inject
@DefaultConfig(sharedRoot = false)
private Path defaultConfig;

private ConfigurationNode rootDefNode;

private boolean lagEnabled;
public static int clearInterval;

@Listener
public void onServerStart(GameStartedServerEvent event) {
    initConfig();
    lagEnabled=rootDefNode.getNode("clearlagEnabled").getBoolean();
    clearInterval=rootDefNode.getNode("clearInterval").getInt();

}
public void initConfig() {
    if (!defaultConfig.toFile().exists()) {
        try {
            plugin.getAsset("core.conf").orElse(null).copyToFile(defaultConfig);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    try {
        rootDefNode = configLoader.load();
        lagEnabled=rootDefNode.getNode("clearlagEnabled").getBoolean();
        clearInterval=rootDefNode.getNode("clearInterval").getInt();
        System.out.println(lagEnabled );
        System.out.println(clearInterval);

    } catch (IOException e) {
        System.out.println("Config doesnt exist");
    }
}

Here is the config file
{
//ClearLag
clearlagEnabled=true
clearInterval=3 //Do not set under 3. Time units in Minutes
}

I think you may need to detect if someone has updated the file and then reload your confignode to get new values.

I can suggest you trying Watch Service which is in the java.nio.file package.

Configs do not update live. It is recommended for you to listen to the GameReloadEvent, and reload your configs then. This event is triggered by the command sponge plugins reload.

1 Like