Search in sources :

Example 66 with DataContainer

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

the class MixinTileEntityNote method toContainer.

@Override
public DataContainer toContainer() {
    DataContainer container = super.toContainer();
    container.set(DataQueries.TILE_NOTE_ID, this.note);
    return container;
}
Also used : DataContainer(org.spongepowered.api.data.DataContainer)

Example 67 with DataContainer

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

the class MixinTileEntitySign method toContainer.

@Override
public DataContainer toContainer() {
    DataContainer container = super.toContainer();
    List<String> lines = Lists.newArrayList();
    for (ITextComponent line : this.signText) {
        lines.add(ITextComponent.Serializer.componentToJson(line));
    }
    container.set(Keys.SIGN_LINES.getQuery(), lines);
    return container;
}
Also used : DataContainer(org.spongepowered.api.data.DataContainer) ITextComponent(net.minecraft.util.text.ITextComponent)

Example 68 with DataContainer

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

the class SpongeBlockSnapshot method toContainer.

@Override
public DataContainer toContainer() {
    final DataContainer container = DataContainer.createNew().set(Queries.CONTENT_VERSION, getContentVersion()).set(Queries.WORLD_ID, this.worldUniqueId.toString()).createView(DataQueries.SNAPSHOT_WORLD_POSITION).set(Queries.POSITION_X, this.pos.getX()).set(Queries.POSITION_Y, this.pos.getY()).set(Queries.POSITION_Z, this.pos.getZ()).getContainer().set(DataQueries.BLOCK_STATE, this.blockState);
    if (this.blockState != this.extendedState) {
        container.set(DataQueries.BLOCK_EXTENDED_STATE, this.extendedState);
    }
    if (this.compound != null) {
        container.set(DataQueries.UNSAFE_NBT, NbtTranslator.getInstance().translateFrom(this.compound));
    }
    final List<DataView> dataList = DataUtil.getSerializedImmutableManipulatorList(this.extraData);
    if (!dataList.isEmpty()) {
        container.set(DataQueries.SNAPSHOT_TILE_DATA, dataList);
    }
    return container;
}
Also used : DataView(org.spongepowered.api.data.DataView) DataContainer(org.spongepowered.api.data.DataContainer)

Example 69 with DataContainer

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

the class SpongeTileEntityArchetypeBuilder method tileData.

@Override
public TileEntityArchetype.Builder tileData(DataView dataView) {
    checkNotNull(dataView, "Provided DataView cannot be null!");
    final DataContainer copy = dataView.copy();
    DataUtil.getValidators(Validations.TILE_ENTITY).validate(copy);
    this.tileData = copy;
    return this;
}
Also used : DataContainer(org.spongepowered.api.data.DataContainer)

Example 70 with DataContainer

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

the class MemoryDataView method set.

@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public DataView set(DataQuery path, Object value) {
    checkNotNull(path, "path");
    checkNotNull(value, "value");
    checkState(this.container != null);
    @Nullable DataManager manager;
    // TODO: this call to getDataManager each set can be cleaned up
    try {
        manager = Sponge.getDataManager();
    } catch (Exception e) {
        manager = null;
    }
    List<String> parts = path.getParts();
    String key = parts.get(0);
    if (parts.size() > 1) {
        DataQuery subQuery = of(key);
        Optional<DataView> subViewOptional = this.getUnsafeView(subQuery);
        DataView subView;
        if (!subViewOptional.isPresent()) {
            this.createView(subQuery);
            subView = (DataView) this.map.get(key);
        } else {
            subView = subViewOptional.get();
        }
        subView.set(path.popFirst(), value);
        return this;
    }
    if (value instanceof DataView) {
        checkArgument(value != this, "Cannot set a DataView to itself.");
        // always have to copy a data view to avoid overwriting existing
        // views and to set the interior path correctly.
        copyDataView(path, (DataView) value);
    } else if (value instanceof DataSerializable) {
        DataContainer valueContainer = ((DataSerializable) value).toContainer();
        checkArgument(!(valueContainer).equals(this), "Cannot insert self-referencing DataSerializable");
        // see above for why this is copied
        copyDataView(path, valueContainer);
    } else if (value instanceof CatalogType) {
        return set(path, ((CatalogType) value).getId());
    } else if (manager != null && manager.getTranslator(value.getClass()).isPresent()) {
        DataTranslator serializer = manager.getTranslator(value.getClass()).get();
        final DataContainer container = serializer.translate(value);
        checkArgument(!container.equals(this), "Cannot insert self-referencing Objects!");
        // see above for why this is copied
        copyDataView(path, container);
    } else if (value instanceof Collection) {
        setCollection(key, (Collection) value);
    } else if (value instanceof Map) {
        setMap(key, (Map) value);
    } else if (value.getClass().isArray()) {
        if (this.safety == SafetyMode.ALL_DATA_CLONED || this.safety == SafetyMode.CLONED_ON_SET) {
            if (value instanceof byte[]) {
                this.map.put(key, ArrayUtils.clone((byte[]) value));
            } else if (value instanceof short[]) {
                this.map.put(key, ArrayUtils.clone((short[]) value));
            } else if (value instanceof int[]) {
                this.map.put(key, ArrayUtils.clone((int[]) value));
            } else if (value instanceof long[]) {
                this.map.put(key, ArrayUtils.clone((long[]) value));
            } else if (value instanceof float[]) {
                this.map.put(key, ArrayUtils.clone((float[]) value));
            } else if (value instanceof double[]) {
                this.map.put(key, ArrayUtils.clone((double[]) value));
            } else if (value instanceof boolean[]) {
                this.map.put(key, ArrayUtils.clone((boolean[]) value));
            } else {
                this.map.put(key, ArrayUtils.clone((Object[]) value));
            }
        } else {
            this.map.put(key, value);
        }
    } else {
        this.map.put(key, value);
    }
    return this;
}
Also used : DataTranslator(org.spongepowered.api.data.persistence.DataTranslator) DataManager(org.spongepowered.api.data.DataManager) DataSerializable(org.spongepowered.api.data.DataSerializable) DataView(org.spongepowered.api.data.DataView) DataContainer(org.spongepowered.api.data.DataContainer) CatalogType(org.spongepowered.api.CatalogType) Collection(java.util.Collection) DataQuery(org.spongepowered.api.data.DataQuery) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Nullable(javax.annotation.Nullable)

Aggregations

DataContainer (org.spongepowered.api.data.DataContainer)110 DataView (org.spongepowered.api.data.DataView)30 DataQuery (org.spongepowered.api.data.DataQuery)28 Test (org.junit.Test)24 List (java.util.List)15 Map (java.util.Map)15 ArrayList (java.util.ArrayList)13 Nullable (javax.annotation.Nullable)11 Collection (java.util.Collection)10 ImmutableList (com.google.common.collect.ImmutableList)9 ImmutableMap (com.google.common.collect.ImmutableMap)9 IOException (java.io.IOException)9 Path (java.nio.file.Path)9 Optional (java.util.Optional)9 CatalogType (org.spongepowered.api.CatalogType)9 Sponge (org.spongepowered.api.Sponge)8 Set (java.util.Set)7 Collectors (java.util.stream.Collectors)7 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)7 DataSerializable (org.spongepowered.api.data.DataSerializable)7