Search in sources :

Example 6 with NBTOutputStream

use of com.sk89q.jnbt.NBTOutputStream in project FastAsyncWorldEdit by IntellectualSites.

the class CompressedSchematicTag method adapt.

@Override
public LZ4BlockInputStream adapt(Clipboard src) throws IOException {
    FastByteArrayOutputStream blocksOut = new FastByteArrayOutputStream();
    try (LZ4BlockOutputStream lz4out = new LZ4BlockOutputStream(blocksOut)) {
        NBTOutputStream nbtOut = new NBTOutputStream(lz4out);
        new FastSchematicWriter(nbtOut).write(getSource());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    FastByteArraysInputStream in = new FastByteArraysInputStream(blocksOut.toByteArrays());
    return new LZ4BlockInputStream(in);
}
Also used : LZ4BlockOutputStream(net.jpountz.lz4.LZ4BlockOutputStream) FastSchematicWriter(com.fastasyncworldedit.core.extent.clipboard.io.FastSchematicWriter) FastByteArrayOutputStream(com.fastasyncworldedit.core.internal.io.FastByteArrayOutputStream) FastByteArraysInputStream(com.fastasyncworldedit.core.internal.io.FastByteArraysInputStream) IOException(java.io.IOException) NBTOutputStream(com.sk89q.jnbt.NBTOutputStream) LZ4BlockInputStream(net.jpountz.lz4.LZ4BlockInputStream)

Example 7 with NBTOutputStream

use of com.sk89q.jnbt.NBTOutputStream in project FastAsyncWorldEdit by IntellectualSites.

the class FastSchematicWriter method write2.

/**
 * Writes a version 2 schematic file.
 *
 * @param clipboard The clipboard
 */
private void write2(Clipboard clipboard) throws IOException {
    Region region = clipboard.getRegion();
    BlockVector3 origin = clipboard.getOrigin();
    BlockVector3 min = region.getMinimumPoint();
    BlockVector3 offset = min.subtract(origin);
    int width = region.getWidth();
    int height = region.getHeight();
    int length = region.getLength();
    if (width > MAX_SIZE) {
        throw new IllegalArgumentException("Width of region too large for a .schematic");
    }
    if (height > MAX_SIZE) {
        throw new IllegalArgumentException("Height of region too large for a .schematic");
    }
    if (length > MAX_SIZE) {
        throw new IllegalArgumentException("Length of region too large for a .schematic");
    }
    final DataOutput rawStream = outputStream.getOutputStream();
    outputStream.writeLazyCompoundTag("Schematic", out -> {
        out.writeNamedTag("DataVersion", WorldEdit.getInstance().getPlatformManager().queryCapability(Capability.WORLD_EDITING).getDataVersion());
        out.writeNamedTag("Version", CURRENT_VERSION);
        out.writeNamedTag("Width", (short) width);
        out.writeNamedTag("Height", (short) height);
        out.writeNamedTag("Length", (short) length);
        // The Sponge format Offset refers to the 'min' points location in the world. That's our 'Origin'
        out.writeNamedTag("Offset", new int[] { min.getBlockX(), min.getBlockY(), min.getBlockZ() });
        out.writeLazyCompoundTag("Metadata", out1 -> {
            out1.writeNamedTag("WEOffsetX", offset.getBlockX());
            out1.writeNamedTag("WEOffsetY", offset.getBlockY());
            out1.writeNamedTag("WEOffsetZ", offset.getBlockZ());
            out1.writeNamedTag("FAWEVersion", Fawe.instance().getVersion().build);
        });
        ByteArrayOutputStream blocksCompressed = new ByteArrayOutputStream();
        FaweOutputStream blocksOut = new FaweOutputStream(new DataOutputStream(new LZ4BlockOutputStream(blocksCompressed)));
        ByteArrayOutputStream tilesCompressed = new ByteArrayOutputStream();
        NBTOutputStream tilesOut = new NBTOutputStream(new LZ4BlockOutputStream(tilesCompressed));
        List<Integer> paletteList = new ArrayList<>();
        char[] palette = new char[BlockTypesCache.states.length];
        Arrays.fill(palette, Character.MAX_VALUE);
        int paletteMax = 0;
        int numTiles = 0;
        Clipboard finalClipboard;
        if (clipboard instanceof BlockArrayClipboard) {
            finalClipboard = ((BlockArrayClipboard) clipboard).getParent();
        } else {
            finalClipboard = clipboard;
        }
        Iterator<BlockVector3> iterator = finalClipboard.iterator(Order.YZX);
        while (iterator.hasNext()) {
            BlockVector3 pos = iterator.next();
            BaseBlock block = pos.getFullBlock(finalClipboard);
            CompoundTag nbt = block.getNbtData();
            if (nbt != null) {
                Map<String, Tag> values = new HashMap<>(nbt.getValue());
                // Positions are kept in NBT, we don't want that.
                values.remove("x");
                values.remove("y");
                values.remove("z");
                values.put("Id", new StringTag(block.getNbtId()));
                // Remove 'id' if it exists. We want 'Id'.
                // Do this after we get "getNbtId" cos otherwise "getNbtId" doesn't work.
                // Dum.
                values.remove("id");
                values.put("Pos", new IntArrayTag(new int[] { pos.getX(), pos.getY(), pos.getZ() }));
                numTiles++;
                tilesOut.writeTagPayload(new CompoundTag(values));
            }
            int ordinal = block.getOrdinal();
            if (ordinal == 0) {
                ordinal = 1;
            }
            char value = palette[ordinal];
            if (value == Character.MAX_VALUE) {
                int size = paletteMax++;
                palette[ordinal] = value = (char) size;
                paletteList.add(ordinal);
            }
            blocksOut.writeVarInt(value);
        }
        // close
        tilesOut.close();
        blocksOut.close();
        out.writeNamedTag("PaletteMax", paletteMax);
        out.writeLazyCompoundTag("Palette", out12 -> {
            for (int i = 0; i < paletteList.size(); i++) {
                int stateOrdinal = paletteList.get(i);
                BlockState state = BlockTypesCache.states[stateOrdinal];
                out12.writeNamedTag(state.getAsString(), i);
            }
        });
        out.writeNamedTagName("BlockData", NBTConstants.TYPE_BYTE_ARRAY);
        rawStream.writeInt(blocksOut.size());
        try (LZ4BlockInputStream in = new LZ4BlockInputStream(new ByteArrayInputStream(blocksCompressed.toByteArray()))) {
            IOUtil.copy(in, rawStream);
        }
        if (numTiles != 0) {
            out.writeNamedTagName("BlockEntities", NBTConstants.TYPE_LIST);
            rawStream.write(NBTConstants.TYPE_COMPOUND);
            rawStream.writeInt(numTiles);
            try (LZ4BlockInputStream in = new LZ4BlockInputStream(new ByteArrayInputStream(tilesCompressed.toByteArray()))) {
                IOUtil.copy(in, rawStream);
            }
        } else {
            out.writeNamedEmptyList("BlockEntities");
        }
        if (finalClipboard.hasBiomes()) {
            writeBiomes(finalClipboard, out);
        }
        List<Tag> entities = new ArrayList<>();
        for (Entity entity : finalClipboard.getEntities()) {
            BaseEntity state = entity.getState();
            if (state != null) {
                Map<String, Tag> values = new HashMap<>();
                // Put NBT provided data
                CompoundTag rawTag = state.getNbtData();
                if (rawTag != null) {
                    values.putAll(rawTag.getValue());
                }
                // Store our location data, overwriting any
                values.remove("id");
                Location loc = entity.getLocation();
                if (!brokenEntities) {
                    loc = loc.setPosition(loc.add(min.toVector3()));
                }
                values.put("Id", new StringTag(state.getType().getId()));
                values.put("Pos", writeVector(loc));
                values.put("Rotation", writeRotation(entity.getLocation()));
                CompoundTag entityTag = new CompoundTag(values);
                entities.add(entityTag);
            }
        }
        if (entities.isEmpty()) {
            out.writeNamedEmptyList("Entities");
        } else {
            out.writeNamedTag("Entities", new ListTag(CompoundTag.class, entities));
        }
    });
}
Also used : StringTag(com.sk89q.jnbt.StringTag) DataOutput(java.io.DataOutput) BaseEntity(com.sk89q.worldedit.entity.BaseEntity) Entity(com.sk89q.worldedit.entity.Entity) HashMap(java.util.HashMap) DataOutputStream(java.io.DataOutputStream) ArrayList(java.util.ArrayList) BaseBlock(com.sk89q.worldedit.world.block.BaseBlock) LZ4BlockOutputStream(net.jpountz.lz4.LZ4BlockOutputStream) CompoundTag(com.sk89q.jnbt.CompoundTag) IntArrayTag(com.sk89q.jnbt.IntArrayTag) BlockArrayClipboard(com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard) BaseEntity(com.sk89q.worldedit.entity.BaseEntity) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BlockVector3(com.sk89q.worldedit.math.BlockVector3) MutableBlockVector3(com.fastasyncworldedit.core.math.MutableBlockVector3) NBTOutputStream(com.sk89q.jnbt.NBTOutputStream) LZ4BlockInputStream(net.jpountz.lz4.LZ4BlockInputStream) ListTag(com.sk89q.jnbt.ListTag) BlockState(com.sk89q.worldedit.world.block.BlockState) ByteArrayInputStream(java.io.ByteArrayInputStream) Region(com.sk89q.worldedit.regions.Region) Clipboard(com.sk89q.worldedit.extent.clipboard.Clipboard) BlockArrayClipboard(com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard) StringTag(com.sk89q.jnbt.StringTag) IntArrayTag(com.sk89q.jnbt.IntArrayTag) ListTag(com.sk89q.jnbt.ListTag) CompoundTag(com.sk89q.jnbt.CompoundTag) Tag(com.sk89q.jnbt.Tag) FaweOutputStream(com.fastasyncworldedit.core.internal.io.FaweOutputStream) Location(com.sk89q.worldedit.util.Location)

Example 8 with NBTOutputStream

use of com.sk89q.jnbt.NBTOutputStream in project FastAsyncWorldEdit by IntellectualSites.

the class FaweDelegateSchematicHandler method upload.

public void upload(final CompoundTag tag, final UUID uuid, final String file, final RunnableVal<URL> whenDone) {
    if (tag == null) {
        LOGGER.warn("Cannot save empty tag");
        if (whenDone != null) {
            TaskManager.runTask(whenDone);
        }
        return;
    }
    final CompoundTag weTag = (CompoundTag) FaweCache.INSTANCE.asTag(tag);
    SchematicHandler.upload(uuid, file, "schem", new RunnableVal<>() {

        @Override
        public void run(OutputStream output) {
            if (weTag instanceof CompressedSchematicTag) {
                Clipboard clipboard = ((CompressedSchematicTag) weTag).getSource();
                BuiltInClipboardFormat.FAST.write(output, clipboard);
            }
            try {
                try (ParallelGZIPOutputStream gzip = new ParallelGZIPOutputStream(output)) {
                    try (NBTOutputStream nos = new NBTOutputStream(gzip)) {
                        Map<String, Tag> map = weTag.getValue();
                        nos.writeNamedTag("Schematic", map.getOrDefault("Schematic", weTag));
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }, whenDone);
}
Also used : CompressedSchematicTag(com.fastasyncworldedit.core.jnbt.CompressedSchematicTag) ParallelGZIPOutputStream(org.anarres.parallelgzip.ParallelGZIPOutputStream) NBTOutputStream(com.sk89q.jnbt.NBTOutputStream) ParallelGZIPOutputStream(org.anarres.parallelgzip.ParallelGZIPOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) Clipboard(com.sk89q.worldedit.extent.clipboard.Clipboard) IOException(java.io.IOException) NBTOutputStream(com.sk89q.jnbt.NBTOutputStream) Map(java.util.Map) CompressedCompoundTag(com.fastasyncworldedit.core.jnbt.CompressedCompoundTag) CompoundTag(com.sk89q.jnbt.CompoundTag)

Example 9 with NBTOutputStream

use of com.sk89q.jnbt.NBTOutputStream in project FastAsyncWorldEdit by IntellectualSites.

the class FaweStreamChangeSet method addTileCreate.

@Override
public void addTileCreate(CompoundTag tag) {
    if (tag == null) {
        return;
    }
    blockSize++;
    try {
        NBTOutputStream nbtos = getTileCreateOS();
        nbtos.writeTag(tag);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : IOException(java.io.IOException) NBTOutputStream(com.sk89q.jnbt.NBTOutputStream)

Example 10 with NBTOutputStream

use of com.sk89q.jnbt.NBTOutputStream in project FastAsyncWorldEdit by IntellectualSites.

the class FaweStreamChangeSet method addEntityCreate.

@Override
public void addEntityCreate(CompoundTag tag) {
    if (tag == null) {
        return;
    }
    blockSize++;
    try {
        NBTOutputStream nbtos = getEntityCreateOS();
        nbtos.writeTag(tag);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : IOException(java.io.IOException) NBTOutputStream(com.sk89q.jnbt.NBTOutputStream)

Aggregations

NBTOutputStream (com.sk89q.jnbt.NBTOutputStream)12 IOException (java.io.IOException)7 FileOutputStream (java.io.FileOutputStream)6 CompoundTag (com.sk89q.jnbt.CompoundTag)3 Clipboard (com.sk89q.worldedit.extent.clipboard.Clipboard)3 LZ4BlockInputStream (net.jpountz.lz4.LZ4BlockInputStream)3 FastSchematicWriter (com.fastasyncworldedit.core.extent.clipboard.io.FastSchematicWriter)2 CompressedCompoundTag (com.fastasyncworldedit.core.jnbt.CompressedCompoundTag)2 CompressedSchematicTag (com.fastasyncworldedit.core.jnbt.CompressedSchematicTag)2 Tag (com.sk89q.jnbt.Tag)2 BufferedOutputStream (java.io.BufferedOutputStream)2 OutputStream (java.io.OutputStream)2 LZ4BlockOutputStream (net.jpountz.lz4.LZ4BlockOutputStream)2 ParallelGZIPOutputStream (org.anarres.parallelgzip.ParallelGZIPOutputStream)2 FastByteArrayOutputStream (com.fastasyncworldedit.core.internal.io.FastByteArrayOutputStream)1 FastByteArraysInputStream (com.fastasyncworldedit.core.internal.io.FastByteArraysInputStream)1 FaweOutputStream (com.fastasyncworldedit.core.internal.io.FaweOutputStream)1 MutableBlockVector3 (com.fastasyncworldedit.core.math.MutableBlockVector3)1 IntArrayTag (com.sk89q.jnbt.IntArrayTag)1 ListTag (com.sk89q.jnbt.ListTag)1