Search in sources :

Example 21 with Item

use of com.viaversion.viaversion.api.minecraft.item.Item in project ViaVersion by ViaVersion.

the class Protocol1_17_1To1_17 method registerPackets.

@Override
protected void registerPackets() {
    registerClientbound(ClientboundPackets1_17.REMOVE_ENTITY, ClientboundPackets1_17_1.REMOVE_ENTITIES, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                // Aaaaand back to an array again!
                int entityId = wrapper.read(Type.VAR_INT);
                wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, new int[] { entityId });
            });
        }
    });
    registerClientbound(ClientboundPackets1_17.SET_SLOT, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Container id
            map(Type.UNSIGNED_BYTE);
            // Add arbitrary state id
            create(Type.VAR_INT, 0);
        }
    });
    registerClientbound(ClientboundPackets1_17.WINDOW_ITEMS, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Container id
            map(Type.UNSIGNED_BYTE);
            // Add arbitrary state id
            create(Type.VAR_INT, 0);
            handler(wrapper -> {
                // Length encoded as var int now
                wrapper.write(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT, wrapper.read(Type.FLAT_VAR_INT_ITEM_ARRAY));
                // Carried item - should work like this
                wrapper.write(Type.FLAT_VAR_INT_ITEM, null);
            });
        }
    });
    registerServerbound(ServerboundPackets1_17.CLICK_WINDOW, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Container id
            map(Type.UNSIGNED_BYTE);
            // Remove state id
            read(Type.VAR_INT);
        }
    });
    registerServerbound(ServerboundPackets1_17.EDIT_BOOK, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                CompoundTag tag = new CompoundTag();
                // Magic value for writable books
                Item item = new DataItem(942, (byte) 1, (short) 0, tag);
                // Write the item, edit the tag down the line
                wrapper.write(Type.FLAT_VAR_INT_ITEM, item);
                int slot = wrapper.read(Type.VAR_INT);
                // Save pages to tag
                int pages = wrapper.read(Type.VAR_INT);
                ListTag pagesTag = new ListTag(StringTag.class);
                for (int i = 0; i < pages; i++) {
                    String page = wrapper.read(PAGE_STRING_TYPE);
                    pagesTag.add(new StringTag(page));
                }
                // Legacy servers don't like an empty pages list
                if (pagesTag.size() == 0) {
                    pagesTag.add(new StringTag(""));
                }
                tag.put("pages", pagesTag);
                if (wrapper.read(Type.BOOLEAN)) {
                    // Save the title to tag
                    String title = wrapper.read(TITLE_STRING_TYPE);
                    tag.put("title", new StringTag(title));
                    // Even if unused, legacy servers check for the author tag
                    tag.put("author", new StringTag(wrapper.user().getProtocolInfo().getUsername()));
                    // Write signing
                    wrapper.write(Type.BOOLEAN, true);
                } else {
                    wrapper.write(Type.BOOLEAN, false);
                }
                // Write the slot
                wrapper.write(Type.VAR_INT, slot);
            });
        }
    });
}
Also used : StringTag(com.github.steveice10.opennbt.tag.builtin.StringTag) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) ClientboundPackets1_17(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.ClientboundPackets1_17) Type(com.viaversion.viaversion.api.type.Type) ListTag(com.github.steveice10.opennbt.tag.builtin.ListTag) ServerboundPackets1_17(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.ServerboundPackets1_17) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) DataItem(com.viaversion.viaversion.api.minecraft.item.DataItem) StringType(com.viaversion.viaversion.api.type.types.StringType) Item(com.viaversion.viaversion.api.minecraft.item.Item) AbstractProtocol(com.viaversion.viaversion.api.protocol.AbstractProtocol) StringTag(com.github.steveice10.opennbt.tag.builtin.StringTag) DataItem(com.viaversion.viaversion.api.minecraft.item.DataItem) Item(com.viaversion.viaversion.api.minecraft.item.Item) DataItem(com.viaversion.viaversion.api.minecraft.item.DataItem) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) ListTag(com.github.steveice10.opennbt.tag.builtin.ListTag) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag)

Example 22 with Item

use of com.viaversion.viaversion.api.minecraft.item.Item in project ViaBackwards by ViaVersion.

the class BlockItemPackets1_12 method registerPackets.

@Override
protected void registerPackets() {
    protocol.registerClientbound(ClientboundPackets1_12.MAP_DATA, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int count = wrapper.passthrough(Type.VAR_INT);
                    for (int i = 0; i < count * 3; i++) {
                        wrapper.passthrough(Type.BYTE);
                    }
                }
            });
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    short columns = wrapper.passthrough(Type.UNSIGNED_BYTE);
                    if (columns <= 0)
                        return;
                    short rows = wrapper.passthrough(Type.UNSIGNED_BYTE);
                    // X
                    wrapper.passthrough(Type.UNSIGNED_BYTE);
                    // Z
                    wrapper.passthrough(Type.UNSIGNED_BYTE);
                    byte[] data = wrapper.read(Type.BYTE_ARRAY_PRIMITIVE);
                    for (int i = 0; i < data.length; i++) {
                        short color = (short) (data[i] & 0xFF);
                        if (color > 143) {
                            color = (short) MapColorMapping.getNearestOldColor(color);
                            data[i] = (byte) color;
                        }
                    }
                    wrapper.write(Type.BYTE_ARRAY_PRIMITIVE, data);
                }
            });
        }
    });
    registerSetSlot(ClientboundPackets1_12.SET_SLOT, Type.ITEM);
    registerWindowItems(ClientboundPackets1_12.WINDOW_ITEMS, Type.ITEM_ARRAY);
    registerEntityEquipment(ClientboundPackets1_12.ENTITY_EQUIPMENT, Type.ITEM);
    // Plugin message Packet -> Trading
    protocol.registerClientbound(ClientboundPackets1_12.PLUGIN_MESSAGE, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    if (wrapper.get(Type.STRING, 0).equalsIgnoreCase("MC|TrList")) {
                        // Passthrough Window ID
                        wrapper.passthrough(Type.INT);
                        int size = wrapper.passthrough(Type.UNSIGNED_BYTE);
                        for (int i = 0; i < size; i++) {
                            // Input Item
                            wrapper.write(Type.ITEM, handleItemToClient(wrapper.read(Type.ITEM)));
                            // Output Item
                            wrapper.write(Type.ITEM, handleItemToClient(wrapper.read(Type.ITEM)));
                            // Has second item
                            boolean secondItem = wrapper.passthrough(Type.BOOLEAN);
                            if (secondItem)
                                // Second Item
                                wrapper.write(Type.ITEM, handleItemToClient(wrapper.read(Type.ITEM)));
                            // Trade disabled
                            wrapper.passthrough(Type.BOOLEAN);
                            // Number of tools uses
                            wrapper.passthrough(Type.INT);
                            // Maximum number of trade uses
                            wrapper.passthrough(Type.INT);
                        }
                    }
                }
            });
        }
    });
    protocol.registerServerbound(ServerboundPackets1_9_3.CLICK_WINDOW, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    if (wrapper.get(Type.VAR_INT, 0) == 1) {
                        // Shift click
                        // https://github.com/ViaVersion/ViaVersion/pull/754
                        // Previously clients grab the item from the clicked slot *before* it has
                        // been moved however now they grab the slot item *after* it has been moved
                        // and send that in the packet.
                        // Set null item (probably will work)
                        wrapper.set(Type.ITEM, 0, null);
                        // Apologize (may happen in some cases, maybe if inventory is full?)
                        PacketWrapper confirm = wrapper.create(ServerboundPackets1_12.WINDOW_CONFIRMATION);
                        confirm.write(Type.UNSIGNED_BYTE, wrapper.get(Type.UNSIGNED_BYTE, 0));
                        confirm.write(Type.SHORT, wrapper.get(Type.SHORT, 1));
                        // Success - not used
                        confirm.write(Type.BOOLEAN, false);
                        wrapper.sendToServer(Protocol1_11_1To1_12.class);
                        wrapper.cancel();
                        confirm.sendToServer(Protocol1_11_1To1_12.class);
                        return;
                    }
                    Item item = wrapper.get(Type.ITEM, 0);
                    handleItemToServer(item);
                }
            });
        }
    });
    registerCreativeInvAction(ServerboundPackets1_9_3.CREATIVE_INVENTORY_ACTION, Type.ITEM);
    protocol.registerClientbound(ClientboundPackets1_12.CHUNK_DATA, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
                    // Use the 1.9.4 Chunk type since nothing changed.
                    Chunk1_9_3_4Type type = new Chunk1_9_3_4Type(clientWorld);
                    Chunk chunk = wrapper.passthrough(type);
                    handleChunk(chunk);
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_12.BLOCK_CHANGE, new PacketRemapper() {

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

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

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    for (BlockChangeRecord record : wrapper.get(Type.BLOCK_CHANGE_RECORD_ARRAY, 0)) {
                        record.setBlockId(handleBlockID(record.getBlockId()));
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_12.BLOCK_ENTITY_DATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Position
            map(Type.POSITION);
            // 1 - Action
            map(Type.UNSIGNED_BYTE);
            // 2 - NBT
            map(Type.NBT);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    // Remove bed color
                    if (wrapper.get(Type.UNSIGNED_BYTE, 0) == 11)
                        wrapper.cancel();
                }
            });
        }
    });
    protocol.getEntityRewriter().filter().handler((event, meta) -> {
        if (// Is Item
        meta.metaType().type().equals(Type.ITEM))
            meta.setValue(handleItemToClient((Item) meta.getValue()));
    });
    protocol.registerServerbound(ServerboundPackets1_9_3.CLIENT_STATUS, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    // Open Inventory
                    if (wrapper.get(Type.VAR_INT, 0) == 2) {
                        wrapper.cancel();
                    }
                }
            });
        }
    });
}
Also used : PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Chunk(com.viaversion.viaversion.api.minecraft.chunks.Chunk) Chunk1_9_3_4Type(com.viaversion.viaversion.protocols.protocol1_9_1_2to1_9_3_4.types.Chunk1_9_3_4Type) Item(com.viaversion.viaversion.api.minecraft.item.Item) BlockChangeRecord(com.viaversion.viaversion.api.minecraft.BlockChangeRecord) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) ClientWorld(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld)

Example 23 with Item

use of com.viaversion.viaversion.api.minecraft.item.Item in project ViaBackwards by ViaVersion.

the class EntityPackets1_13 method registerRewrites.

@Override
protected void registerRewrites() {
    // Rewrite new Entity 'drowned'
    mapEntityTypeWithData(Entity1_13Types.EntityType.DROWNED, Entity1_13Types.EntityType.ZOMBIE_VILLAGER).plainName();
    // Fishy
    mapEntityTypeWithData(Entity1_13Types.EntityType.COD, Entity1_13Types.EntityType.SQUID).plainName();
    mapEntityTypeWithData(Entity1_13Types.EntityType.SALMON, Entity1_13Types.EntityType.SQUID).plainName();
    mapEntityTypeWithData(Entity1_13Types.EntityType.PUFFERFISH, Entity1_13Types.EntityType.SQUID).plainName();
    mapEntityTypeWithData(Entity1_13Types.EntityType.TROPICAL_FISH, Entity1_13Types.EntityType.SQUID).plainName();
    // Phantom
    mapEntityTypeWithData(Entity1_13Types.EntityType.PHANTOM, Entity1_13Types.EntityType.PARROT).plainName().spawnMetadata(storage -> {
        // The phantom is grey/blue so let's do yellow/blue
        storage.add(new Metadata(15, MetaType1_12.VarInt, 3));
    });
    // Dolphin
    mapEntityTypeWithData(Entity1_13Types.EntityType.DOLPHIN, Entity1_13Types.EntityType.SQUID).plainName();
    // Turtle
    mapEntityTypeWithData(Entity1_13Types.EntityType.TURTLE, Entity1_13Types.EntityType.OCELOT).plainName();
    // Rewrite Meta types
    filter().handler((event, meta) -> {
        int typeId = meta.metaType().typeId();
        // Rewrite optional chat to string
        if (typeId == 5) {
            // Json -> Legacy is done below
            meta.setTypeAndValue(MetaType1_12.String, meta.getValue() != null ? meta.getValue().toString() : "");
        } else // Rewrite items
        if (typeId == 6) {
            Item item = (Item) meta.getValue();
            meta.setTypeAndValue(MetaType1_12.Slot, protocol.getItemRewriter().handleItemToClient(item));
        } else // Discontinue particles
        if (typeId == 15) {
            event.cancel();
        } else // Rewrite to 1.12 ids
        if (typeId > 5) {
            meta.setMetaType(MetaType1_12.byId(typeId - 1));
        }
    });
    // Rewrite Custom Name from Chat to String
    filter().filterFamily(Entity1_13Types.EntityType.ENTITY).index(2).handler((event, meta) -> {
        String value = meta.getValue().toString();
        if (!value.isEmpty()) {
            meta.setValue(ChatRewriter.jsonToLegacyText(value));
        }
    });
    // Handle zombie metadata
    filter().filterFamily(Entity1_13Types.EntityType.ZOMBIE).removeIndex(15);
    // Handle turtle metadata (Remove them all for now)
    // Home pos
    filter().type(Entity1_13Types.EntityType.TURTLE).cancel(13);
    // Has egg
    filter().type(Entity1_13Types.EntityType.TURTLE).cancel(14);
    // Laying egg
    filter().type(Entity1_13Types.EntityType.TURTLE).cancel(15);
    // Travel pos
    filter().type(Entity1_13Types.EntityType.TURTLE).cancel(16);
    // Going home
    filter().type(Entity1_13Types.EntityType.TURTLE).cancel(17);
    // Traveling
    filter().type(Entity1_13Types.EntityType.TURTLE).cancel(18);
    // Remove additional fish meta
    filter().filterFamily(Entity1_13Types.EntityType.ABSTRACT_FISHES).cancel(12);
    filter().filterFamily(Entity1_13Types.EntityType.ABSTRACT_FISHES).cancel(13);
    // Remove phantom size
    filter().type(Entity1_13Types.EntityType.PHANTOM).cancel(12);
    // Remove boat splash timer
    filter().type(Entity1_13Types.EntityType.BOAT).cancel(12);
    // Remove Trident special loyalty level
    filter().type(Entity1_13Types.EntityType.TRIDENT).cancel(7);
    // Handle new wolf colors
    filter().type(Entity1_13Types.EntityType.WOLF).index(17).handler((event, meta) -> {
        meta.setValue(15 - (int) meta.getValue());
    });
    // Rewrite AreaEffectCloud
    filter().type(Entity1_13Types.EntityType.AREA_EFFECT_CLOUD).index(9).handler((event, meta) -> {
        Particle particle = (Particle) meta.getValue();
        ParticleMapping.ParticleData data = ParticleMapping.getMapping(particle.getId());
        int firstArg = 0;
        int secondArg = 0;
        int[] particleArgs = data.rewriteMeta(protocol, particle.getArguments());
        if (particleArgs != null && particleArgs.length != 0) {
            if (data.getHandler().isBlockHandler() && particleArgs[0] == 0) {
                // Air doesn't have a break particle for sub 1.13 clients -> glass pane
                particleArgs[0] = 102;
            }
            firstArg = particleArgs[0];
            secondArg = particleArgs.length == 2 ? particleArgs[1] : 0;
        }
        event.createExtraMeta(new Metadata(9, MetaType1_12.VarInt, data.getHistoryId()));
        event.createExtraMeta(new Metadata(10, MetaType1_12.VarInt, firstArg));
        event.createExtraMeta(new Metadata(11, MetaType1_12.VarInt, secondArg));
        event.cancel();
    });
}
Also used : Particle(com.viaversion.viaversion.api.type.types.Particle) Item(com.viaversion.viaversion.api.minecraft.item.Item) Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata) ParticleMapping(com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.data.ParticleMapping)

Example 24 with Item

use of com.viaversion.viaversion.api.minecraft.item.Item in project ViaBackwards by ViaVersion.

the class EntityPackets1_14 method registerRewrites.

@Override
protected void registerRewrites() {
    mapTypes(Entity1_14Types.values(), Entity1_13Types.EntityType.class);
    mapEntityTypeWithData(Entity1_14Types.CAT, Entity1_14Types.OCELOT).jsonName();
    mapEntityTypeWithData(Entity1_14Types.TRADER_LLAMA, Entity1_14Types.LLAMA).jsonName();
    mapEntityTypeWithData(Entity1_14Types.FOX, Entity1_14Types.WOLF).jsonName();
    mapEntityTypeWithData(Entity1_14Types.PANDA, Entity1_14Types.POLAR_BEAR).jsonName();
    mapEntityTypeWithData(Entity1_14Types.PILLAGER, Entity1_14Types.VILLAGER).jsonName();
    mapEntityTypeWithData(Entity1_14Types.WANDERING_TRADER, Entity1_14Types.VILLAGER).jsonName();
    mapEntityTypeWithData(Entity1_14Types.RAVAGER, Entity1_14Types.COW).jsonName();
    filter().handler((event, meta) -> {
        int typeId = meta.metaType().typeId();
        if (typeId <= 15) {
            meta.setMetaType(Types1_13_2.META_TYPES.byId(typeId));
        }
        MetaType type = meta.metaType();
        if (type == Types1_13_2.META_TYPES.itemType) {
            Item item = (Item) meta.getValue();
            meta.setValue(protocol.getItemRewriter().handleItemToClient(item));
        } else if (type == Types1_13_2.META_TYPES.blockStateType) {
            int blockstate = meta.value();
            meta.setValue(protocol.getMappingData().getNewBlockStateId(blockstate));
        }
    });
    filter().type(Entity1_14Types.PILLAGER).cancel(15);
    filter().type(Entity1_14Types.FOX).cancel(15);
    filter().type(Entity1_14Types.FOX).cancel(16);
    filter().type(Entity1_14Types.FOX).cancel(17);
    filter().type(Entity1_14Types.FOX).cancel(18);
    filter().type(Entity1_14Types.PANDA).cancel(15);
    filter().type(Entity1_14Types.PANDA).cancel(16);
    filter().type(Entity1_14Types.PANDA).cancel(17);
    filter().type(Entity1_14Types.PANDA).cancel(18);
    filter().type(Entity1_14Types.PANDA).cancel(19);
    filter().type(Entity1_14Types.PANDA).cancel(20);
    filter().type(Entity1_14Types.CAT).cancel(18);
    filter().type(Entity1_14Types.CAT).cancel(19);
    filter().type(Entity1_14Types.CAT).cancel(20);
    filter().handler((event, meta) -> {
        EntityType type = event.entityType();
        if (type == null)
            return;
        if (type.isOrHasParent(Entity1_14Types.ABSTRACT_ILLAGER_BASE) || type == Entity1_14Types.RAVAGER || type == Entity1_14Types.WITCH) {
            int index = event.index();
            if (index == 14) {
                event.cancel();
            } else if (index > 14) {
                event.setIndex(index - 1);
            }
        }
    });
    filter().type(Entity1_14Types.AREA_EFFECT_CLOUD).index(10).handler((event, meta) -> {
        rewriteParticle((Particle) meta.getValue());
    });
    filter().type(Entity1_14Types.FIREWORK_ROCKET).index(8).handler((event, meta) -> {
        meta.setMetaType(Types1_13_2.META_TYPES.varIntType);
        Integer value = (Integer) meta.getValue();
        if (value == null) {
            meta.setValue(0);
        }
    });
    filter().filterFamily(Entity1_14Types.ABSTRACT_ARROW).removeIndex(9);
    // Head shake timer
    filter().type(Entity1_14Types.VILLAGER).cancel(15);
    MetaHandler villagerDataHandler = (event, meta) -> {
        VillagerData villagerData = (VillagerData) meta.getValue();
        meta.setTypeAndValue(Types1_13_2.META_TYPES.varIntType, villagerDataToProfession(villagerData));
        if (meta.id() == 16) {
            // decreased by 2 again in one of the following handlers
            event.setIndex(15);
        }
    };
    filter().type(Entity1_14Types.ZOMBIE_VILLAGER).index(18).handler(villagerDataHandler);
    filter().type(Entity1_14Types.VILLAGER).index(16).handler(villagerDataHandler);
    // Holding arms up - from bitfield into own boolean
    filter().filterFamily(Entity1_14Types.ABSTRACT_SKELETON).index(13).handler((event, meta) -> {
        byte value = (byte) meta.getValue();
        if ((value & 4) != 0) {
            event.createExtraMeta(new Metadata(14, Types1_13_2.META_TYPES.booleanType, true));
        }
    });
    filter().filterFamily(Entity1_14Types.ZOMBIE).index(13).handler((event, meta) -> {
        byte value = (byte) meta.getValue();
        if ((value & 4) != 0) {
            event.createExtraMeta(new Metadata(16, Types1_13_2.META_TYPES.booleanType, true));
        }
    });
    filter().filterFamily(Entity1_14Types.ZOMBIE).addIndex(16);
    // Remove bed location
    filter().filterFamily(Entity1_14Types.LIVINGENTITY).handler((event, meta) -> {
        int index = event.index();
        if (index == 12) {
            Position position = (Position) meta.getValue();
            if (position != null) {
                // Use bed
                PacketWrapper wrapper = PacketWrapper.create(ClientboundPackets1_13.USE_BED, null, event.user());
                wrapper.write(Type.VAR_INT, event.entityId());
                wrapper.write(Type.POSITION, position);
                try {
                    wrapper.scheduleSend(Protocol1_13_2To1_14.class);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            event.cancel();
        } else if (index > 12) {
            event.setIndex(index - 1);
        }
    });
    // Pose
    filter().removeIndex(6);
    filter().type(Entity1_14Types.OCELOT).index(13).handler((event, meta) -> {
        event.setIndex(15);
        meta.setTypeAndValue(Types1_13_2.META_TYPES.varIntType, 0);
    });
    filter().type(Entity1_14Types.CAT).handler((event, meta) -> {
        if (event.index() == 15) {
            meta.setValue(1);
        } else if (event.index() == 13) {
            meta.setValue((byte) ((byte) meta.getValue() & 0x4));
        }
    });
    filter().handler((event, meta) -> {
        if (meta.metaType().typeId() > 15) {
            throw new IllegalArgumentException("Unhandled metadata: " + meta);
        }
    });
}
Also used : PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) MetaType(com.viaversion.viaversion.api.minecraft.metadata.MetaType) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) Entity1_13Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_13Types) EntityPositionStorage1_14(com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.storage.EntityPositionStorage1_14) Item(com.viaversion.viaversion.api.minecraft.item.Item) Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata) Types1_14(com.viaversion.viaversion.api.type.types.version.Types1_14) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) ChunkLightStorage(com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.storage.ChunkLightStorage) Protocol1_13_2To1_14(com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.Protocol1_13_2To1_14) EntityData(com.viaversion.viabackwards.api.entities.storage.EntityData) Particle(com.viaversion.viaversion.api.type.types.Particle) LegacyEntityRewriter(com.viaversion.viabackwards.api.rewriters.LegacyEntityRewriter) Position(com.viaversion.viaversion.api.minecraft.Position) Types1_13_2(com.viaversion.viaversion.api.type.types.version.Types1_13_2) ClientWorld(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld) EntityTracker(com.viaversion.viaversion.api.data.entity.EntityTracker) EntityType(com.viaversion.viaversion.api.minecraft.entities.EntityType) VillagerData(com.viaversion.viaversion.api.minecraft.VillagerData) EntityPositionHandler(com.viaversion.viabackwards.api.entities.storage.EntityPositionHandler) ClientboundPackets1_13(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13) DifficultyStorage(com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.storage.DifficultyStorage) MetaHandler(com.viaversion.viaversion.rewriter.meta.MetaHandler) ViaBackwards(com.viaversion.viabackwards.ViaBackwards) ClientboundPackets1_14(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14) Entity1_14Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_14Types) Position(com.viaversion.viaversion.api.minecraft.Position) Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata) EntityType(com.viaversion.viaversion.api.minecraft.entities.EntityType) Item(com.viaversion.viaversion.api.minecraft.item.Item) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) VillagerData(com.viaversion.viaversion.api.minecraft.VillagerData) Entity1_13Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_13Types) MetaHandler(com.viaversion.viaversion.rewriter.meta.MetaHandler) MetaType(com.viaversion.viaversion.api.minecraft.metadata.MetaType)

Example 25 with Item

use of com.viaversion.viaversion.api.minecraft.item.Item in project ViaBackwards by ViaVersion.

the class Protocol1_13To1_13_1 method registerPackets.

@Override
protected void registerPackets() {
    executeAsyncAfterLoaded(Protocol1_13_1To1_13.class, MAPPINGS::load);
    entityRewriter.register();
    itemRewriter.register();
    WorldPackets1_13_1.register(this);
    TranslatableRewriter translatableRewriter = new TranslatableRewriter(this);
    translatableRewriter.registerChatMessage(ClientboundPackets1_13.CHAT_MESSAGE);
    translatableRewriter.registerCombatEvent(ClientboundPackets1_13.COMBAT_EVENT);
    translatableRewriter.registerDisconnect(ClientboundPackets1_13.DISCONNECT);
    translatableRewriter.registerTabList(ClientboundPackets1_13.TAB_LIST);
    translatableRewriter.registerTitle(ClientboundPackets1_13.TITLE);
    translatableRewriter.registerPing();
    new CommandRewriter1_13_1(this).registerDeclareCommands(ClientboundPackets1_13.DECLARE_COMMANDS);
    registerServerbound(ServerboundPackets1_13.TAB_COMPLETE, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.VAR_INT);
            map(Type.STRING, new ValueTransformer<String, String>(Type.STRING) {

                @Override
                public String transform(PacketWrapper wrapper, String inputValue) {
                    // 1.13 starts sending slash at start, so we remove it for compatibility
                    return !inputValue.startsWith("/") ? "/" + inputValue : inputValue;
                }
            });
        }
    });
    registerServerbound(ServerboundPackets1_13.EDIT_BOOK, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    itemRewriter.handleItemToServer(wrapper.get(Type.FLAT_ITEM, 0));
                    wrapper.write(Type.VAR_INT, 0);
                }
            });
        }
    });
    registerClientbound(ClientboundPackets1_13.OPEN_WINDOW, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Id
            map(Type.UNSIGNED_BYTE);
            // Window Type
            map(Type.STRING);
            handler(wrapper -> {
                JsonElement title = wrapper.passthrough(Type.COMPONENT);
                translatableRewriter.processText(title);
                if (ViaBackwards.getConfig().fix1_13FormattedInventoryTitle()) {
                    if (title.isJsonObject() && title.getAsJsonObject().size() == 1 && title.getAsJsonObject().has("translate")) {
                        // Hotfix simple translatable components from being converted to legacy text
                        return;
                    }
                    // https://bugs.mojang.com/browse/MC-124543
                    JsonObject legacyComponent = new JsonObject();
                    legacyComponent.addProperty("text", ChatRewriter.jsonToLegacyText(title.toString()));
                    wrapper.set(Type.COMPONENT, 0, legacyComponent);
                }
            });
        }
    });
    registerClientbound(ClientboundPackets1_13.TAB_COMPLETE, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Transaction id
            map(Type.VAR_INT);
            // Start
            map(Type.VAR_INT);
            // Length
            map(Type.VAR_INT);
            // Count
            map(Type.VAR_INT);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int start = wrapper.get(Type.VAR_INT, 1);
                    // Offset by +1 to take into account / at beginning
                    wrapper.set(Type.VAR_INT, 1, start - 1);
                    // Passthrough suggestions
                    int count = wrapper.get(Type.VAR_INT, 3);
                    for (int i = 0; i < count; i++) {
                        wrapper.passthrough(Type.STRING);
                        boolean hasTooltip = wrapper.passthrough(Type.BOOLEAN);
                        if (hasTooltip) {
                            // JSON Tooltip
                            wrapper.passthrough(Type.STRING);
                        }
                    }
                }
            });
        }
    });
    registerClientbound(ClientboundPackets1_13.BOSSBAR, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.UUID);
            map(Type.VAR_INT);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int action = wrapper.get(Type.VAR_INT, 0);
                    if (action == 0 || action == 3) {
                        translatableRewriter.processText(wrapper.passthrough(Type.COMPONENT));
                        if (action == 0) {
                            wrapper.passthrough(Type.FLOAT);
                            wrapper.passthrough(Type.VAR_INT);
                            wrapper.passthrough(Type.VAR_INT);
                            short flags = wrapper.read(Type.UNSIGNED_BYTE);
                            if ((flags & 0x04) != 0)
                                flags |= 0x02;
                            wrapper.write(Type.UNSIGNED_BYTE, flags);
                        }
                    }
                }
            });
        }
    });
    registerClientbound(ClientboundPackets1_13.ADVANCEMENTS, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    // Reset/clear
                    wrapper.passthrough(Type.BOOLEAN);
                    // Mapping size
                    int size = wrapper.passthrough(Type.VAR_INT);
                    for (int i = 0; i < size; i++) {
                        // Identifier
                        wrapper.passthrough(Type.STRING);
                        // Parent
                        if (wrapper.passthrough(Type.BOOLEAN))
                            wrapper.passthrough(Type.STRING);
                        // Display data
                        if (wrapper.passthrough(Type.BOOLEAN)) {
                            // Title
                            wrapper.passthrough(Type.COMPONENT);
                            // Description
                            wrapper.passthrough(Type.COMPONENT);
                            Item icon = wrapper.passthrough(Type.FLAT_ITEM);
                            itemRewriter.handleItemToClient(icon);
                            // Frame type
                            wrapper.passthrough(Type.VAR_INT);
                            // Flags
                            int flags = wrapper.passthrough(Type.INT);
                            if ((flags & 1) != 0)
                                // Background texture
                                wrapper.passthrough(Type.STRING);
                            // X
                            wrapper.passthrough(Type.FLOAT);
                            // Y
                            wrapper.passthrough(Type.FLOAT);
                        }
                        // Criteria
                        wrapper.passthrough(Type.STRING_ARRAY);
                        int arrayLength = wrapper.passthrough(Type.VAR_INT);
                        for (int array = 0; array < arrayLength; array++) {
                            // String array
                            wrapper.passthrough(Type.STRING_ARRAY);
                        }
                    }
                }
            });
        }
    });
    new TagRewriter(this).register(ClientboundPackets1_13.TAGS, RegistryType.ITEM);
    new StatisticsRewriter(this).register(ClientboundPackets1_13.STATISTICS);
}
Also used : ValueTransformer(com.viaversion.viaversion.api.protocol.remapper.ValueTransformer) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) TranslatableRewriter(com.viaversion.viabackwards.api.rewriters.TranslatableRewriter) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) Entity1_13Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_13Types) Item(com.viaversion.viaversion.api.minecraft.item.Item) BackwardsProtocol(com.viaversion.viabackwards.api.BackwardsProtocol) RegistryType(com.viaversion.viaversion.api.minecraft.RegistryType) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) ServerboundPackets1_13(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ServerboundPackets1_13) EntityTrackerBase(com.viaversion.viaversion.data.entity.EntityTrackerBase) UserConnection(com.viaversion.viaversion.api.connection.UserConnection) ValueTransformer(com.viaversion.viaversion.api.protocol.remapper.ValueTransformer) ItemRewriter(com.viaversion.viaversion.api.rewriter.ItemRewriter) CommandRewriter1_13_1(com.viaversion.viabackwards.protocol.protocol1_13to1_13_1.data.CommandRewriter1_13_1) JsonElement(com.viaversion.viaversion.libs.gson.JsonElement) InventoryPackets1_13_1(com.viaversion.viabackwards.protocol.protocol1_13to1_13_1.packets.InventoryPackets1_13_1) ClientWorld(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld) WorldPackets1_13_1(com.viaversion.viabackwards.protocol.protocol1_13to1_13_1.packets.WorldPackets1_13_1) EntityPackets1_13_1(com.viaversion.viabackwards.protocol.protocol1_13to1_13_1.packets.EntityPackets1_13_1) ClientboundPackets1_13(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13) JsonObject(com.viaversion.viaversion.libs.gson.JsonObject) TagRewriter(com.viaversion.viaversion.rewriter.TagRewriter) StatisticsRewriter(com.viaversion.viaversion.rewriter.StatisticsRewriter) BackwardsMappings(com.viaversion.viabackwards.api.data.BackwardsMappings) ChatRewriter(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ChatRewriter) ViaBackwards(com.viaversion.viabackwards.ViaBackwards) EntityRewriter(com.viaversion.viaversion.api.rewriter.EntityRewriter) Protocol1_13_1To1_13(com.viaversion.viaversion.protocols.protocol1_13_1to1_13.Protocol1_13_1To1_13) CommandRewriter1_13_1(com.viaversion.viabackwards.protocol.protocol1_13to1_13_1.data.CommandRewriter1_13_1) TranslatableRewriter(com.viaversion.viabackwards.api.rewriters.TranslatableRewriter) TagRewriter(com.viaversion.viaversion.rewriter.TagRewriter) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) JsonObject(com.viaversion.viaversion.libs.gson.JsonObject) Item(com.viaversion.viaversion.api.minecraft.item.Item) StatisticsRewriter(com.viaversion.viaversion.rewriter.StatisticsRewriter) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) JsonElement(com.viaversion.viaversion.libs.gson.JsonElement)

Aggregations

Item (com.viaversion.viaversion.api.minecraft.item.Item)43 PacketWrapper (com.viaversion.viaversion.api.protocol.packet.PacketWrapper)28 PacketRemapper (com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)27 PacketHandler (com.viaversion.viaversion.api.protocol.remapper.PacketHandler)23 Type (com.viaversion.viaversion.api.type.Type)15 DataItem (com.viaversion.viaversion.api.minecraft.item.DataItem)13 ClientWorld (com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld)9 Metadata (com.viaversion.viaversion.api.minecraft.metadata.Metadata)8 ArrayList (java.util.ArrayList)8 CompoundTag (com.github.steveice10.opennbt.tag.builtin.CompoundTag)6 Position (com.viaversion.viaversion.api.minecraft.Position)6 Chunk (com.viaversion.viaversion.api.minecraft.chunks.Chunk)6 ClientboundPackets1_13 (com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13)6 List (java.util.List)6 StringTag (com.github.steveice10.opennbt.tag.builtin.StringTag)5 Via (com.viaversion.viaversion.api.Via)5 UserConnection (com.viaversion.viaversion.api.connection.UserConnection)5 UUID (java.util.UUID)5 ListTag (com.github.steveice10.opennbt.tag.builtin.ListTag)4 ViaBackwards (com.viaversion.viabackwards.ViaBackwards)4