Search in sources :

Example 51 with PacketHandler

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

the class WorldPackets method register.

public static void register(Protocol1_15To1_14_4 protocol) {
    BlockRewriter blockRewriter = new BlockRewriter(protocol, Type.POSITION1_14);
    blockRewriter.registerBlockAction(ClientboundPackets1_14.BLOCK_ACTION);
    blockRewriter.registerBlockChange(ClientboundPackets1_14.BLOCK_CHANGE);
    blockRewriter.registerMultiBlockChange(ClientboundPackets1_14.MULTI_BLOCK_CHANGE);
    blockRewriter.registerAcknowledgePlayerDigging(ClientboundPackets1_14.ACKNOWLEDGE_PLAYER_DIGGING);
    protocol.registerClientbound(ClientboundPackets1_14.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_14Type());
                    wrapper.write(new Chunk1_15Type(), chunk);
                    if (chunk.isFullChunk()) {
                        int[] biomeData = chunk.getBiomeData();
                        int[] newBiomeData = new int[1024];
                        if (biomeData != null) {
                            // Now in 4x4x4 areas - take the biome of each "middle"
                            for (int i = 0; i < 4; ++i) {
                                for (int j = 0; j < 4; ++j) {
                                    int x = (j << 2) + 2;
                                    int z = (i << 2) + 2;
                                    int oldIndex = (z << 4 | x);
                                    newBiomeData[i << 2 | j] = biomeData[oldIndex];
                                }
                            }
                            // ... and copy it to the new y layers
                            for (int i = 1; i < 64; ++i) {
                                System.arraycopy(newBiomeData, 0, newBiomeData, i * 16, 16);
                            }
                        }
                        chunk.setBiomeData(newBiomeData);
                    }
                    for (int s = 0; s < chunk.getSections().length; s++) {
                        ChunkSection section = chunk.getSections()[s];
                        if (section == null)
                            continue;
                        for (int i = 0; i < section.getPaletteSize(); i++) {
                            int old = section.getPaletteEntry(i);
                            int newId = protocol.getMappingData().getNewBlockStateId(old);
                            section.setPaletteEntry(i, newId);
                        }
                    }
                }
            });
        }
    });
    blockRewriter.registerEffect(ClientboundPackets1_14.EFFECT, 1010, 2001);
    protocol.registerClientbound(ClientboundPackets1_14.SPAWN_PARTICLE, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Particle ID
            map(Type.INT);
            // 1 - Long Distance
            map(Type.BOOLEAN);
            // 2 - X
            map(Type.FLOAT, Type.DOUBLE);
            // 3 - Y
            map(Type.FLOAT, Type.DOUBLE);
            // 4 - Z
            map(Type.FLOAT, Type.DOUBLE);
            // 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) {
                        protocol.getItemRewriter().handleItemToClient(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM));
                    }
                }
            });
        }
    });
}
Also used : BlockRewriter(com.viaversion.viaversion.rewriter.BlockRewriter) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) 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) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection)

Example 52 with PacketHandler

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

the class Protocol1_14To1_13_2 method registerPackets.

@Override
protected void registerPackets() {
    metadataRewriter.register();
    itemRewriter.register();
    EntityPackets.register(this);
    WorldPackets.register(this);
    PlayerPackets.register(this);
    new SoundRewriter(this).registerSound(ClientboundPackets1_13.SOUND);
    new StatisticsRewriter(this).register(ClientboundPackets1_13.STATISTICS);
    ComponentRewriter componentRewriter = new ComponentRewriter1_14(this);
    componentRewriter.registerComponentPacket(ClientboundPackets1_13.CHAT_MESSAGE);
    CommandRewriter1_14 commandRewriter = new CommandRewriter1_14(this);
    commandRewriter.registerDeclareCommands(ClientboundPackets1_13.DECLARE_COMMANDS);
    registerClientbound(ClientboundPackets1_13.TAGS, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int blockTagsSize = wrapper.read(Type.VAR_INT);
                    // block tags
                    wrapper.write(Type.VAR_INT, blockTagsSize + 6);
                    for (int i = 0; i < blockTagsSize; i++) {
                        wrapper.passthrough(Type.STRING);
                        int[] blockIds = wrapper.passthrough(Type.VAR_INT_ARRAY_PRIMITIVE);
                        for (int j = 0; j < blockIds.length; j++) {
                            blockIds[j] = getMappingData().getNewBlockId(blockIds[j]);
                        }
                    }
                    // Minecraft crashes if we not send signs tags
                    wrapper.write(Type.STRING, "minecraft:signs");
                    wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, new int[] { getMappingData().getNewBlockId(150), getMappingData().getNewBlockId(155) });
                    wrapper.write(Type.STRING, "minecraft:wall_signs");
                    wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, new int[] { getMappingData().getNewBlockId(155) });
                    wrapper.write(Type.STRING, "minecraft:standing_signs");
                    wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, new int[] { getMappingData().getNewBlockId(150) });
                    // Fences and walls tags - used for block connections
                    wrapper.write(Type.STRING, "minecraft:fences");
                    wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, new int[] { 189, 248, 472, 473, 474, 475 });
                    wrapper.write(Type.STRING, "minecraft:walls");
                    wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, new int[] { 271, 272 });
                    wrapper.write(Type.STRING, "minecraft:wooden_fences");
                    wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, new int[] { 189, 472, 473, 474, 475 });
                    int itemTagsSize = wrapper.read(Type.VAR_INT);
                    // item tags
                    wrapper.write(Type.VAR_INT, itemTagsSize + 2);
                    for (int i = 0; i < itemTagsSize; i++) {
                        wrapper.passthrough(Type.STRING);
                        int[] itemIds = wrapper.passthrough(Type.VAR_INT_ARRAY_PRIMITIVE);
                        for (int j = 0; j < itemIds.length; j++) {
                            itemIds[j] = getMappingData().getNewItemId(itemIds[j]);
                        }
                    }
                    // Should fix fuel shift clicking
                    wrapper.write(Type.STRING, "minecraft:signs");
                    wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, new int[] { getMappingData().getNewItemId(541) });
                    // Arrows tag (used by bow)
                    wrapper.write(Type.STRING, "minecraft:arrows");
                    wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, new int[] { 526, 825, 826 });
                    // fluid tags
                    int fluidTagsSize = wrapper.passthrough(Type.VAR_INT);
                    for (int i = 0; i < fluidTagsSize; i++) {
                        wrapper.passthrough(Type.STRING);
                        wrapper.passthrough(Type.VAR_INT_ARRAY_PRIMITIVE);
                    }
                    // new entity tags - do we need to send this?
                    wrapper.write(Type.VAR_INT, 0);
                }
            });
        }
    });
    // Set Difficulty packet added in 19w11a
    cancelServerbound(ServerboundPackets1_14.SET_DIFFICULTY);
    // Lock Difficulty packet added in 19w11a
    cancelServerbound(ServerboundPackets1_14.LOCK_DIFFICULTY);
    // Unknown packet added in 19w13a
    cancelServerbound(ServerboundPackets1_14.UPDATE_JIGSAW_BLOCK);
}
Also used : StatisticsRewriter(com.viaversion.viaversion.rewriter.StatisticsRewriter) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) ComponentRewriter(com.viaversion.viaversion.rewriter.ComponentRewriter) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) CommandRewriter1_14(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.data.CommandRewriter1_14) SoundRewriter(com.viaversion.viaversion.rewriter.SoundRewriter) ComponentRewriter1_14(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.data.ComponentRewriter1_14)

Example 53 with PacketHandler

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

the class EntityPackets method register.

public static void register(Protocol1_13To1_12_2 protocol) {
    MetadataRewriter1_13To1_12_2 metadataRewriter = protocol.get(MetadataRewriter1_13To1_12_2.class);
    protocol.registerClientbound(ClientboundPackets1_12_1.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);
            // Track Entity
            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)
                        return;
                    // Register Type ID
                    wrapper.user().getEntityTracker(Protocol1_13To1_12_2.class).addEntity(entityId, entType);
                    if (entType.is(Entity1_13Types.EntityType.FALLING_BLOCK)) {
                        int oldId = wrapper.get(Type.INT, 0);
                        int combined = (((oldId & 4095) << 4) | (oldId >> 12 & 15));
                        wrapper.set(Type.INT, 0, WorldPackets.toNewId(combined));
                    }
                    // Fix ItemFrame hitbox
                    if (entType.is(Entity1_13Types.EntityType.ITEM_FRAME)) {
                        int data = wrapper.get(Type.INT, 0);
                        switch(data) {
                            // South
                            case 0:
                                data = 3;
                                break;
                            // West
                            case 1:
                                data = 4;
                                break;
                            // East
                            case 3:
                                data = 5;
                                break;
                        }
                        wrapper.set(Type.INT, 0, data);
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_12_1.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_12.METADATA_LIST, Types1_13.METADATA_LIST);
            handler(metadataRewriter.trackerAndRewriterHandler(Types1_13.METADATA_LIST));
        }
    });
    protocol.registerClientbound(ClientboundPackets1_12_1.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_12.METADATA_LIST, Types1_13.METADATA_LIST);
            handler(metadataRewriter.trackerAndRewriterHandler(Types1_13.METADATA_LIST, Entity1_13Types.EntityType.PLAYER));
        }
    });
    metadataRewriter.registerRemoveEntities(ClientboundPackets1_12_1.DESTROY_ENTITIES);
    metadataRewriter.registerMetadataRewriter(ClientboundPackets1_12_1.ENTITY_METADATA, Types1_12.METADATA_LIST, 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) Protocol1_13To1_12_2(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.Protocol1_13To1_12_2) MetadataRewriter1_13To1_12_2(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.metadata.MetadataRewriter1_13To1_12_2) Entity1_13Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_13Types)

Example 54 with PacketHandler

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

the class InventoryPackets method register.

public static void register(Protocol protocol) {
    protocol.registerClientbound(ClientboundPackets1_8.WINDOW_PROPERTY, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Window ID
            map(Type.UNSIGNED_BYTE);
            // 1 - Property Key
            map(Type.SHORT);
            // 2 - Property Value
            map(Type.SHORT);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    final short windowId = wrapper.get(Type.UNSIGNED_BYTE, 0);
                    final short property = wrapper.get(Type.SHORT, 0);
                    short value = wrapper.get(Type.SHORT, 1);
                    InventoryTracker inventoryTracker = wrapper.user().get(InventoryTracker.class);
                    if (inventoryTracker.getInventory() != null) {
                        if (inventoryTracker.getInventory().equalsIgnoreCase("minecraft:enchanting_table")) {
                            if (property > 3 && property < 7) {
                                // Send 2 properties, splitting it into enchantID & level
                                final short level = (short) (value >> 8);
                                final short enchantID = (short) (value & 0xFF);
                                wrapper.create(wrapper.getId(), new PacketHandler() {

                                    @Override
                                    public void handle(PacketWrapper wrapper) throws Exception {
                                        wrapper.write(Type.UNSIGNED_BYTE, windowId);
                                        wrapper.write(Type.SHORT, property);
                                        wrapper.write(Type.SHORT, enchantID);
                                    }
                                }).scheduleSend(Protocol1_9To1_8.class);
                                wrapper.set(Type.SHORT, 0, (short) (property + 3));
                                wrapper.set(Type.SHORT, 1, level);
                            }
                        }
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.OPEN_WINDOW, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Window ID
            map(Type.UNSIGNED_BYTE);
            // 1 - Window Type
            map(Type.STRING);
            // 2 - Window Title
            map(Type.STRING, Protocol1_9To1_8.FIX_JSON);
            // 3 - Slot Count
            map(Type.UNSIGNED_BYTE);
            // There is a horse parameter after this, we don't handle it and let it passthrough
            // Inventory tracking
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    String inventory = wrapper.get(Type.STRING, 0);
                    InventoryTracker inventoryTracker = wrapper.user().get(InventoryTracker.class);
                    inventoryTracker.setInventory(inventory);
                }
            });
            // Brewing patch
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    String inventory = wrapper.get(Type.STRING, 0);
                    if (inventory.equals("minecraft:brewing_stand")) {
                        wrapper.set(Type.UNSIGNED_BYTE, 1, (short) (wrapper.get(Type.UNSIGNED_BYTE, 1) + 1));
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.SET_SLOT, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Window ID
            map(Type.UNSIGNED_BYTE);
            // 1 - Slot ID
            map(Type.SHORT);
            // 2 - Slot Value
            map(Type.ITEM);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    Item stack = wrapper.get(Type.ITEM, 0);
                    boolean showShieldWhenSwordInHand = Via.getConfig().isShowShieldWhenSwordInHand() && Via.getConfig().isShieldBlocking();
                    // Check if it is the inventory of the player
                    if (showShieldWhenSwordInHand) {
                        InventoryTracker inventoryTracker = wrapper.user().get(InventoryTracker.class);
                        EntityTracker1_9 entityTracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                        short slotID = wrapper.get(Type.SHORT, 0);
                        byte windowId = wrapper.get(Type.UNSIGNED_BYTE, 0).byteValue();
                        // Store item in slot
                        inventoryTracker.setItemId(windowId, slotID, stack == null ? 0 : stack.identifier());
                        // Sync shield item in offhand with main hand
                        entityTracker.syncShieldWithSword();
                    }
                    ItemRewriter.toClient(stack);
                }
            });
            // Brewing patch
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    InventoryTracker inventoryTracker = wrapper.user().get(InventoryTracker.class);
                    short slotID = wrapper.get(Type.SHORT, 0);
                    if (inventoryTracker.getInventory() != null) {
                        if (inventoryTracker.getInventory().equals("minecraft:brewing_stand")) {
                            if (slotID >= 4) {
                                wrapper.set(Type.SHORT, 0, (short) (slotID + 1));
                            }
                        }
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.WINDOW_ITEMS, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Window ID
            map(Type.UNSIGNED_BYTE);
            // 1 - Window Values
            map(Type.ITEM_ARRAY);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    Item[] stacks = wrapper.get(Type.ITEM_ARRAY, 0);
                    Short windowId = wrapper.get(Type.UNSIGNED_BYTE, 0);
                    InventoryTracker inventoryTracker = wrapper.user().get(InventoryTracker.class);
                    EntityTracker1_9 entityTracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                    boolean showShieldWhenSwordInHand = Via.getConfig().isShowShieldWhenSwordInHand() && Via.getConfig().isShieldBlocking();
                    for (short i = 0; i < stacks.length; i++) {
                        Item stack = stacks[i];
                        // Store items in slots
                        if (showShieldWhenSwordInHand) {
                            inventoryTracker.setItemId(windowId, i, stack == null ? 0 : stack.identifier());
                        }
                        ItemRewriter.toClient(stack);
                    }
                    // Sync shield item in offhand with main hand
                    if (showShieldWhenSwordInHand) {
                        entityTracker.syncShieldWithSword();
                    }
                }
            });
            // Brewing Patch
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    InventoryTracker inventoryTracker = wrapper.user().get(InventoryTracker.class);
                    if (inventoryTracker.getInventory() != null) {
                        if (inventoryTracker.getInventory().equals("minecraft:brewing_stand")) {
                            Item[] oldStack = wrapper.get(Type.ITEM_ARRAY, 0);
                            Item[] newStack = new Item[oldStack.length + 1];
                            for (int i = 0; i < newStack.length; i++) {
                                if (i > 4) {
                                    newStack[i] = oldStack[i - 1];
                                } else {
                                    if (i != 4) {
                                        // Leave index 3 blank
                                        newStack[i] = oldStack[i];
                                    }
                                }
                            }
                            wrapper.set(Type.ITEM_ARRAY, 0, newStack);
                        }
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.CLOSE_WINDOW, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Window ID
            map(Type.UNSIGNED_BYTE);
            // Inventory tracking
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    InventoryTracker inventoryTracker = wrapper.user().get(InventoryTracker.class);
                    inventoryTracker.setInventory(null);
                    inventoryTracker.resetInventory(wrapper.get(Type.UNSIGNED_BYTE, 0));
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.MAP_DATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Map ID
            map(Type.VAR_INT);
            // 1 - Map Scale
            map(Type.BYTE);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) {
                    // 2 - Show marker
                    wrapper.write(Type.BOOLEAN, true);
                }
            });
        // Everything else is passed through
        }
    });
    /* Incoming Packets */
    protocol.registerServerbound(ServerboundPackets1_9.CREATIVE_INVENTORY_ACTION, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Slot ID
            map(Type.SHORT);
            // 1 - Item
            map(Type.ITEM);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    Item stack = wrapper.get(Type.ITEM, 0);
                    boolean showShieldWhenSwordInHand = Via.getConfig().isShowShieldWhenSwordInHand() && Via.getConfig().isShieldBlocking();
                    if (showShieldWhenSwordInHand) {
                        InventoryTracker inventoryTracker = wrapper.user().get(InventoryTracker.class);
                        EntityTracker1_9 entityTracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                        short slotID = wrapper.get(Type.SHORT, 0);
                        // Update item in slot
                        inventoryTracker.setItemId((short) 0, slotID, stack == null ? 0 : stack.identifier());
                        // Sync shield item in offhand with main hand
                        entityTracker.syncShieldWithSword();
                    }
                    ItemRewriter.toServer(stack);
                }
            });
            // Elytra throw patch
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    final short slot = wrapper.get(Type.SHORT, 0);
                    boolean throwItem = (slot == 45);
                    if (throwItem) {
                        // Send a packet wiping the slot
                        wrapper.create(ClientboundPackets1_9.SET_SLOT, new PacketHandler() {

                            @Override
                            public void handle(PacketWrapper wrapper) throws Exception {
                                wrapper.write(Type.UNSIGNED_BYTE, (short) 0);
                                wrapper.write(Type.SHORT, slot);
                                wrapper.write(Type.ITEM, null);
                            }
                        }).send(Protocol1_9To1_8.class);
                        // Finally reset to simulate throwing item
                        // Set slot to -999
                        wrapper.set(Type.SHORT, 0, (short) -999);
                    }
                }
            });
        }
    });
    protocol.registerServerbound(ServerboundPackets1_9.CLICK_WINDOW, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Window ID
            map(Type.UNSIGNED_BYTE);
            // 1 - Slot ID
            map(Type.SHORT);
            // 2 - Button
            map(Type.BYTE);
            // 3 - Action
            map(Type.SHORT);
            // 4 - Mode
            map(Type.VAR_INT, Type.BYTE);
            // 5 - Clicked Item
            map(Type.ITEM);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    Item stack = wrapper.get(Type.ITEM, 0);
                    if (Via.getConfig().isShowShieldWhenSwordInHand()) {
                        Short windowId = wrapper.get(Type.UNSIGNED_BYTE, 0);
                        byte mode = wrapper.get(Type.BYTE, 1);
                        short hoverSlot = wrapper.get(Type.SHORT, 0);
                        byte button = wrapper.get(Type.BYTE, 0);
                        // Move items in inventory to track the sword location
                        InventoryTracker inventoryTracker = wrapper.user().get(InventoryTracker.class);
                        inventoryTracker.handleWindowClick(wrapper.user(), windowId, mode, hoverSlot, button);
                    }
                    ItemRewriter.toServer(stack);
                }
            });
            // Brewing patch and elytra throw patch
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    final short windowID = wrapper.get(Type.UNSIGNED_BYTE, 0);
                    final short slot = wrapper.get(Type.SHORT, 0);
                    boolean throwItem = (slot == 45 && windowID == 0);
                    InventoryTracker inventoryTracker = wrapper.user().get(InventoryTracker.class);
                    if (inventoryTracker.getInventory() != null) {
                        if (inventoryTracker.getInventory().equals("minecraft:brewing_stand")) {
                            if (slot == 4) {
                                throwItem = true;
                            }
                            if (slot > 4) {
                                wrapper.set(Type.SHORT, 0, (short) (slot - 1));
                            }
                        }
                    }
                    if (throwItem) {
                        // Send a packet wiping the slot
                        wrapper.create(ClientboundPackets1_9.SET_SLOT, new PacketHandler() {

                            @Override
                            public void handle(PacketWrapper wrapper) throws Exception {
                                wrapper.write(Type.UNSIGNED_BYTE, (short) windowID);
                                wrapper.write(Type.SHORT, slot);
                                wrapper.write(Type.ITEM, null);
                            }
                        }).scheduleSend(Protocol1_9To1_8.class);
                        // Finally reset to simulate throwing item
                        // Set button to 0
                        wrapper.set(Type.BYTE, 0, (byte) 0);
                        // Set mode to 0
                        wrapper.set(Type.BYTE, 1, (byte) 0);
                        // Set slot to -999
                        wrapper.set(Type.SHORT, 0, (short) -999);
                    }
                }
            });
        }
    });
    protocol.registerServerbound(ServerboundPackets1_9.CLOSE_WINDOW, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Window ID
            map(Type.UNSIGNED_BYTE);
            // Inventory tracking
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    InventoryTracker inventoryTracker = wrapper.user().get(InventoryTracker.class);
                    inventoryTracker.setInventory(null);
                    inventoryTracker.resetInventory(wrapper.get(Type.UNSIGNED_BYTE, 0));
                }
            });
        }
    });
    protocol.registerServerbound(ServerboundPackets1_9.HELD_ITEM_CHANGE, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Slot id
            map(Type.SHORT);
            // Blocking patch
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    boolean showShieldWhenSwordInHand = Via.getConfig().isShowShieldWhenSwordInHand() && Via.getConfig().isShieldBlocking();
                    EntityTracker1_9 entityTracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                    if (entityTracker.isBlocking()) {
                        entityTracker.setBlocking(false);
                        if (!showShieldWhenSwordInHand) {
                            entityTracker.setSecondHand(null);
                        }
                    }
                    if (showShieldWhenSwordInHand) {
                        // Update current held item slot index
                        entityTracker.setHeldItemSlot(wrapper.get(Type.SHORT, 0));
                        // Sync shield item in offhand with main hand
                        entityTracker.syncShieldWithSword();
                    }
                }
            });
        }
    });
}
Also used : EntityTracker1_9(com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.EntityTracker1_9) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) InventoryTracker(com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.InventoryTracker) Item(com.viaversion.viaversion.api.minecraft.item.Item) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper)

Example 55 with PacketHandler

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

the class SpawnPackets method register.

public static void register(Protocol1_9To1_8 protocol) {
    protocol.registerClientbound(ClientboundPackets1_8.SPAWN_ENTITY, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.VAR_INT);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int entityID = wrapper.get(Type.VAR_INT, 0);
                    EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                    // 1 - UUID
                    wrapper.write(Type.UUID, tracker.getEntityUUID(entityID));
                }
            });
            // 2 - Type
            map(Type.BYTE);
            // Parse this info
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int entityID = wrapper.get(Type.VAR_INT, 0);
                    int typeID = wrapper.get(Type.BYTE, 0);
                    EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                    tracker.addEntity(entityID, Entity1_10Types.getTypeFromId(typeID, true));
                    tracker.sendMetadataBuffer(entityID);
                }
            });
            // 3 - X - Needs to be divide by 32
            map(Type.INT, toNewDouble);
            // 4 - Y - Needs to be divide by 32
            map(Type.INT, toNewDouble);
            // 5 - Z - Needs to be divide by 32
            map(Type.INT, toNewDouble);
            // 6 - Pitch
            map(Type.BYTE);
            // 7 - Yaw
            map(Type.BYTE);
            // 8 - Data
            map(Type.INT);
            // Create last 3 shorts
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    // Data (1st Integer)
                    int data = wrapper.get(Type.INT, 0);
                    short vX = 0, vY = 0, vZ = 0;
                    if (data > 0) {
                        vX = wrapper.read(Type.SHORT);
                        vY = wrapper.read(Type.SHORT);
                        vZ = wrapper.read(Type.SHORT);
                    }
                    wrapper.write(Type.SHORT, vX);
                    wrapper.write(Type.SHORT, vY);
                    wrapper.write(Type.SHORT, vZ);
                }
            });
            // Handle potions
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    final int entityID = wrapper.get(Type.VAR_INT, 0);
                    // Data
                    final int data = wrapper.get(Type.INT, 0);
                    int typeID = wrapper.get(Type.BYTE, 0);
                    if (Entity1_10Types.getTypeFromId(typeID, true) == Entity1_10Types.EntityType.SPLASH_POTION) {
                        // Convert this to meta data, woo!
                        PacketWrapper metaPacket = wrapper.create(ClientboundPackets1_9.ENTITY_METADATA, new PacketHandler() {

                            @Override
                            public void handle(PacketWrapper wrapper) throws Exception {
                                wrapper.write(Type.VAR_INT, entityID);
                                List<Metadata> meta = new ArrayList<>();
                                // Potion
                                Item item = new DataItem(373, (byte) 1, (short) data, null);
                                // Rewrite so that it gets the right nbt
                                ItemRewriter.toClient(item);
                                // TEMP FIX FOR POTIONS UNTIL WE FIGURE OUT HOW TO TRANSFORM SENT PACKETS
                                Metadata potion = new Metadata(5, MetaType1_9.Slot, item);
                                meta.add(potion);
                                wrapper.write(Types1_9.METADATA_LIST, meta);
                            }
                        });
                        // Fix packet order
                        wrapper.send(Protocol1_9To1_8.class);
                        metaPacket.send(Protocol1_9To1_8.class);
                        wrapper.cancel();
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.SPAWN_EXPERIENCE_ORB, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.VAR_INT);
            // Parse this info
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int entityID = wrapper.get(Type.VAR_INT, 0);
                    EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                    tracker.addEntity(entityID, Entity1_10Types.EntityType.EXPERIENCE_ORB);
                    tracker.sendMetadataBuffer(entityID);
                }
            });
            // 1 - X - Needs to be divide by 32
            map(Type.INT, toNewDouble);
            // 2 - Y - Needs to be divide by 32
            map(Type.INT, toNewDouble);
            // 3 - Z - Needs to be divide by 32
            map(Type.INT, toNewDouble);
            // 4 - Amount to spawn
            map(Type.SHORT);
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.SPAWN_GLOBAL_ENTITY, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.VAR_INT);
            // 1 - Type
            map(Type.BYTE);
            // Parse this info
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    // Currently only lightning uses this
                    int entityID = wrapper.get(Type.VAR_INT, 0);
                    EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                    tracker.addEntity(entityID, Entity1_10Types.EntityType.LIGHTNING);
                    tracker.sendMetadataBuffer(entityID);
                }
            });
            // 2 - X - Needs to be divide by 32
            map(Type.INT, toNewDouble);
            // 3 - Y - Needs to be divide by 32
            map(Type.INT, toNewDouble);
            // 4 - Z - Needs to be divide by 32
            map(Type.INT, toNewDouble);
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.SPAWN_MOB, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.VAR_INT);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int entityID = wrapper.get(Type.VAR_INT, 0);
                    EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                    // 1 - UUID
                    wrapper.write(Type.UUID, tracker.getEntityUUID(entityID));
                }
            });
            // 2 - Type
            map(Type.UNSIGNED_BYTE);
            // Parse this info
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int entityID = wrapper.get(Type.VAR_INT, 0);
                    int typeID = wrapper.get(Type.UNSIGNED_BYTE, 0);
                    EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                    tracker.addEntity(entityID, Entity1_10Types.getTypeFromId(typeID, false));
                    tracker.sendMetadataBuffer(entityID);
                }
            });
            // 3 - X - Needs to be divide by 32
            map(Type.INT, toNewDouble);
            // 4 - Y - Needs to be divide by 32
            map(Type.INT, toNewDouble);
            // 5 - Z - Needs to be divide by 32
            map(Type.INT, toNewDouble);
            // 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);
            map(Types1_8.METADATA_LIST, Types1_9.METADATA_LIST);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    List<Metadata> metadataList = wrapper.get(Types1_9.METADATA_LIST, 0);
                    int entityId = wrapper.get(Type.VAR_INT, 0);
                    EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                    if (tracker.hasEntity(entityId)) {
                        protocol.get(MetadataRewriter1_9To1_8.class).handleMetadata(entityId, metadataList, wrapper.user());
                    } else {
                        Via.getPlatform().getLogger().warning("Unable to find entity for metadata, entity ID: " + entityId);
                        metadataList.clear();
                    }
                }
            });
            // Handler for meta data
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    List<Metadata> metadataList = wrapper.get(Types1_9.METADATA_LIST, 0);
                    int entityID = wrapper.get(Type.VAR_INT, 0);
                    EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                    tracker.handleMetadata(entityID, metadataList);
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.SPAWN_PAINTING, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.VAR_INT);
            // Parse this info
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int entityID = wrapper.get(Type.VAR_INT, 0);
                    EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                    tracker.addEntity(entityID, Entity1_10Types.EntityType.PAINTING);
                    tracker.sendMetadataBuffer(entityID);
                }
            });
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int entityID = wrapper.get(Type.VAR_INT, 0);
                    EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                    // 1 - UUID
                    wrapper.write(Type.UUID, tracker.getEntityUUID(entityID));
                }
            });
            // 2 - Title
            map(Type.STRING);
            // 3 - Position
            map(Type.POSITION);
            // 4 - Direction
            map(Type.BYTE);
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.SPAWN_PLAYER, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.VAR_INT);
            // 1 - Player UUID
            map(Type.UUID);
            // Parse this info
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int entityID = wrapper.get(Type.VAR_INT, 0);
                    EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                    tracker.addEntity(entityID, Entity1_10Types.EntityType.PLAYER);
                    tracker.sendMetadataBuffer(entityID);
                }
            });
            // 2 - X - Needs to be divide by 32
            map(Type.INT, toNewDouble);
            // 3 - Y - Needs to be divide by 32
            map(Type.INT, toNewDouble);
            // 4 - Z - Needs to be divide by 32
            map(Type.INT, toNewDouble);
            // 5 - Yaw
            map(Type.BYTE);
            // 6 - Pitch
            map(Type.BYTE);
            handler(new // Handle discontinued player hand item
            PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    short item = wrapper.read(Type.SHORT);
                    if (item != 0) {
                        PacketWrapper packet = PacketWrapper.create(ClientboundPackets1_9.ENTITY_EQUIPMENT, null, wrapper.user());
                        packet.write(Type.VAR_INT, wrapper.get(Type.VAR_INT, 0));
                        packet.write(Type.VAR_INT, 0);
                        packet.write(Type.ITEM, new DataItem(item, (byte) 1, (short) 0, null));
                        try {
                            packet.send(Protocol1_9To1_8.class);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
            map(Types1_8.METADATA_LIST, Types1_9.METADATA_LIST);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    List<Metadata> metadataList = wrapper.get(Types1_9.METADATA_LIST, 0);
                    int entityId = wrapper.get(Type.VAR_INT, 0);
                    EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                    if (tracker.hasEntity(entityId)) {
                        protocol.get(MetadataRewriter1_9To1_8.class).handleMetadata(entityId, metadataList, wrapper.user());
                    } else {
                        Via.getPlatform().getLogger().warning("Unable to find entity for metadata, entity ID: " + entityId);
                        metadataList.clear();
                    }
                }
            });
            // Handler for meta data
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    List<Metadata> metadataList = wrapper.get(Types1_9.METADATA_LIST, 0);
                    int entityID = wrapper.get(Type.VAR_INT, 0);
                    EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                    tracker.handleMetadata(entityID, metadataList);
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.DESTROY_ENTITIES, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entities to destroy
            map(Type.VAR_INT_ARRAY_PRIMITIVE);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int[] entities = wrapper.get(Type.VAR_INT_ARRAY_PRIMITIVE, 0);
                    EntityTracker tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                    for (int entity : entities) {
                        // EntityTracker
                        tracker.removeEntity(entity);
                    }
                }
            });
        }
    });
}
Also used : EntityTracker1_9(com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.EntityTracker1_9) DataItem(com.viaversion.viaversion.api.minecraft.item.DataItem) EntityTracker(com.viaversion.viaversion.api.data.entity.EntityTracker) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata) Item(com.viaversion.viaversion.api.minecraft.item.Item) DataItem(com.viaversion.viaversion.api.minecraft.item.DataItem) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) ArrayList(java.util.ArrayList) List(java.util.List) MetadataRewriter1_9To1_8(com.viaversion.viaversion.protocols.protocol1_9to1_8.metadata.MetadataRewriter1_9To1_8)

Aggregations

PacketHandler (com.viaversion.viaversion.api.protocol.remapper.PacketHandler)64 PacketRemapper (com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)64 PacketWrapper (com.viaversion.viaversion.api.protocol.packet.PacketWrapper)62 Type (com.viaversion.viaversion.api.type.Type)26 Item (com.viaversion.viaversion.api.minecraft.item.Item)25 ClientWorld (com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld)20 Chunk (com.viaversion.viaversion.api.minecraft.chunks.Chunk)17 Metadata (com.viaversion.viaversion.api.minecraft.metadata.Metadata)13 ChunkSection (com.viaversion.viaversion.api.minecraft.chunks.ChunkSection)12 ClientboundPackets1_13 (com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13)12 Via (com.viaversion.viaversion.api.Via)11 Position (com.viaversion.viaversion.api.minecraft.Position)11 CompoundTag (com.github.steveice10.opennbt.tag.builtin.CompoundTag)10 Chunk1_9_3_4Type (com.viaversion.viaversion.protocols.protocol1_9_1_2to1_9_3_4.types.Chunk1_9_3_4Type)10 UserConnection (com.viaversion.viaversion.api.connection.UserConnection)9 StringTag (com.github.steveice10.opennbt.tag.builtin.StringTag)8 ViaBackwards (com.viaversion.viabackwards.ViaBackwards)7 Entity1_13Types (com.viaversion.viaversion.api.minecraft.entities.Entity1_13Types)7 EntityType (com.viaversion.viaversion.api.minecraft.entities.EntityType)7 List (java.util.List)7