Search in sources :

Example 66 with PacketWrapper

use of com.viaversion.viaversion.api.protocol.packet.PacketWrapper in project ViaBackwards by ViaVersion.

the class BlockItemPackets1_15 method registerPackets.

@Override
protected void registerPackets() {
    BlockRewriter blockRewriter = new BlockRewriter(protocol, Type.POSITION1_14);
    new RecipeRewriter1_14(protocol).registerDefaultHandler(ClientboundPackets1_15.DECLARE_RECIPES);
    protocol.registerServerbound(ServerboundPackets1_14.EDIT_BOOK, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> handleItemToServer(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)));
        }
    });
    registerSetCooldown(ClientboundPackets1_15.COOLDOWN);
    registerWindowItems(ClientboundPackets1_15.WINDOW_ITEMS, Type.FLAT_VAR_INT_ITEM_ARRAY);
    registerSetSlot(ClientboundPackets1_15.SET_SLOT, Type.FLAT_VAR_INT_ITEM);
    registerTradeList(ClientboundPackets1_15.TRADE_LIST, Type.FLAT_VAR_INT_ITEM);
    registerEntityEquipment(ClientboundPackets1_15.ENTITY_EQUIPMENT, Type.FLAT_VAR_INT_ITEM);
    registerAdvancements(ClientboundPackets1_15.ADVANCEMENTS, Type.FLAT_VAR_INT_ITEM);
    registerClickWindow(ServerboundPackets1_14.CLICK_WINDOW, Type.FLAT_VAR_INT_ITEM);
    registerCreativeInvAction(ServerboundPackets1_14.CREATIVE_INVENTORY_ACTION, Type.FLAT_VAR_INT_ITEM);
    blockRewriter.registerAcknowledgePlayerDigging(ClientboundPackets1_15.ACKNOWLEDGE_PLAYER_DIGGING);
    blockRewriter.registerBlockAction(ClientboundPackets1_15.BLOCK_ACTION);
    blockRewriter.registerBlockChange(ClientboundPackets1_15.BLOCK_CHANGE);
    blockRewriter.registerMultiBlockChange(ClientboundPackets1_15.MULTI_BLOCK_CHANGE);
    protocol.registerClientbound(ClientboundPackets1_15.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_15Type());
                    wrapper.write(new Chunk1_14Type(), chunk);
                    if (chunk.isFullChunk()) {
                        int[] biomeData = chunk.getBiomeData();
                        int[] newBiomeData = new int[256];
                        for (int i = 0; i < 4; ++i) {
                            for (int j = 0; j < 4; ++j) {
                                int x = j << 2;
                                int z = i << 2;
                                int newIndex = z << 4 | x;
                                int oldIndex = i << 2 | j;
                                int biome = biomeData[oldIndex];
                                for (int k = 0; k < 4; k++) {
                                    int offX = newIndex + (k << 4);
                                    for (int l = 0; l < 4; l++) {
                                        newBiomeData[offX + l] = biome;
                                    }
                                }
                            }
                        }
                        chunk.setBiomeData(newBiomeData);
                    }
                    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);
                            int newId = protocol.getMappingData().getNewBlockStateId(old);
                            section.setPaletteEntry(j, newId);
                        }
                    }
                }
            });
        }
    });
    blockRewriter.registerEffect(ClientboundPackets1_15.EFFECT, 1010, 2001);
    protocol.registerClientbound(ClientboundPackets1_15.SPAWN_PARTICLE, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Particle ID
            map(Type.INT);
            // 1 - Long Distance
            map(Type.BOOLEAN);
            // 2 - X
            map(Type.DOUBLE, Type.FLOAT);
            // 3 - Y
            map(Type.DOUBLE, Type.FLOAT);
            // 4 - Z
            map(Type.DOUBLE, Type.FLOAT);
            // 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) {
                        Item item = handleItemToClient(wrapper.read(Type.FLAT_VAR_INT_ITEM));
                        wrapper.write(Type.FLAT_VAR_INT_ITEM, item);
                    }
                    int mappedId = protocol.getMappingData().getNewParticleId(id);
                    if (id != mappedId) {
                        wrapper.set(Type.INT, 0, mappedId);
                    }
                }
            });
        }
    });
}
Also used : PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) RecipeRewriter1_14(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.data.RecipeRewriter1_14) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) Chunk1_14Type(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.types.Chunk1_14Type) Item(com.viaversion.viaversion.api.minecraft.item.Item) ServerboundPackets1_14(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ServerboundPackets1_14) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) Chunk(com.viaversion.viaversion.api.minecraft.chunks.Chunk) BlockRewriter(com.viaversion.viaversion.rewriter.BlockRewriter) Protocol1_14_4To1_15(com.viaversion.viabackwards.protocol.protocol1_14_4to1_15.Protocol1_14_4To1_15) Chunk1_15Type(com.viaversion.viaversion.protocols.protocol1_15to1_14_4.types.Chunk1_15Type) ClientboundPackets1_15(com.viaversion.viaversion.protocols.protocol1_15to1_14_4.ClientboundPackets1_15) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection) RecipeRewriter1_14(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.data.RecipeRewriter1_14) 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) BlockRewriter(com.viaversion.viaversion.rewriter.BlockRewriter) Item(com.viaversion.viaversion.api.minecraft.item.Item) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection)

Example 67 with PacketWrapper

use of com.viaversion.viaversion.api.protocol.packet.PacketWrapper in project ViaBackwards by ViaVersion.

the class BlockItemPackets1_18 method registerPackets.

@Override
protected void registerPackets() {
    new RecipeRewriter1_16(protocol).registerDefaultHandler(ClientboundPackets1_18.DECLARE_RECIPES);
    registerSetCooldown(ClientboundPackets1_18.COOLDOWN);
    registerWindowItems1_17_1(ClientboundPackets1_18.WINDOW_ITEMS, Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT, Type.FLAT_VAR_INT_ITEM);
    registerSetSlot1_17_1(ClientboundPackets1_18.SET_SLOT, Type.FLAT_VAR_INT_ITEM);
    registerEntityEquipmentArray(ClientboundPackets1_18.ENTITY_EQUIPMENT, Type.FLAT_VAR_INT_ITEM);
    registerTradeList(ClientboundPackets1_18.TRADE_LIST, Type.FLAT_VAR_INT_ITEM);
    registerAdvancements(ClientboundPackets1_18.ADVANCEMENTS, Type.FLAT_VAR_INT_ITEM);
    registerClickWindow1_17_1(ServerboundPackets1_17.CLICK_WINDOW, Type.FLAT_VAR_INT_ITEM);
    protocol.registerClientbound(ClientboundPackets1_18.EFFECT, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Effect id
            map(Type.INT);
            // Location
            map(Type.POSITION1_14);
            // Data
            map(Type.INT);
            handler(wrapper -> {
                int id = wrapper.get(Type.INT, 0);
                int data = wrapper.get(Type.INT, 1);
                if (id == 1010) {
                    // Play record
                    wrapper.set(Type.INT, 1, protocol.getMappingData().getNewItemId(data));
                }
            });
        }
    });
    registerCreativeInvAction(ServerboundPackets1_17.CREATIVE_INVENTORY_ACTION, Type.FLAT_VAR_INT_ITEM);
    protocol.registerClientbound(ClientboundPackets1_18.SPAWN_PARTICLE, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Particle id
            map(Type.INT);
            // Override limiter
            map(Type.BOOLEAN);
            // X
            map(Type.DOUBLE);
            // Y
            map(Type.DOUBLE);
            // Z
            map(Type.DOUBLE);
            // Offset X
            map(Type.FLOAT);
            // Offset Y
            map(Type.FLOAT);
            // Offset Z
            map(Type.FLOAT);
            // Max speed
            map(Type.FLOAT);
            // Particle Count
            map(Type.INT);
            handler(wrapper -> {
                int id = wrapper.get(Type.INT, 0);
                if (id == 3) {
                    // Block marker
                    int blockState = wrapper.read(Type.VAR_INT);
                    if (blockState == 7786) {
                        // Light block
                        wrapper.set(Type.INT, 0, 3);
                    } else {
                        // Else assume barrier block
                        wrapper.set(Type.INT, 0, 2);
                    }
                    return;
                }
                ParticleMappings mappings = protocol.getMappingData().getParticleMappings();
                if (mappings.isBlockParticle(id)) {
                    int data = wrapper.passthrough(Type.VAR_INT);
                    wrapper.set(Type.VAR_INT, 0, protocol.getMappingData().getNewBlockStateId(data));
                } else if (mappings.isItemParticle(id)) {
                    handleItemToClient(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM));
                }
                int newId = protocol.getMappingData().getNewParticleId(id);
                if (newId != id) {
                    wrapper.set(Type.INT, 0, newId);
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_18.BLOCK_ENTITY_DATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.POSITION1_14);
            handler(wrapper -> {
                final int id = wrapper.read(Type.VAR_INT);
                final CompoundTag tag = wrapper.read(Type.NBT);
                final int mappedId = BlockEntityIds.mappedId(id);
                if (mappedId == -1) {
                    wrapper.cancel();
                    return;
                }
                final String identifier = protocol.getMappingData().blockEntities().get(id);
                if (identifier == null) {
                    wrapper.cancel();
                    return;
                }
                // The 1.18 server doesn't include the id and positions (x, y, z) in the NBT anymore
                // If those were the only fields on the block entity (e.g.: skull, bed), we'll receive a null NBT
                // We initialize one and add the missing fields, so it can be handled correctly down the line
                final CompoundTag newTag = tag == null ? new CompoundTag() : tag;
                final Position pos = wrapper.get(Type.POSITION1_14, 0);
                // The protocol converters downstream rely on this field, let's add it back
                newTag.put("id", new StringTag("minecraft:" + identifier));
                // Weird glitches happen with the 1.17 client and below if these fields are missing
                // Some examples are block entity models becoming invisible (e.g.: signs, banners)
                newTag.put("x", new IntTag(pos.x()));
                newTag.put("y", new IntTag(pos.y()));
                newTag.put("z", new IntTag(pos.z()));
                handleSpawner(id, newTag);
                wrapper.write(Type.UNSIGNED_BYTE, (short) mappedId);
                wrapper.write(Type.NBT, newTag);
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_18.CHUNK_DATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                final EntityTracker tracker = protocol.getEntityRewriter().tracker(wrapper.user());
                final Chunk1_18Type chunkType = new Chunk1_18Type(tracker.currentWorldSectionHeight(), MathUtil.ceilLog2(protocol.getMappingData().getBlockStateMappings().size()), MathUtil.ceilLog2(tracker.biomesSent()));
                final Chunk oldChunk = wrapper.read(chunkType);
                final ChunkSection[] sections = oldChunk.getSections();
                final BitSet mask = new BitSet(oldChunk.getSections().length);
                final int[] biomeData = new int[sections.length * ChunkSection.BIOME_SIZE];
                int biomeIndex = 0;
                for (int j = 0; j < sections.length; j++) {
                    final ChunkSection section = sections[j];
                    // Write biome palette into biome array
                    final DataPalette biomePalette = section.palette(PaletteType.BIOMES);
                    for (int i = 0; i < ChunkSection.BIOME_SIZE; i++) {
                        biomeData[biomeIndex++] = biomePalette.idAt(i);
                    }
                    // Rewrite to empty section
                    if (section.getNonAirBlocksCount() == 0) {
                        sections[j] = null;
                    } else {
                        mask.set(j);
                    }
                }
                final List<CompoundTag> blockEntityTags = new ArrayList<>(oldChunk.blockEntities().size());
                for (final BlockEntity blockEntity : oldChunk.blockEntities()) {
                    final String id = protocol.getMappingData().blockEntities().get(blockEntity.typeId());
                    if (id == null) {
                        // Shrug
                        continue;
                    }
                    final CompoundTag tag;
                    if (blockEntity.tag() != null) {
                        tag = blockEntity.tag();
                        handleSpawner(blockEntity.typeId(), tag);
                    } else {
                        tag = new CompoundTag();
                    }
                    blockEntityTags.add(tag);
                    tag.put("x", new IntTag((oldChunk.getX() << 4) + blockEntity.sectionX()));
                    tag.put("y", new IntTag(blockEntity.y()));
                    tag.put("z", new IntTag((oldChunk.getZ() << 4) + blockEntity.sectionZ()));
                    tag.put("id", new StringTag("minecraft:" + id));
                }
                final Chunk chunk = new BaseChunk(oldChunk.getX(), oldChunk.getZ(), true, false, mask, oldChunk.getSections(), biomeData, oldChunk.getHeightMap(), blockEntityTags);
                wrapper.write(new Chunk1_17Type(tracker.currentWorldSectionHeight()), chunk);
                // Create and send light packet first
                final PacketWrapper lightPacket = wrapper.create(ClientboundPackets1_17_1.UPDATE_LIGHT);
                lightPacket.write(Type.VAR_INT, chunk.getX());
                lightPacket.write(Type.VAR_INT, chunk.getZ());
                // Trust edges
                lightPacket.write(Type.BOOLEAN, wrapper.read(Type.BOOLEAN));
                // Sky light mask
                lightPacket.write(Type.LONG_ARRAY_PRIMITIVE, wrapper.read(Type.LONG_ARRAY_PRIMITIVE));
                // Block light mask
                lightPacket.write(Type.LONG_ARRAY_PRIMITIVE, wrapper.read(Type.LONG_ARRAY_PRIMITIVE));
                // Empty sky light mask
                lightPacket.write(Type.LONG_ARRAY_PRIMITIVE, wrapper.read(Type.LONG_ARRAY_PRIMITIVE));
                // Empty block light mask
                lightPacket.write(Type.LONG_ARRAY_PRIMITIVE, wrapper.read(Type.LONG_ARRAY_PRIMITIVE));
                final int skyLightLength = wrapper.read(Type.VAR_INT);
                lightPacket.write(Type.VAR_INT, skyLightLength);
                for (int i = 0; i < skyLightLength; i++) {
                    lightPacket.write(Type.BYTE_ARRAY_PRIMITIVE, wrapper.read(Type.BYTE_ARRAY_PRIMITIVE));
                }
                final int blockLightLength = wrapper.read(Type.VAR_INT);
                lightPacket.write(Type.VAR_INT, blockLightLength);
                for (int i = 0; i < blockLightLength; i++) {
                    lightPacket.write(Type.BYTE_ARRAY_PRIMITIVE, wrapper.read(Type.BYTE_ARRAY_PRIMITIVE));
                }
                lightPacket.send(Protocol1_17_1To1_18.class);
            });
        }
    });
    protocol.cancelClientbound(ClientboundPackets1_18.SET_SIMULATION_DISTANCE);
}
Also used : MathUtil(com.viaversion.viaversion.util.MathUtil) ServerboundPackets1_17(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.ServerboundPackets1_17) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) ArrayList(java.util.ArrayList) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) Chunk1_17Type(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.types.Chunk1_17Type) Chunk(com.viaversion.viaversion.api.minecraft.chunks.Chunk) Protocol1_17_1To1_18(com.viaversion.viabackwards.protocol.protocol1_17_1to1_18.Protocol1_17_1To1_18) ClientboundPackets1_17_1(com.viaversion.viaversion.protocols.protocol1_17_1to1_17.ClientboundPackets1_17_1) PaletteType(com.viaversion.viaversion.api.minecraft.chunks.PaletteType) IntTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.IntTag) Chunk1_18Type(com.viaversion.viaversion.protocols.protocol1_18to1_17_1.types.Chunk1_18Type) BlockEntity(com.viaversion.viaversion.api.minecraft.blockentity.BlockEntity) Position(com.viaversion.viaversion.api.minecraft.Position) ClientboundPackets1_18(com.viaversion.viaversion.protocols.protocol1_18to1_17_1.ClientboundPackets1_18) BlockEntityIds(com.viaversion.viabackwards.protocol.protocol1_17_1to1_18.data.BlockEntityIds) BaseChunk(com.viaversion.viaversion.api.minecraft.chunks.BaseChunk) EntityTracker(com.viaversion.viaversion.api.data.entity.EntityTracker) StringTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag) DataPalette(com.viaversion.viaversion.api.minecraft.chunks.DataPalette) List(java.util.List) ItemRewriter(com.viaversion.viabackwards.api.rewriters.ItemRewriter) ParticleMappings(com.viaversion.viaversion.api.data.ParticleMappings) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection) BitSet(java.util.BitSet) RecipeRewriter1_16(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.data.RecipeRewriter1_16) StringTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag) BaseChunk(com.viaversion.viaversion.api.minecraft.chunks.BaseChunk) Chunk1_17Type(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.types.Chunk1_17Type) RecipeRewriter1_16(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.data.RecipeRewriter1_16) Position(com.viaversion.viaversion.api.minecraft.Position) EntityTracker(com.viaversion.viaversion.api.data.entity.EntityTracker) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) BitSet(java.util.BitSet) Protocol1_17_1To1_18(com.viaversion.viabackwards.protocol.protocol1_17_1to1_18.Protocol1_17_1To1_18) Chunk(com.viaversion.viaversion.api.minecraft.chunks.Chunk) BaseChunk(com.viaversion.viaversion.api.minecraft.chunks.BaseChunk) DataPalette(com.viaversion.viaversion.api.minecraft.chunks.DataPalette) Chunk1_18Type(com.viaversion.viaversion.protocols.protocol1_18to1_17_1.types.Chunk1_18Type) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) ArrayList(java.util.ArrayList) List(java.util.List) ParticleMappings(com.viaversion.viaversion.api.data.ParticleMappings) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag) IntTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.IntTag) BlockEntity(com.viaversion.viaversion.api.minecraft.blockentity.BlockEntity)

Example 68 with PacketWrapper

use of com.viaversion.viaversion.api.protocol.packet.PacketWrapper in project ViaBackwards by ViaVersion.

the class EntityPackets1_13_1 method registerPackets.

@Override
protected void registerPackets() {
    protocol.registerClientbound(ClientboundPackets1_13.SPAWN_ENTITY, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity id
            map(Type.VAR_INT);
            // 1 - UUID
            map(Type.UUID);
            // 2 - Type
            map(Type.BYTE);
            // 3 - X
            map(Type.DOUBLE);
            // 4 - Y
            map(Type.DOUBLE);
            // 5 - Z
            map(Type.DOUBLE);
            // 6 - Pitch
            map(Type.BYTE);
            // 7 - Yaw
            map(Type.BYTE);
            // 8 - Data
            map(Type.INT);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int entityId = wrapper.get(Type.VAR_INT, 0);
                    byte type = wrapper.get(Type.BYTE, 0);
                    Entity1_13Types.EntityType entType = Entity1_13Types.getTypeFromId(type, true);
                    if (entType == null) {
                        ViaBackwards.getPlatform().getLogger().warning("Could not find 1.13 entity type " + type);
                        return;
                    }
                    // Rewrite falling block
                    if (entType.is(Entity1_13Types.EntityType.FALLING_BLOCK)) {
                        int data = wrapper.get(Type.INT, 0);
                        wrapper.set(Type.INT, 0, protocol.getMappingData().getNewBlockStateId(data));
                    }
                    // Track Entity
                    tracker(wrapper.user()).addEntity(entityId, entType);
                }
            });
        }
    });
    registerTracker(ClientboundPackets1_13.SPAWN_EXPERIENCE_ORB, Entity1_13Types.EntityType.EXPERIENCE_ORB);
    registerTracker(ClientboundPackets1_13.SPAWN_GLOBAL_ENTITY, Entity1_13Types.EntityType.LIGHTNING_BOLT);
    protocol.registerClientbound(ClientboundPackets1_13.SPAWN_MOB, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.VAR_INT);
            // 1 - Entity UUID
            map(Type.UUID);
            // 2 - Entity Type
            map(Type.VAR_INT);
            // 3 - X
            map(Type.DOUBLE);
            // 4 - Y
            map(Type.DOUBLE);
            // 5 - Z
            map(Type.DOUBLE);
            // 6 - Yaw
            map(Type.BYTE);
            // 7 - Pitch
            map(Type.BYTE);
            // 8 - Head Pitch
            map(Type.BYTE);
            // 9 - Velocity X
            map(Type.SHORT);
            // 10 - Velocity Y
            map(Type.SHORT);
            // 11 - Velocity Z
            map(Type.SHORT);
            // 12 - Metadata
            map(Types1_13.METADATA_LIST);
            // Track Entity
            handler(getTrackerHandler());
            // Rewrite Metadata
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    List<Metadata> metadata = wrapper.get(Types1_13.METADATA_LIST, 0);
                    handleMetadata(wrapper.get(Type.VAR_INT, 0), metadata, wrapper.user());
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_13.SPAWN_PLAYER, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.VAR_INT);
            // 1 - Player UUID
            map(Type.UUID);
            // 2 - X
            map(Type.DOUBLE);
            // 3 - Y
            map(Type.DOUBLE);
            // 4 - Z
            map(Type.DOUBLE);
            // 5 - Yaw
            map(Type.BYTE);
            // 6 - Pitch
            map(Type.BYTE);
            // 7 - Metadata
            map(Types1_13.METADATA_LIST);
            handler(getTrackerAndMetaHandler(Types1_13.METADATA_LIST, Entity1_13Types.EntityType.PLAYER));
        }
    });
    registerTracker(ClientboundPackets1_13.SPAWN_PAINTING, Entity1_13Types.EntityType.PAINTING);
    registerJoinGame(ClientboundPackets1_13.JOIN_GAME, Entity1_13Types.EntityType.PLAYER);
    registerRespawn(ClientboundPackets1_13.RESPAWN);
    registerRemoveEntities(ClientboundPackets1_13.DESTROY_ENTITIES);
    registerMetadataRewriter(ClientboundPackets1_13.ENTITY_METADATA, Types1_13.METADATA_LIST);
}
Also used : PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata) Entity1_13Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_13Types)

Example 69 with PacketWrapper

use of com.viaversion.viaversion.api.protocol.packet.PacketWrapper in project ViaBackwards by ViaVersion.

the class WorldPackets1_13_1 method register.

public static void register(Protocol protocol) {
    BlockRewriter blockRewriter = new BlockRewriter(protocol, Type.POSITION);
    protocol.registerClientbound(ClientboundPackets1_13.CHUNK_DATA, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
                    Chunk chunk = wrapper.passthrough(new Chunk1_13Type(clientWorld));
                    for (ChunkSection section : chunk.getSections()) {
                        if (section != null) {
                            for (int i = 0; i < section.getPaletteSize(); i++) {
                                section.setPaletteEntry(i, protocol.getMappingData().getNewBlockStateId(section.getPaletteEntry(i)));
                            }
                        }
                    }
                }
            });
        }
    });
    blockRewriter.registerBlockAction(ClientboundPackets1_13.BLOCK_ACTION);
    blockRewriter.registerBlockChange(ClientboundPackets1_13.BLOCK_CHANGE);
    blockRewriter.registerMultiBlockChange(ClientboundPackets1_13.MULTI_BLOCK_CHANGE);
    blockRewriter.registerEffect(ClientboundPackets1_13.EFFECT, 1010, 2001);
}
Also used : BlockRewriter(com.viaversion.viaversion.rewriter.BlockRewriter) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) Chunk1_13Type(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.types.Chunk1_13Type) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) ClientWorld(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld) Chunk(com.viaversion.viaversion.api.minecraft.chunks.Chunk) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection)

Example 70 with PacketWrapper

use of com.viaversion.viaversion.api.protocol.packet.PacketWrapper in project ViaBackwards by ViaVersion.

the class Protocol1_14_3To1_14_4 method registerPackets.

@Override
protected void registerPackets() {
    // Acknowledge Player Digging - added in pre4
    registerClientbound(ClientboundPackets1_14.ACKNOWLEDGE_PLAYER_DIGGING, ClientboundPackets1_14.BLOCK_CHANGE, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.POSITION1_14);
            map(Type.VAR_INT);
            handler(wrapper -> {
                int status = wrapper.read(Type.VAR_INT);
                boolean allGood = wrapper.read(Type.BOOLEAN);
                if (allGood && status == 0) {
                    wrapper.cancel();
                }
            });
        }
    });
    registerClientbound(ClientboundPackets1_14.TRADE_LIST, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    wrapper.passthrough(Type.VAR_INT);
                    int size = wrapper.passthrough(Type.UNSIGNED_BYTE);
                    for (int i = 0; i < size; i++) {
                        wrapper.passthrough(Type.FLAT_VAR_INT_ITEM);
                        wrapper.passthrough(Type.FLAT_VAR_INT_ITEM);
                        if (wrapper.passthrough(Type.BOOLEAN)) {
                            wrapper.passthrough(Type.FLAT_VAR_INT_ITEM);
                        }
                        wrapper.passthrough(Type.BOOLEAN);
                        wrapper.passthrough(Type.INT);
                        wrapper.passthrough(Type.INT);
                        wrapper.passthrough(Type.INT);
                        wrapper.passthrough(Type.INT);
                        wrapper.passthrough(Type.FLOAT);
                        // demand value added in pre-5
                        wrapper.read(Type.INT);
                    }
                }
            });
        }
    });
}
Also used : PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) ClientboundPackets1_14(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14) ServerboundPackets1_14(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ServerboundPackets1_14) BackwardsProtocol(com.viaversion.viabackwards.api.BackwardsProtocol) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)

Aggregations

PacketWrapper (com.viaversion.viaversion.api.protocol.packet.PacketWrapper)107 PacketRemapper (com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)76 PacketHandler (com.viaversion.viaversion.api.protocol.remapper.PacketHandler)63 Type (com.viaversion.viaversion.api.type.Type)38 Item (com.viaversion.viaversion.api.minecraft.item.Item)32 Chunk (com.viaversion.viaversion.api.minecraft.chunks.Chunk)22 Metadata (com.viaversion.viaversion.api.minecraft.metadata.Metadata)22 ClientWorld (com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld)22 ChunkSection (com.viaversion.viaversion.api.minecraft.chunks.ChunkSection)18 ArrayList (java.util.ArrayList)17 UserConnection (com.viaversion.viaversion.api.connection.UserConnection)16 Position (com.viaversion.viaversion.api.minecraft.Position)16 List (java.util.List)14 ClientboundPackets1_13 (com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13)13 CompoundTag (com.github.steveice10.opennbt.tag.builtin.CompoundTag)11 EntityType (com.viaversion.viaversion.api.minecraft.entities.EntityType)11 ViaBackwards (com.viaversion.viabackwards.ViaBackwards)10 Via (com.viaversion.viaversion.api.Via)9 BlockChangeRecord (com.viaversion.viaversion.api.minecraft.BlockChangeRecord)9 ClientboundPackets1_14 (com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14)9