Search in sources :

Example 1 with DataBuilder

use of org.spongepowered.api.data.persistence.DataBuilder in project LanternServer by LanternPowered.

the class DataSerializableTypeSerializer method deserialize.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public DataSerializable deserialize(TypeToken<?> type, ConfigurationNode value) throws ObjectMappingException {
    final DataManager dataManager = Sponge.getDataManager();
    final Optional<DataBuilder<?>> builderOpt = (Optional) dataManager.getBuilder(type.getRawType().asSubclass(DataSerializable.class));
    if (!builderOpt.isPresent()) {
        throw new ObjectMappingException("No data builder is registered for " + type);
    }
    final Optional<? extends DataSerializable> built = builderOpt.get().build(ConfigurateTranslator.instance().translate(value));
    if (!built.isPresent()) {
        throw new ObjectMappingException("Unable to build instance of " + type);
    }
    return built.get();
}
Also used : Optional(java.util.Optional) DataBuilder(org.spongepowered.api.data.persistence.DataBuilder) DataManager(org.spongepowered.api.data.DataManager) ObjectMappingException(ninja.leaping.configurate.objectmapping.ObjectMappingException)

Aggregations

Optional (java.util.Optional)1 ObjectMappingException (ninja.leaping.configurate.objectmapping.ObjectMappingException)1 DataManager (org.spongepowered.api.data.DataManager)1 DataBuilder (org.spongepowered.api.data.persistence.DataBuilder)1