Search in sources :

Example 6 with DataManager

use of org.spongepowered.api.data.DataManager 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

DataManager (org.spongepowered.api.data.DataManager)6 DataContainer (org.spongepowered.api.data.DataContainer)3 DataView (org.spongepowered.api.data.DataView)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 Collection (java.util.Collection)2 Map (java.util.Map)2 Nullable (javax.annotation.Nullable)2 CatalogType (org.spongepowered.api.CatalogType)2 DataSerializable (org.spongepowered.api.data.DataSerializable)2 DataTranslator (org.spongepowered.api.data.persistence.DataTranslator)2 ImmutableList (com.google.common.collect.ImmutableList)1 Optional (java.util.Optional)1 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)1 ObjectMappingException (ninja.leaping.configurate.objectmapping.ObjectMappingException)1 Test (org.junit.Test)1 DataQuery (org.spongepowered.api.data.DataQuery)1 DataBuilder (org.spongepowered.api.data.persistence.DataBuilder)1 Listener (org.spongepowered.api.event.Listener)1 ImmutableFriendsData (org.spongepowered.test.myhomes.data.friends.ImmutableFriendsData)1 FriendsDataBuilder (org.spongepowered.test.myhomes.data.friends.impl.FriendsDataBuilder)1