Search in sources :

Example 1 with DataProvider

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);
        });
    }
}
Also used : DataContainer(org.spongepowered.api.data.persistence.DataContainer) Arrays(java.util.Arrays) SpongeDataRegistrationBuilder(org.spongepowered.common.data.SpongeDataRegistrationBuilder) BiFunction(java.util.function.BiFunction) DataRegistration(org.spongepowered.api.data.DataRegistration) DataView(org.spongepowered.api.data.persistence.DataView) ImmutableDataProviderBuilder(org.spongepowered.api.data.ImmutableDataProviderBuilder) DataTransactionResult(org.spongepowered.api.data.DataTransactionResult) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Value(org.spongepowered.api.data.value.Value) SpongeDataManager(org.spongepowered.common.data.SpongeDataManager) DataManipulator(org.spongepowered.api.data.DataManipulator) DataStore(org.spongepowered.api.data.persistence.DataStore) BiConsumer(java.util.function.BiConsumer) ResourceKey(org.spongepowered.api.ResourceKey) MutableDataProviderBuilder(org.spongepowered.api.data.MutableDataProviderBuilder) DataContainerHolder(org.spongepowered.common.bridge.data.DataContainerHolder) Nullable(org.checkerframework.checker.nullness.qual.Nullable) SpongeDataStoreBuilder(org.spongepowered.common.data.persistence.datastore.SpongeDataStoreBuilder) DataContentUpdater(org.spongepowered.api.data.persistence.DataContentUpdater) SpongeDataRegistration(org.spongepowered.common.data.SpongeDataRegistration) DefaultedRegistryReference(org.spongepowered.api.registry.DefaultedRegistryReference) TypeTokenUtil(org.spongepowered.common.util.TypeTokenUtil) DataProvider(org.spongepowered.api.data.DataProvider) Sponge(org.spongepowered.api.Sponge) DataHolder(org.spongepowered.api.data.DataHolder) TypeToken(io.leangen.geantyref.TypeToken) Key(org.spongepowered.api.data.Key) Consumer(java.util.function.Consumer) CopyHelper(org.spongepowered.common.util.CopyHelper) Type(java.lang.reflect.Type) Optional(java.util.Optional) OptBool(org.spongepowered.api.util.OptBool) GenericTypeReflector(io.leangen.geantyref.GenericTypeReflector) DataContainer(org.spongepowered.api.data.persistence.DataContainer) DataContainerHolder(org.spongepowered.common.bridge.data.DataContainerHolder) DataManipulator(org.spongepowered.api.data.DataManipulator)

Example 2 with DataProvider

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);
    }
}
Also used : DataProvider(org.spongepowered.api.data.DataProvider) DataHolder(org.spongepowered.api.data.DataHolder) SimpleNBTDataHolder(org.spongepowered.common.data.holder.SimpleNBTDataHolder) Value(org.spongepowered.api.data.value.Value)

Aggregations

DataHolder (org.spongepowered.api.data.DataHolder)2 DataProvider (org.spongepowered.api.data.DataProvider)2 Value (org.spongepowered.api.data.value.Value)2 GenericTypeReflector (io.leangen.geantyref.GenericTypeReflector)1 TypeToken (io.leangen.geantyref.TypeToken)1 Type (java.lang.reflect.Type)1 Arrays (java.util.Arrays)1 Optional (java.util.Optional)1 BiConsumer (java.util.function.BiConsumer)1 BiFunction (java.util.function.BiFunction)1 Consumer (java.util.function.Consumer)1 Function (java.util.function.Function)1 Supplier (java.util.function.Supplier)1 Nullable (org.checkerframework.checker.nullness.qual.Nullable)1 ResourceKey (org.spongepowered.api.ResourceKey)1 Sponge (org.spongepowered.api.Sponge)1 DataManipulator (org.spongepowered.api.data.DataManipulator)1 DataRegistration (org.spongepowered.api.data.DataRegistration)1 DataTransactionResult (org.spongepowered.api.data.DataTransactionResult)1 ImmutableDataProviderBuilder (org.spongepowered.api.data.ImmutableDataProviderBuilder)1