Search in sources :

Example 1 with IntValueReader

use of com.fastasyncworldedit.core.jnbt.streamer.IntValueReader in project FastAsyncWorldEdit by IntellectualSites.

the class FastSchematicWriter method writeBiomes.

private void writeBiomes(Clipboard clipboard, NBTOutputStream out) throws IOException {
    ByteArrayOutputStream biomesCompressed = new ByteArrayOutputStream();
    DataOutputStream biomesOut = new DataOutputStream(new LZ4BlockOutputStream(biomesCompressed));
    List<Integer> paletteList = new ArrayList<>();
    int[] palette = new int[BiomeTypes.getMaxId() + 1];
    Arrays.fill(palette, Integer.MAX_VALUE);
    int[] paletteMax = { 0 };
    IntValueReader task = new IntValueReader() {

        @Override
        public void applyInt(int index, int ordinal) {
            try {
                int value = palette[ordinal];
                if (value == Integer.MAX_VALUE) {
                    int size = paletteMax[0]++;
                    palette[ordinal] = value = size;
                    paletteList.add(ordinal);
                }
                IOUtil.writeVarInt(biomesOut, value);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    };
    BlockVector3 min = clipboard.getMinimumPoint();
    int width = clipboard.getRegion().getWidth();
    int length = clipboard.getRegion().getLength();
    MutableBlockVector3 mutable = new MutableBlockVector3();
    for (int z = 0, i = 0; z < length; z++) {
        int z0 = min.getBlockZ() + z;
        for (int x = 0; x < width; x++, i++) {
            int x0 = min.getBlockX() + x;
            BiomeType biome = clipboard.getBiome(mutable.setComponents(x0, min.getY(), z0));
            task.applyInt(i, biome.getInternalId());
        }
    }
    biomesOut.close();
    out.writeNamedTag("BiomePaletteMax", paletteMax[0]);
    out.writeLazyCompoundTag("BiomePalette", out12 -> {
        for (int i = 0; i < paletteList.size(); i++) {
            int ordinal = paletteList.get(i);
            BiomeType state = BiomeTypes.get(ordinal);
            out12.writeNamedTag(state.getId(), i);
        }
    });
    out.writeNamedTagName("BiomeData", NBTConstants.TYPE_BYTE_ARRAY);
    out.writeInt(biomesOut.size());
    try (LZ4BlockInputStream in = new LZ4BlockInputStream(new ByteArrayInputStream(biomesCompressed.toByteArray()))) {
        IOUtil.copy(in, (DataOutput) out);
    }
}
Also used : DataOutputStream(java.io.DataOutputStream) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) BlockVector3(com.sk89q.worldedit.math.BlockVector3) MutableBlockVector3(com.fastasyncworldedit.core.math.MutableBlockVector3) LZ4BlockInputStream(net.jpountz.lz4.LZ4BlockInputStream) BiomeType(com.sk89q.worldedit.world.biome.BiomeType) LZ4BlockOutputStream(net.jpountz.lz4.LZ4BlockOutputStream) MutableBlockVector3(com.fastasyncworldedit.core.math.MutableBlockVector3) ByteArrayInputStream(java.io.ByteArrayInputStream) IntValueReader(com.fastasyncworldedit.core.jnbt.streamer.IntValueReader)

Aggregations

IntValueReader (com.fastasyncworldedit.core.jnbt.streamer.IntValueReader)1 MutableBlockVector3 (com.fastasyncworldedit.core.math.MutableBlockVector3)1 BlockVector3 (com.sk89q.worldedit.math.BlockVector3)1 BiomeType (com.sk89q.worldedit.world.biome.BiomeType)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataOutputStream (java.io.DataOutputStream)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 LZ4BlockInputStream (net.jpountz.lz4.LZ4BlockInputStream)1 LZ4BlockOutputStream (net.jpountz.lz4.LZ4BlockOutputStream)1