Config file keeps being overwritten

Hey all,

I’m testing the loading, modification, and saving of .conf files. So far, it looks like my plugin is overwriting any changes i make to the .conf file, and I’m not sure where this is happening.

Here’s how I load it, with the createConfigs() method

@Plugin(.....)
public class Geology2MinecraftClassicMain implements IGeoWorldRockTransformer,IRockTransformerDebugger {
private GlobalDefaults defaults;
...
    ...
@Inject @DefaultConfig(sharedRoot = true)
private File file; 
@Inject @DefaultConfig(sharedRoot = true) 
private ConfigurationLoader<CommentedConfigurationNode> loader;

public Geology2MinecraftClassicMain() {
	PluginMain = this;
}
@Listener
public void onGamePreInitialization(GamePreInitializationEvent event) throws IOException, ObjectMappingException {
 createConfigs();
}
....
public void createConfigs() throws IOException, ObjectMappingException {
	ConfigurationNode node = loader.createEmptyNode();
	node.setValue(GlobalDefaults.type, defaults == null ? (defaults= new GlobalDefaults()) : defaults);
	loader.save(node);
}

Global Defaults is a pretty straightforward class

@ConfigSerializable
public class GlobalDefaults implements IGlobalDefaults {

public static final TypeToken&lt;GlobalDefaults&gt; type = TypeToken.of(GlobalDefaults.class);

@Setting

private List<BlockDefault> blockStateList;

public GlobalDefaults() {

         blockStateList =new BlockConfigFactory().getRocks();

 }

@Override
public List<IBlockDefaults> getBlockDefaultsList() {
	List<IBlockDefaults> interfaceList = new ArrayList<>();
	for(BlockDefault blockStateDefault : blockStateList) {
		interfaceList.add((IBlockDefaults)blockStateDefault);
	}
	return interfaceList;
}
}

All I’m doing in the config file is adding a definition to the list:

from:

 blockStateList=[
 {
    metaData {
        name=granite
    }
}
]

to:

blockStateList=[
 {
    metaData {
        name=granite
    }
},
{
    }
    metaData {
        name=notgranite
    }
},
]

The ‘notgranite’ block state is never added to the blockStateList in the GlobalDefaults object, and the modified .conf file is overwritten. What gives?

Well, you haven’t actually shared with us the code that contains ‘notgranite’.

At no point in your code that you’ve given us have you actually loaded your config file. On pre-init, you create the defaults, put them into an empty node and save those with no regard to the file on disc.

What I think you want to do is load the configs, merge the defaults into your file, then save the outcome.

I think you want something like this (from memory, may need tweaking).

Note that this is one way of doing it, I’m sure there are others that would do it a different way.

Do bear in mind that if Sponge can’t load the config, it will throw a wobbly. You may want to figure out how to handle configuration errors gracefully - exercise for the reader!

This isn’t relevant to this specific problem (he’s just wanting to give the item a name in metadata, I believe, and therefore has no bearing on this).

Ah, misread.

@pie_flavor, @dualspiral,

So I fixed it via the following:

public void loadConfig() throws IOException, ObjectMappingException {
		ConfigurationNode config =null;
		try {
			config       = loader.load();
		}
		catch (IOException e) {
			GetLog().info(e.getMessage());
			GetLog().info("creating default config files");
			config       = loader.createEmptyNode();
		}
		ConfigurationNode defaultsNode = loader.createEmptyNode();
		defaults = new GlobalDefaults();
		defaultsNode.setValue(GlobalDefaults.type, defaults);
		config.mergeValuesFrom(defaultsNode);
		defaults = config.getValue(GlobalDefaults.type);
		loader.save(config);
	}

and confirmed that the defaults variable was indeed loading my changed config files.

However, there seems to be an issue with the dependency management via the guice injectors. It seems rather than binding my edited config from file it is calling the Defaults Constructor, then returning a new instance. Not sure if this is an appropriate addendum to this thread or if I should make a new one.

ALSO not sure exactly how much code I should throw down here. What would you suggest?