use of org.spongepowered.common.bridge.data.DataContainerHolder in project SpongeCommon by SpongePowered.
the class DataProviderRegistrator method registerDataStoreDelegatingProvider.
public <H extends DataHolder, K, V extends Value<K>> void registerDataStoreDelegatingProvider(final Key<V> key, final Type typeToken) {
// Create dataprovider for mutable and immutable DataContainerHolders
if (GenericTypeReflector.isSuperType(DataProviderRegistrator.MUTABLE, typeToken)) {
this.asMutable(GenericTypeReflector.erase(typeToken)).create(key).get(holder -> {
final DataContainer dataContainer = ((DataContainerHolder) holder).data$getDataContainer();
return SpongeDataManager.getDatastoreRegistry().getDataStore(key, typeToken).deserialize(dataContainer).get(key).orElse(null);
}).set((holder, v) -> {
final DataContainer dataContainer = ((DataContainerHolder) holder).data$getDataContainer();
final DataManipulator.Mutable manipulator = DataManipulator.mutableOf();
manipulator.set(key, v);
SpongeDataManager.getDatastoreRegistry().getDataStore(key, typeToken).serialize(manipulator, dataContainer);
((DataContainerHolder.Mutable) holder).data$setDataContainer(dataContainer);
});
} else if (GenericTypeReflector.isSuperType(DataProviderRegistrator.IMMUTABLE, typeToken)) {
this.asImmutable((Class<? super H>) GenericTypeReflector.erase(typeToken)).create(key).get(holder -> {
final DataContainer dataContainer = ((DataContainerHolder) holder).data$getDataContainer();
return SpongeDataManager.getDatastoreRegistry().getDataStore(key, typeToken).deserialize(dataContainer).get(key).orElse(null);
}).set((holder, v) -> {
final DataContainer dataContainer = ((DataContainerHolder) holder).data$getDataContainer();
final DataManipulator.Mutable manipulator = DataManipulator.mutableOf();
manipulator.set(key, v);
SpongeDataManager.getDatastoreRegistry().getDataStore(key, typeToken).serialize(manipulator, dataContainer);
return (H) ((DataContainerHolder.Immutable) holder).data$withDataContainer(dataContainer);
});
}
}
Aggregations