Search in sources :

Example 16 with DataView

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

the class DataUpdaterDelegate method update.

@Override
public DataView update(DataView content) {
    // backup
    final DataView copied = content.copy();
    DataView updated = copied;
    for (DataContentUpdater updater : this.updaters) {
        try {
            updated = updater.update(updated);
        } catch (Exception e) {
            Exception exception = new RuntimeException("There was error attempting to update some data for the content updater:" + updater.getClass().getName() + "\nThe original data is being returned, possibly causing " + "issues later on, \nbut the original data should not be lost. Please notify the developer " + "of this exception with the stacktrace.", e);
            exception.printStackTrace();
            return copied;
        }
    }
    return updated;
}
Also used : DataView(org.spongepowered.api.data.DataView) DataContentUpdater(org.spongepowered.api.data.persistence.DataContentUpdater)

Example 17 with DataView

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

the class MemoryDataView method createView.

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

Example 18 with DataView

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

the class MemoryDataView method get.

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

Example 19 with DataView

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

the class MemoryDataView method createView.

@Override
public DataView createView(DataQuery path) {
    checkNotNull(path, "path");
    List<String> queryParts = path.getParts();
    int sz = queryParts.size();
    checkArgument(sz != 0, "The size of the query must be at least 1");
    String key = queryParts.get(0);
    DataQuery keyQuery = of(key);
    if (sz == 1) {
        DataView result = new MemoryDataView(this, keyQuery, this.safety);
        this.map.put(key, result);
        return result;
    }
    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.DataView) DataQuery(org.spongepowered.api.data.DataQuery)

Example 20 with DataView

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

the class MemoryDataView method setCollection.

@SuppressWarnings({ "unchecked", "rawtypes" })
private void setCollection(String key, Collection<?> value) {
    ImmutableList.Builder<Object> builder = ImmutableList.builder();
    @Nullable DataManager manager;
    try {
        manager = Sponge.getDataManager();
    } catch (Exception e) {
        manager = null;
    }
    for (Object object : value) {
        if (object instanceof DataSerializable) {
            builder.add(((DataSerializable) object).toContainer());
        } else if (object instanceof DataView) {
            if (this.safety == SafetyMode.ALL_DATA_CLONED || this.safety == SafetyMode.CLONED_ON_SET) {
                MemoryDataView view = new MemoryDataContainer(this.safety);
                DataView internalView = (DataView) object;
                for (Map.Entry<DataQuery, Object> entry : internalView.getValues(false).entrySet()) {
                    view.set(entry.getKey(), entry.getValue());
                }
                builder.add(view);
            } else {
                builder.add(object);
            }
        } else if (object instanceof CatalogType) {
            builder.add(((CatalogType) object).getId());
        } else if (object instanceof Map) {
            builder.add(ensureSerialization((Map) object));
        } else if (object instanceof Collection) {
            builder.add(ensureSerialization((Collection) object));
        } else {
            if (manager != null) {
                final Optional<? extends DataTranslator<?>> translatorOptional = manager.getTranslator(object.getClass());
                if (translatorOptional.isPresent()) {
                    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.DataSerializable) DataView(org.spongepowered.api.data.DataView) CatalogType(org.spongepowered.api.CatalogType) DataContainer(org.spongepowered.api.data.DataContainer) Collection(java.util.Collection) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Nullable(javax.annotation.Nullable)

Aggregations

DataView (org.spongepowered.api.data.DataView)100 DataContainer (org.spongepowered.api.data.DataContainer)30 DataQuery (org.spongepowered.api.data.DataQuery)24 ArrayList (java.util.ArrayList)21 Map (java.util.Map)17 List (java.util.List)13 Vector3i (com.flowpowered.math.vector.Vector3i)11 LanternItemStack (org.lanternpowered.server.inventory.LanternItemStack)11 ItemStack (org.spongepowered.api.item.inventory.ItemStack)11 UUID (java.util.UUID)10 Nullable (javax.annotation.Nullable)10 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)10 ImmutableList (com.google.common.collect.ImmutableList)8 IOException (java.io.IOException)8 Test (org.junit.Test)8 CatalogType (org.spongepowered.api.CatalogType)8 Path (java.nio.file.Path)7 DataTypeSerializer (org.lanternpowered.server.data.persistence.DataTypeSerializer)7 InvalidDataException (org.spongepowered.api.data.persistence.InvalidDataException)7 ImmutableMap (com.google.common.collect.ImmutableMap)6