Search in sources :

Example 16 with ChunkSection

use of com.viaversion.viaversion.api.minecraft.chunks.ChunkSection in project ViaVersion by ViaVersion.

the class Chunk1_8Type method deserialize.

// Used for normal and bulk chunks
public static Chunk deserialize(final int chunkX, final int chunkZ, final boolean fullChunk, final boolean skyLight, final int bitmask, final byte[] data) throws Exception {
    final ByteBuf input = Unpooled.wrappedBuffer(data);
    final ChunkSection[] sections = new ChunkSection[16];
    int[] biomeData = null;
    // Read blocks
    for (int i = 0; i < sections.length; i++) {
        if ((bitmask & 1 << i) == 0)
            continue;
        sections[i] = Types1_8.CHUNK_SECTION.read(input);
    }
    // Read block light
    for (int i = 0; i < sections.length; i++) {
        if ((bitmask & 1 << i) == 0)
            continue;
        sections[i].getLight().readBlockLight(input);
    }
    // Read sky light
    if (skyLight) {
        for (int i = 0; i < sections.length; i++) {
            if ((bitmask & 1 << i) == 0)
                continue;
            sections[i].getLight().readSkyLight(input);
        }
    }
    // Read biome data
    if (fullChunk) {
        biomeData = new int[256];
        for (int i = 0; i < 256; i++) {
            biomeData[i] = input.readUnsignedByte();
        }
    }
    input.release();
    return new BaseChunk(chunkX, chunkZ, fullChunk, false, bitmask, sections, biomeData, new ArrayList<>());
}
Also used : BaseChunk(com.viaversion.viaversion.api.minecraft.chunks.BaseChunk) ByteBuf(io.netty.buffer.ByteBuf) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection)

Example 17 with ChunkSection

use of com.viaversion.viaversion.api.minecraft.chunks.ChunkSection in project ViaVersion by ViaVersion.

the class ChunkBulk1_8Type method write.

@Override
public void write(ByteBuf output, ClientWorld world, Chunk[] chunks) throws Exception {
    boolean skyLight = false;
    loop1: for (Chunk c : chunks) {
        for (ChunkSection section : c.getSections()) {
            if (section != null && section.getLight().hasSkyLight()) {
                skyLight = true;
                break loop1;
            }
        }
    }
    output.writeBoolean(skyLight);
    Type.VAR_INT.writePrimitive(output, chunks.length);
    // Write metadata
    for (Chunk c : chunks) {
        output.writeInt(c.getX());
        output.writeInt(c.getZ());
        output.writeShort(c.getBitmask());
    }
    // Write data
    for (Chunk c : chunks) {
        output.writeBytes(Chunk1_8Type.serialize(c));
    }
}
Also used : Chunk(com.viaversion.viaversion.api.minecraft.chunks.Chunk) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection)

Example 18 with ChunkSection

use of com.viaversion.viaversion.api.minecraft.chunks.ChunkSection in project ViaVersion by ViaVersion.

the class Chunk1_9_3_4Type method read.

@Override
public Chunk read(ByteBuf input, ClientWorld world) throws Exception {
    int chunkX = input.readInt();
    int chunkZ = input.readInt();
    boolean fullChunk = input.readBoolean();
    int primaryBitmask = Type.VAR_INT.readPrimitive(input);
    Type.VAR_INT.readPrimitive(input);
    // Read sections
    ChunkSection[] sections = new ChunkSection[16];
    for (int i = 0; i < 16; i++) {
        // Section not set
        if ((primaryBitmask & (1 << i)) == 0)
            continue;
        ChunkSection section = Types1_9.CHUNK_SECTION.read(input);
        sections[i] = section;
        section.getLight().readBlockLight(input);
        if (world.getEnvironment() == Environment.NORMAL) {
            section.getLight().readSkyLight(input);
        }
    }
    int[] biomeData = fullChunk ? new int[256] : null;
    if (fullChunk) {
        for (int i = 0; i < 256; i++) {
            biomeData[i] = input.readByte() & 0xFF;
        }
    }
    List<CompoundTag> nbtData = new ArrayList<>(Arrays.asList(Type.NBT_ARRAY.read(input)));
    // Read all the remaining bytes (workaround for #681)
    if (input.readableBytes() > 0) {
        byte[] array = Type.REMAINING_BYTES.read(input);
        if (Via.getManager().isDebug()) {
            Via.getPlatform().getLogger().warning("Found " + array.length + " more bytes than expected while reading the chunk: " + chunkX + "/" + chunkZ);
        }
    }
    return new BaseChunk(chunkX, chunkZ, fullChunk, false, primaryBitmask, sections, biomeData, nbtData);
}
Also used : BaseChunk(com.viaversion.viaversion.api.minecraft.chunks.BaseChunk) ArrayList(java.util.ArrayList) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag)

Example 19 with ChunkSection

use of com.viaversion.viaversion.api.minecraft.chunks.ChunkSection in project ViaVersion by ViaVersion.

the class Chunk1_9_1_2Type method read.

@Override
public Chunk read(ByteBuf input, ClientWorld world) throws Exception {
    int chunkX = input.readInt();
    int chunkZ = input.readInt();
    boolean groundUp = input.readBoolean();
    int primaryBitmask = Type.VAR_INT.readPrimitive(input);
    // Size (unused)
    Type.VAR_INT.readPrimitive(input);
    BitSet usedSections = new BitSet(16);
    ChunkSection[] sections = new ChunkSection[16];
    // Calculate section count from bitmask
    for (int i = 0; i < 16; i++) {
        if ((primaryBitmask & (1 << i)) != 0) {
            usedSections.set(i);
        }
    }
    // Read sections
    for (int i = 0; i < 16; i++) {
        // Section not set
        if (!usedSections.get(i))
            continue;
        ChunkSection section = Types1_9.CHUNK_SECTION.read(input);
        sections[i] = section;
        section.getLight().readBlockLight(input);
        if (world.getEnvironment() == Environment.NORMAL) {
            section.getLight().readSkyLight(input);
        }
    }
    int[] biomeData = groundUp ? new int[256] : null;
    if (groundUp) {
        for (int i = 0; i < 256; i++) {
            biomeData[i] = input.readByte() & 0xFF;
        }
    }
    return new BaseChunk(chunkX, chunkZ, groundUp, false, primaryBitmask, sections, biomeData, new ArrayList<CompoundTag>());
}
Also used : BaseChunk(com.viaversion.viaversion.api.minecraft.chunks.BaseChunk) BitSet(java.util.BitSet) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag)

Example 20 with ChunkSection

use of com.viaversion.viaversion.api.minecraft.chunks.ChunkSection in project ViaVersion by ViaVersion.

the class Chunk1_18Type method write.

@Override
public void write(final ByteBuf buffer, final Chunk chunk) throws Exception {
    buffer.writeInt(chunk.getX());
    buffer.writeInt(chunk.getZ());
    Type.NBT.write(buffer, chunk.getHeightMap());
    final ByteBuf sectionBuffer = buffer.alloc().buffer();
    try {
        for (final ChunkSection section : chunk.getSections()) {
            sectionType.write(sectionBuffer, section);
        }
        sectionBuffer.readerIndex(0);
        Type.VAR_INT.writePrimitive(buffer, sectionBuffer.readableBytes());
        buffer.writeBytes(sectionBuffer);
    } finally {
        // release buffer
        sectionBuffer.release();
    }
    Type.VAR_INT.writePrimitive(buffer, chunk.blockEntities().size());
    for (final BlockEntity blockEntity : chunk.blockEntities()) {
        Types1_18.BLOCK_ENTITY.write(buffer, blockEntity);
    }
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection) BlockEntity(com.viaversion.viaversion.api.minecraft.blockentity.BlockEntity)

Aggregations

ChunkSection (com.viaversion.viaversion.api.minecraft.chunks.ChunkSection)48 Chunk (com.viaversion.viaversion.api.minecraft.chunks.Chunk)20 CompoundTag (com.github.steveice10.opennbt.tag.builtin.CompoundTag)19 PacketRemapper (com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)19 PacketWrapper (com.viaversion.viaversion.api.protocol.packet.PacketWrapper)18 ArrayList (java.util.ArrayList)16 Type (com.viaversion.viaversion.api.type.Type)14 ByteBuf (io.netty.buffer.ByteBuf)13 PacketHandler (com.viaversion.viaversion.api.protocol.remapper.PacketHandler)12 BlockRewriter (com.viaversion.viaversion.rewriter.BlockRewriter)12 BaseChunk (com.viaversion.viaversion.api.minecraft.chunks.BaseChunk)11 ClientWorld (com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld)10 Position (com.viaversion.viaversion.api.minecraft.Position)8 BlockChangeRecord (com.viaversion.viaversion.api.minecraft.BlockChangeRecord)7 CompoundTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag)7 List (java.util.List)7 StringTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag)6 Tag (com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag)6 StringTag (com.github.steveice10.opennbt.tag.builtin.StringTag)5 Via (com.viaversion.viaversion.api.Via)5