I’m trying to save a List of Vector3i to config and i know that i need to serialize it in some way and took a look at the docs but i’m having a very hard time understanding it. If anyone can help me on this it would be very much appreciated.
You have two options:
-
Create a custom serializer for
Vector3i
. This allows the configurate (which sponge uses for configuration files) to read/write a Vector3i as a config object instead. Then you can save the list directly to the config, and get it back withConfigurationNode#getList(TypeToken.of(Vector3i.class))
. An example implementation is here and here -
Manually serialize/deserialize the object. You take in a list of
Vector3i
, and then create a list ofConfigurationNode
s based on the vector values. This is more straightforward, but will do effectively the same as the above.
I had already created a custom Serializer but my problem was actually registering it, i figured it out and for anyone in the future that wants to know how here’s an example config [code] public class Config {
private static ClaimConfig claimConfig = new ClaimConfig();
private Path configFile = //path to config file
private ConfigurationLoader configLoader = HoconConfigurationLoader.builder().setPath(configFile).build();
private CommentedConfigurationNode configNode;
private TypeToken token = new TypeToken() {};
private TypeSerializerCollection serializers = TypeSerializers.getDefaultSerializers().newChild().registerType(token, new Vector3iSerializer());
private ConfigurationOptions options = ConfigurationOptions.defaults().setSerializers(serializers);
public void setup() {
//setup config
}
public void save() {
//save config
}
public void load() {
try {
configNode = configLoader.load(options);
} catch (IOException e) {
e.printStackTrace();
}
}
public void setDefaults() {
final TypeToken<List> token = new TypeToken<List>() {};
List list = new ArrayList<>();
list.add(new Vector3i(0, 0, 0));
list.add(new Vector3i(5, 10, 20));
try {
configNode.getNode(“test”).setValue(token, list);
} catch (ObjectMappingException e) {
e.printStackTrace();
}
}
}[/code]