Hey all,
I’ve been trying to implement a configuration load/write/create subroutine for my mod. I’ve had some trouble understanding the docs, but I tried to do as much as I can.
My questions are:
- How do I get the exception IO catch to create and fill a defaults file if it determines that no file yet exists?
- What exactly does a node value mean? is it how you access the specific node in the node object/name of the node or is it the value given to that node (like int x = 1?).
- if I’m serializing a complex object (an object that contains arrays, Lists, enums, and custom objects), do I have to do the @Inject, @ConfigSerializable, and @Setting for each object within the serialized object (excluding primitives)?
- Are there any red flags you can see in how I’ve written the code so far? The information I need serialized and accessible to users doesn’t lend itself well to bare-bones tutorials i’ve found (in general).
Also I put in a lot of effort formatting this question, but if changing the formatting will make it more readable, let me know.
So far this is what I have:
@Plugin(id = "SomeName", name = "Some Longer Name", version = "1.0a")
public class SomeNameMain {
//The Variables
@Inject
@DefaultConfig(sharedRoot = true)
private File defaultConfig;
@Inject
@DefaultConfig(sharedRoot = true)
private ConfigurationLoader<CommentedConfigurationNode> configManager;
@Inject private GuiceObjectMapperFactory factory;
@Inject private ConfigurationLoader<CommentedConfigurationNode> loader
private Defaults newDefaults;
@Subscribe
public void enable(GamePreInitializationEvent event) {
File potentialFile = new File("config.conf");
ConfigurationLoader<CommentedConfigurationNode> loader =
HoconConfigurationLoader.builder().setFile(potentialFile).build();
CommentedConfigurationNode node;
try {
//Attempt to load object 'Defaults' from file. If successful, populate newDefaults
//Object.
node =loader.load(ConfigurationOptions.defaults().setObjectMapperFactory(factory));
newDefaults = node.getValue(TypeToken.of(Defaults.class));
} catch(IOException e) {
//If loading can't be done (i.e. file doesn't exist):
//1. Create empty node
//2. Create Defaults object with a special constructor that tells it to populate w/ default values
//3. Assign this new Defaults object to the node tree
//4.Save node to directory.
node = loader.createEmptyNode(ConfigurationOptions.defaults());
newDefaults = new Defaults(false);
node.setValue(newDefaults);
}
}
}
The Defaults class is:
@ConfigSerializable
public class Defaults {
@Setting(value="size", comment="The Size of This Object")
private int size;
@Setting(value="ages", comment="The Number of Ages")
private int chrons;
//Not sure how to do this:
@Setting(value="notsurethevalue", comment="The object List")
//private List<CustomComplexObject> chronval;
@Inject
public Defaults(){
// Will it auto fill the fields? or do I have to do that
//with node.etc;?
}
public Defaults(Boolean populate) {
if(populate){
fillDefaults();
}
}
private void fillDefaults() {
//fill defaults
}
}