Search in sources :

Example 1 with ServerboundPackets1_14

use of com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ServerboundPackets1_14 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 ServerboundPackets1_14

use of com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ServerboundPackets1_14 in project ViaBackwards by ViaVersion.

the class EntityPackets1_15 method registerPackets.

@Override
protected void registerPackets() {
    protocol.registerClientbound(ClientboundPackets1_15.UPDATE_HEALTH, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                float health = wrapper.passthrough(Type.FLOAT);
                if (health > 0)
                    return;
                if (!wrapper.user().get(ImmediateRespawn.class).isImmediateRespawn())
                    return;
                // Instantly request respawn when 1.15 gamerule is set
                PacketWrapper statusPacket = wrapper.create(ServerboundPackets1_14.CLIENT_STATUS);
                statusPacket.write(Type.VAR_INT, 0);
                statusPacket.sendToServer(Protocol1_14_4To1_15.class);
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_15.GAME_EVENT, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.UNSIGNED_BYTE);
            map(Type.FLOAT);
            handler(wrapper -> {
                if (wrapper.get(Type.UNSIGNED_BYTE, 0) == 11) {
                    wrapper.user().get(ImmediateRespawn.class).setImmediateRespawn(wrapper.get(Type.FLOAT, 0) == 1);
                }
            });
        }
    });
    registerTrackerWithData(ClientboundPackets1_15.SPAWN_ENTITY, Entity1_15Types.FALLING_BLOCK);
    protocol.registerClientbound(ClientboundPackets1_15.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);
            // Metadata is no longer sent in 1.15, so we have to send an empty one
            handler(wrapper -> wrapper.write(Types1_14.METADATA_LIST, new ArrayList<>()));
            handler(wrapper -> {
                int type = wrapper.get(Type.VAR_INT, 1);
                EntityType entityType = Entity1_15Types.getTypeFromId(type);
                tracker(wrapper.user()).addEntity(wrapper.get(Type.VAR_INT, 0), entityType);
                wrapper.set(Type.VAR_INT, 1, EntityTypeMapping.getOldEntityId(type));
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_15.RESPAWN, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.INT);
            // Seed
            map(Type.LONG, Type.NOTHING);
        }
    });
    protocol.registerClientbound(ClientboundPackets1_15.JOIN_GAME, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.INT);
            // 1 - Gamemode
            map(Type.UNSIGNED_BYTE);
            // 2 - Dimension
            map(Type.INT);
            // Seed
            map(Type.LONG, Type.NOTHING);
            // 3 - Max Players
            map(Type.UNSIGNED_BYTE);
            // 4 - Level Type
            map(Type.STRING);
            // 5 - View Distance
            map(Type.VAR_INT);
            // 6 - Reduce Debug Info
            map(Type.BOOLEAN);
            handler(getTrackerHandler(Entity1_15Types.PLAYER, Type.INT));
            handler(wrapper -> {
                // Inverted
                boolean immediateRespawn = !wrapper.read(Type.BOOLEAN);
                wrapper.user().get(ImmediateRespawn.class).setImmediateRespawn(immediateRespawn);
            });
        }
    });
    registerTracker(ClientboundPackets1_15.SPAWN_EXPERIENCE_ORB, Entity1_15Types.EXPERIENCE_ORB);
    registerTracker(ClientboundPackets1_15.SPAWN_GLOBAL_ENTITY, Entity1_15Types.LIGHTNING_BOLT);
    registerTracker(ClientboundPackets1_15.SPAWN_PAINTING, Entity1_15Types.PAINTING);
    protocol.registerClientbound(ClientboundPackets1_15.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);
            // Metadata is no longer sent in 1.15, so we have to send an empty one
            handler(wrapper -> wrapper.write(Types1_14.METADATA_LIST, new ArrayList<>()));
            handler(getTrackerHandler(Entity1_15Types.PLAYER, Type.VAR_INT));
        }
    });
    registerRemoveEntities(ClientboundPackets1_15.DESTROY_ENTITIES);
    registerMetadataRewriter(ClientboundPackets1_15.ENTITY_METADATA, Types1_14.METADATA_LIST);
    // Attributes (get rid of generic.flyingSpeed for the Bee remap)
    protocol.registerClientbound(ClientboundPackets1_15.ENTITY_PROPERTIES, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.VAR_INT);
            map(Type.INT);
            handler(wrapper -> {
                int entityId = wrapper.get(Type.VAR_INT, 0);
                EntityType entityType = tracker(wrapper.user()).entityType(entityId);
                if (entityType != Entity1_15Types.BEE)
                    return;
                int size = wrapper.get(Type.INT, 0);
                int newSize = size;
                for (int i = 0; i < size; i++) {
                    String key = wrapper.read(Type.STRING);
                    if (key.equals("generic.flyingSpeed")) {
                        newSize--;
                        wrapper.read(Type.DOUBLE);
                        int modSize = wrapper.read(Type.VAR_INT);
                        for (int j = 0; j < modSize; j++) {
                            wrapper.read(Type.UUID);
                            wrapper.read(Type.DOUBLE);
                            wrapper.read(Type.BYTE);
                        }
                    } else {
                        wrapper.write(Type.STRING, key);
                        wrapper.passthrough(Type.DOUBLE);
                        int modSize = wrapper.passthrough(Type.VAR_INT);
                        for (int j = 0; j < modSize; j++) {
                            wrapper.passthrough(Type.UUID);
                            wrapper.passthrough(Type.DOUBLE);
                            wrapper.passthrough(Type.BYTE);
                        }
                    }
                }
                if (newSize != size) {
                    wrapper.set(Type.INT, 0, newSize);
                }
            });
        }
    });
}
Also used : Entity1_15Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_15Types) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) EntityType(com.viaversion.viaversion.api.minecraft.entities.EntityType) ServerboundPackets1_14(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ServerboundPackets1_14) EntityTypeMapping(com.viaversion.viabackwards.protocol.protocol1_14_4to1_15.data.EntityTypeMapping) Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata) Types1_14(com.viaversion.viaversion.api.type.types.version.Types1_14) ArrayList(java.util.ArrayList) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) ImmediateRespawn(com.viaversion.viabackwards.protocol.protocol1_14_4to1_15.data.ImmediateRespawn) Protocol1_14_4To1_15(com.viaversion.viabackwards.protocol.protocol1_14_4to1_15.Protocol1_14_4To1_15) 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) ImmediateRespawn(com.viaversion.viabackwards.protocol.protocol1_14_4to1_15.data.ImmediateRespawn) Protocol1_14_4To1_15(com.viaversion.viabackwards.protocol.protocol1_14_4to1_15.Protocol1_14_4To1_15) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)

Example 3 with ServerboundPackets1_14

use of com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ServerboundPackets1_14 in project ViaVersion by ViaVersion.

the class InventoryPackets method registerPackets.

@Override
public void registerPackets() {
    registerSetCooldown(ClientboundPackets1_13.COOLDOWN);
    registerAdvancements(ClientboundPackets1_13.ADVANCEMENTS, Type.FLAT_VAR_INT_ITEM);
    protocol.registerClientbound(ClientboundPackets1_13.OPEN_WINDOW, null, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    Short windowId = wrapper.read(Type.UNSIGNED_BYTE);
                    String type = wrapper.read(Type.STRING);
                    JsonElement title = wrapper.read(Type.COMPONENT);
                    COMPONENT_REWRITER.processText(title);
                    Short slots = wrapper.read(Type.UNSIGNED_BYTE);
                    if (type.equals("EntityHorse")) {
                        wrapper.setId(0x1F);
                        int entityId = wrapper.read(Type.INT);
                        wrapper.write(Type.UNSIGNED_BYTE, windowId);
                        wrapper.write(Type.VAR_INT, slots.intValue());
                        wrapper.write(Type.INT, entityId);
                    } else {
                        wrapper.setId(0x2E);
                        wrapper.write(Type.VAR_INT, windowId.intValue());
                        int typeId = -1;
                        switch(type) {
                            case "minecraft:crafting_table":
                                typeId = 11;
                                break;
                            case "minecraft:furnace":
                                typeId = 13;
                                break;
                            case "minecraft:dropper":
                            case "minecraft:dispenser":
                                typeId = 6;
                                break;
                            case "minecraft:enchanting_table":
                                typeId = 12;
                                break;
                            case "minecraft:brewing_stand":
                                typeId = 10;
                                break;
                            case "minecraft:villager":
                                typeId = 18;
                                break;
                            case "minecraft:beacon":
                                typeId = 8;
                                break;
                            case "minecraft:anvil":
                                typeId = 7;
                                break;
                            case "minecraft:hopper":
                                typeId = 15;
                                break;
                            case "minecraft:shulker_box":
                                typeId = 19;
                                break;
                            case "minecraft:container":
                            case "minecraft:chest":
                            default:
                                if (slots > 0 && slots <= 54) {
                                    typeId = slots / 9 - 1;
                                }
                                break;
                        }
                        if (typeId == -1) {
                            Via.getPlatform().getLogger().warning("Can't open inventory for 1.14 player! Type: " + type + " Size: " + slots);
                        }
                        wrapper.write(Type.VAR_INT, typeId);
                        wrapper.write(Type.COMPONENT, title);
                    }
                }
            });
        }
    });
    registerWindowItems(ClientboundPackets1_13.WINDOW_ITEMS, Type.FLAT_VAR_INT_ITEM_ARRAY);
    registerSetSlot(ClientboundPackets1_13.SET_SLOT, Type.FLAT_VAR_INT_ITEM);
    protocol.registerClientbound(ClientboundPackets1_13.PLUGIN_MESSAGE, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    String channel = wrapper.get(Type.STRING, 0);
                    if (channel.equals("minecraft:trader_list") || channel.equals("trader_list")) {
                        wrapper.setId(0x27);
                        wrapper.resetReader();
                        // Remove channel
                        wrapper.read(Type.STRING);
                        int windowId = wrapper.read(Type.INT);
                        EntityTracker1_14 tracker = wrapper.user().getEntityTracker(Protocol1_14To1_13_2.class);
                        tracker.setLatestTradeWindowId(windowId);
                        wrapper.write(Type.VAR_INT, windowId);
                        int size = wrapper.passthrough(Type.UNSIGNED_BYTE);
                        for (int i = 0; i < size; i++) {
                            // Input Item
                            handleItemToClient(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM));
                            // Output Item
                            handleItemToClient(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM));
                            // Has second item
                            boolean secondItem = wrapper.passthrough(Type.BOOLEAN);
                            if (secondItem) {
                                // Second Item
                                handleItemToClient(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM));
                            }
                            // Trade disabled
                            wrapper.passthrough(Type.BOOLEAN);
                            // Number of tools uses
                            wrapper.passthrough(Type.INT);
                            // Maximum number of trade uses
                            wrapper.passthrough(Type.INT);
                            wrapper.write(Type.INT, 0);
                            wrapper.write(Type.INT, 0);
                            wrapper.write(Type.FLOAT, 0f);
                        }
                        wrapper.write(Type.VAR_INT, 0);
                        wrapper.write(Type.VAR_INT, 0);
                        wrapper.write(Type.BOOLEAN, false);
                    } else if (channel.equals("minecraft:book_open") || channel.equals("book_open")) {
                        int hand = wrapper.read(Type.VAR_INT);
                        wrapper.clearPacket();
                        wrapper.setId(0x2D);
                        wrapper.write(Type.VAR_INT, hand);
                    }
                }
            });
        }
    });
    registerEntityEquipment(ClientboundPackets1_13.ENTITY_EQUIPMENT, Type.FLAT_VAR_INT_ITEM);
    RecipeRewriter recipeRewriter = new RecipeRewriter1_13_2(protocol);
    protocol.registerClientbound(ClientboundPackets1_13.DECLARE_RECIPES, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                int size = wrapper.passthrough(Type.VAR_INT);
                int deleted = 0;
                for (int i = 0; i < size; i++) {
                    // Recipe Identifier
                    String id = wrapper.read(Type.STRING);
                    String type = wrapper.read(Type.STRING);
                    if (REMOVED_RECIPE_TYPES.contains(type)) {
                        deleted++;
                        continue;
                    }
                    wrapper.write(Type.STRING, type);
                    wrapper.write(Type.STRING, id);
                    recipeRewriter.handle(wrapper, type);
                }
                wrapper.set(Type.VAR_INT, 0, size - deleted);
            });
        }
    });
    registerClickWindow(ServerboundPackets1_14.CLICK_WINDOW, Type.FLAT_VAR_INT_ITEM);
    protocol.registerServerbound(ServerboundPackets1_14.SELECT_TRADE, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    // Selecting trade now moves the items, we need to resync the inventory
                    PacketWrapper resyncPacket = wrapper.create(0x08);
                    EntityTracker1_14 tracker = wrapper.user().getEntityTracker(Protocol1_14To1_13_2.class);
                    // 0 - Window ID
                    resyncPacket.write(Type.UNSIGNED_BYTE, ((short) tracker.getLatestTradeWindowId()));
                    // 1 - Slot
                    resyncPacket.write(Type.SHORT, ((short) -999));
                    // 2 - Button - End left click
                    resyncPacket.write(Type.BYTE, (byte) 2);
                    // 3 - Action number
                    resyncPacket.write(Type.SHORT, ((short) ThreadLocalRandom.current().nextInt()));
                    // 4 - Mode - Drag
                    resyncPacket.write(Type.VAR_INT, 5);
                    CompoundTag tag = new CompoundTag();
                    // Tags with NaN are not equal
                    tag.put("force_resync", new DoubleTag(Double.NaN));
                    // 5 - Clicked Item
                    resyncPacket.write(Type.FLAT_VAR_INT_ITEM, new DataItem(1, (byte) 1, (short) 0, tag));
                    resyncPacket.scheduleSendToServer(Protocol1_14To1_13_2.class);
                }
            });
        }
    });
    registerCreativeInvAction(ServerboundPackets1_14.CREATIVE_INVENTORY_ACTION, Type.FLAT_VAR_INT_ITEM);
    registerSpawnParticle(ClientboundPackets1_13.SPAWN_PARTICLE, Type.FLAT_VAR_INT_ITEM, Type.FLOAT);
}
Also used : PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) ComponentRewriter(com.viaversion.viaversion.rewriter.ComponentRewriter) JsonObject(com.google.gson.JsonObject) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) Item(com.viaversion.viaversion.api.minecraft.item.Item) JsonElement(com.google.gson.JsonElement) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) Protocol1_14To1_13_2(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.Protocol1_14To1_13_2) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) DataItem(com.viaversion.viaversion.api.minecraft.item.DataItem) ItemRewriter(com.viaversion.viaversion.rewriter.ItemRewriter) EntityTracker1_14(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.storage.EntityTracker1_14) ListTag(com.github.steveice10.opennbt.tag.builtin.ListTag) Set(java.util.Set) RecipeRewriter1_13_2(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.RecipeRewriter1_13_2) Via(com.viaversion.viaversion.api.Via) ServerboundPackets1_14(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ServerboundPackets1_14) Sets(com.google.common.collect.Sets) RecipeRewriter(com.viaversion.viaversion.rewriter.RecipeRewriter) StringTag(com.github.steveice10.opennbt.tag.builtin.StringTag) ClientboundPackets1_13(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13) Tag(com.github.steveice10.opennbt.tag.builtin.Tag) ChatRewriter(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ChatRewriter) DoubleTag(com.github.steveice10.opennbt.tag.builtin.DoubleTag) EntityTracker1_14(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.storage.EntityTracker1_14) DataItem(com.viaversion.viaversion.api.minecraft.item.DataItem) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) RecipeRewriter(com.viaversion.viaversion.rewriter.RecipeRewriter) RecipeRewriter1_13_2(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.RecipeRewriter1_13_2) DoubleTag(com.github.steveice10.opennbt.tag.builtin.DoubleTag) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) JsonElement(com.google.gson.JsonElement) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag)

Example 4 with ServerboundPackets1_14

use of com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ServerboundPackets1_14 in project ViaVersion by ViaVersion.

the class InventoryPackets method registerPackets.

@Override
public void registerPackets() {
    registerSetCooldown(ClientboundPackets1_14.COOLDOWN);
    registerWindowItems(ClientboundPackets1_14.WINDOW_ITEMS, Type.FLAT_VAR_INT_ITEM_ARRAY);
    registerTradeList(ClientboundPackets1_14.TRADE_LIST, Type.FLAT_VAR_INT_ITEM);
    registerSetSlot(ClientboundPackets1_14.SET_SLOT, Type.FLAT_VAR_INT_ITEM);
    registerEntityEquipment(ClientboundPackets1_14.ENTITY_EQUIPMENT, Type.FLAT_VAR_INT_ITEM);
    registerAdvancements(ClientboundPackets1_14.ADVANCEMENTS, Type.FLAT_VAR_INT_ITEM);
    new RecipeRewriter1_14(protocol).registerDefaultHandler(ClientboundPackets1_14.DECLARE_RECIPES);
    registerClickWindow(ServerboundPackets1_14.CLICK_WINDOW, Type.FLAT_VAR_INT_ITEM);
    registerCreativeInvAction(ServerboundPackets1_14.CREATIVE_INVENTORY_ACTION, Type.FLAT_VAR_INT_ITEM);
}
Also used : RecipeRewriter1_14(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.data.RecipeRewriter1_14)

Example 5 with ServerboundPackets1_14

use of com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ServerboundPackets1_14 in project ViaVersion by ViaVersion.

the class Protocol1_15To1_14_4 method registerPackets.

@Override
protected void registerPackets() {
    metadataRewriter.register();
    itemRewriter.register();
    EntityPackets.register(this);
    PlayerPackets.register(this);
    WorldPackets.register(this);
    SoundRewriter soundRewriter = new SoundRewriter(this);
    // Entity Sound Effect (added somewhere in 1.14)
    soundRewriter.registerSound(ClientboundPackets1_14.ENTITY_SOUND);
    soundRewriter.registerSound(ClientboundPackets1_14.SOUND);
    new StatisticsRewriter(this).register(ClientboundPackets1_14.STATISTICS);
    registerServerbound(ServerboundPackets1_14.EDIT_BOOK, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> itemRewriter.handleItemToServer(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)));
        }
    });
    tagRewriter = new TagRewriter(this);
    tagRewriter.register(ClientboundPackets1_14.TAGS, RegistryType.ENTITY);
}
Also used : InventoryPackets(com.viaversion.viaversion.protocols.protocol1_15to1_14_4.packets.InventoryPackets) Entity1_15Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_15Types) SoundRewriter(com.viaversion.viaversion.rewriter.SoundRewriter) MappingData(com.viaversion.viaversion.protocols.protocol1_15to1_14_4.data.MappingData) MetadataRewriter1_15To1_14_4(com.viaversion.viaversion.protocols.protocol1_15to1_14_4.metadata.MetadataRewriter1_15To1_14_4) EntityPackets(com.viaversion.viaversion.protocols.protocol1_15to1_14_4.packets.EntityPackets) AbstractProtocol(com.viaversion.viaversion.api.protocol.AbstractProtocol) ServerboundPackets1_14(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ServerboundPackets1_14) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) RegistryType(com.viaversion.viaversion.api.minecraft.RegistryType) Type(com.viaversion.viaversion.api.type.Type) TagRewriter(com.viaversion.viaversion.rewriter.TagRewriter) StatisticsRewriter(com.viaversion.viaversion.rewriter.StatisticsRewriter) EntityTrackerBase(com.viaversion.viaversion.data.entity.EntityTrackerBase) UserConnection(com.viaversion.viaversion.api.connection.UserConnection) ItemRewriter(com.viaversion.viaversion.api.rewriter.ItemRewriter) ClientboundPackets1_14(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14) EntityRewriter(com.viaversion.viaversion.api.rewriter.EntityRewriter) PlayerPackets(com.viaversion.viaversion.protocols.protocol1_15to1_14_4.packets.PlayerPackets) WorldPackets(com.viaversion.viaversion.protocols.protocol1_15to1_14_4.packets.WorldPackets) StatisticsRewriter(com.viaversion.viaversion.rewriter.StatisticsRewriter) TagRewriter(com.viaversion.viaversion.rewriter.TagRewriter) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) SoundRewriter(com.viaversion.viaversion.rewriter.SoundRewriter)

Aggregations

PacketRemapper (com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)7 Type (com.viaversion.viaversion.api.type.Type)6 ServerboundPackets1_14 (com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ServerboundPackets1_14)6 PacketWrapper (com.viaversion.viaversion.api.protocol.packet.PacketWrapper)5 ClientboundPackets1_15 (com.viaversion.viaversion.protocols.protocol1_15to1_14_4.ClientboundPackets1_15)4 Item (com.viaversion.viaversion.api.minecraft.item.Item)3 PacketHandler (com.viaversion.viaversion.api.protocol.remapper.PacketHandler)3 RecipeRewriter1_14 (com.viaversion.viaversion.protocols.protocol1_14to1_13_2.data.RecipeRewriter1_14)3 Protocol1_14_4To1_15 (com.viaversion.viabackwards.protocol.protocol1_14_4to1_15.Protocol1_14_4To1_15)2 UserConnection (com.viaversion.viaversion.api.connection.UserConnection)2 RegistryType (com.viaversion.viaversion.api.minecraft.RegistryType)2 Chunk (com.viaversion.viaversion.api.minecraft.chunks.Chunk)2 ChunkSection (com.viaversion.viaversion.api.minecraft.chunks.ChunkSection)2 Entity1_15Types (com.viaversion.viaversion.api.minecraft.entities.Entity1_15Types)2 EntityRewriter (com.viaversion.viaversion.api.rewriter.EntityRewriter)2 StatisticsRewriter (com.viaversion.viaversion.rewriter.StatisticsRewriter)2 ArrayList (java.util.ArrayList)2 CompoundTag (com.github.steveice10.opennbt.tag.builtin.CompoundTag)1 DoubleTag (com.github.steveice10.opennbt.tag.builtin.DoubleTag)1 ListTag (com.github.steveice10.opennbt.tag.builtin.ListTag)1