Search in sources :

Example 61 with DataView

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

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

Example 63 with DataView

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

the class MemoryDataView method remove.

@Override
public DataView remove(DataQuery path) {
    checkNotNull(path, "path");
    List<String> parts = path.getParts();
    if (parts.size() > 1) {
        String subKey = parts.get(0);
        DataQuery subQuery = of(subKey);
        Optional<DataView> subViewOptional = this.getUnsafeView(subQuery);
        if (!subViewOptional.isPresent()) {
            return this;
        }
        DataView subView = subViewOptional.get();
        subView.remove(path.popFirst());
    } else {
        this.map.remove(parts.get(0));
    }
    return this;
}
Also used : DataView(org.spongepowered.api.data.DataView) DataQuery(org.spongepowered.api.data.DataQuery)

Example 64 with DataView

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

the class LegacySchematicTranslator method addTo.

@Override
public DataView addTo(Schematic schematic, DataView data) {
    final int xMin = schematic.getBlockMin().getX();
    final int yMin = schematic.getBlockMin().getY();
    final int zMin = schematic.getBlockMin().getZ();
    final int width = schematic.getBlockSize().getX();
    final int height = schematic.getBlockSize().getY();
    final int length = schematic.getBlockSize().getZ();
    if (width > MAX_SIZE || height > MAX_SIZE || length > MAX_SIZE) {
        throw new IllegalArgumentException(String.format("Schematic is larger than maximum allowable size (found: (%d, %d, %d) max: (%d, %<d, %<d)", width, height, length, MAX_SIZE));
    }
    data.set(DataQueries.Schematic.WIDTH, width);
    data.set(DataQueries.Schematic.HEIGHT, height);
    data.set(DataQueries.Schematic.LENGTH, length);
    data.set(DataQueries.Schematic.LEGACY_MATERIALS, "Alpha");
    // These are added for better interop with WorldEdit
    data.set(DataQueries.Schematic.LEGACY_OFFSET_X, -xMin);
    data.set(DataQueries.Schematic.LEGACY_OFFSET_Y, -yMin);
    data.set(DataQueries.Schematic.LEGACY_OFFSET_Z, -zMin);
    SaveIterator itr = new SaveIterator(width, height, length);
    schematic.getBlockWorker().iterate(itr);
    byte[] blockids = itr.blockids;
    byte[] extraids = itr.extraids;
    byte[] blockdata = itr.blockdata;
    data.set(DataQueries.Schematic.LEGACY_BLOCKS, blockids);
    data.set(DataQueries.Schematic.LEGACY_BLOCK_DATA, blockdata);
    if (extraids != null) {
        data.set(DataQueries.Schematic.LEGACY_ADD_BLOCKS, extraids);
    }
    List<DataView> tileEntities = Lists.newArrayList();
    for (Map.Entry<Vector3i, TileEntityArchetype> entry : schematic.getTileEntityArchetypes().entrySet()) {
        Vector3i pos = entry.getKey();
        DataContainer tiledata = entry.getValue().getTileData();
        tiledata.set(DataQueries.X_POS, pos.getX() - xMin);
        tiledata.set(DataQueries.Y_POS, pos.getY() - yMin);
        tiledata.set(DataQueries.Z_POS, pos.getZ() - zMin);
        tileEntities.add(tiledata);
    }
    data.set(DataQueries.Schematic.LEGACY_TILEDATA, tileEntities);
    return data;
}
Also used : DataView(org.spongepowered.api.data.DataView) DataContainer(org.spongepowered.api.data.DataContainer) Vector3i(com.flowpowered.math.vector.Vector3i) TileEntityArchetype(org.spongepowered.api.block.tileentity.TileEntityArchetype) Map(java.util.Map)

Example 65 with DataView

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

the class SchematicTranslator method addTo.

@Override
public DataView addTo(Schematic schematic, DataView data) {
    final int xMin = schematic.getBlockMin().getX();
    final int yMin = schematic.getBlockMin().getY();
    final int zMin = schematic.getBlockMin().getZ();
    final int width = schematic.getBlockSize().getX();
    final int height = schematic.getBlockSize().getY();
    final int length = schematic.getBlockSize().getZ();
    if (width > MAX_SIZE || height > MAX_SIZE || length > MAX_SIZE) {
        throw new IllegalArgumentException(String.format("Schematic is larger than maximum allowable size (found: (%d, %d, %d) max: (%d, %<d, %<d)", width, height, length, MAX_SIZE));
    }
    data.set(DataQueries.Schematic.WIDTH, width);
    data.set(DataQueries.Schematic.HEIGHT, height);
    data.set(DataQueries.Schematic.LENGTH, length);
    data.set(DataQueries.Schematic.VERSION, VERSION);
    for (DataQuery metaKey : schematic.getMetadata().getKeys(false)) {
        data.set(DataQueries.Schematic.METADATA.then(metaKey), schematic.getMetadata().get(metaKey).get());
    }
    int[] offset = new int[] { -xMin, -yMin, -zMin };
    data.set(DataQueries.Schematic.OFFSET, offset);
    BlockPalette palette = schematic.getPalette();
    ByteArrayOutputStream buffer = new ByteArrayOutputStream(width * height * length);
    for (int y = 0; y < height; y++) {
        int y0 = yMin + y;
        for (int z = 0; z < length; z++) {
            int z0 = zMin + z;
            for (int x = 0; x < width; x++) {
                int x0 = xMin + x;
                BlockState state = schematic.getBlock(x0, y0, z0);
                int id = palette.getOrAssign(state);
                while ((id & -128) != 0) {
                    buffer.write(id & 127 | 128);
                    id >>>= 7;
                }
                buffer.write(id);
            }
        }
    }
    data.set(DataQueries.Schematic.BLOCK_DATA, buffer.toByteArray());
    if (palette.getType() == BlockPaletteTypes.LOCAL) {
        DataQuery paletteQuery = DataQueries.Schematic.PALETTE;
        for (BlockState state : palette.getEntries()) {
            // getOrAssign to skip the optional, it will never assign
            data.set(paletteQuery.then(state.getId()), palette.getOrAssign(state));
        }
        data.set(DataQueries.Schematic.PALETTE_MAX, palette.getHighestId());
    }
    List<DataView> tileEntities = Lists.newArrayList();
    for (Map.Entry<Vector3i, TileEntityArchetype> entry : schematic.getTileEntityArchetypes().entrySet()) {
        Vector3i pos = entry.getKey();
        DataContainer tiledata = entry.getValue().getTileData();
        int[] apos = new int[] { pos.getX() - xMin, pos.getY() - yMin, pos.getZ() - zMin };
        tiledata.set(DataQueries.Schematic.TILEENTITY_POS, apos);
        if (!tiledata.contains(DataQueries.CONTENT_VERSION)) {
            // Set a default content version of 1
            tiledata.set(DataQueries.CONTENT_VERSION, 1);
        }
        tileEntities.add(tiledata);
    }
    data.set(DataQueries.Schematic.TILEENTITY_DATA, tileEntities);
    return data;
}
Also used : BlockPalette(org.spongepowered.api.world.schematic.BlockPalette) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataView(org.spongepowered.api.data.DataView) DataContainer(org.spongepowered.api.data.DataContainer) BlockState(org.spongepowered.api.block.BlockState) Vector3i(com.flowpowered.math.vector.Vector3i) DataQuery(org.spongepowered.api.data.DataQuery) TileEntityArchetype(org.spongepowered.api.block.tileentity.TileEntityArchetype) Map(java.util.Map)

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