use of org.spongepowered.api.data.DataProvider 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);
});
}
}
use of org.spongepowered.api.data.DataProvider in project SpongeCommon by SpongePowered.
the class SpongeDataHolderMixin method bridge$mergeDeserialized.
@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public void bridge$mergeDeserialized(final DataManipulator.Mutable manipulator) {
if (this.impl$manipulator == null) {
this.impl$manipulator = DataManipulator.mutableOf();
}
if (this instanceof DataHolder.Mutable && !(this instanceof org.spongepowered.api.item.inventory.ItemStack)) {
// Does not work when adding ItemStacks to inventory because the Item may be empty (see Inventory#addResource)
for (final Value.Immutable<?> value : manipulator.getValues()) {
final DataProvider provider = SpongeDataManager.getProviderRegistry().getProvider(value.key(), this.getClass());
provider.offerValue((DataHolder.Mutable) this, value);
}
} else {
this.impl$manipulator.copyFrom(manipulator);
}
}
Aggregations