Search in sources :

Example 71 with DataContainer

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

the class LegacySchematicTranslator method translate.

@Override
public DataContainer translate(Schematic schematic) throws InvalidDataException {
    DataContainer data = DataContainer.createNew(DataView.SafetyMode.NO_DATA_CLONED);
    addTo(schematic, data);
    return data;
}
Also used : DataContainer(org.spongepowered.api.data.DataContainer)

Example 72 with DataContainer

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

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

the class NbtTranslator method getViewFromCompound.

private static DataContainer getViewFromCompound(NBTTagCompound compound) {
    checkNotNull(compound);
    DataContainer container = DataContainer.createNew(DataView.SafetyMode.NO_DATA_CLONED);
    NbtTranslator.getInstance().addTo(compound, container);
    return container;
}
Also used : DataContainer(org.spongepowered.api.data.DataContainer)

Example 74 with DataContainer

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

Example 75 with DataContainer

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

the class CustomDataNbtUtil method remove.

public static DataTransactionResult remove(NBTTagCompound data, Class<? extends DataManipulator<?, ?>> containerClass) {
    if (!data.hasKey(NbtDataUtil.FORGE_DATA, NbtDataUtil.TAG_COMPOUND)) {
        return DataTransactionResult.successNoData();
    }
    final NBTTagCompound forgeTag = data.getCompoundTag(NbtDataUtil.FORGE_DATA);
    if (!forgeTag.hasKey(NbtDataUtil.SPONGE_DATA, NbtDataUtil.TAG_COMPOUND)) {
        return DataTransactionResult.successNoData();
    }
    final NBTTagCompound spongeData = forgeTag.getCompoundTag(NbtDataUtil.SPONGE_DATA);
    if (!spongeData.hasKey(NbtDataUtil.CUSTOM_MANIPULATOR_TAG_LIST, NbtDataUtil.TAG_LIST)) {
        return DataTransactionResult.successNoData();
    }
    final NBTTagList dataList = spongeData.getTagList(NbtDataUtil.CUSTOM_MANIPULATOR_TAG_LIST, NbtDataUtil.TAG_COMPOUND);
    if (dataList.tagCount() == 0) {
        return DataTransactionResult.successNoData();
    }
    boolean isRemoving;
    for (int i = 0; i < dataList.tagCount(); i++) {
        final NBTTagCompound dataCompound = dataList.getCompoundTagAt(i);
        final String dataClass = dataCompound.getString(NbtDataUtil.CUSTOM_DATA_CLASS);
        if (containerClass.getName().equals(dataClass)) {
            final NBTTagCompound current = dataCompound.getCompoundTag(NbtDataUtil.CUSTOM_DATA);
            final DataContainer currentView = NbtTranslator.getInstance().translate(current);
            DataManipulator<?, ?> existing = deserialize(dataClass, currentView);
            isRemoving = existing != null;
            dataList.removeTag(i);
            if (isRemoving) {
                return DataTransactionResult.successRemove(existing.getValues());
            }
            return DataTransactionResult.successNoData();
        }
    }
    return DataTransactionResult.successNoData();
}
Also used : NBTTagList(net.minecraft.nbt.NBTTagList) DataContainer(org.spongepowered.api.data.DataContainer) NBTTagCompound(net.minecraft.nbt.NBTTagCompound)

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