Search in sources :

Example 21 with DataContainer

use of org.spongepowered.api.data.persistence.DataContainer 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 22 with DataContainer

use of org.spongepowered.api.data.persistence.DataContainer in project SpongeCommon by SpongePowered.

the class JsonDataFormat method createContainer.

private static DataContainer createContainer(JsonReader reader) throws IOException {
    DataContainer container = DataContainer.createNew(DataView.SafetyMode.NO_DATA_CLONED);
    JsonDataFormat.readView(reader, container);
    return container;
}
Also used : DataContainer(org.spongepowered.api.data.persistence.DataContainer)

Example 23 with DataContainer

use of org.spongepowered.api.data.persistence.DataContainer in project SpongeCommon by SpongePowered.

the class NBTTranslator method getViewFromCompound.

private static DataContainer getViewFromCompound(CompoundTag compound) {
    checkNotNull(compound);
    DataContainer container = DataContainer.createNew(DataView.SafetyMode.NO_DATA_CLONED);
    NBTTranslator.INSTANCE.addTo(compound, container);
    return container;
}
Also used : DataContainer(org.spongepowered.api.data.persistence.DataContainer)

Example 24 with DataContainer

use of org.spongepowered.api.data.persistence.DataContainer in project SpongeCommon by SpongePowered.

the class SpongeFluidStack method toContainer.

@Override
@NonNull
public DataContainer toContainer() {
    final ResourceKey resourceKey = Sponge.game().registry(RegistryTypes.FLUID_TYPE).valueKey(this.fluidType);
    final DataContainer container = DataContainer.createNew().set(Queries.CONTENT_VERSION, this.contentVersion()).set(Constants.Fluids.FLUID_TYPE, resourceKey).set(Constants.Fluids.FLUID_VOLUME, this.volume);
    if (this.extraData != null) {
        container.set(Constants.Sponge.UNSAFE_NBT, this.extraData);
    }
    return container;
}
Also used : DataContainer(org.spongepowered.api.data.persistence.DataContainer) ResourceKey(org.spongepowered.api.ResourceKey) NonNull(org.checkerframework.checker.nullness.qual.NonNull)

Example 25 with DataContainer

use of org.spongepowered.api.data.persistence.DataContainer in project SpongeCommon by SpongePowered.

the class SpongeFluidStackSnapshot method toContainer.

@Override
@NonNull
public DataContainer toContainer() {
    final ResourceKey resourceKey = Sponge.game().registry(RegistryTypes.FLUID_TYPE).valueKey(this.fluidType);
    final DataContainer container = DataContainer.createNew().set(Queries.CONTENT_VERSION, this.contentVersion()).set(Constants.Fluids.FLUID_TYPE, resourceKey).set(Constants.Fluids.FLUID_VOLUME, this.volume);
    if (this.extraData != null) {
        container.set(Constants.Sponge.UNSAFE_NBT, this.extraData);
    }
    return container;
}
Also used : DataContainer(org.spongepowered.api.data.persistence.DataContainer) ResourceKey(org.spongepowered.api.ResourceKey) NonNull(org.checkerframework.checker.nullness.qual.NonNull)

Aggregations

DataContainer (org.spongepowered.api.data.persistence.DataContainer)43 ResourceKey (org.spongepowered.api.ResourceKey)14 DataView (org.spongepowered.api.data.persistence.DataView)12 CompoundTag (net.minecraft.nbt.CompoundTag)9 Test (org.junit.jupiter.api.Test)9 ArrayList (java.util.ArrayList)7 DataQuery (org.spongepowered.api.data.persistence.DataQuery)7 BasicConfigurationNode (org.spongepowered.configurate.BasicConfigurationNode)7 CommentedConfigurationNode (org.spongepowered.configurate.CommentedConfigurationNode)7 ConfigurationNode (org.spongepowered.configurate.ConfigurationNode)7 Optional (java.util.Optional)6 Nullable (org.checkerframework.checker.nullness.qual.Nullable)6 Map (java.util.Map)5 NonNull (org.checkerframework.checker.nullness.qual.NonNull)5 Sponge (org.spongepowered.api.Sponge)5 DataTranslator (org.spongepowered.api.data.persistence.DataTranslator)5 ImmutableList (com.google.common.collect.ImmutableList)4 ImmutableMap (com.google.common.collect.ImmutableMap)4 Collection (java.util.Collection)4 DataManager (org.spongepowered.api.data.DataManager)4