Search in sources :

Example 1 with DataView

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

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

the class MemoryDataView method createView.

@Override
public DataView createView(final DataQuery path) {
    Objects.requireNonNull(path, "path");
    final List<String> queryParts = path.parts();
    final int sz = queryParts.size();
    checkArgument(sz != 0, "The size of the query must be at least 1");
    final String key = queryParts.get(0);
    final DataQuery keyQuery = DataQuery.of(key);
    if (sz == 1) {
        final DataView result = new MemoryDataView(this, keyQuery, this.safety);
        this.map.put(key, result);
        return result;
    }
    final DataQuery subQuery = path.popFirst();
    DataView subView = (DataView) this.map.get(key);
    if (subView == null) {
        subView = new MemoryDataView(this.parent, keyQuery, this.safety);
        this.map.put(key, subView);
    }
    return subView.createView(subQuery);
}
Also used : DataView(org.spongepowered.api.data.persistence.DataView) DataQuery(org.spongepowered.api.data.persistence.DataQuery)

Example 3 with DataView

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

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

the class MemoryDataView method createView.

@Override
public DataView createView(final DataQuery path, final Map<?, ?> map) {
    Objects.requireNonNull(path, "path");
    final DataView section = this.createView(path);
    for (final Map.Entry<?, ?> entry : map.entrySet()) {
        if (entry.getValue() instanceof Map) {
            section.createView(DataQuery.of('.', entry.getKey().toString()), (Map<?, ?>) entry.getValue());
        } else {
            section.set(DataQuery.of('.', entry.getKey().toString()), entry.getValue());
        }
    }
    return section;
}
Also used : DataView(org.spongepowered.api.data.persistence.DataView) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 5 with DataView

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

the class MemoryDataView method get.

@Override
public Optional<Object> get(final DataQuery path) {
    Objects.requireNonNull(path, "path");
    final List<String> queryParts = path.parts();
    final int sz = queryParts.size();
    if (sz == 0) {
        return Optional.of(this);
    }
    final String key = queryParts.get(0);
    if (sz == 1) {
        final Object object = this.map.get(key);
        if (object == null) {
            return Optional.empty();
        }
        if (this.safety == org.spongepowered.api.data.persistence.DataView.SafetyMode.ALL_DATA_CLONED) {
            if (object.getClass().isArray()) {
                if (object instanceof byte[]) {
                    return Optional.of(ArrayUtils.clone((byte[]) object));
                } else if (object instanceof short[]) {
                    return Optional.of(ArrayUtils.clone((short[]) object));
                } else if (object instanceof int[]) {
                    return Optional.of(ArrayUtils.clone((int[]) object));
                } else if (object instanceof long[]) {
                    return Optional.of(ArrayUtils.clone((long[]) object));
                } else if (object instanceof float[]) {
                    return Optional.of(ArrayUtils.clone((float[]) object));
                } else if (object instanceof double[]) {
                    return Optional.of(ArrayUtils.clone((double[]) object));
                } else if (object instanceof boolean[]) {
                    return Optional.of(ArrayUtils.clone((boolean[]) object));
                } else {
                    return Optional.of(ArrayUtils.clone((Object[]) object));
                }
            }
        }
        return Optional.of(object);
    }
    final Optional<DataView> subViewOptional = this.getUnsafeView(key);
    if (!subViewOptional.isPresent()) {
        return Optional.empty();
    }
    final DataView subView = subViewOptional.get();
    return subView.get(path.popFirst());
}
Also used : DataView(org.spongepowered.api.data.persistence.DataView)

Aggregations

DataView (org.spongepowered.api.data.persistence.DataView)32 DataQuery (org.spongepowered.api.data.persistence.DataQuery)16 DataContainer (org.spongepowered.api.data.persistence.DataContainer)10 CompoundTag (net.minecraft.nbt.CompoundTag)9 ResourceKey (org.spongepowered.api.ResourceKey)8 Map (java.util.Map)6 InvalidDataException (org.spongepowered.api.data.persistence.InvalidDataException)6 Optional (java.util.Optional)5 Nullable (org.checkerframework.checker.nullness.qual.Nullable)5 ImmutableMap (com.google.common.collect.ImmutableMap)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Collection (java.util.Collection)3 DataContentUpdater (org.spongepowered.api.data.persistence.DataContentUpdater)3 DataSerializable (org.spongepowered.api.data.persistence.DataSerializable)3 DataStore (org.spongepowered.api.data.persistence.DataStore)3 DataTranslator (org.spongepowered.api.data.persistence.DataTranslator)3 Vector3i (org.spongepowered.math.vector.Vector3i)3 ImmutableList (com.google.common.collect.ImmutableList)2 TypeToken (io.leangen.geantyref.TypeToken)2