Search in sources :

Example 1 with BlockChangeRecord1_16_2

use of com.viaversion.viaversion.api.minecraft.BlockChangeRecord1_16_2 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 2 with BlockChangeRecord1_16_2

use of com.viaversion.viaversion.api.minecraft.BlockChangeRecord1_16_2 in project ViaVersion by ViaVersion.

the class WorldPackets method writeMultiBlockChangePacket.

private static void writeMultiBlockChangePacket(PacketWrapper wrapper, Chunk chunk) throws Exception {
    long chunkPosition = (chunk.getX() & 0x3FFFFFL) << 42;
    chunkPosition |= (chunk.getZ() & 0x3FFFFFL) << 20;
    ChunkSection[] sections = chunk.getSections();
    for (int chunkY = 0; chunkY < sections.length; chunkY++) {
        ChunkSection section = sections[chunkY];
        if (section == null)
            continue;
        PacketWrapper blockChangePacket = wrapper.create(ClientboundPackets1_17.MULTI_BLOCK_CHANGE);
        blockChangePacket.write(Type.LONG, chunkPosition | (chunkY & 0xFFFFFL));
        // Suppress light updates
        blockChangePacket.write(Type.BOOLEAN, true);
        // TODO this can be optimized
        BlockChangeRecord[] blockChangeRecords = new BlockChangeRecord[4096];
        int j = 0;
        for (int x = 0; x < 16; x++) {
            for (int y = 0; y < 16; y++) {
                for (int z = 0; z < 16; z++) {
                    int blockStateId = Protocol1_17To1_16_4.MAPPINGS.getNewBlockStateId(section.getFlatBlock(x, y, z));
                    blockChangeRecords[j++] = new BlockChangeRecord1_16_2(x, y, z, blockStateId);
                }
            }
        }
        blockChangePacket.write(Type.VAR_LONG_BLOCK_CHANGE_RECORD_ARRAY, blockChangeRecords);
        blockChangePacket.send(Protocol1_17To1_16_4.class);
    }
}
Also used : BlockChangeRecord(com.viaversion.viaversion.api.minecraft.BlockChangeRecord) BlockChangeRecord1_16_2(com.viaversion.viaversion.api.minecraft.BlockChangeRecord1_16_2) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection)

Aggregations

BlockChangeRecord (com.viaversion.viaversion.api.minecraft.BlockChangeRecord)2 BlockChangeRecord1_16_2 (com.viaversion.viaversion.api.minecraft.BlockChangeRecord1_16_2)2 ChunkSection (com.viaversion.viaversion.api.minecraft.chunks.ChunkSection)2 PacketWrapper (com.viaversion.viaversion.api.protocol.packet.PacketWrapper)2 Chunk (com.viaversion.viaversion.api.minecraft.chunks.Chunk)1 Protocol (com.viaversion.viaversion.api.protocol.Protocol)1 PacketRemapper (com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)1 Type (com.viaversion.viaversion.api.type.Type)1 ClientboundPackets1_16_2 (com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.ClientboundPackets1_16_2)1 Protocol1_16_2To1_16_1 (com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.Protocol1_16_2To1_16_1)1 Chunk1_16_2Type (com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.types.Chunk1_16_2Type)1 ClientboundPackets1_16 (com.viaversion.viaversion.protocols.protocol1_16to1_15_2.ClientboundPackets1_16)1 Chunk1_16Type (com.viaversion.viaversion.protocols.protocol1_16to1_15_2.types.Chunk1_16Type)1 BlockRewriter (com.viaversion.viaversion.rewriter.BlockRewriter)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1