Copy new config settings to existing config file

I’m using the asset api to load a default config file the firt time the server runs or when there’s no config file for my plugin. Everything works, except i can’t figure out how to update the existing config is there is one. Imagine you run the server and then a new version of the plugin got released with new settings in the config file. How would you put these new settings in the existing configuration file wihtout deleting it (so preventing whatever value the user has changed)? :slight_smile:

How i would do it is get all the keys with values from the internal config (as in the one you are copying from) - this can be done though the configurationLoader provided by Sponge). After that check that the key is there in the external config (the config you are coping to) - this is done through the ‘has’ function). If it is there then do nothing, else get the value from the internal config and set it to the external.

Im not giving you code as I dont want to “spoonfeed” you

Edit:
Another way would be to copy all the values from the external one, delete it, set the external as the internal and then set all the copied values back.

Better to create a node with default values and use mergeValuesFrom on the current config’s node - https://github.com/SpongePowered/configurate/blob/master/configurate-core/src/main/java/ninja/leaping/configurate/ConfigurationNode.java#L581

1 Like

That is what i was going to do, but seems like there is a cleaner solution from dualspiral

Thank you :smiley: I’ve ended up doing this and it worked very well :slight_smile:

private CommentedConfigurationNode load(String fileName){
        try {
            String configFileName = fileName + ".conf";
            File configFile = new File(plugin.getConfigDirectory().toFile(), configFileName);
            ConfigurationLoader<CommentedConfigurationNode> configLoader = HoconConfigurationLoader.builder().setFile(configFile).build();
            Optional<Asset> asset = plugin.getPluginContainer().getAsset(configFileName);
            if(asset.isPresent()){
                if(!configFile.exists()) {
                    asset.get().copyToFile(configFile.toPath());
                }
                else {
                    CommentedConfigurationNode newNode = HoconConfigurationLoader.builder().setURL(asset.get().getUrl()).build().load();
                    CommentedConfigurationNode oldNode = configLoader.load();
                    oldNode.mergeValuesFrom(newNode);
                    configLoader.save(oldNode);
                }
                return configLoader.load(ConfigurationOptions.defaults().setShouldCopyDefaults(true));
            }
        }
        catch (IOException e) {
            LogUtils.log(Level.WARN, "An error occurred while loading the " + fileName + " file!");
        }
        return null;
    }

Fun fact: i copied/pasted this code from IntelliJ and i had tou se Ctrl+Shift+V to make it look good, otherwise i got tons of &32; where is a space O_o