Create TypeToken for my class and get my class object from config?

Hello.
I want to create TypeToken for my class, to automatically receive my class object from the config.
Is it possible? If so, how?

[code] //What I have.
public static MyExampleClass getMyExampleClass1() {
Config config = MCNEVENDAAR.getPlugin().getConfig();
ConfigurationNode node = config.getNode(“my.example.path”);
String string = node.getString();
String[] splited = string.split(" ");
String param1 = splited[0];
String param2 = splited[1];
String param3 = splited[2];
MyExampleClass example = new MyExampleClass(param1, param2, param3);
return example;
}

//What I want.
public static MyExampleClass getMyExampleClass2() {
    Config config = MCNEVENDAAR.getPlugin().getConfig();
    ConfigurationNode node = config.getNode("my.example.class");
    try {
        MyExampleClass example = node.getValue(TypeToken.of(MyExampleClass.class));
        return example;
    } catch (ObjectMappingException exception) {
        //Of cource I got this exception, because TypeToken for MyExampleClass does not exists..
        //How to create (and register(?)) it???
        return null;
    }
}

public static class MyExampleClass {

    String param1;
    String param2;
    String param3;

    public MyExampleClass(String param1, String param2, String param3) {
        this.param1 = param1;
        this.param2 = param2;
        this.param3 = param3;
    }
}

[/code]

node.getOptions().getSerializers().registerType(yourToken, yourSerializer) shoud allow you to register your serializer (that must implements TypeSerializer<YourClass>)

1 Like

As an alternative to what @Yeregorix said, you can also use the @ConfigSerializable annotation.

@ConfigSerializable
public static class MyExampleClass {
    @Setting String param1;
    @Setting String param2;
    @Setting String param3;
    public MyExampleClass(String param1, String param2, String param3) {
        this.param1 = param1;
        this.param2 = param2;
        this.param3 = param3;
    }
}
2 Likes

Thanks guys I will check it as soon as I can!
Can I ask you one more question? :blush:
What is better way to check, is config node exists?
Is there an analogue to Bukkit configurationSection.isSet(“path”) ?

if (node.isVirtual()) {
1 Like

Oh, thank you again!

Hello again.
I try to register my TypeToken.
IDE dont show any errors,
but I got compilation error:

LocationSerializer code:

[code]package ua.gwm.sponge_plugin.mc_nevendaar.config.type_serilizers;

import com.google.common.reflect.TypeToken;
import ninja.leaping.configurate.ConfigurationNode;
import ninja.leaping.configurate.objectmapping.ObjectMappingException;
import ninja.leaping.configurate.objectmapping.serialize.TypeSerializer;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.world.Location;
import org.spongepowered.api.world.World;

import java.util.Optional;

public class LocationSerializer implements TypeSerializer<Location> {

@Override
public Location<World> deserialize(TypeToken<?> token, ConfigurationNode node) throws ObjectMappingException {
    if (!node.getNode("WORLD").isVirtual()) {
        throw new ObjectMappingException("World does not specified!");
    }
    if (!node.getNode("X").isVirtual()) {
        throw new ObjectMappingException("X coordinate does not specified!");
    }
    if (!node.getNode("Y").isVirtual()) {
        throw new ObjectMappingException("Y coordinate does not specified");
    }
    if (!node.getNode("Z").isVirtual()) {
        throw new ObjectMappingException("Z coordinate does not specified!");
    }
    String world_name = node.getNode("WORLD").getString();
    Optional<World> optional_world = Sponge.getServer().getWorld(world_name);
    if (!optional_world.isPresent()) {
        throw new ObjectMappingException("World \"" + world_name + "\" does not exist!");
    }
    World world = optional_world.get();
    double x = node.getNode("X").getDouble();
    double y = node.getNode("Y").getDouble();
    double z = node.getNode("Z").getDouble();
    Location<World> location = new Location<World>(world, x, y, z);
    return location;
}

@Override
public void serialize(TypeToken<?> token, Location<World> location, ConfigurationNode node) throws ObjectMappingException {
    World world = location.getExtent();
    double x = location.getX();
    double y = location.getY();
    double z = location.getZ();
    String world_name = world.getName();
    node.getNode("WORLD").setValue(world_name);
    node.getNode("X").setValue(x);
    node.getNode("Y").setValue(y);
    node.getNode("Z").setValue(z);
}

}[/code]

When you register TypeToken.of(Location.class), this isn’t valid. The TypeToken needs to be exactly the same type parameters as the TypeSerializer. So you construct the TypeToken like this: new TypeToken<Location<World>>(){}

1 Like

And again thank you :smiley: