Search in sources :

Example 11 with ClientboundPackets1_16_2

use of com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.ClientboundPackets1_16_2 in project ViaVersion by ViaVersion.

the class WorldPackets method register.

public static void register(Protocol1_17To1_16_4 protocol) {
    BlockRewriter blockRewriter = new BlockRewriter(protocol, Type.POSITION1_14);
    blockRewriter.registerBlockAction(ClientboundPackets1_16_2.BLOCK_ACTION);
    blockRewriter.registerBlockChange(ClientboundPackets1_16_2.BLOCK_CHANGE);
    blockRewriter.registerVarLongMultiBlockChange(ClientboundPackets1_16_2.MULTI_BLOCK_CHANGE);
    blockRewriter.registerAcknowledgePlayerDigging(ClientboundPackets1_16_2.ACKNOWLEDGE_PLAYER_DIGGING);
    protocol.registerClientbound(ClientboundPackets1_16_2.WORLD_BORDER, null, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                // Border packet actions have been split into individual packets (the content hasn't changed)
                int type = wrapper.read(Type.VAR_INT);
                ClientboundPacketType packetType;
                switch(type) {
                    case 0:
                        packetType = ClientboundPackets1_17.WORLD_BORDER_SIZE;
                        break;
                    case 1:
                        packetType = ClientboundPackets1_17.WORLD_BORDER_LERP_SIZE;
                        break;
                    case 2:
                        packetType = ClientboundPackets1_17.WORLD_BORDER_CENTER;
                        break;
                    case 3:
                        packetType = ClientboundPackets1_17.WORLD_BORDER_INIT;
                        break;
                    case 4:
                        packetType = ClientboundPackets1_17.WORLD_BORDER_WARNING_DELAY;
                        break;
                    case 5:
                        packetType = ClientboundPackets1_17.WORLD_BORDER_WARNING_DISTANCE;
                        break;
                    default:
                        throw new IllegalArgumentException("Invalid world border type received: " + type);
                }
                wrapper.setId(packetType.getId());
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_16_2.UPDATE_LIGHT, new PacketRemapper() {

        @Override
        public void registerMap() {
            // x
            map(Type.VAR_INT);
            // y
            map(Type.VAR_INT);
            // trust edges
            map(Type.BOOLEAN);
            handler(wrapper -> {
                int skyLightMask = wrapper.read(Type.VAR_INT);
                int blockLightMask = wrapper.read(Type.VAR_INT);
                // Now all written as a representation of BitSets
                // Sky light mask
                wrapper.write(Type.LONG_ARRAY_PRIMITIVE, toBitSetLongArray(skyLightMask));
                // Block light mask
                wrapper.write(Type.LONG_ARRAY_PRIMITIVE, toBitSetLongArray(blockLightMask));
                // Empty sky light mask
                wrapper.write(Type.LONG_ARRAY_PRIMITIVE, toBitSetLongArray(wrapper.read(Type.VAR_INT)));
                // Empty block light mask
                wrapper.write(Type.LONG_ARRAY_PRIMITIVE, toBitSetLongArray(wrapper.read(Type.VAR_INT)));
                writeLightArrays(wrapper, skyLightMask);
                writeLightArrays(wrapper, blockLightMask);
            });
        }

        private void writeLightArrays(PacketWrapper wrapper, int bitMask) throws Exception {
            List<byte[]> light = new ArrayList<>();
            for (int i = 0; i < 18; i++) {
                if (isSet(bitMask, i)) {
                    light.add(wrapper.read(Type.BYTE_ARRAY_PRIMITIVE));
                }
            }
            // Now needs the length of the bytearray-array
            wrapper.write(Type.VAR_INT, light.size());
            for (byte[] bytes : light) {
                wrapper.write(Type.BYTE_ARRAY_PRIMITIVE, bytes);
            }
        }

        private long[] toBitSetLongArray(int bitmask) {
            return new long[] { bitmask };
        }

        private boolean isSet(int mask, int i) {
            return (mask & (1 << i)) != 0;
        }
    });
    protocol.registerClientbound(ClientboundPackets1_16_2.CHUNK_DATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                Chunk chunk = wrapper.read(new Chunk1_16_2Type());
                if (!chunk.isFullChunk()) {
                    // All chunks are full chunk packets now (1.16 already stopped sending non-full chunks)
                    // Construct multi block change packets instead
                    // Height map updates are lost (unless we want to fully cache and resend entire chunks)
                    // Block entities are always empty for non-full chunks in Vanilla
                    writeMultiBlockChangePacket(wrapper, chunk);
                    wrapper.cancel();
                    return;
                }
                // Normal full chunk writing
                wrapper.write(new Chunk1_17Type(chunk.getSections().length), chunk);
                // 1.17 uses a bitset for the mask
                chunk.setChunkMask(BitSet.valueOf(new long[] { chunk.getBitmask() }));
                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_2.JOIN_GAME, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Entity ID
            map(Type.INT);
            // Hardcore
            map(Type.BOOLEAN);
            // Gamemode
            map(Type.UNSIGNED_BYTE);
            // Previous Gamemode
            map(Type.BYTE);
            // World List
            map(Type.STRING_ARRAY);
            // Registry
            map(Type.NBT);
            // Current dimension
            map(Type.NBT);
            handler(wrapper -> {
                // Add new dimension fields
                CompoundTag dimensionRegistry = wrapper.get(Type.NBT, 0).get("minecraft:dimension_type");
                ListTag dimensions = dimensionRegistry.get("value");
                for (Tag dimension : dimensions) {
                    CompoundTag dimensionCompound = ((CompoundTag) dimension).get("element");
                    addNewDimensionData(dimensionCompound);
                }
                CompoundTag currentDimensionTag = wrapper.get(Type.NBT, 1);
                addNewDimensionData(currentDimensionTag);
                UserConnection user = wrapper.user();
                user.getEntityTracker(Protocol1_17To1_16_4.class).addEntity(wrapper.get(Type.INT, 0), Entity1_17Types.PLAYER);
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_16_2.RESPAWN, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                CompoundTag dimensionData = wrapper.passthrough(Type.NBT);
                addNewDimensionData(dimensionData);
            });
        }
    });
    blockRewriter.registerEffect(ClientboundPackets1_16_2.EFFECT, 1010, 2001);
}
Also used : ClientboundPackets1_17(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.ClientboundPackets1_17) ListTag(com.github.steveice10.opennbt.tag.builtin.ListTag) BlockChangeRecord1_16_2(com.viaversion.viaversion.api.minecraft.BlockChangeRecord1_16_2) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) IntTag(com.github.steveice10.opennbt.tag.builtin.IntTag) 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) ClientboundPacketType(com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Protocol1_17To1_16_4(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.Protocol1_17To1_16_4) Type(com.viaversion.viaversion.api.type.Type) Chunk1_17Type(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.types.Chunk1_17Type) List(java.util.List) Tag(com.github.steveice10.opennbt.tag.builtin.Tag) Chunk(com.viaversion.viaversion.api.minecraft.chunks.Chunk) Entity1_17Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_17Types) BlockRewriter(com.viaversion.viaversion.rewriter.BlockRewriter) Chunk1_16_2Type(com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.types.Chunk1_16_2Type) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) UserConnection(com.viaversion.viaversion.api.connection.UserConnection) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection) BitSet(java.util.BitSet) Chunk1_17Type(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.types.Chunk1_17Type) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) ClientboundPacketType(com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType) Chunk(com.viaversion.viaversion.api.minecraft.chunks.Chunk) ListTag(com.github.steveice10.opennbt.tag.builtin.ListTag) UserConnection(com.viaversion.viaversion.api.connection.UserConnection) BlockRewriter(com.viaversion.viaversion.rewriter.BlockRewriter) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) ArrayList(java.util.ArrayList) List(java.util.List) Chunk1_16_2Type(com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.types.Chunk1_16_2Type) ListTag(com.github.steveice10.opennbt.tag.builtin.ListTag) IntTag(com.github.steveice10.opennbt.tag.builtin.IntTag) Tag(com.github.steveice10.opennbt.tag.builtin.Tag) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag)

Aggregations

PacketRemapper (com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)11 Type (com.viaversion.viaversion.api.type.Type)11 ClientboundPackets1_16_2 (com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.ClientboundPackets1_16_2)11 PacketWrapper (com.viaversion.viaversion.api.protocol.packet.PacketWrapper)7 Entity1_17Types (com.viaversion.viaversion.api.minecraft.entities.Entity1_17Types)5 ServerboundPackets1_16_2 (com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.ServerboundPackets1_16_2)5 ClientboundPackets1_17 (com.viaversion.viaversion.protocols.protocol1_17to1_16_4.ClientboundPackets1_17)5 BlockChangeRecord (com.viaversion.viaversion.api.minecraft.BlockChangeRecord)4 Chunk (com.viaversion.viaversion.api.minecraft.chunks.Chunk)4 ChunkSection (com.viaversion.viaversion.api.minecraft.chunks.ChunkSection)4 Entity1_16_2Types (com.viaversion.viaversion.api.minecraft.entities.Entity1_16_2Types)4 CompoundTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag)4 Chunk1_16_2Type (com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.types.Chunk1_16_2Type)4 BlockRewriter (com.viaversion.viaversion.rewriter.BlockRewriter)4 ViaBackwards (com.viaversion.viabackwards.ViaBackwards)3 UserConnection (com.viaversion.viaversion.api.connection.UserConnection)3 RegistryType (com.viaversion.viaversion.api.minecraft.RegistryType)3 EntityType (com.viaversion.viaversion.api.minecraft.entities.EntityType)3 ClientboundPacketType (com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType)3 EntityRewriter (com.viaversion.viaversion.api.rewriter.EntityRewriter)3