Search in sources :

Example 6 with ClientboundPackets1_16_2

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

the class BlockItemPackets1_16_2 method registerPackets.

@Override
protected void registerPackets() {
    BlockRewriter blockRewriter = new BlockRewriter(protocol, Type.POSITION1_14);
    new RecipeRewriter1_16(protocol).registerDefaultHandler(ClientboundPackets1_16_2.DECLARE_RECIPES);
    registerSetCooldown(ClientboundPackets1_16_2.COOLDOWN);
    registerWindowItems(ClientboundPackets1_16_2.WINDOW_ITEMS, Type.FLAT_VAR_INT_ITEM_ARRAY);
    registerSetSlot(ClientboundPackets1_16_2.SET_SLOT, Type.FLAT_VAR_INT_ITEM);
    registerEntityEquipmentArray(ClientboundPackets1_16_2.ENTITY_EQUIPMENT, Type.FLAT_VAR_INT_ITEM);
    registerTradeList(ClientboundPackets1_16_2.TRADE_LIST, Type.FLAT_VAR_INT_ITEM);
    registerAdvancements(ClientboundPackets1_16_2.ADVANCEMENTS, Type.FLAT_VAR_INT_ITEM);
    protocol.registerClientbound(ClientboundPackets1_16_2.UNLOCK_RECIPES, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                wrapper.passthrough(Type.VAR_INT);
                // Open
                wrapper.passthrough(Type.BOOLEAN);
                // Filter
                wrapper.passthrough(Type.BOOLEAN);
                // Furnace Open
                wrapper.passthrough(Type.BOOLEAN);
                // Filter furnace
                wrapper.passthrough(Type.BOOLEAN);
                // Blast furnace / smoker
                wrapper.read(Type.BOOLEAN);
                wrapper.read(Type.BOOLEAN);
                wrapper.read(Type.BOOLEAN);
                wrapper.read(Type.BOOLEAN);
            });
        }
    });
    blockRewriter.registerAcknowledgePlayerDigging(ClientboundPackets1_16_2.ACKNOWLEDGE_PLAYER_DIGGING);
    blockRewriter.registerBlockAction(ClientboundPackets1_16_2.BLOCK_ACTION);
    blockRewriter.registerBlockChange(ClientboundPackets1_16_2.BLOCK_CHANGE);
    protocol.registerClientbound(ClientboundPackets1_16_2.CHUNK_DATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                Chunk chunk = wrapper.read(new Chunk1_16_2Type());
                wrapper.write(new Chunk1_16Type(), chunk);
                chunk.setIgnoreOldLightData(true);
                for (int i = 0; i < chunk.getSections().length; i++) {
                    ChunkSection section = chunk.getSections()[i];
                    if (section == null)
                        continue;
                    for (int j = 0; j < section.getPaletteSize(); j++) {
                        int old = section.getPaletteEntry(j);
                        section.setPaletteEntry(j, protocol.getMappingData().getNewBlockStateId(old));
                    }
                }
                for (CompoundTag blockEntity : chunk.getBlockEntities()) {
                    if (blockEntity != null) {
                        handleBlockEntity(blockEntity);
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_16_2.BLOCK_ENTITY_DATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.POSITION1_14);
            map(Type.UNSIGNED_BYTE);
            handler(wrapper -> {
                handleBlockEntity(wrapper.passthrough(Type.NBT));
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_16_2.MULTI_BLOCK_CHANGE, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                long chunkPosition = wrapper.read(Type.LONG);
                // Ignore old light data
                wrapper.read(Type.BOOLEAN);
                int chunkX = (int) (chunkPosition >> 42);
                int chunkY = (int) (chunkPosition << 44 >> 44);
                int chunkZ = (int) (chunkPosition << 22 >> 42);
                wrapper.write(Type.INT, chunkX);
                wrapper.write(Type.INT, chunkZ);
                BlockChangeRecord[] blockChangeRecord = wrapper.read(Type.VAR_LONG_BLOCK_CHANGE_RECORD_ARRAY);
                wrapper.write(Type.BLOCK_CHANGE_RECORD_ARRAY, blockChangeRecord);
                for (int i = 0; i < blockChangeRecord.length; i++) {
                    BlockChangeRecord record = blockChangeRecord[i];
                    int blockId = protocol.getMappingData().getNewBlockStateId(record.getBlockId());
                    // Relative y -> absolute y
                    blockChangeRecord[i] = new BlockChangeRecord1_8(record.getSectionX(), record.getY(chunkY), record.getSectionZ(), blockId);
                }
            });
        }
    });
    blockRewriter.registerEffect(ClientboundPackets1_16_2.EFFECT, 1010, 2001);
    registerSpawnParticle(ClientboundPackets1_16_2.SPAWN_PARTICLE, Type.FLAT_VAR_INT_ITEM, Type.DOUBLE);
    registerClickWindow(ServerboundPackets1_16.CLICK_WINDOW, Type.FLAT_VAR_INT_ITEM);
    registerCreativeInvAction(ServerboundPackets1_16.CREATIVE_INVENTORY_ACTION, Type.FLAT_VAR_INT_ITEM);
    protocol.registerServerbound(ServerboundPackets1_16.EDIT_BOOK, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> handleItemToServer(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)));
        }
    });
}
Also used : BlockChangeRecord1_8(com.viaversion.viaversion.api.minecraft.BlockChangeRecord1_8) Tag(com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag) StringTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag) ListTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag) BlockChangeRecord(com.viaversion.viaversion.api.minecraft.BlockChangeRecord) ClientboundPackets1_16_2(com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.ClientboundPackets1_16_2) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) ServerboundPackets1_16(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.ServerboundPackets1_16) 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) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag) Protocol1_16_1To1_16_2(com.viaversion.viabackwards.protocol.protocol1_16_1to1_16_2.Protocol1_16_1To1_16_2) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection) IntArrayTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.IntArrayTag) RecipeRewriter1_16(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.data.RecipeRewriter1_16) BlockChangeRecord1_8(com.viaversion.viaversion.api.minecraft.BlockChangeRecord1_8) BlockRewriter(com.viaversion.viaversion.rewriter.BlockRewriter) BlockChangeRecord(com.viaversion.viaversion.api.minecraft.BlockChangeRecord) RecipeRewriter1_16(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.data.RecipeRewriter1_16) Chunk1_16Type(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.types.Chunk1_16Type) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Chunk1_16_2Type(com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.types.Chunk1_16_2Type) Chunk(com.viaversion.viaversion.api.minecraft.chunks.Chunk) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag)

Example 7 with ClientboundPackets1_16_2

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

the class EntityPackets1_16_2 method registerPackets.

@Override
protected void registerPackets() {
    registerTrackerWithData(ClientboundPackets1_16_2.SPAWN_ENTITY, Entity1_16_2Types.FALLING_BLOCK);
    registerSpawnTracker(ClientboundPackets1_16_2.SPAWN_MOB);
    registerTracker(ClientboundPackets1_16_2.SPAWN_EXPERIENCE_ORB, Entity1_16_2Types.EXPERIENCE_ORB);
    registerTracker(ClientboundPackets1_16_2.SPAWN_PAINTING, Entity1_16_2Types.PAINTING);
    registerTracker(ClientboundPackets1_16_2.SPAWN_PLAYER, Entity1_16_2Types.PLAYER);
    registerRemoveEntities(ClientboundPackets1_16_2.DESTROY_ENTITIES);
    registerMetadataRewriter(ClientboundPackets1_16_2.ENTITY_METADATA, Types1_16.METADATA_LIST);
    protocol.registerClientbound(ClientboundPackets1_16_2.JOIN_GAME, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Entity ID
            map(Type.INT);
            handler(wrapper -> {
                boolean hardcore = wrapper.read(Type.BOOLEAN);
                short gamemode = wrapper.read(Type.UNSIGNED_BYTE);
                if (hardcore) {
                    gamemode |= 0x08;
                }
                wrapper.write(Type.UNSIGNED_BYTE, gamemode);
            });
            // Previous Gamemode
            map(Type.BYTE);
            // World List
            map(Type.STRING_ARRAY);
            handler(wrapper -> {
                // Just screw the registry and write the defaults for 1.16 and 1.16.1 clients
                wrapper.read(Type.NBT);
                wrapper.write(Type.NBT, EntityPackets.DIMENSIONS_TAG);
                CompoundTag dimensionData = wrapper.read(Type.NBT);
                wrapper.write(Type.STRING, getDimensionFromData(dimensionData));
            });
            // Dimension
            map(Type.STRING);
            // Seed
            map(Type.LONG);
            handler(wrapper -> {
                int maxPlayers = wrapper.read(Type.VAR_INT);
                wrapper.write(Type.UNSIGNED_BYTE, (short) Math.max(maxPlayers, 255));
            });
            // ...
            handler(getTrackerHandler(Entity1_16_2Types.PLAYER, Type.INT));
        }
    });
    protocol.registerClientbound(ClientboundPackets1_16_2.RESPAWN, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                CompoundTag dimensionData = wrapper.read(Type.NBT);
                wrapper.write(Type.STRING, getDimensionFromData(dimensionData));
            });
        }
    });
}
Also used : Set(java.util.Set) EntityType(com.viaversion.viaversion.api.minecraft.entities.EntityType) StringTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag) Sets(com.google.common.collect.Sets) Entity1_16_2Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_16_2Types) Types1_16(com.viaversion.viaversion.api.type.types.version.Types1_16) ClientboundPackets1_16_2(com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.ClientboundPackets1_16_2) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) Entity1_16Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_16Types) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag) Protocol1_16_1To1_16_2(com.viaversion.viabackwards.protocol.protocol1_16_1to1_16_2.Protocol1_16_1To1_16_2) EntityRewriter(com.viaversion.viabackwards.api.rewriters.EntityRewriter) EntityPackets(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.packets.EntityPackets) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag)

Example 8 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(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 9 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 EntityPackets method registerPackets.

@Override
public void registerPackets() {
    registerTrackerWithData(ClientboundPackets1_16_2.SPAWN_ENTITY, Entity1_17Types.FALLING_BLOCK);
    registerTracker(ClientboundPackets1_16_2.SPAWN_MOB);
    registerTracker(ClientboundPackets1_16_2.SPAWN_PLAYER, Entity1_17Types.PLAYER);
    registerMetadataRewriter(ClientboundPackets1_16_2.ENTITY_METADATA, Types1_16.METADATA_LIST, Types1_17.METADATA_LIST);
    protocol.registerClientbound(ClientboundPackets1_16_2.DESTROY_ENTITIES, null, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                int[] entityIds = wrapper.read(Type.VAR_INT_ARRAY_PRIMITIVE);
                wrapper.cancel();
                EntityTracker entityTracker = wrapper.user().getEntityTracker(Protocol1_17To1_16_4.class);
                for (int entityId : entityIds) {
                    entityTracker.removeEntity(entityId);
                    // Send individual remove packets
                    PacketWrapper newPacket = wrapper.create(ClientboundPackets1_17.REMOVE_ENTITY);
                    newPacket.write(Type.VAR_INT, entityId);
                    newPacket.send(Protocol1_17To1_16_4.class);
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_16_2.ENTITY_PROPERTIES, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Entity id
            map(Type.VAR_INT);
            handler(wrapper -> {
                // Collection length is now a var int
                wrapper.write(Type.VAR_INT, wrapper.read(Type.INT));
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_16_2.PLAYER_POSITION, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.DOUBLE);
            map(Type.DOUBLE);
            map(Type.DOUBLE);
            map(Type.FLOAT);
            map(Type.FLOAT);
            map(Type.BYTE);
            map(Type.VAR_INT);
            handler(wrapper -> {
                // Dismount vehicle
                wrapper.write(Type.BOOLEAN, false);
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_16_2.COMBAT_EVENT, null, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                // Combat 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.COMBAT_ENTER;
                        break;
                    case 1:
                        packetType = ClientboundPackets1_17.COMBAT_END;
                        break;
                    case 2:
                        packetType = ClientboundPackets1_17.COMBAT_KILL;
                        break;
                    default:
                        throw new IllegalArgumentException("Invalid combat type received: " + type);
                }
                wrapper.setId(packetType.getId());
            });
        }
    });
    // The parent class of the other entity move packets that is never actually used has finally been removed from the id list
    protocol.cancelClientbound(ClientboundPackets1_16_2.ENTITY_MOVEMENT);
}
Also used : ClientboundPackets1_17(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.ClientboundPackets1_17) EntityRewriter(com.viaversion.viaversion.rewriter.EntityRewriter) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) EntityTracker(com.viaversion.viaversion.api.data.entity.EntityTracker) EntityType(com.viaversion.viaversion.api.minecraft.entities.EntityType) Types1_17(com.viaversion.viaversion.api.type.types.version.Types1_17) Entity1_16_2Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_16_2Types) Types1_16(com.viaversion.viaversion.api.type.types.version.Types1_16) ClientboundPackets1_16_2(com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.ClientboundPackets1_16_2) 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) Entity1_17Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_17Types) Protocol1_17To1_16_4(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.Protocol1_17To1_16_4) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) EntityTracker(com.viaversion.viaversion.api.data.entity.EntityTracker) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) ClientboundPacketType(com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType)

Example 10 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 InventoryPackets method registerPackets.

@Override
public void registerPackets() {
    registerSetCooldown(ClientboundPackets1_16_2.COOLDOWN);
    registerWindowItems(ClientboundPackets1_16_2.WINDOW_ITEMS, Type.FLAT_VAR_INT_ITEM_ARRAY);
    registerTradeList(ClientboundPackets1_16_2.TRADE_LIST, Type.FLAT_VAR_INT_ITEM);
    registerSetSlot(ClientboundPackets1_16_2.SET_SLOT, Type.FLAT_VAR_INT_ITEM);
    registerAdvancements(ClientboundPackets1_16_2.ADVANCEMENTS, Type.FLAT_VAR_INT_ITEM);
    registerEntityEquipmentArray(ClientboundPackets1_16_2.ENTITY_EQUIPMENT, Type.FLAT_VAR_INT_ITEM);
    registerSpawnParticle(ClientboundPackets1_16_2.SPAWN_PARTICLE, Type.FLAT_VAR_INT_ITEM, Type.DOUBLE);
    new RecipeRewriter1_16(protocol).registerDefaultHandler(ClientboundPackets1_16_2.DECLARE_RECIPES);
    registerCreativeInvAction(ServerboundPackets1_17.CREATIVE_INVENTORY_ACTION, Type.FLAT_VAR_INT_ITEM);
    protocol.registerServerbound(ServerboundPackets1_17.EDIT_BOOK, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> handleItemToServer(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)));
        }
    });
    protocol.registerServerbound(ServerboundPackets1_17.CLICK_WINDOW, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Window Id
            map(Type.UNSIGNED_BYTE);
            // Slot
            map(Type.SHORT);
            // Button
            map(Type.BYTE);
            // Action id - doesn't matter, as the sent out confirmation packet will be cancelled
            handler(wrapper -> wrapper.write(Type.SHORT, (short) 0));
            // Action
            map(Type.VAR_INT);
            handler(wrapper -> {
                // Affected items - throw them away!
                int length = wrapper.read(Type.VAR_INT);
                for (int i = 0; i < length; i++) {
                    // Slot
                    wrapper.read(Type.SHORT);
                    wrapper.read(Type.FLAT_VAR_INT_ITEM);
                }
                // 1.17 clients send the then carried item, but 1.16 expects the clicked one
                Item item = wrapper.read(Type.FLAT_VAR_INT_ITEM);
                int action = wrapper.get(Type.VAR_INT, 0);
                if (action == 5 || action == 1) {
                    // Quick craft (= dragging / mouse movement while clicking on an empty slot)
                    // OR Quick move (= shift click to move a whole stack to the other inventory)
                    // The server always expects an empty item here
                    item = null;
                } else {
                    // Use the item sent
                    handleItemToServer(item);
                }
                wrapper.write(Type.FLAT_VAR_INT_ITEM, item);
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_16_2.WINDOW_CONFIRMATION, null, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                short inventoryId = wrapper.read(Type.UNSIGNED_BYTE);
                short confirmationId = wrapper.read(Type.SHORT);
                boolean accepted = wrapper.read(Type.BOOLEAN);
                if (!accepted) {
                    // Use the new ping packet to replace the removed acknowledgement, extra bit for fast dismissal
                    int id = (1 << 30) | (inventoryId << 16) | (confirmationId & 0xFFFF);
                    wrapper.user().get(InventoryAcknowledgements.class).addId(id);
                    PacketWrapper pingPacket = wrapper.create(ClientboundPackets1_17.PING);
                    pingPacket.write(Type.INT, id);
                    pingPacket.send(Protocol1_17To1_16_4.class);
                }
                wrapper.cancel();
            });
        }
    });
    // New pong packet
    protocol.registerServerbound(ServerboundPackets1_17.PONG, null, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                int id = wrapper.read(Type.INT);
                // Check extra bit for fast dismissal
                if ((id & (1 << 30)) != 0 && wrapper.user().get(InventoryAcknowledgements.class).removeId(id)) {
                    // Decode our requested inventory acknowledgement
                    short inventoryId = (short) ((id >> 16) & 0xFF);
                    short confirmationId = (short) (id & 0xFFFF);
                    PacketWrapper packet = wrapper.create(ServerboundPackets1_16_2.WINDOW_CONFIRMATION);
                    packet.write(Type.UNSIGNED_BYTE, inventoryId);
                    packet.write(Type.SHORT, confirmationId);
                    // Accept
                    packet.write(Type.BOOLEAN, true);
                    packet.sendToServer(Protocol1_17To1_16_4.class);
                }
                wrapper.cancel();
            });
        }
    });
}
Also used : PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) ClientboundPackets1_17(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.ClientboundPackets1_17) Protocol1_17To1_16_4(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.Protocol1_17To1_16_4) ItemRewriter(com.viaversion.viaversion.rewriter.ItemRewriter) Type(com.viaversion.viaversion.api.type.Type) ServerboundPackets1_16_2(com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.ServerboundPackets1_16_2) InventoryAcknowledgements(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.storage.InventoryAcknowledgements) ServerboundPackets1_17(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.ServerboundPackets1_17) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) Item(com.viaversion.viaversion.api.minecraft.item.Item) RecipeRewriter1_16(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.data.RecipeRewriter1_16) ClientboundPackets1_16_2(com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.ClientboundPackets1_16_2) InventoryAcknowledgements(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.storage.InventoryAcknowledgements) Item(com.viaversion.viaversion.api.minecraft.item.Item) Protocol1_17To1_16_4(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.Protocol1_17To1_16_4) RecipeRewriter1_16(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.data.RecipeRewriter1_16) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)

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