Search in sources :

Example 1 with ClientboundPackets1_14

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

the class Protocol1_14_2To1_14_3 method registerPackets.

@Override
protected void registerPackets() {
    registerClientbound(ClientboundPackets1_14.TRADE_LIST, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    wrapper.passthrough(Type.VAR_INT);
                    int size = wrapper.passthrough(Type.UNSIGNED_BYTE);
                    for (int i = 0; i < size; i++) {
                        wrapper.passthrough(Type.FLAT_VAR_INT_ITEM);
                        wrapper.passthrough(Type.FLAT_VAR_INT_ITEM);
                        if (wrapper.passthrough(Type.BOOLEAN)) {
                            wrapper.passthrough(Type.FLAT_VAR_INT_ITEM);
                        }
                        wrapper.passthrough(Type.BOOLEAN);
                        wrapper.passthrough(Type.INT);
                        wrapper.passthrough(Type.INT);
                        wrapper.passthrough(Type.INT);
                        wrapper.passthrough(Type.INT);
                        wrapper.passthrough(Type.FLOAT);
                    }
                    wrapper.passthrough(Type.VAR_INT);
                    wrapper.passthrough(Type.VAR_INT);
                    wrapper.passthrough(Type.BOOLEAN);
                    wrapper.read(Type.BOOLEAN);
                }
            });
        }
    });
    RecipeRewriter recipeHandler = new RecipeRewriter1_14(this);
    registerClientbound(ClientboundPackets1_14.DECLARE_RECIPES, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                int size = wrapper.passthrough(Type.VAR_INT);
                int deleted = 0;
                for (int i = 0; i < size; i++) {
                    String fullType = wrapper.read(Type.STRING);
                    String type = fullType.replace("minecraft:", "");
                    // id
                    String id = wrapper.read(Type.STRING);
                    if (type.equals("crafting_special_repairitem")) {
                        deleted++;
                        continue;
                    }
                    wrapper.write(Type.STRING, fullType);
                    wrapper.write(Type.STRING, id);
                    recipeHandler.handle(wrapper, type);
                }
                wrapper.set(Type.VAR_INT, 0, size - deleted);
            });
        }
    });
}
Also used : PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) RecipeRewriter1_14(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.data.RecipeRewriter1_14) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) ClientboundPackets1_14(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14) ServerboundPackets1_14(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ServerboundPackets1_14) BackwardsProtocol(com.viaversion.viabackwards.api.BackwardsProtocol) RecipeRewriter(com.viaversion.viaversion.rewriter.RecipeRewriter) RecipeRewriter1_14(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.data.RecipeRewriter1_14) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) RecipeRewriter(com.viaversion.viaversion.rewriter.RecipeRewriter)

Example 2 with ClientboundPackets1_14

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

the class PlayerPackets1_14 method registerPackets.

@Override
protected void registerPackets() {
    protocol.registerClientbound(ClientboundPackets1_14.SERVER_DIFFICULTY, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.UNSIGNED_BYTE);
            // Locked
            map(Type.BOOLEAN, Type.NOTHING);
            handler(wrapper -> {
                byte difficulty = wrapper.get(Type.UNSIGNED_BYTE, 0).byteValue();
                wrapper.user().get(DifficultyStorage.class).setDifficulty(difficulty);
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_14.OPEN_SIGN_EDITOR, new // c
    PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.POSITION1_14, Type.POSITION);
        }
    });
    protocol.registerServerbound(ServerboundPackets1_13.QUERY_BLOCK_NBT, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.VAR_INT);
            map(Type.POSITION, Type.POSITION1_14);
        }
    });
    protocol.registerServerbound(ServerboundPackets1_13.PLAYER_DIGGING, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Action
            map(Type.VAR_INT);
            // Position
            map(Type.POSITION, Type.POSITION1_14);
        }
    });
    protocol.registerServerbound(ServerboundPackets1_13.RECIPE_BOOK_DATA, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int type = wrapper.get(Type.VAR_INT, 0);
                    if (type == 0) {
                        wrapper.passthrough(Type.STRING);
                    } else if (type == 1) {
                        // Crafting Recipe Book Open
                        wrapper.passthrough(Type.BOOLEAN);
                        // Crafting Recipe Filter Active
                        wrapper.passthrough(Type.BOOLEAN);
                        // Smelting Recipe Book Open
                        wrapper.passthrough(Type.BOOLEAN);
                        // Smelting Recipe Filter Active
                        wrapper.passthrough(Type.BOOLEAN);
                        // Blast furnace/smoker data
                        wrapper.write(Type.BOOLEAN, false);
                        wrapper.write(Type.BOOLEAN, false);
                        wrapper.write(Type.BOOLEAN, false);
                        wrapper.write(Type.BOOLEAN, false);
                    }
                }
            });
        }
    });
    protocol.registerServerbound(ServerboundPackets1_13.UPDATE_COMMAND_BLOCK, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.POSITION, Type.POSITION1_14);
        }
    });
    protocol.registerServerbound(ServerboundPackets1_13.UPDATE_STRUCTURE_BLOCK, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.POSITION, Type.POSITION1_14);
        }
    });
    protocol.registerServerbound(ServerboundPackets1_13.UPDATE_SIGN, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.POSITION, Type.POSITION1_14);
        }
    });
    protocol.registerServerbound(ServerboundPackets1_13.PLAYER_BLOCK_PLACEMENT, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    Position position = wrapper.read(Type.POSITION);
                    int face = wrapper.read(Type.VAR_INT);
                    int hand = wrapper.read(Type.VAR_INT);
                    float x = wrapper.read(Type.FLOAT);
                    float y = wrapper.read(Type.FLOAT);
                    float z = wrapper.read(Type.FLOAT);
                    wrapper.write(Type.VAR_INT, hand);
                    wrapper.write(Type.POSITION1_14, position);
                    wrapper.write(Type.VAR_INT, face);
                    wrapper.write(Type.FLOAT, x);
                    wrapper.write(Type.FLOAT, y);
                    wrapper.write(Type.FLOAT, z);
                    // Inside block
                    wrapper.write(Type.BOOLEAN, false);
                }
            });
        }
    });
}
Also used : PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) Position(com.viaversion.viaversion.api.minecraft.Position) ServerboundPackets1_13(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ServerboundPackets1_13) DifficultyStorage(com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.storage.DifficultyStorage) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) Protocol1_13_2To1_14(com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.Protocol1_13_2To1_14) ClientboundPackets1_14(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14) RewriterBase(com.viaversion.viaversion.api.rewriter.RewriterBase) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) Position(com.viaversion.viaversion.api.minecraft.Position) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)

Example 3 with ClientboundPackets1_14

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

the class SoundPackets1_14 method registerPackets.

@Override
protected void registerPackets() {
    SoundRewriter soundRewriter = new SoundRewriter(protocol);
    soundRewriter.registerSound(ClientboundPackets1_14.SOUND);
    soundRewriter.registerNamedSound(ClientboundPackets1_14.NAMED_SOUND);
    soundRewriter.registerStopSound(ClientboundPackets1_14.STOP_SOUND);
    // Entity Sound Effect
    protocol.registerClientbound(ClientboundPackets1_14.ENTITY_SOUND, null, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                wrapper.cancel();
                int soundId = wrapper.read(Type.VAR_INT);
                int newId = protocol.getMappingData().getSoundMappings().getNewId(soundId);
                if (newId == -1)
                    return;
                int category = wrapper.read(Type.VAR_INT);
                int entityId = wrapper.read(Type.VAR_INT);
                StoredEntityData storedEntity = wrapper.user().getEntityTracker(protocol.getClass()).entityData(entityId);
                EntityPositionStorage1_14 entityStorage;
                if (storedEntity == null || (entityStorage = storedEntity.get(EntityPositionStorage1_14.class)) == null) {
                    ViaBackwards.getPlatform().getLogger().warning("Untracked entity with id " + entityId);
                    return;
                }
                float volume = wrapper.read(Type.FLOAT);
                float pitch = wrapper.read(Type.FLOAT);
                int x = (int) (entityStorage.getX() * 8D);
                int y = (int) (entityStorage.getY() * 8D);
                int z = (int) (entityStorage.getZ() * 8D);
                PacketWrapper soundPacket = wrapper.create(ClientboundPackets1_13.SOUND);
                soundPacket.write(Type.VAR_INT, newId);
                soundPacket.write(Type.VAR_INT, category);
                soundPacket.write(Type.INT, x);
                soundPacket.write(Type.INT, y);
                soundPacket.write(Type.INT, z);
                soundPacket.write(Type.FLOAT, volume);
                soundPacket.write(Type.FLOAT, pitch);
                soundPacket.send(Protocol1_13_2To1_14.class);
            });
        }
    });
}
Also used : PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) ClientboundPackets1_13(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13) Type(com.viaversion.viaversion.api.type.Type) StoredEntityData(com.viaversion.viaversion.api.data.entity.StoredEntityData) ViaBackwards(com.viaversion.viabackwards.ViaBackwards) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) Protocol1_13_2To1_14(com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.Protocol1_13_2To1_14) EntityPositionStorage1_14(com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.storage.EntityPositionStorage1_14) ClientboundPackets1_14(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14) RewriterBase(com.viaversion.viaversion.api.rewriter.RewriterBase) SoundRewriter(com.viaversion.viabackwards.api.rewriters.SoundRewriter) EntityPositionStorage1_14(com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.storage.EntityPositionStorage1_14) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) SoundRewriter(com.viaversion.viabackwards.api.rewriters.SoundRewriter) StoredEntityData(com.viaversion.viaversion.api.data.entity.StoredEntityData) Protocol1_13_2To1_14(com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.Protocol1_13_2To1_14)

Example 4 with ClientboundPackets1_14

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

the class BlockItemPackets1_14 method registerPackets.

@Override
protected void registerPackets() {
    protocol.registerServerbound(ServerboundPackets1_13.EDIT_BOOK, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> handleItemToServer(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)));
        }
    });
    protocol.registerClientbound(ClientboundPackets1_14.OPEN_WINDOW, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int windowId = wrapper.read(Type.VAR_INT);
                    wrapper.write(Type.UNSIGNED_BYTE, (short) windowId);
                    int type = wrapper.read(Type.VAR_INT);
                    String stringType = null;
                    String containerTitle = null;
                    int slotSize = 0;
                    if (type < 6) {
                        if (type == 2)
                            containerTitle = "Barrel";
                        stringType = "minecraft:container";
                        slotSize = (type + 1) * 9;
                    } else {
                        switch(type) {
                            case 11:
                                stringType = "minecraft:crafting_table";
                                break;
                            // blast furnace
                            case 9:
                            // smoker
                            case 20:
                            // furnace
                            case 13:
                            case // grindstone
                            14:
                                if (type == 9)
                                    containerTitle = "Blast Furnace";
                                else if (type == 20)
                                    containerTitle = "Smoker";
                                else if (type == 14)
                                    containerTitle = "Grindstone";
                                stringType = "minecraft:furnace";
                                slotSize = 3;
                                break;
                            case 6:
                                stringType = "minecraft:dropper";
                                slotSize = 9;
                                break;
                            case 12:
                                stringType = "minecraft:enchanting_table";
                                break;
                            case 10:
                                stringType = "minecraft:brewing_stand";
                                slotSize = 5;
                                break;
                            case 18:
                                stringType = "minecraft:villager";
                                break;
                            case 8:
                                stringType = "minecraft:beacon";
                                slotSize = 1;
                                break;
                            // cartography_table
                            case 21:
                            case 7:
                                if (type == 21)
                                    containerTitle = "Cartography Table";
                                stringType = "minecraft:anvil";
                                break;
                            case 15:
                                stringType = "minecraft:hopper";
                                slotSize = 5;
                                break;
                            case 19:
                                stringType = "minecraft:shulker_box";
                                slotSize = 27;
                                break;
                        }
                    }
                    if (stringType == null) {
                        ViaBackwards.getPlatform().getLogger().warning("Can't open inventory for 1.13 player! Type: " + type);
                        wrapper.cancel();
                        return;
                    }
                    wrapper.write(Type.STRING, stringType);
                    JsonElement title = wrapper.read(Type.COMPONENT);
                    if (containerTitle != null) {
                        // Don't rewrite renamed, only translatable titles
                        JsonObject object;
                        if (title.isJsonObject() && (object = title.getAsJsonObject()).has("translate")) {
                            // Don't rewrite other 9x3 translatable containers
                            if (type != 2 || object.getAsJsonPrimitive("translate").getAsString().equals("container.barrel")) {
                                title = ChatRewriter.legacyTextToJson(containerTitle);
                            }
                        }
                    }
                    wrapper.write(Type.COMPONENT, title);
                    wrapper.write(Type.UNSIGNED_BYTE, (short) slotSize);
                }
            });
        }
    });
    // Horse window -> Open Window
    protocol.registerClientbound(ClientboundPackets1_14.OPEN_HORSE_WINDOW, ClientboundPackets1_13.OPEN_WINDOW, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    // Window id
                    wrapper.passthrough(Type.UNSIGNED_BYTE);
                    // Type
                    wrapper.write(Type.STRING, "EntityHorse");
                    JsonObject object = new JsonObject();
                    object.addProperty("translate", "minecraft.horse");
                    // Title
                    wrapper.write(Type.COMPONENT, object);
                    // Number of slots
                    wrapper.write(Type.UNSIGNED_BYTE, wrapper.read(Type.VAR_INT).shortValue());
                    // Entity id
                    wrapper.passthrough(Type.INT);
                }
            });
        }
    });
    BlockRewriter blockRewriter = new BlockRewriter(protocol, Type.POSITION);
    registerSetCooldown(ClientboundPackets1_14.COOLDOWN);
    registerWindowItems(ClientboundPackets1_14.WINDOW_ITEMS, Type.FLAT_VAR_INT_ITEM_ARRAY);
    registerSetSlot(ClientboundPackets1_14.SET_SLOT, Type.FLAT_VAR_INT_ITEM);
    registerAdvancements(ClientboundPackets1_14.ADVANCEMENTS, Type.FLAT_VAR_INT_ITEM);
    // Trade List -> Plugin Message
    protocol.registerClientbound(ClientboundPackets1_14.TRADE_LIST, ClientboundPackets1_13.PLUGIN_MESSAGE, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    wrapper.write(Type.STRING, "minecraft:trader_list");
                    int windowId = wrapper.read(Type.VAR_INT);
                    wrapper.write(Type.INT, windowId);
                    int size = wrapper.passthrough(Type.UNSIGNED_BYTE);
                    for (int i = 0; i < size; i++) {
                        // Input Item
                        Item input = wrapper.read(Type.FLAT_VAR_INT_ITEM);
                        input = handleItemToClient(input);
                        wrapper.write(Type.FLAT_VAR_INT_ITEM, input);
                        // Output Item
                        Item output = wrapper.read(Type.FLAT_VAR_INT_ITEM);
                        output = handleItemToClient(output);
                        wrapper.write(Type.FLAT_VAR_INT_ITEM, output);
                        // Has second item
                        boolean secondItem = wrapper.passthrough(Type.BOOLEAN);
                        if (secondItem) {
                            // Second Item
                            Item second = wrapper.read(Type.FLAT_VAR_INT_ITEM);
                            second = handleItemToClient(second);
                            wrapper.write(Type.FLAT_VAR_INT_ITEM, second);
                        }
                        // Trade disabled
                        wrapper.passthrough(Type.BOOLEAN);
                        // Number of tools uses
                        wrapper.passthrough(Type.INT);
                        // Maximum number of trade uses
                        wrapper.passthrough(Type.INT);
                        wrapper.read(Type.INT);
                        wrapper.read(Type.INT);
                        wrapper.read(Type.FLOAT);
                    }
                    wrapper.read(Type.VAR_INT);
                    wrapper.read(Type.VAR_INT);
                    wrapper.read(Type.BOOLEAN);
                }
            });
        }
    });
    // Open Book -> Plugin Message
    protocol.registerClientbound(ClientboundPackets1_14.OPEN_BOOK, ClientboundPackets1_13.PLUGIN_MESSAGE, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    wrapper.write(Type.STRING, "minecraft:book_open");
                    wrapper.passthrough(Type.VAR_INT);
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_14.ENTITY_EQUIPMENT, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.VAR_INT);
            // 1 - Slot ID
            map(Type.VAR_INT);
            // 2 - Item
            map(Type.FLAT_VAR_INT_ITEM);
            handler(itemToClientHandler(Type.FLAT_VAR_INT_ITEM));
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int entityId = wrapper.get(Type.VAR_INT, 0);
                    EntityType entityType = wrapper.user().getEntityTracker(Protocol1_13_2To1_14.class).entityType(entityId);
                    if (entityType == null)
                        return;
                    if (entityType.isOrHasParent(Entity1_14Types.ABSTRACT_HORSE)) {
                        wrapper.setId(0x3F);
                        wrapper.resetReader();
                        wrapper.passthrough(Type.VAR_INT);
                        wrapper.read(Type.VAR_INT);
                        Item item = wrapper.read(Type.FLAT_VAR_INT_ITEM);
                        int armorType = item == null || item.identifier() == 0 ? 0 : item.identifier() - 726;
                        if (armorType < 0 || armorType > 3) {
                            ViaBackwards.getPlatform().getLogger().warning("Received invalid horse armor: " + item);
                            wrapper.cancel();
                            return;
                        }
                        List<Metadata> metadataList = new ArrayList<>();
                        metadataList.add(new Metadata(16, Types1_13_2.META_TYPES.varIntType, armorType));
                        wrapper.write(Types1_13.METADATA_LIST, metadataList);
                    }
                }
            });
        }
    });
    RecipeRewriter recipeHandler = new RecipeRewriter1_13_2(protocol);
    protocol.registerClientbound(ClientboundPackets1_14.DECLARE_RECIPES, new PacketRemapper() {

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

                private final Set<String> removedTypes = ImmutableSet.of("crafting_special_suspiciousstew", "blasting", "smoking", "campfire_cooking", "stonecutting");

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int size = wrapper.passthrough(Type.VAR_INT);
                    int deleted = 0;
                    for (int i = 0; i < size; i++) {
                        String type = wrapper.read(Type.STRING);
                        // Recipe Identifier
                        String id = wrapper.read(Type.STRING);
                        type = type.replace("minecraft:", "");
                        if (removedTypes.contains(type)) {
                            switch(type) {
                                case "blasting":
                                case "smoking":
                                case "campfire_cooking":
                                    // Group
                                    wrapper.read(Type.STRING);
                                    // Ingredients
                                    wrapper.read(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT);
                                    wrapper.read(Type.FLAT_VAR_INT_ITEM);
                                    // EXP
                                    wrapper.read(Type.FLOAT);
                                    // Cooking time
                                    wrapper.read(Type.VAR_INT);
                                    break;
                                case "stonecutting":
                                    // Group?
                                    wrapper.read(Type.STRING);
                                    // Ingredients
                                    wrapper.read(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT);
                                    // Result
                                    wrapper.read(Type.FLAT_VAR_INT_ITEM);
                                    break;
                            }
                            deleted++;
                            continue;
                        }
                        wrapper.write(Type.STRING, id);
                        wrapper.write(Type.STRING, type);
                        // Handle the rest of the types
                        recipeHandler.handle(wrapper, type);
                    }
                    wrapper.set(Type.VAR_INT, 0, size - deleted);
                }
            });
        }
    });
    registerClickWindow(ServerboundPackets1_13.CLICK_WINDOW, Type.FLAT_VAR_INT_ITEM);
    registerCreativeInvAction(ServerboundPackets1_13.CREATIVE_INVENTORY_ACTION, Type.FLAT_VAR_INT_ITEM);
    protocol.registerClientbound(ClientboundPackets1_14.BLOCK_BREAK_ANIMATION, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.VAR_INT);
            map(Type.POSITION1_14, Type.POSITION);
            map(Type.BYTE);
        }
    });
    protocol.registerClientbound(ClientboundPackets1_14.BLOCK_ENTITY_DATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.POSITION1_14, Type.POSITION);
        }
    });
    protocol.registerClientbound(ClientboundPackets1_14.BLOCK_ACTION, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Location
            map(Type.POSITION1_14, Type.POSITION);
            // Action id
            map(Type.UNSIGNED_BYTE);
            // Action param
            map(Type.UNSIGNED_BYTE);
            // Block id - /!\ NOT BLOCK STATE
            map(Type.VAR_INT);
            handler(wrapper -> {
                int mappedId = protocol.getMappingData().getNewBlockId(wrapper.get(Type.VAR_INT, 0));
                if (mappedId == -1) {
                    wrapper.cancel();
                    return;
                }
                wrapper.set(Type.VAR_INT, 0, mappedId);
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_14.BLOCK_CHANGE, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int id = wrapper.get(Type.VAR_INT, 0);
                    wrapper.set(Type.VAR_INT, 0, protocol.getMappingData().getNewBlockStateId(id));
                }
            });
        }
    });
    blockRewriter.registerMultiBlockChange(ClientboundPackets1_14.MULTI_BLOCK_CHANGE);
    protocol.registerClientbound(ClientboundPackets1_14.EXPLOSION, new PacketRemapper() {

        @Override
        public void registerMap() {
            // X
            map(Type.FLOAT);
            // Y
            map(Type.FLOAT);
            // Z
            map(Type.FLOAT);
            // Radius
            map(Type.FLOAT);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    for (int i = 0; i < 3; i++) {
                        float coord = wrapper.get(Type.FLOAT, i);
                        if (coord < 0f) {
                            coord = (float) Math.floor(coord);
                            wrapper.set(Type.FLOAT, i, coord);
                        }
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_14.CHUNK_DATA, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
                    Chunk chunk = wrapper.read(new Chunk1_14Type());
                    wrapper.write(new Chunk1_13Type(clientWorld), chunk);
                    ChunkLightStorage.ChunkLight chunkLight = wrapper.user().get(ChunkLightStorage.class).getStoredLight(chunk.getX(), chunk.getZ());
                    for (int i = 0; i < chunk.getSections().length; i++) {
                        ChunkSection section = chunk.getSections()[i];
                        if (section == null)
                            continue;
                        ChunkSectionLight sectionLight = new ChunkSectionLightImpl();
                        section.setLight(sectionLight);
                        if (chunkLight == null) {
                            sectionLight.setBlockLight(ChunkLightStorage.FULL_LIGHT);
                            if (clientWorld.getEnvironment() == Environment.NORMAL) {
                                sectionLight.setSkyLight(ChunkLightStorage.FULL_LIGHT);
                            }
                        } else {
                            byte[] blockLight = chunkLight.getBlockLight()[i];
                            sectionLight.setBlockLight(blockLight != null ? blockLight : ChunkLightStorage.FULL_LIGHT);
                            if (clientWorld.getEnvironment() == Environment.NORMAL) {
                                byte[] skyLight = chunkLight.getSkyLight()[i];
                                sectionLight.setSkyLight(skyLight != null ? skyLight : ChunkLightStorage.FULL_LIGHT);
                            }
                        }
                        if (Via.getConfig().isNonFullBlockLightFix() && section.getNonAirBlocksCount() != 0 && sectionLight.hasBlockLight()) {
                            for (int x = 0; x < 16; x++) {
                                for (int y = 0; y < 16; y++) {
                                    for (int z = 0; z < 16; z++) {
                                        int id = section.getFlatBlock(x, y, z);
                                        if (Protocol1_14To1_13_2.MAPPINGS.getNonFullBlocks().contains(id)) {
                                            sectionLight.getBlockLightNibbleArray().set(x, y, z, 0);
                                        }
                                    }
                                }
                            }
                        }
                        for (int j = 0; j < section.getPaletteSize(); j++) {
                            int old = section.getPaletteEntry(j);
                            int newId = protocol.getMappingData().getNewBlockStateId(old);
                            section.setPaletteEntry(j, newId);
                        }
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_14.UNLOAD_CHUNK, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int x = wrapper.passthrough(Type.INT);
                    int z = wrapper.passthrough(Type.INT);
                    wrapper.user().get(ChunkLightStorage.class).unloadChunk(x, z);
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_14.EFFECT, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Effect Id
            map(Type.INT);
            // Location
            map(Type.POSITION1_14, Type.POSITION);
            // Data
            map(Type.INT);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int id = wrapper.get(Type.INT, 0);
                    int data = wrapper.get(Type.INT, 1);
                    if (id == 1010) {
                        // Play record
                        wrapper.set(Type.INT, 1, protocol.getMappingData().getNewItemId(data));
                    } else if (id == 2001) {
                        // Block break + block break sound
                        wrapper.set(Type.INT, 1, protocol.getMappingData().getNewBlockStateId(data));
                    }
                }
            });
        }
    });
    registerSpawnParticle(ClientboundPackets1_14.SPAWN_PARTICLE, Type.FLAT_VAR_INT_ITEM, Type.FLOAT);
    protocol.registerClientbound(ClientboundPackets1_14.MAP_DATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.VAR_INT);
            map(Type.BYTE);
            map(Type.BOOLEAN);
            // Locked
            map(Type.BOOLEAN, Type.NOTHING);
        }
    });
    protocol.registerClientbound(ClientboundPackets1_14.SPAWN_POSITION, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.POSITION1_14, Type.POSITION);
        }
    });
}
Also used : PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) Environment(com.viaversion.viaversion.api.minecraft.Environment) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) Chunk1_14Type(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.types.Chunk1_14Type) Types1_13(com.viaversion.viaversion.api.type.types.version.Types1_13) Item(com.viaversion.viaversion.api.minecraft.item.Item) ListTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag) Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata) ArrayList(java.util.ArrayList) 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) Chunk(com.viaversion.viaversion.api.minecraft.chunks.Chunk) ServerboundPackets1_13(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ServerboundPackets1_13) Protocol1_14To1_13_2(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.Protocol1_14To1_13_2) Protocol1_13_2To1_14(com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.Protocol1_13_2To1_14) Chunk1_13Type(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.types.Chunk1_13Type) ChunkSectionLightImpl(com.viaversion.viaversion.api.minecraft.chunks.ChunkSectionLightImpl) ImmutableSet(com.google.common.collect.ImmutableSet) Types1_13_2(com.viaversion.viaversion.api.type.types.version.Types1_13_2) EnchantmentRewriter(com.viaversion.viabackwards.api.rewriters.EnchantmentRewriter) JsonElement(com.viaversion.viaversion.libs.gson.JsonElement) Tag(com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag) ClientWorld(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld) Set(java.util.Set) EntityType(com.viaversion.viaversion.api.minecraft.entities.EntityType) StringTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag) RecipeRewriter1_13_2(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.RecipeRewriter1_13_2) Via(com.viaversion.viaversion.api.Via) ChunkSectionLight(com.viaversion.viaversion.api.minecraft.chunks.ChunkSectionLight) RecipeRewriter(com.viaversion.viaversion.rewriter.RecipeRewriter) ClientboundPackets1_13(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13) JsonObject(com.viaversion.viaversion.libs.gson.JsonObject) List(java.util.List) BlockRewriter(com.viaversion.viaversion.rewriter.BlockRewriter) ChatRewriter(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ChatRewriter) ViaBackwards(com.viaversion.viabackwards.ViaBackwards) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection) ClientboundPackets1_14(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14) Entity1_14Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_14Types) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata) ArrayList(java.util.ArrayList) Chunk1_14Type(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.types.Chunk1_14Type) JsonObject(com.viaversion.viaversion.libs.gson.JsonObject) RecipeRewriter1_13_2(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.RecipeRewriter1_13_2) BlockRewriter(com.viaversion.viaversion.rewriter.BlockRewriter) Item(com.viaversion.viaversion.api.minecraft.item.Item) ChunkSectionLight(com.viaversion.viaversion.api.minecraft.chunks.ChunkSectionLight) Chunk1_13Type(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.types.Chunk1_13Type) ChunkLightStorage(com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.storage.ChunkLightStorage) RecipeRewriter(com.viaversion.viaversion.rewriter.RecipeRewriter) ChunkSectionLightImpl(com.viaversion.viaversion.api.minecraft.chunks.ChunkSectionLightImpl) Chunk(com.viaversion.viaversion.api.minecraft.chunks.Chunk) EntityType(com.viaversion.viaversion.api.minecraft.entities.EntityType) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) JsonElement(com.viaversion.viaversion.libs.gson.JsonElement) ClientWorld(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection) Protocol1_13_2To1_14(com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.Protocol1_13_2To1_14)

Example 5 with ClientboundPackets1_14

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

the class MetadataRewriter1_14To1_13_2 method handleMetadata.

@Override
protected void handleMetadata(int entityId, EntityType type, Metadata metadata, List<Metadata> metadatas, UserConnection connection) throws Exception {
    metadata.setMetaType(Types1_14.META_TYPES.byId(metadata.metaType().typeId()));
    EntityTracker1_14 tracker = tracker(connection);
    if (metadata.metaType() == Types1_14.META_TYPES.itemType) {
        protocol.getItemRewriter().handleItemToClient((Item) metadata.getValue());
    } else if (metadata.metaType() == Types1_14.META_TYPES.blockStateType) {
        // Convert to new block id
        int data = (int) metadata.getValue();
        metadata.setValue(protocol.getMappingData().getNewBlockStateId(data));
    } else if (metadata.metaType() == Types1_14.META_TYPES.particleType) {
        rewriteParticle((Particle) metadata.getValue());
    }
    if (type == null)
        return;
    // Metadata 6 added to abstract_entity
    if (metadata.id() > 5) {
        metadata.setId(metadata.id() + 1);
    }
    if (metadata.id() == 8 && type.isOrHasParent(Entity1_14Types.LIVINGENTITY)) {
        final float v = ((Number) metadata.getValue()).floatValue();
        if (Float.isNaN(v) && Via.getConfig().is1_14HealthNaNFix()) {
            metadata.setValue(1F);
        }
    }
    // Metadata 12 added to living_entity
    if (metadata.id() > 11 && type.isOrHasParent(Entity1_14Types.LIVINGENTITY)) {
        metadata.setId(metadata.id() + 1);
    }
    if (type.isOrHasParent(Entity1_14Types.ABSTRACT_INSENTIENT)) {
        if (metadata.id() == 13) {
            tracker.setInsentientData(entityId, (byte) ((((Number) metadata.getValue()).byteValue() & ~0x4) | // New attacking metadata
            (tracker.getInsentientData(entityId) & 0x4)));
            metadata.setValue(tracker.getInsentientData(entityId));
        }
    }
    if (type.isOrHasParent(Entity1_14Types.PLAYER)) {
        if (entityId != tracker.clientEntityId()) {
            if (metadata.id() == 0) {
                byte flags = ((Number) metadata.getValue()).byteValue();
                // Mojang overrides the client-side pose updater, see OtherPlayerEntity#updateSize
                tracker.setEntityFlags(entityId, flags);
            } else if (metadata.id() == 7) {
                tracker.setRiptide(entityId, (((Number) metadata.getValue()).byteValue() & 0x4) != 0);
            }
            if (metadata.id() == 0 || metadata.id() == 7) {
                metadatas.add(new Metadata(6, Types1_14.META_TYPES.poseType, recalculatePlayerPose(entityId, tracker)));
            }
        }
    } else if (type.isOrHasParent(Entity1_14Types.ZOMBIE)) {
        if (metadata.id() == 16) {
            tracker.setInsentientData(entityId, (byte) ((tracker.getInsentientData(entityId) & ~0x4) | // New attacking
            ((boolean) metadata.getValue() ? 0x4 : 0)));
            // "Are hands held up"
            metadatas.remove(metadata);
            metadatas.add(new Metadata(13, Types1_14.META_TYPES.byteType, tracker.getInsentientData(entityId)));
        } else if (metadata.id() > 16) {
            metadata.setId(metadata.id() - 1);
        }
    }
    if (type.isOrHasParent(Entity1_14Types.MINECART_ABSTRACT)) {
        if (metadata.id() == 10) {
            // New block format
            int data = (int) metadata.getValue();
            metadata.setValue(protocol.getMappingData().getNewBlockStateId(data));
        }
    } else if (type.is(Entity1_14Types.HORSE)) {
        if (metadata.id() == 18) {
            metadatas.remove(metadata);
            int armorType = (int) metadata.getValue();
            Item armorItem = null;
            if (armorType == 1) {
                // iron armor
                armorItem = new DataItem(protocol.getMappingData().getNewItemId(727), (byte) 1, (short) 0, null);
            } else if (armorType == 2) {
                // gold armor
                armorItem = new DataItem(protocol.getMappingData().getNewItemId(728), (byte) 1, (short) 0, null);
            } else if (armorType == 3) {
                // diamond armor
                armorItem = new DataItem(protocol.getMappingData().getNewItemId(729), (byte) 1, (short) 0, null);
            }
            PacketWrapper equipmentPacket = PacketWrapper.create(ClientboundPackets1_14.ENTITY_EQUIPMENT, null, connection);
            equipmentPacket.write(Type.VAR_INT, entityId);
            equipmentPacket.write(Type.VAR_INT, 4);
            equipmentPacket.write(Type.FLAT_VAR_INT_ITEM, armorItem);
            equipmentPacket.scheduleSend(Protocol1_14To1_13_2.class);
        }
    } else if (type.is(Entity1_14Types.VILLAGER)) {
        if (metadata.id() == 15) {
            // plains
            metadata.setTypeAndValue(Types1_14.META_TYPES.villagerDatatType, new VillagerData(2, getNewProfessionId((int) metadata.getValue()), 0));
        }
    } else if (type.is(Entity1_14Types.ZOMBIE_VILLAGER)) {
        if (metadata.id() == 18) {
            // plains
            metadata.setTypeAndValue(Types1_14.META_TYPES.villagerDatatType, new VillagerData(2, getNewProfessionId((int) metadata.getValue()), 0));
        }
    } else if (type.isOrHasParent(Entity1_14Types.ABSTRACT_ARROW)) {
        if (metadata.id() >= 9) {
            // New piercing
            metadata.setId(metadata.id() + 1);
        }
    } else if (type.is(Entity1_14Types.FIREWORK_ROCKET)) {
        if (metadata.id() == 8) {
            metadata.setMetaType(Types1_14.META_TYPES.optionalVarIntType);
            if (metadata.getValue().equals(0)) {
                // https://bugs.mojang.com/browse/MC-111480
                metadata.setValue(null);
            }
        }
    } else if (type.isOrHasParent(Entity1_14Types.ABSTRACT_SKELETON)) {
        if (metadata.id() == 14) {
            tracker.setInsentientData(entityId, (byte) ((tracker.getInsentientData(entityId) & ~0x4) | // New attacking
            ((boolean) metadata.getValue() ? 0x4 : 0)));
            // "Is swinging arms"
            metadatas.remove(metadata);
            metadatas.add(new Metadata(13, Types1_14.META_TYPES.byteType, tracker.getInsentientData(entityId)));
        }
    }
    if (type.isOrHasParent(Entity1_14Types.ABSTRACT_ILLAGER_BASE)) {
        if (metadata.id() == 14) {
            tracker.setInsentientData(entityId, (byte) ((tracker.getInsentientData(entityId) & ~0x4) | // New attacking
            (((Number) metadata.getValue()).byteValue() != 0 ? 0x4 : 0)));
            // "Has target (aggressive state)"
            metadatas.remove(metadata);
            metadatas.add(new Metadata(13, Types1_14.META_TYPES.byteType, tracker.getInsentientData(entityId)));
        }
    }
    if (type.is(Entity1_14Types.WITCH) || type.is(Entity1_14Types.RAVAGER) || type.isOrHasParent(Entity1_14Types.ABSTRACT_ILLAGER_BASE)) {
        if (metadata.id() >= 14) {
            // 19w13 added a new boolean (raid participant - is celebrating) with id 14
            metadata.setId(metadata.id() + 1);
        }
    }
}
Also used : Item(com.viaversion.viaversion.api.minecraft.item.Item) DataItem(com.viaversion.viaversion.api.minecraft.item.DataItem) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) EntityTracker1_14(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.storage.EntityTracker1_14) DataItem(com.viaversion.viaversion.api.minecraft.item.DataItem) Protocol1_14To1_13_2(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.Protocol1_14To1_13_2) Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata) VillagerData(com.viaversion.viaversion.api.minecraft.VillagerData)

Aggregations

PacketRemapper (com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)13 PacketWrapper (com.viaversion.viaversion.api.protocol.packet.PacketWrapper)12 Type (com.viaversion.viaversion.api.type.Type)11 ClientboundPackets1_14 (com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14)11 PacketHandler (com.viaversion.viaversion.api.protocol.remapper.PacketHandler)8 Metadata (com.viaversion.viaversion.api.minecraft.metadata.Metadata)5 Protocol1_13_2To1_14 (com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.Protocol1_13_2To1_14)4 Entity1_15Types (com.viaversion.viaversion.api.minecraft.entities.Entity1_15Types)4 ClientboundPackets1_13 (com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13)4 Protocol1_14To1_13_2 (com.viaversion.viaversion.protocols.protocol1_14to1_13_2.Protocol1_14To1_13_2)4 ServerboundPackets1_14 (com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ServerboundPackets1_14)4 ViaBackwards (com.viaversion.viabackwards.ViaBackwards)3 BackwardsProtocol (com.viaversion.viabackwards.api.BackwardsProtocol)3 Via (com.viaversion.viaversion.api.Via)3 UserConnection (com.viaversion.viaversion.api.connection.UserConnection)3 Position (com.viaversion.viaversion.api.minecraft.Position)3 Chunk (com.viaversion.viaversion.api.minecraft.chunks.Chunk)3 ChunkSection (com.viaversion.viaversion.api.minecraft.chunks.ChunkSection)3 Entity1_14Types (com.viaversion.viaversion.api.minecraft.entities.Entity1_14Types)3 EntityType (com.viaversion.viaversion.api.minecraft.entities.EntityType)3