Search in sources :

Example 31 with ChunkSection

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

the class Chunk1_14Type method read.

@Override
public Chunk read(ByteBuf input) throws Exception {
    int chunkX = input.readInt();
    int chunkZ = input.readInt();
    boolean fullChunk = input.readBoolean();
    int primaryBitmask = Type.VAR_INT.readPrimitive(input);
    CompoundTag heightMap = Type.NBT.read(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;
        short nonAirBlocksCount = input.readShort();
        ChunkSection section = Types1_13.CHUNK_SECTION.read(input);
        section.setNonAirBlocksCount(nonAirBlocksCount);
        sections[i] = section;
    }
    int[] biomeData = fullChunk ? new int[256] : null;
    if (fullChunk) {
        for (int i = 0; i < 256; i++) {
            biomeData[i] = input.readInt();
        }
    }
    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, heightMap, 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 32 with ChunkSection

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

the class WorldPackets method register.

public static void register(Protocol protocol) {
    BlockRewriter blockRewriter = new BlockRewriter(protocol, Type.POSITION1_14);
    blockRewriter.registerBlockAction(ClientboundPackets1_16.BLOCK_ACTION);
    blockRewriter.registerBlockChange(ClientboundPackets1_16.BLOCK_CHANGE);
    blockRewriter.registerAcknowledgePlayerDigging(ClientboundPackets1_16.ACKNOWLEDGE_PLAYER_DIGGING);
    protocol.registerClientbound(ClientboundPackets1_16.CHUNK_DATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                Chunk chunk = wrapper.read(new Chunk1_16Type());
                wrapper.write(new Chunk1_16_2Type(), chunk);
                for (int s = 0; s < chunk.getSections().length; s++) {
                    ChunkSection section = chunk.getSections()[s];
                    if (section == null)
                        continue;
                    for (int i = 0; i < section.getPaletteSize(); i++) {
                        int old = section.getPaletteEntry(i);
                        section.setPaletteEntry(i, protocol.getMappingData().getNewBlockStateId(old));
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_16.MULTI_BLOCK_CHANGE, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                wrapper.cancel();
                int chunkX = wrapper.read(Type.INT);
                int chunkZ = wrapper.read(Type.INT);
                long chunkPosition = 0;
                chunkPosition |= (chunkX & 0x3FFFFFL) << 42;
                chunkPosition |= (chunkZ & 0x3FFFFFL) << 20;
                List<BlockChangeRecord>[] sectionRecords = new List[16];
                BlockChangeRecord[] blockChangeRecord = wrapper.read(Type.BLOCK_CHANGE_RECORD_ARRAY);
                for (BlockChangeRecord record : blockChangeRecord) {
                    int chunkY = record.getY() >> 4;
                    List<BlockChangeRecord> list = sectionRecords[chunkY];
                    if (list == null) {
                        sectionRecords[chunkY] = (list = new ArrayList<>());
                    }
                    // Absolute y -> relative chunk section y
                    int blockId = protocol.getMappingData().getNewBlockStateId(record.getBlockId());
                    list.add(new BlockChangeRecord1_16_2(record.getSectionX(), record.getSectionY(), record.getSectionZ(), blockId));
                }
                // Now send separate packets for the different chunk sections
                for (int chunkY = 0; chunkY < sectionRecords.length; chunkY++) {
                    List<BlockChangeRecord> sectionRecord = sectionRecords[chunkY];
                    if (sectionRecord == null)
                        continue;
                    PacketWrapper newPacket = wrapper.create(ClientboundPackets1_16_2.MULTI_BLOCK_CHANGE);
                    newPacket.write(Type.LONG, chunkPosition | (chunkY & 0xFFFFFL));
                    // Ignore light updates
                    newPacket.write(Type.BOOLEAN, false);
                    newPacket.write(Type.VAR_LONG_BLOCK_CHANGE_RECORD_ARRAY, sectionRecord.toArray(EMPTY_RECORDS));
                    newPacket.send(Protocol1_16_2To1_16_1.class);
                }
            });
        }
    });
    blockRewriter.registerEffect(ClientboundPackets1_16.EFFECT, 1010, 2001);
}
Also used : Protocol1_16_2To1_16_1(com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.Protocol1_16_2To1_16_1) BlockChangeRecord1_16_2(com.viaversion.viaversion.api.minecraft.BlockChangeRecord1_16_2) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) Protocol(com.viaversion.viaversion.api.protocol.Protocol) BlockChangeRecord(com.viaversion.viaversion.api.minecraft.BlockChangeRecord) ClientboundPackets1_16_2(com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.ClientboundPackets1_16_2) ArrayList(java.util.ArrayList) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) ClientboundPackets1_16(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.ClientboundPackets1_16) Type(com.viaversion.viaversion.api.type.Type) List(java.util.List) Chunk(com.viaversion.viaversion.api.minecraft.chunks.Chunk) BlockRewriter(com.viaversion.viaversion.rewriter.BlockRewriter) Chunk1_16_2Type(com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.types.Chunk1_16_2Type) Chunk1_16Type(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.types.Chunk1_16Type) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection) Protocol1_16_2To1_16_1(com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.Protocol1_16_2To1_16_1) BlockChangeRecord1_16_2(com.viaversion.viaversion.api.minecraft.BlockChangeRecord1_16_2) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) ArrayList(java.util.ArrayList) Chunk(com.viaversion.viaversion.api.minecraft.chunks.Chunk) BlockRewriter(com.viaversion.viaversion.rewriter.BlockRewriter) BlockChangeRecord(com.viaversion.viaversion.api.minecraft.BlockChangeRecord) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) Chunk1_16Type(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.types.Chunk1_16Type) Chunk1_16_2Type(com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.types.Chunk1_16_2Type) ArrayList(java.util.ArrayList) List(java.util.List) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection)

Example 33 with ChunkSection

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

the class Chunk1_16_2Type method read.

@Override
public Chunk read(ByteBuf input) throws Exception {
    int chunkX = input.readInt();
    int chunkZ = input.readInt();
    boolean fullChunk = input.readBoolean();
    int primaryBitmask = Type.VAR_INT.readPrimitive(input);
    CompoundTag heightMap = Type.NBT.read(input);
    int[] biomeData = null;
    if (fullChunk) {
        biomeData = Type.VAR_INT_ARRAY_PRIMITIVE.read(input);
    }
    // data size in bytes
    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;
        short nonAirBlocksCount = input.readShort();
        ChunkSection section = Types1_16.CHUNK_SECTION.read(input);
        section.setNonAirBlocksCount(nonAirBlocksCount);
        sections[i] = section;
    }
    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, heightMap, 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 34 with ChunkSection

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

the class Chunk1_16_2Type method write.

@Override
public void write(ByteBuf output, Chunk chunk) throws Exception {
    output.writeInt(chunk.getX());
    output.writeInt(chunk.getZ());
    output.writeBoolean(chunk.isFullChunk());
    Type.VAR_INT.writePrimitive(output, chunk.getBitmask());
    Type.NBT.write(output, chunk.getHeightMap());
    // Write biome data
    if (chunk.isBiomeData()) {
        Type.VAR_INT_ARRAY_PRIMITIVE.write(output, chunk.getBiomeData());
    }
    ByteBuf buf = output.alloc().buffer();
    try {
        for (int i = 0; i < 16; i++) {
            ChunkSection section = chunk.getSections()[i];
            // Section not set
            if (section == null)
                continue;
            buf.writeShort(section.getNonAirBlocksCount());
            Types1_16.CHUNK_SECTION.write(buf, section);
        }
        buf.readerIndex(0);
        Type.VAR_INT.writePrimitive(output, buf.readableBytes());
        output.writeBytes(buf);
    } finally {
        // release buffer
        buf.release();
    }
    // Write Block Entities
    Type.NBT_ARRAY.write(output, chunk.getBlockEntities().toArray(EMPTY_COMPOUNDS));
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection)

Example 35 with ChunkSection

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

the class WorldPackets method register.

public static void register(Protocol1_15To1_14_4 protocol) {
    BlockRewriter blockRewriter = new BlockRewriter(protocol, Type.POSITION1_14);
    blockRewriter.registerBlockAction(ClientboundPackets1_14.BLOCK_ACTION);
    blockRewriter.registerBlockChange(ClientboundPackets1_14.BLOCK_CHANGE);
    blockRewriter.registerMultiBlockChange(ClientboundPackets1_14.MULTI_BLOCK_CHANGE);
    blockRewriter.registerAcknowledgePlayerDigging(ClientboundPackets1_14.ACKNOWLEDGE_PLAYER_DIGGING);
    protocol.registerClientbound(ClientboundPackets1_14.CHUNK_DATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    Chunk chunk = wrapper.read(new Chunk1_14Type());
                    wrapper.write(new Chunk1_15Type(), chunk);
                    if (chunk.isFullChunk()) {
                        int[] biomeData = chunk.getBiomeData();
                        int[] newBiomeData = new int[1024];
                        if (biomeData != null) {
                            // Now in 4x4x4 areas - take the biome of each "middle"
                            for (int i = 0; i < 4; ++i) {
                                for (int j = 0; j < 4; ++j) {
                                    int x = (j << 2) + 2;
                                    int z = (i << 2) + 2;
                                    int oldIndex = (z << 4 | x);
                                    newBiomeData[i << 2 | j] = biomeData[oldIndex];
                                }
                            }
                            // ... and copy it to the new y layers
                            for (int i = 1; i < 64; ++i) {
                                System.arraycopy(newBiomeData, 0, newBiomeData, i * 16, 16);
                            }
                        }
                        chunk.setBiomeData(newBiomeData);
                    }
                    for (int s = 0; s < chunk.getSections().length; s++) {
                        ChunkSection section = chunk.getSections()[s];
                        if (section == null)
                            continue;
                        for (int i = 0; i < section.getPaletteSize(); i++) {
                            int old = section.getPaletteEntry(i);
                            int newId = protocol.getMappingData().getNewBlockStateId(old);
                            section.setPaletteEntry(i, newId);
                        }
                    }
                }
            });
        }
    });
    blockRewriter.registerEffect(ClientboundPackets1_14.EFFECT, 1010, 2001);
    protocol.registerClientbound(ClientboundPackets1_14.SPAWN_PARTICLE, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Particle ID
            map(Type.INT);
            // 1 - Long Distance
            map(Type.BOOLEAN);
            // 2 - X
            map(Type.FLOAT, Type.DOUBLE);
            // 3 - Y
            map(Type.FLOAT, Type.DOUBLE);
            // 4 - Z
            map(Type.FLOAT, Type.DOUBLE);
            // 5 - Offset X
            map(Type.FLOAT);
            // 6 - Offset Y
            map(Type.FLOAT);
            // 7 - Offset Z
            map(Type.FLOAT);
            // 8 - Particle Data
            map(Type.FLOAT);
            // 9 - Particle Count
            map(Type.INT);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int id = wrapper.get(Type.INT, 0);
                    if (id == 3 || id == 23) {
                        int data = wrapper.passthrough(Type.VAR_INT);
                        wrapper.set(Type.VAR_INT, 0, protocol.getMappingData().getNewBlockStateId(data));
                    } else if (id == 32) {
                        protocol.getItemRewriter().handleItemToClient(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM));
                    }
                }
            });
        }
    });
}
Also used : BlockRewriter(com.viaversion.viaversion.rewriter.BlockRewriter) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Chunk1_15Type(com.viaversion.viaversion.protocols.protocol1_15to1_14_4.types.Chunk1_15Type) Chunk1_14Type(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.types.Chunk1_14Type) Chunk(com.viaversion.viaversion.api.minecraft.chunks.Chunk) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection)

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