Search in sources :

Example 1 with DataContainer

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

the class MemoryDataView method setCollection.

@SuppressWarnings({ "unchecked", "rawtypes" })
private void setCollection(final String key, final Collection<?> value) {
    final ImmutableList.Builder<Object> builder = ImmutableList.builder();
    @Nullable DataManager manager;
    try {
        manager = Sponge.dataManager();
    } catch (final Exception e) {
        manager = null;
    }
    for (final Object object : value) {
        if (object instanceof DataSerializable) {
            builder.add(((DataSerializable) object).toContainer());
        } else if (object instanceof DataView) {
            if (this.safety == org.spongepowered.api.data.persistence.DataView.SafetyMode.ALL_DATA_CLONED || this.safety == org.spongepowered.api.data.persistence.DataView.SafetyMode.CLONED_ON_SET) {
                final MemoryDataView view = new MemoryDataContainer(this.safety);
                final DataView internalView = (DataView) object;
                for (final Map.Entry<DataQuery, Object> entry : internalView.values(false).entrySet()) {
                    view.set(entry.getKey(), entry.getValue());
                }
                builder.add(view);
            } else {
                builder.add(object);
            }
        } else if (object instanceof ResourceKey) {
            builder.add(object.toString());
        } else if (object instanceof Map) {
            builder.add(this.ensureSerialization((Map) object));
        } else if (object instanceof Collection) {
            builder.add(this.ensureSerialization((Collection) object));
        } else {
            if (manager != null) {
                final Optional<? extends DataTranslator<?>> translatorOptional = manager.translator(object.getClass());
                if (translatorOptional.isPresent()) {
                    final DataTranslator translator = translatorOptional.get();
                    final DataContainer container = translator.translate(object);
                    checkArgument(!container.equals(this), "Cannot insert self-referencing Objects!");
                    builder.add(container);
                } else {
                    builder.add(object);
                }
            } else {
                builder.add(object);
            }
        }
    }
    this.map.put(key, builder.build());
}
Also used : DataTranslator(org.spongepowered.api.data.persistence.DataTranslator) ImmutableList(com.google.common.collect.ImmutableList) DataManager(org.spongepowered.api.data.DataManager) DataSerializable(org.spongepowered.api.data.persistence.DataSerializable) ResourceKey(org.spongepowered.api.ResourceKey) DataView(org.spongepowered.api.data.persistence.DataView) DataContainer(org.spongepowered.api.data.persistence.DataContainer) Collection(java.util.Collection) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 2 with DataContainer

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

the class MemoryDataView method copy.

@Override
public DataContainer copy() {
    final DataContainer container = new MemoryDataContainer(this.safety);
    this.keys(false).forEach(query -> this.get(query).ifPresent(obj -> container.set(query, obj)));
    return container;
}
Also used : DataManager(org.spongepowered.api.data.DataManager) DataContainer(org.spongepowered.api.data.persistence.DataContainer) Arrays(java.util.Arrays) RegistryHolder(org.spongepowered.api.registry.RegistryHolder) KeyProvider(org.spongepowered.common.registry.provider.KeyProvider) ArrayUtils(org.apache.commons.lang3.ArrayUtils) DataView(org.spongepowered.api.data.persistence.DataView) Supplier(java.util.function.Supplier) Value(org.spongepowered.api.data.value.Value) ArrayList(java.util.ArrayList) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) DataQuery(org.spongepowered.api.data.persistence.DataQuery) Map(java.util.Map) ResourceKey(org.spongepowered.api.ResourceKey) Nullable(org.checkerframework.checker.nullness.qual.Nullable) ImmutableSet(com.google.common.collect.ImmutableSet) DataSerializable(org.spongepowered.api.data.persistence.DataSerializable) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) MoreObjects(com.google.common.base.MoreObjects) Sponge(org.spongepowered.api.Sponge) Set(java.util.Set) RegistryType(org.spongepowered.api.registry.RegistryType) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) Key(org.spongepowered.api.data.Key) Preconditions.checkState(com.google.common.base.Preconditions.checkState) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) Coerce(org.spongepowered.api.util.Coerce) DataTranslator(org.spongepowered.api.data.persistence.DataTranslator) Optional(java.util.Optional) DataContainer(org.spongepowered.api.data.persistence.DataContainer)

Example 3 with DataContainer

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

the class DataUtil method serializeSpongeData.

public static <T extends SpongeDataHolderBridge & DataCompoundHolder> boolean serializeSpongeData(final T dataHolder) {
    CompoundTag compound = dataHolder.data$getCompound();
    if (compound == null) {
        compound = new CompoundTag();
        dataHolder.data$setCompound(compound);
    }
    // Remove all previous SpongeData
    compound.remove(Constants.Sponge.Data.V3.SPONGE_DATA_ROOT.asString("."));
    final DataContainer allData = NBTTranslator.INSTANCE.translate(compound);
    // Clear old custom data root
    final DataView customDataRoot = allData.createView(Constants.Sponge.Data.V3.SPONGE_DATA_ROOT);
    // Add back failed data
    dataHolder.bridge$getFailedData().forEach(customDataRoot::set);
    final DataManipulator.Mutable manipulator = dataHolder.bridge$getManipulator();
    final Type dataHolderType = dataHolder.getClass();
    manipulator.getKeys().stream().map(key -> SpongeDataManager.getDatastoreRegistry().getDataStore(key, dataHolderType)).forEach(dataStore -> dataStore.serialize(manipulator, allData));
    // If data is still present after cleanup merge it back into nbt
    if (DataUtil.cleanupEmptySpongeData(allData)) {
        compound.merge(NBTTranslator.INSTANCE.translate(allData));
    }
    if (compound.isEmpty()) {
        dataHolder.data$setCompound(null);
        return false;
    }
    return true;
}
Also used : DataContainer(org.spongepowered.api.data.persistence.DataContainer) Constants(org.spongepowered.common.util.Constants) SpongeDataHolderBridge(org.spongepowered.common.bridge.data.SpongeDataHolderBridge) DataHolder(org.spongepowered.api.data.DataHolder) DataView(org.spongepowered.api.data.persistence.DataView) SpongeCommon(org.spongepowered.common.SpongeCommon) Queries(org.spongepowered.api.data.persistence.Queries) NBTTranslator(org.spongepowered.common.data.persistence.NBTTranslator) DataManipulator(org.spongepowered.api.data.DataManipulator) DataStore(org.spongepowered.api.data.persistence.DataStore) CompoundTag(net.minecraft.nbt.CompoundTag) DataCompoundHolder(org.spongepowered.common.bridge.data.DataCompoundHolder) DataQuery(org.spongepowered.api.data.persistence.DataQuery) Type(java.lang.reflect.Type) ResourceKey(org.spongepowered.api.ResourceKey) Optional(java.util.Optional) Collections(java.util.Collections) SpongeDataStore(org.spongepowered.common.data.persistence.datastore.SpongeDataStore) DataView(org.spongepowered.api.data.persistence.DataView) DataContainer(org.spongepowered.api.data.persistence.DataContainer) Type(java.lang.reflect.Type) DataManipulator(org.spongepowered.api.data.DataManipulator) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 4 with DataContainer

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

the class SpongeItemStackSnapshot method toContainer.

@Override
public DataContainer toContainer() {
    final ResourceKey resourceKey = Sponge.game().registry(RegistryTypes.ITEM_TYPE).valueKey(this.itemType);
    final DataContainer container = DataContainer.createNew().set(Queries.CONTENT_VERSION, this.contentVersion()).set(Constants.ItemStack.TYPE, resourceKey).set(Constants.ItemStack.COUNT, this.quantity).set(Constants.ItemStack.DAMAGE_VALUE, this.damageValue);
    if (!this.manipulators.isEmpty()) {
    // TODO       container.set(Constants.Sponge.DATA_MANIPULATORS, DataUtil.getSerializedImmutableManipulatorList(this.manipulators));
    }
    if (this.compound != null) {
        container.set(Constants.Sponge.UNSAFE_NBT, NBTTranslator.INSTANCE.translateFrom(this.compound));
    }
    return container;
}
Also used : DataContainer(org.spongepowered.api.data.persistence.DataContainer) ResourceKey(org.spongepowered.api.ResourceKey)

Example 5 with DataContainer

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

the class ItemStackMixin_API method toContainer.

@Override
public DataContainer toContainer() {
    final ResourceKey key = (ResourceKey) (Object) Registry.ITEM.getKey((Item) this.itemStack$type());
    final DataContainer container = DataContainer.createNew().set(Queries.CONTENT_VERSION, this.contentVersion()).set(Constants.ItemStack.TYPE, key).set(Constants.ItemStack.COUNT, this.itemStack$quantity()).set(Constants.ItemStack.DAMAGE_VALUE, this.shadow$getDamageValue());
    if (this.shadow$hasTag()) {
        // no tag? no data, simple as that.
        final CompoundTag compound = this.shadow$getTag().copy();
        if (compound.contains(Constants.Sponge.Data.V2.SPONGE_DATA)) {
            final CompoundTag spongeCompound = compound.getCompound(Constants.Sponge.Data.V2.SPONGE_DATA);
            if (spongeCompound.contains(Constants.Sponge.Data.V2.CUSTOM_MANIPULATOR_TAG_LIST)) {
                spongeCompound.remove(Constants.Sponge.Data.V2.CUSTOM_MANIPULATOR_TAG_LIST);
            }
        }
        // We must filter the custom data so it isn't stored twice
        Constants.NBT.filterSpongeCustomData(compound);
        if (!compound.isEmpty()) {
            final DataContainer unsafeNbt = NBTTranslator.INSTANCE.translateFrom(compound);
            container.set(Constants.Sponge.UNSAFE_NBT, unsafeNbt);
        }
    }
    try {
        PlatformHooks.INSTANCE.getItemHooks().writeItemStackCapabilitiesToDataView(container, (net.minecraft.world.item.ItemStack) (Object) this);
    } catch (final Exception e) {
        e.printStackTrace();
    }
    return container;
}
Also used : DataContainer(org.spongepowered.api.data.persistence.DataContainer) CompoundTag(net.minecraft.nbt.CompoundTag) InvalidDataException(org.spongepowered.api.data.persistence.InvalidDataException) ResourceKey(org.spongepowered.api.ResourceKey)

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