Search in sources :

Example 1 with PacketRemapper

use of com.viaversion.viaversion.api.protocol.remapper.PacketRemapper in project ViaBackwards by ViaVersion.

the class BlockItemPackets1_16 method registerPackets.

@Override
protected void registerPackets() {
    BlockRewriter blockRewriter = new BlockRewriter(protocol, Type.POSITION1_14);
    RecipeRewriter1_14 recipeRewriter = new RecipeRewriter1_14(protocol);
    // Remove new smithing type, only in this handler
    protocol.registerClientbound(ClientboundPackets1_16.DECLARE_RECIPES, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                int size = wrapper.passthrough(Type.VAR_INT);
                int newSize = size;
                for (int i = 0; i < size; i++) {
                    String originalType = wrapper.read(Type.STRING);
                    String type = originalType.replace("minecraft:", "");
                    if (type.equals("smithing")) {
                        newSize--;
                        wrapper.read(Type.STRING);
                        wrapper.read(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT);
                        wrapper.read(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT);
                        wrapper.read(Type.FLAT_VAR_INT_ITEM);
                        continue;
                    }
                    wrapper.write(Type.STRING, originalType);
                    // Recipe Identifier
                    String id = wrapper.passthrough(Type.STRING);
                    recipeRewriter.handle(wrapper, type);
                }
                wrapper.set(Type.VAR_INT, 0, newSize);
            });
        }
    });
    registerSetCooldown(ClientboundPackets1_16.COOLDOWN);
    registerWindowItems(ClientboundPackets1_16.WINDOW_ITEMS, Type.FLAT_VAR_INT_ITEM_ARRAY);
    registerSetSlot(ClientboundPackets1_16.SET_SLOT, Type.FLAT_VAR_INT_ITEM);
    registerTradeList(ClientboundPackets1_16.TRADE_LIST, Type.FLAT_VAR_INT_ITEM);
    registerAdvancements(ClientboundPackets1_16.ADVANCEMENTS, Type.FLAT_VAR_INT_ITEM);
    blockRewriter.registerAcknowledgePlayerDigging(ClientboundPackets1_16.ACKNOWLEDGE_PLAYER_DIGGING);
    blockRewriter.registerBlockAction(ClientboundPackets1_16.BLOCK_ACTION);
    blockRewriter.registerBlockChange(ClientboundPackets1_16.BLOCK_CHANGE);
    blockRewriter.registerMultiBlockChange(ClientboundPackets1_16.MULTI_BLOCK_CHANGE);
    protocol.registerClientbound(ClientboundPackets1_16.ENTITY_EQUIPMENT, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                int entityId = wrapper.passthrough(Type.VAR_INT);
                List<EquipmentData> equipmentData = new ArrayList<>();
                byte slot;
                do {
                    slot = wrapper.read(Type.BYTE);
                    Item item = handleItemToClient(wrapper.read(Type.FLAT_VAR_INT_ITEM));
                    int rawSlot = slot & 0x7F;
                    equipmentData.add(new EquipmentData(rawSlot, item));
                } while ((slot & 0xFFFFFF80) != 0);
                // Send first data in the current packet
                EquipmentData firstData = equipmentData.get(0);
                wrapper.write(Type.VAR_INT, firstData.slot);
                wrapper.write(Type.FLAT_VAR_INT_ITEM, firstData.item);
                // If there are more items, send new packets for them
                for (int i = 1; i < equipmentData.size(); i++) {
                    PacketWrapper equipmentPacket = wrapper.create(ClientboundPackets1_15.ENTITY_EQUIPMENT);
                    EquipmentData data = equipmentData.get(i);
                    equipmentPacket.write(Type.VAR_INT, entityId);
                    equipmentPacket.write(Type.VAR_INT, data.slot);
                    equipmentPacket.write(Type.FLAT_VAR_INT_ITEM, data.item);
                    equipmentPacket.send(Protocol1_15_2To1_16.class);
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_16.UPDATE_LIGHT, new PacketRemapper() {

        @Override
        public void registerMap() {
            // x
            map(Type.VAR_INT);
            // y
            map(Type.VAR_INT);
            map(Type.BOOLEAN, Type.NOTHING);
        }
    });
    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_15Type(), chunk);
                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));
                    }
                }
                CompoundTag heightMaps = chunk.getHeightMap();
                for (Tag heightMapTag : heightMaps.values()) {
                    LongArrayTag heightMap = (LongArrayTag) heightMapTag;
                    int[] heightMapData = new int[256];
                    CompactArrayUtil.iterateCompactArrayWithPadding(9, heightMapData.length, heightMap.getValue(), (i, v) -> heightMapData[i] = v);
                    heightMap.setValue(CompactArrayUtil.createCompactArray(9, heightMapData.length, i -> heightMapData[i]));
                }
                if (chunk.isBiomeData()) {
                    for (int i = 0; i < 1024; i++) {
                        int biome = chunk.getBiomeData()[i];
                        switch(biome) {
                            // new nether biomes
                            case 170:
                            case 171:
                            case 172:
                            case 173:
                                chunk.getBiomeData()[i] = 8;
                                break;
                        }
                    }
                }
                if (chunk.getBlockEntities() == null)
                    return;
                for (CompoundTag blockEntity : chunk.getBlockEntities()) {
                    handleBlockEntity(blockEntity);
                }
            });
        }
    });
    blockRewriter.registerEffect(ClientboundPackets1_16.EFFECT, 1010, 2001);
    registerSpawnParticle(ClientboundPackets1_16.SPAWN_PARTICLE, Type.FLAT_VAR_INT_ITEM, Type.DOUBLE);
    protocol.registerClientbound(ClientboundPackets1_16.WINDOW_PROPERTY, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Window id
            map(Type.UNSIGNED_BYTE);
            // Property
            map(Type.SHORT);
            // Value
            map(Type.SHORT);
            handler(wrapper -> {
                short property = wrapper.get(Type.SHORT, 0);
                if (property >= 4 && property <= 6) {
                    // Enchantment id
                    short enchantmentId = wrapper.get(Type.SHORT, 1);
                    if (enchantmentId > 11) {
                        // soul_speed
                        wrapper.set(Type.SHORT, 1, --enchantmentId);
                    } else if (enchantmentId == 11) {
                        wrapper.set(Type.SHORT, 1, (short) 9);
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_16.MAP_DATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Map ID
            map(Type.VAR_INT);
            // Scale
            map(Type.BYTE);
            // Tracking Position
            map(Type.BOOLEAN);
            // Locked
            map(Type.BOOLEAN);
            handler(MapColorRewriter.getRewriteHandler(MapColorRewrites::getMappedColor));
        }
    });
    protocol.registerClientbound(ClientboundPackets1_16.BLOCK_ENTITY_DATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                Position position = wrapper.passthrough(Type.POSITION1_14);
                short action = wrapper.passthrough(Type.UNSIGNED_BYTE);
                CompoundTag tag = wrapper.passthrough(Type.NBT);
                handleBlockEntity(tag);
            });
        }
    });
    registerClickWindow(ServerboundPackets1_14.CLICK_WINDOW, Type.FLAT_VAR_INT_ITEM);
    registerCreativeInvAction(ServerboundPackets1_14.CREATIVE_INVENTORY_ACTION, Type.FLAT_VAR_INT_ITEM);
    protocol.registerServerbound(ServerboundPackets1_14.EDIT_BOOK, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> handleItemToServer(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)));
        }
    });
}
Also used : PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) LongArrayTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.LongArrayTag) Item(com.viaversion.viaversion.api.minecraft.item.Item) Protocol1_15_2To1_16(com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.Protocol1_15_2To1_16) ArrayList(java.util.ArrayList) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) Chunk(com.viaversion.viaversion.api.minecraft.chunks.Chunk) Chunk1_16Type(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.types.Chunk1_16Type) Map(java.util.Map) MapColorRewrites(com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.data.MapColorRewrites) IntArrayTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.IntArrayTag) MapColorRewriter(com.viaversion.viabackwards.api.rewriters.MapColorRewriter) Position(com.viaversion.viaversion.api.minecraft.Position) EnchantmentRewriter(com.viaversion.viabackwards.api.rewriters.EnchantmentRewriter) Tag(com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag) RecipeRewriter1_14(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.data.RecipeRewriter1_14) InventoryPackets(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.packets.InventoryPackets) StringTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag) UUID(java.util.UUID) UUIDIntArrayType(com.viaversion.viaversion.api.type.types.UUIDIntArrayType) ServerboundPackets1_14(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ServerboundPackets1_14) CompactArrayUtil(com.viaversion.viaversion.util.CompactArrayUtil) ClientboundPackets1_16(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.ClientboundPackets1_16) List(java.util.List) BlockRewriter(com.viaversion.viaversion.rewriter.BlockRewriter) Chunk1_15Type(com.viaversion.viaversion.protocols.protocol1_15to1_14_4.types.Chunk1_15Type) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag) 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) Position(com.viaversion.viaversion.api.minecraft.Position) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Chunk1_15Type(com.viaversion.viaversion.protocols.protocol1_15to1_14_4.types.Chunk1_15Type) MapColorRewrites(com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.data.MapColorRewrites) Protocol1_15_2To1_16(com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.Protocol1_15_2To1_16) Chunk(com.viaversion.viaversion.api.minecraft.chunks.Chunk) BlockRewriter(com.viaversion.viaversion.rewriter.BlockRewriter) Item(com.viaversion.viaversion.api.minecraft.item.Item) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) LongArrayTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.LongArrayTag) Chunk1_16Type(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.types.Chunk1_16Type) ArrayList(java.util.ArrayList) List(java.util.List) LongArrayTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.LongArrayTag) IntArrayTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.IntArrayTag) Tag(com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag) StringTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag)

Example 2 with PacketRemapper

use of com.viaversion.viaversion.api.protocol.remapper.PacketRemapper in project ViaBackwards by ViaVersion.

the class EntityPackets1_16 method registerPackets.

@Override
protected void registerPackets() {
    protocol.registerClientbound(ClientboundPackets1_16.SPAWN_ENTITY, 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 - Pitch
            map(Type.BYTE);
            // 7 - Yaw
            map(Type.BYTE);
            // 8 - Data
            map(Type.INT);
            handler(wrapper -> {
                EntityType entityType = typeFromId(wrapper.get(Type.VAR_INT, 1));
                if (entityType == Entity1_16Types.LIGHTNING_BOLT) {
                    // Map to old weather entity packet
                    wrapper.cancel();
                    PacketWrapper spawnLightningPacket = wrapper.create(ClientboundPackets1_15.SPAWN_GLOBAL_ENTITY);
                    // Entity id
                    spawnLightningPacket.write(Type.VAR_INT, wrapper.get(Type.VAR_INT, 0));
                    // Lightning type
                    spawnLightningPacket.write(Type.BYTE, (byte) 1);
                    // X
                    spawnLightningPacket.write(Type.DOUBLE, wrapper.get(Type.DOUBLE, 0));
                    // Y
                    spawnLightningPacket.write(Type.DOUBLE, wrapper.get(Type.DOUBLE, 1));
                    // Z
                    spawnLightningPacket.write(Type.DOUBLE, wrapper.get(Type.DOUBLE, 2));
                    spawnLightningPacket.send(Protocol1_15_2To1_16.class);
                }
            });
            handler(getSpawnTrackerWithDataHandler(Entity1_16Types.FALLING_BLOCK));
        }
    });
    registerSpawnTracker(ClientboundPackets1_16.SPAWN_MOB);
    protocol.registerClientbound(ClientboundPackets1_16.RESPAWN, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Dimension Type
            map(dimensionTransformer);
            handler(wrapper -> {
                // Grab the tracker for world names
                WorldNameTracker worldNameTracker = wrapper.user().get(WorldNameTracker.class);
                // World Name
                String nextWorldName = wrapper.read(Type.STRING);
                // Seed
                wrapper.passthrough(Type.LONG);
                // Gamemode
                wrapper.passthrough(Type.UNSIGNED_BYTE);
                // Previous gamemode
                wrapper.read(Type.BYTE);
                // Grab client world
                ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
                int dimension = wrapper.get(Type.INT, 0);
                // Send a dummy respawn with a different dimension if the world name was different and the same dimension was used
                if (clientWorld.getEnvironment() != null && dimension == clientWorld.getEnvironment().getId() && (wrapper.user().isClientSide() || Via.getPlatform().isProxy() || // Hotfix for https://github.com/ViaVersion/ViaBackwards/issues/381
                wrapper.user().getProtocolInfo().getProtocolVersion() <= ProtocolVersion.v1_12_2.getVersion() || !nextWorldName.equals(worldNameTracker.getWorldName()))) {
                    PacketWrapper packet = wrapper.create(ClientboundPackets1_15.RESPAWN);
                    packet.write(Type.INT, dimension == 0 ? -1 : 0);
                    packet.write(Type.LONG, 0L);
                    packet.write(Type.UNSIGNED_BYTE, (short) 0);
                    packet.write(Type.STRING, "default");
                    packet.send(Protocol1_15_2To1_16.class);
                }
                clientWorld.setEnvironment(dimension);
                // Level type
                wrapper.write(Type.STRING, "default");
                // Debug
                wrapper.read(Type.BOOLEAN);
                if (wrapper.read(Type.BOOLEAN)) {
                    wrapper.set(Type.STRING, 0, "flat");
                }
                // Keep all playerdata
                wrapper.read(Type.BOOLEAN);
                // Finally update the world name
                worldNameTracker.setWorldName(nextWorldName);
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_16.JOIN_GAME, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Entity ID
            map(Type.INT);
            // Gamemode
            map(Type.UNSIGNED_BYTE);
            // Previous gamemode
            map(Type.BYTE, Type.NOTHING);
            // World list
            map(Type.STRING_ARRAY, Type.NOTHING);
            // whatever this is
            map(Type.NBT, Type.NOTHING);
            // Dimension Type
            map(dimensionTransformer);
            handler(wrapper -> {
                WorldNameTracker worldNameTracker = wrapper.user().get(WorldNameTracker.class);
                // Save the world name
                worldNameTracker.setWorldName(wrapper.read(Type.STRING));
            });
            // Seed
            map(Type.LONG);
            // Max players
            map(Type.UNSIGNED_BYTE);
            handler(wrapper -> {
                ClientWorld clientChunks = wrapper.user().get(ClientWorld.class);
                clientChunks.setEnvironment(wrapper.get(Type.INT, 1));
                tracker(wrapper.user()).addEntity(wrapper.get(Type.INT, 0), Entity1_16Types.PLAYER);
                // Level type
                wrapper.write(Type.STRING, "default");
                // View distance
                wrapper.passthrough(Type.VAR_INT);
                // Reduced debug info
                wrapper.passthrough(Type.BOOLEAN);
                // Show death screen
                wrapper.passthrough(Type.BOOLEAN);
                // Debug
                wrapper.read(Type.BOOLEAN);
                if (wrapper.read(Type.BOOLEAN)) {
                    wrapper.set(Type.STRING, 0, "flat");
                }
            });
        }
    });
    registerTracker(ClientboundPackets1_16.SPAWN_EXPERIENCE_ORB, Entity1_16Types.EXPERIENCE_ORB);
    // F Spawn Global Object, it is no longer with us :(
    registerTracker(ClientboundPackets1_16.SPAWN_PAINTING, Entity1_16Types.PAINTING);
    registerTracker(ClientboundPackets1_16.SPAWN_PLAYER, Entity1_16Types.PLAYER);
    registerRemoveEntities(ClientboundPackets1_16.DESTROY_ENTITIES);
    registerMetadataRewriter(ClientboundPackets1_16.ENTITY_METADATA, Types1_16.METADATA_LIST, Types1_14.METADATA_LIST);
    protocol.registerClientbound(ClientboundPackets1_16.ENTITY_PROPERTIES, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                wrapper.passthrough(Type.VAR_INT);
                int size = wrapper.passthrough(Type.INT);
                for (int i = 0; i < size; i++) {
                    String attributeIdentifier = wrapper.read(Type.STRING);
                    String oldKey = protocol.getMappingData().getAttributeMappings().get(attributeIdentifier);
                    wrapper.write(Type.STRING, oldKey != null ? oldKey : attributeIdentifier.replace("minecraft:", ""));
                    wrapper.passthrough(Type.DOUBLE);
                    int modifierSize = wrapper.passthrough(Type.VAR_INT);
                    for (int j = 0; j < modifierSize; j++) {
                        wrapper.passthrough(Type.UUID);
                        wrapper.passthrough(Type.DOUBLE);
                        wrapper.passthrough(Type.BYTE);
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_16.PLAYER_INFO, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(packetWrapper -> {
                int action = packetWrapper.passthrough(Type.VAR_INT);
                int playerCount = packetWrapper.passthrough(Type.VAR_INT);
                for (int i = 0; i < playerCount; i++) {
                    packetWrapper.passthrough(Type.UUID);
                    if (action == 0) {
                        // Add
                        packetWrapper.passthrough(Type.STRING);
                        int properties = packetWrapper.passthrough(Type.VAR_INT);
                        for (int j = 0; j < properties; j++) {
                            packetWrapper.passthrough(Type.STRING);
                            packetWrapper.passthrough(Type.STRING);
                            if (packetWrapper.passthrough(Type.BOOLEAN)) {
                                packetWrapper.passthrough(Type.STRING);
                            }
                        }
                        packetWrapper.passthrough(Type.VAR_INT);
                        packetWrapper.passthrough(Type.VAR_INT);
                        if (packetWrapper.passthrough(Type.BOOLEAN)) {
                            // Display Name
                            protocol.getTranslatableRewriter().processText(packetWrapper.passthrough(Type.COMPONENT));
                        }
                    } else if (action == 1) {
                        // Update Game Mode
                        packetWrapper.passthrough(Type.VAR_INT);
                    } else if (action == 2) {
                        // Update Ping
                        packetWrapper.passthrough(Type.VAR_INT);
                    } else if (action == 3) {
                        // Update Display Name
                        if (packetWrapper.passthrough(Type.BOOLEAN)) {
                            // Display name
                            protocol.getTranslatableRewriter().processText(packetWrapper.passthrough(Type.COMPONENT));
                        }
                    }
                // 4 = Remove Player
                }
            });
        }
    });
}
Also used : Particle(com.viaversion.viaversion.api.type.types.Particle) Entity1_15Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_15Types) JsonElement(com.viaversion.viaversion.libs.gson.JsonElement) WorldNameTracker(com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.data.WorldNameTracker) WolfDataMaskStorage(com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.storage.WolfDataMaskStorage) MetaType(com.viaversion.viaversion.api.minecraft.metadata.MetaType) ClientWorld(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) EntityType(com.viaversion.viaversion.api.minecraft.entities.EntityType) Via(com.viaversion.viaversion.api.Via) Item(com.viaversion.viaversion.api.minecraft.item.Item) ProtocolVersion(com.viaversion.viaversion.api.protocol.version.ProtocolVersion) Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata) Types1_14(com.viaversion.viaversion.api.type.types.version.Types1_14) Protocol1_15_2To1_16(com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.Protocol1_15_2To1_16) Types1_16(com.viaversion.viaversion.api.type.types.version.Types1_16) 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) StoredEntityData(com.viaversion.viaversion.api.data.entity.StoredEntityData) Entity1_16Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_16Types) ValueTransformer(com.viaversion.viaversion.api.protocol.remapper.ValueTransformer) ClientboundPackets1_15(com.viaversion.viaversion.protocols.protocol1_15to1_14_4.ClientboundPackets1_15) EntityRewriter(com.viaversion.viabackwards.api.rewriters.EntityRewriter) EntityType(com.viaversion.viaversion.api.minecraft.entities.EntityType) WorldNameTracker(com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.data.WorldNameTracker) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Protocol1_15_2To1_16(com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.Protocol1_15_2To1_16) ClientWorld(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld)

Example 3 with PacketRemapper

use of com.viaversion.viaversion.api.protocol.remapper.PacketRemapper in project ViaBackwards by ViaVersion.

the class Protocol1_16_4To1_17 method registerPackets.

@Override
protected void registerPackets() {
    executeAsyncAfterLoaded(Protocol1_17To1_16_4.class, MAPPINGS::load);
    translatableRewriter.registerChatMessage(ClientboundPackets1_17.CHAT_MESSAGE);
    translatableRewriter.registerBossBar(ClientboundPackets1_17.BOSSBAR);
    translatableRewriter.registerDisconnect(ClientboundPackets1_17.DISCONNECT);
    translatableRewriter.registerTabList(ClientboundPackets1_17.TAB_LIST);
    translatableRewriter.registerOpenWindow(ClientboundPackets1_17.OPEN_WINDOW);
    translatableRewriter.registerPing();
    blockItemPackets = new BlockItemPackets1_17(this);
    blockItemPackets.register();
    entityRewriter.register();
    SoundRewriter soundRewriter = new SoundRewriter(this);
    soundRewriter.registerSound(ClientboundPackets1_17.SOUND);
    soundRewriter.registerSound(ClientboundPackets1_17.ENTITY_SOUND);
    soundRewriter.registerNamedSound(ClientboundPackets1_17.NAMED_SOUND);
    soundRewriter.registerStopSound(ClientboundPackets1_17.STOP_SOUND);
    TagRewriter tagRewriter = new TagRewriter(this);
    registerClientbound(ClientboundPackets1_17.TAGS, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                Map<String, List<TagData>> tags = new HashMap<>();
                int length = wrapper.read(Type.VAR_INT);
                for (int i = 0; i < length; i++) {
                    String resourceKey = wrapper.read(Type.STRING);
                    if (resourceKey.startsWith("minecraft:")) {
                        resourceKey = resourceKey.substring(10);
                    }
                    List<TagData> tagList = new ArrayList<>();
                    tags.put(resourceKey, tagList);
                    int tagLength = wrapper.read(Type.VAR_INT);
                    for (int j = 0; j < tagLength; j++) {
                        String identifier = wrapper.read(Type.STRING);
                        int[] entries = wrapper.read(Type.VAR_INT_ARRAY_PRIMITIVE);
                        tagList.add(new TagData(identifier, entries));
                    }
                }
                // Put them into the hardcoded order of Vanilla tags (and only those), rewrite ids
                for (RegistryType type : RegistryType.getValues()) {
                    List<TagData> tagList = tags.get(type.getResourceLocation());
                    IdRewriteFunction rewriter = tagRewriter.getRewriter(type);
                    wrapper.write(Type.VAR_INT, tagList.size());
                    for (TagData tagData : tagList) {
                        int[] entries = tagData.entries();
                        if (rewriter != null) {
                            // Handle id rewriting now
                            IntList idList = new IntArrayList(entries.length);
                            for (int id : entries) {
                                int mappedId = rewriter.rewrite(id);
                                if (mappedId != -1) {
                                    idList.add(mappedId);
                                }
                            }
                            entries = idList.toArray(EMPTY_ARRAY);
                        }
                        wrapper.write(Type.STRING, tagData.identifier());
                        wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, entries);
                    }
                    // Stop after the entity types
                    if (type == RegistryType.ENTITY) {
                        break;
                    }
                }
            });
        }
    });
    new StatisticsRewriter(this).register(ClientboundPackets1_17.STATISTICS);
    registerClientbound(ClientboundPackets1_17.RESOURCE_PACK, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                wrapper.passthrough(Type.STRING);
                wrapper.passthrough(Type.STRING);
                // Required
                wrapper.read(Type.BOOLEAN);
                // Prompt message
                wrapper.read(Type.OPTIONAL_COMPONENT);
            });
        }
    });
    registerClientbound(ClientboundPackets1_17.EXPLOSION, new PacketRemapper() {

        @Override
        public void registerMap() {
            // X
            map(Type.FLOAT);
            // Y
            map(Type.FLOAT);
            // Z
            map(Type.FLOAT);
            // Strength
            map(Type.FLOAT);
            handler(wrapper -> {
                // Collection length
                wrapper.write(Type.INT, wrapper.read(Type.VAR_INT));
            });
        }
    });
    registerClientbound(ClientboundPackets1_17.SPAWN_POSITION, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.POSITION1_14);
            handler(wrapper -> {
                // Angle (which Mojang just forgot to write to the buffer, lol)
                wrapper.read(Type.FLOAT);
            });
        }
    });
    registerClientbound(ClientboundPackets1_17.PING, null, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                wrapper.cancel();
                int id = wrapper.read(Type.INT);
                short shortId = (short) id;
                if (id == shortId && ViaBackwards.getConfig().handlePingsAsInvAcknowledgements()) {
                    wrapper.user().get(PingRequests.class).addId(shortId);
                    // Send inventory acknowledgement to replace ping packet functionality in the unsigned byte range
                    PacketWrapper acknowledgementPacket = wrapper.create(ClientboundPackets1_16_2.WINDOW_CONFIRMATION);
                    // Inventory id
                    acknowledgementPacket.write(Type.UNSIGNED_BYTE, (short) 0);
                    // Confirmation id
                    acknowledgementPacket.write(Type.SHORT, shortId);
                    // Accepted
                    acknowledgementPacket.write(Type.BOOLEAN, false);
                    acknowledgementPacket.send(Protocol1_16_4To1_17.class);
                    return;
                }
                // Plugins expecting a real response will have to handle this accordingly themselves
                PacketWrapper pongPacket = wrapper.create(ServerboundPackets1_17.PONG);
                pongPacket.write(Type.INT, id);
                pongPacket.sendToServer(Protocol1_16_4To1_17.class);
            });
        }
    });
    registerServerbound(ServerboundPackets1_16_2.CLIENT_SETTINGS, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Locale
            map(Type.STRING);
            // View distance
            map(Type.BYTE);
            // Chat mode
            map(Type.VAR_INT);
            // Chat colors
            map(Type.BOOLEAN);
            // Chat flags
            map(Type.UNSIGNED_BYTE);
            // Main hand
            map(Type.VAR_INT);
            handler(wrapper -> {
                // Text filtering
                wrapper.write(Type.BOOLEAN, false);
            });
        }
    });
    // TODO translatables
    mergePacket(ClientboundPackets1_17.TITLE_TEXT, ClientboundPackets1_16_2.TITLE, 0);
    mergePacket(ClientboundPackets1_17.TITLE_SUBTITLE, ClientboundPackets1_16_2.TITLE, 1);
    mergePacket(ClientboundPackets1_17.ACTIONBAR, ClientboundPackets1_16_2.TITLE, 2);
    mergePacket(ClientboundPackets1_17.TITLE_TIMES, ClientboundPackets1_16_2.TITLE, 3);
    registerClientbound(ClientboundPackets1_17.CLEAR_TITLES, ClientboundPackets1_16_2.TITLE, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                if (wrapper.read(Type.BOOLEAN)) {
                    // Reset times
                    wrapper.write(Type.VAR_INT, 5);
                } else {
                    // Simple clear
                    wrapper.write(Type.VAR_INT, 4);
                }
            });
        }
    });
    cancelClientbound(ClientboundPackets1_17.ADD_VIBRATION_SIGNAL);
}
Also used : ClientboundPackets1_17(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.ClientboundPackets1_17) TranslatableRewriter(com.viaversion.viabackwards.api.rewriters.TranslatableRewriter) ServerboundPackets1_17(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.ServerboundPackets1_17) PingRequests(com.viaversion.viabackwards.protocol.protocol1_16_4to1_17.storage.PingRequests) IdRewriteFunction(com.viaversion.viaversion.rewriter.IdRewriteFunction) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) HashMap(java.util.HashMap) BackwardsProtocol(com.viaversion.viabackwards.api.BackwardsProtocol) ArrayList(java.util.ArrayList) RegistryType(com.viaversion.viaversion.api.minecraft.RegistryType) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) Map(java.util.Map) EntityTrackerBase(com.viaversion.viaversion.data.entity.EntityTrackerBase) UserConnection(com.viaversion.viaversion.api.connection.UserConnection) ItemRewriter(com.viaversion.viaversion.api.rewriter.ItemRewriter) SoundRewriter(com.viaversion.viabackwards.api.rewriters.SoundRewriter) TagData(com.viaversion.viaversion.api.minecraft.TagData) EntityPackets1_17(com.viaversion.viabackwards.protocol.protocol1_16_4to1_17.packets.EntityPackets1_17) ClientboundPackets1_16_2(com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.ClientboundPackets1_16_2) Protocol1_17To1_16_4(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.Protocol1_17To1_16_4) BlockItemPackets1_17(com.viaversion.viabackwards.protocol.protocol1_16_4to1_17.packets.BlockItemPackets1_17) List(java.util.List) Entity1_17Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_17Types) IntArrayList(com.viaversion.viaversion.libs.fastutil.ints.IntArrayList) ServerboundPackets1_16_2(com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.ServerboundPackets1_16_2) TagRewriter(com.viaversion.viaversion.rewriter.TagRewriter) StatisticsRewriter(com.viaversion.viaversion.rewriter.StatisticsRewriter) BackwardsMappings(com.viaversion.viabackwards.api.data.BackwardsMappings) IntList(com.viaversion.viaversion.libs.fastutil.ints.IntList) ViaBackwards(com.viaversion.viabackwards.ViaBackwards) EntityRewriter(com.viaversion.viaversion.api.rewriter.EntityRewriter) BlockItemPackets1_17(com.viaversion.viabackwards.protocol.protocol1_16_4to1_17.packets.BlockItemPackets1_17) RegistryType(com.viaversion.viaversion.api.minecraft.RegistryType) TagRewriter(com.viaversion.viaversion.rewriter.TagRewriter) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) IntList(com.viaversion.viaversion.libs.fastutil.ints.IntList) StatisticsRewriter(com.viaversion.viaversion.rewriter.StatisticsRewriter) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) TagData(com.viaversion.viaversion.api.minecraft.TagData) SoundRewriter(com.viaversion.viabackwards.api.rewriters.SoundRewriter) ArrayList(java.util.ArrayList) List(java.util.List) IntArrayList(com.viaversion.viaversion.libs.fastutil.ints.IntArrayList) IntList(com.viaversion.viaversion.libs.fastutil.ints.IntList) IdRewriteFunction(com.viaversion.viaversion.rewriter.IdRewriteFunction) IntArrayList(com.viaversion.viaversion.libs.fastutil.ints.IntArrayList) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with PacketRemapper

use of com.viaversion.viaversion.api.protocol.remapper.PacketRemapper in project ViaBackwards by ViaVersion.

the class Protocol1_17_1To1_18 method registerPackets.

@Override
protected void registerPackets() {
    executeAsyncAfterLoaded(Protocol1_18To1_17_1.class, MAPPINGS::load);
    translatableRewriter.registerComponentPacket(ClientboundPackets1_18.CHAT_MESSAGE);
    translatableRewriter.registerComponentPacket(ClientboundPackets1_18.ACTIONBAR);
    translatableRewriter.registerComponentPacket(ClientboundPackets1_18.TITLE_TEXT);
    translatableRewriter.registerComponentPacket(ClientboundPackets1_18.TITLE_SUBTITLE);
    translatableRewriter.registerBossBar(ClientboundPackets1_18.BOSSBAR);
    translatableRewriter.registerDisconnect(ClientboundPackets1_18.DISCONNECT);
    translatableRewriter.registerTabList(ClientboundPackets1_18.TAB_LIST);
    translatableRewriter.registerOpenWindow(ClientboundPackets1_18.OPEN_WINDOW);
    translatableRewriter.registerCombatKill(ClientboundPackets1_18.COMBAT_KILL);
    translatableRewriter.registerPing();
    itemRewriter = new BlockItemPackets1_18(this);
    entityRewriter.register();
    itemRewriter.register();
    final SoundRewriter soundRewriter = new SoundRewriter(this);
    soundRewriter.registerSound(ClientboundPackets1_18.SOUND);
    soundRewriter.registerSound(ClientboundPackets1_18.ENTITY_SOUND);
    soundRewriter.registerStopSound(ClientboundPackets1_18.STOP_SOUND);
    soundRewriter.registerNamedSound(ClientboundPackets1_18.NAMED_SOUND);
    final TagRewriter tagRewriter = new TagRewriter(this);
    tagRewriter.addEmptyTag(RegistryType.BLOCK, "minecraft:lava_pool_stone_replaceables");
    tagRewriter.registerGeneric(ClientboundPackets1_18.TAGS);
    registerServerbound(ServerboundPackets1_17.CLIENT_SETTINGS, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Language
            map(Type.STRING);
            // View distance
            map(Type.BYTE);
            // Chat visibility
            map(Type.VAR_INT);
            // Chat colors
            map(Type.BOOLEAN);
            // Model customization
            map(Type.UNSIGNED_BYTE);
            // Main hand
            map(Type.VAR_INT);
            // Text filtering enabled
            map(Type.BOOLEAN);
            // Allow listing in server list preview
            create(Type.BOOLEAN, true);
        }
    });
    registerClientbound(ClientboundPackets1_18.SCOREBOARD_OBJECTIVE, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Name
            map(Type.STRING);
            handler(cutName(0, 16));
        }
    });
    registerClientbound(ClientboundPackets1_18.DISPLAY_SCOREBOARD, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Slot
            map(Type.BYTE);
            // Name
            map(Type.STRING);
            handler(cutName(0, 16));
        }
    });
    registerClientbound(ClientboundPackets1_18.TEAMS, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Name
            map(Type.STRING);
            handler(cutName(0, 16));
        }
    });
    registerClientbound(ClientboundPackets1_18.UPDATE_SCORE, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Owner
            map(Type.STRING);
            // Method
            map(Type.VAR_INT);
            // Name
            map(Type.STRING);
            handler(cutName(0, 40));
            handler(cutName(1, 16));
        }
    });
}
Also used : TagRewriter(com.viaversion.viaversion.rewriter.TagRewriter) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) SoundRewriter(com.viaversion.viabackwards.api.rewriters.SoundRewriter) BlockItemPackets1_18(com.viaversion.viabackwards.protocol.protocol1_17_1to1_18.packets.BlockItemPackets1_18)

Example 5 with PacketRemapper

use of com.viaversion.viaversion.api.protocol.remapper.PacketRemapper in project ViaBackwards by ViaVersion.

the class BlockItemPackets1_10 method registerPackets.

@Override
protected void registerPackets() {
    registerSetSlot(ClientboundPackets1_9_3.SET_SLOT, Type.ITEM);
    registerWindowItems(ClientboundPackets1_9_3.WINDOW_ITEMS, Type.ITEM_ARRAY);
    // Entity Equipment Packet
    registerEntityEquipment(ClientboundPackets1_9_3.ENTITY_EQUIPMENT, Type.ITEM);
    protocol.registerClientbound(ClientboundPackets1_9_3.PLUGIN_MESSAGE, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Channel
            map(Type.STRING);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    if (wrapper.get(Type.STRING, 0).equalsIgnoreCase("MC|TrList")) {
                        // Passthrough Window ID
                        wrapper.passthrough(Type.INT);
                        int size = wrapper.passthrough(Type.UNSIGNED_BYTE);
                        for (int i = 0; i < size; i++) {
                            // Input Item
                            wrapper.write(Type.ITEM, handleItemToClient(wrapper.read(Type.ITEM)));
                            // Output Item
                            wrapper.write(Type.ITEM, handleItemToClient(wrapper.read(Type.ITEM)));
                            // Has second item
                            boolean secondItem = wrapper.passthrough(Type.BOOLEAN);
                            if (secondItem) {
                                // Second Item
                                wrapper.write(Type.ITEM, handleItemToClient(wrapper.read(Type.ITEM)));
                            }
                            // Trade disabled
                            wrapper.passthrough(Type.BOOLEAN);
                            // Number of tools uses
                            wrapper.passthrough(Type.INT);
                            // Maximum number of trade uses
                            wrapper.passthrough(Type.INT);
                        }
                    }
                }
            });
        }
    });
    registerClickWindow(ServerboundPackets1_9_3.CLICK_WINDOW, Type.ITEM);
    registerCreativeInvAction(ServerboundPackets1_9_3.CREATIVE_INVENTORY_ACTION, Type.ITEM);
    protocol.registerClientbound(ClientboundPackets1_9_3.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);
                    Chunk1_9_3_4Type type = new Chunk1_9_3_4Type(clientWorld);
                    Chunk chunk = wrapper.passthrough(type);
                    handleChunk(chunk);
                }
            });
        }
    });
    // Block Change Packet
    protocol.registerClientbound(ClientboundPackets1_9_3.BLOCK_CHANGE, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Block Position
            map(Type.POSITION);
            // 1 - Block
            map(Type.VAR_INT);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int idx = wrapper.get(Type.VAR_INT, 0);
                    wrapper.set(Type.VAR_INT, 0, handleBlockID(idx));
                }
            });
        }
    });
    // Multi Block Change Packet
    protocol.registerClientbound(ClientboundPackets1_9_3.MULTI_BLOCK_CHANGE, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Chunk X
            map(Type.INT);
            // 1 - Chunk Z
            map(Type.INT);
            map(Type.BLOCK_CHANGE_RECORD_ARRAY);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    for (BlockChangeRecord record : wrapper.get(Type.BLOCK_CHANGE_RECORD_ARRAY, 0)) {
                        record.setBlockId(handleBlockID(record.getBlockId()));
                    }
                }
            });
        }
    });
    // Rewrite metadata items
    protocol.getEntityRewriter().filter().handler((event, meta) -> {
        if (// Is Item
        meta.metaType().type().equals(Type.ITEM))
            meta.setValue(handleItemToClient((Item) meta.getValue()));
    });
    // Particle
    protocol.registerClientbound(ClientboundPackets1_9_3.SPAWN_PARTICLE, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.INT);
            map(Type.BOOLEAN);
            map(Type.FLOAT);
            map(Type.FLOAT);
            map(Type.FLOAT);
            map(Type.FLOAT);
            map(Type.FLOAT);
            map(Type.FLOAT);
            map(Type.FLOAT);
            map(Type.INT);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int id = wrapper.get(Type.INT, 0);
                    if (id == 46) {
                        // new falling_dust
                        // -> block_dust
                        wrapper.set(Type.INT, 0, 38);
                    }
                }
            });
        }
    });
}
Also used : BlockChangeRecord(com.viaversion.viaversion.api.minecraft.BlockChangeRecord) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) 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) Chunk1_9_3_4Type(com.viaversion.viaversion.protocols.protocol1_9_1_2to1_9_3_4.types.Chunk1_9_3_4Type)

Aggregations

PacketRemapper (com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)109 PacketWrapper (com.viaversion.viaversion.api.protocol.packet.PacketWrapper)78 PacketHandler (com.viaversion.viaversion.api.protocol.remapper.PacketHandler)65 Type (com.viaversion.viaversion.api.type.Type)64 Item (com.viaversion.viaversion.api.minecraft.item.Item)31 Chunk (com.viaversion.viaversion.api.minecraft.chunks.Chunk)24 ClientWorld (com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld)24 UserConnection (com.viaversion.viaversion.api.connection.UserConnection)21 Via (com.viaversion.viaversion.api.Via)19 ChunkSection (com.viaversion.viaversion.api.minecraft.chunks.ChunkSection)19 CompoundTag (com.github.steveice10.opennbt.tag.builtin.CompoundTag)17 ArrayList (java.util.ArrayList)17 List (java.util.List)17 Metadata (com.viaversion.viaversion.api.minecraft.metadata.Metadata)16 Position (com.viaversion.viaversion.api.minecraft.Position)14 EntityType (com.viaversion.viaversion.api.minecraft.entities.EntityType)14 ClientboundPackets1_13 (com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13)14 StringTag (com.github.steveice10.opennbt.tag.builtin.StringTag)13 CompoundTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag)13 StatisticsRewriter (com.viaversion.viaversion.rewriter.StatisticsRewriter)13