Search in sources :

Example 16 with Item

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

the class Protocol1_13To1_12_2 method registerPackets.

@Override
protected void registerPackets() {
    entityRewriter.register();
    itemRewriter.register();
    EntityPackets.register(this);
    WorldPackets.register(this);
    registerClientbound(State.LOGIN, 0x0, 0x0, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> componentRewriter.processText(wrapper.passthrough(Type.COMPONENT)));
        }
    });
    registerClientbound(State.STATUS, 0x00, 0x00, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    String response = wrapper.get(Type.STRING, 0);
                    try {
                        JsonObject json = GsonUtil.getGson().fromJson(response, JsonObject.class);
                        if (json.has("favicon")) {
                            json.addProperty("favicon", json.get("favicon").getAsString().replace("\n", ""));
                        }
                        wrapper.set(Type.STRING, 0, GsonUtil.getGson().toJson(json));
                    } catch (JsonParseException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    });
    // New packet 0x04 - Login Plugin Message
    // Statistics
    registerClientbound(ClientboundPackets1_12_1.STATISTICS, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int size = wrapper.read(Type.VAR_INT);
                    List<StatisticData> remappedStats = new ArrayList<>();
                    for (int i = 0; i < size; i++) {
                        String name = wrapper.read(Type.STRING);
                        String[] split = name.split("\\.");
                        int categoryId = 0;
                        int newId = -1;
                        int value = wrapper.read(Type.VAR_INT);
                        if (split.length == 2) {
                            // Custom types
                            categoryId = 8;
                            Integer newIdRaw = StatisticMappings.CUSTOM_STATS.get(name);
                            if (newIdRaw != null) {
                                newId = newIdRaw;
                            } else {
                                Via.getPlatform().getLogger().warning("Could not find 1.13 -> 1.12.2 statistic mapping for " + name);
                            }
                        } else if (split.length > 2) {
                            String category = split[1];
                            // TODO convert string ids (blocks, items, entities)
                            switch(category) {
                                case "mineBlock":
                                    categoryId = 0;
                                    break;
                                case "craftItem":
                                    categoryId = 1;
                                    break;
                                case "useItem":
                                    categoryId = 2;
                                    break;
                                case "breakItem":
                                    categoryId = 3;
                                    break;
                                case "pickup":
                                    categoryId = 4;
                                    break;
                                case "drop":
                                    categoryId = 5;
                                    break;
                                case "killEntity":
                                    categoryId = 6;
                                    break;
                                case "entityKilledBy":
                                    categoryId = 7;
                                    break;
                            }
                        }
                        if (newId != -1)
                            remappedStats.add(new StatisticData(categoryId, newId, value));
                    }
                    // size
                    wrapper.write(Type.VAR_INT, remappedStats.size());
                    for (StatisticData stat : remappedStats) {
                        // category id
                        wrapper.write(Type.VAR_INT, stat.getCategoryId());
                        // statistics id
                        wrapper.write(Type.VAR_INT, stat.getNewId());
                        // value
                        wrapper.write(Type.VAR_INT, stat.getValue());
                    }
                }
            });
        }
    });
    componentRewriter.registerBossBar(ClientboundPackets1_12_1.BOSSBAR);
    componentRewriter.registerComponentPacket(ClientboundPackets1_12_1.CHAT_MESSAGE);
    registerClientbound(ClientboundPackets1_12_1.TAB_COMPLETE, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    wrapper.write(Type.VAR_INT, wrapper.user().get(TabCompleteTracker.class).getTransactionId());
                    String input = wrapper.user().get(TabCompleteTracker.class).getInput();
                    // Start & End
                    int index;
                    int length;
                    // If no input or new word (then it's the start)
                    if (input.endsWith(" ") || input.isEmpty()) {
                        index = input.length();
                        length = 0;
                    } else {
                        // Otherwise find the last space (+1 as we include it)
                        int lastSpace = input.lastIndexOf(' ') + 1;
                        index = lastSpace;
                        length = input.length() - lastSpace;
                    }
                    // Write index + length
                    wrapper.write(Type.VAR_INT, index);
                    wrapper.write(Type.VAR_INT, length);
                    int count = wrapper.passthrough(Type.VAR_INT);
                    for (int i = 0; i < count; i++) {
                        String suggestion = wrapper.read(Type.STRING);
                        // If we're at the start then handle removing slash
                        if (suggestion.startsWith("/") && index == 0) {
                            suggestion = suggestion.substring(1);
                        }
                        wrapper.write(Type.STRING, suggestion);
                        wrapper.write(Type.BOOLEAN, false);
                    }
                }
            });
        }
    });
    registerClientbound(ClientboundPackets1_12_1.OPEN_WINDOW, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Id
            map(Type.UNSIGNED_BYTE);
            // Window type
            map(Type.STRING);
            // Title
            handler(wrapper -> componentRewriter.processText(wrapper.passthrough(Type.COMPONENT)));
        }
    });
    registerClientbound(ClientboundPackets1_12_1.COOLDOWN, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int item = wrapper.read(Type.VAR_INT);
                    int ticks = wrapper.read(Type.VAR_INT);
                    wrapper.cancel();
                    if (item == 383) {
                        // Spawn egg
                        for (int i = 0; i < 44; i++) {
                            Integer newItem = getMappingData().getItemMappings().get(item << 16 | i);
                            if (newItem != null) {
                                PacketWrapper packet = wrapper.create(ClientboundPackets1_13.COOLDOWN);
                                packet.write(Type.VAR_INT, newItem);
                                packet.write(Type.VAR_INT, ticks);
                                packet.send(Protocol1_13To1_12_2.class);
                            } else {
                                break;
                            }
                        }
                    } else {
                        for (int i = 0; i < 16; i++) {
                            int newItem = getMappingData().getItemMappings().get(item << 4 | i);
                            if (newItem != -1) {
                                PacketWrapper packet = wrapper.create(ClientboundPackets1_13.COOLDOWN);
                                packet.write(Type.VAR_INT, newItem);
                                packet.write(Type.VAR_INT, ticks);
                                packet.send(Protocol1_13To1_12_2.class);
                            } else {
                                break;
                            }
                        }
                    }
                }
            });
        }
    });
    componentRewriter.registerComponentPacket(ClientboundPackets1_12_1.DISCONNECT);
    registerClientbound(ClientboundPackets1_12_1.EFFECT, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Effect Id
            map(Type.INT);
            // Location
            map(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, getMappingData().getItemMappings().get(data << 4));
                    } else if (id == 2001) {
                        // Block break + block break sound
                        int blockId = data & 0xFFF;
                        int blockData = data >> 12;
                        wrapper.set(Type.INT, 1, WorldPackets.toNewId(blockId << 4 | blockData));
                    }
                }
            });
        }
    });
    registerClientbound(ClientboundPackets1_12_1.JOIN_GAME, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.INT);
            // 1 - Gamemode
            map(Type.UNSIGNED_BYTE);
            // 2 - Dimension
            map(Type.INT);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    // Store the player
                    int entityId = wrapper.get(Type.INT, 0);
                    wrapper.user().getEntityTracker(Protocol1_13To1_12_2.class).addEntity(entityId, Entity1_13Types.EntityType.PLAYER);
                    ClientWorld clientChunks = wrapper.user().get(ClientWorld.class);
                    int dimensionId = wrapper.get(Type.INT, 1);
                    clientChunks.setEnvironment(dimensionId);
                }
            });
            handler(SEND_DECLARE_COMMANDS_AND_TAGS);
        }
    });
    registerClientbound(ClientboundPackets1_12_1.CRAFT_RECIPE_RESPONSE, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.BYTE);
            handler(wrapper -> wrapper.write(Type.STRING, "viaversion:legacy/" + wrapper.read(Type.VAR_INT)));
        }
    });
    componentRewriter.registerCombatEvent(ClientboundPackets1_12_1.COMBAT_EVENT);
    registerClientbound(ClientboundPackets1_12_1.MAP_DATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Map id
            map(Type.VAR_INT);
            // 1 - Scale
            map(Type.BYTE);
            // 2 - Tracking Position
            map(Type.BOOLEAN);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int iconCount = wrapper.passthrough(Type.VAR_INT);
                    for (int i = 0; i < iconCount; i++) {
                        byte directionAndType = wrapper.read(Type.BYTE);
                        int type = (directionAndType & 0xF0) >> 4;
                        wrapper.write(Type.VAR_INT, type);
                        // Icon X
                        wrapper.passthrough(Type.BYTE);
                        // Icon Z
                        wrapper.passthrough(Type.BYTE);
                        byte direction = (byte) (directionAndType & 0x0F);
                        wrapper.write(Type.BYTE, direction);
                        // Display Name
                        wrapper.write(Type.OPTIONAL_COMPONENT, null);
                    }
                }
            });
        }
    });
    registerClientbound(ClientboundPackets1_12_1.UNLOCK_RECIPES, new PacketRemapper() {

        @Override
        public void registerMap() {
            // action
            map(Type.VAR_INT);
            // crafting book open
            map(Type.BOOLEAN);
            // crafting filter active
            map(Type.BOOLEAN);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    // smelting book open
                    wrapper.write(Type.BOOLEAN, false);
                    // smelting filter active
                    wrapper.write(Type.BOOLEAN, false);
                }
            });
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int action = wrapper.get(Type.VAR_INT, 0);
                    for (int i = 0; i < (action == 0 ? 2 : 1); i++) {
                        int[] ids = wrapper.read(Type.VAR_INT_ARRAY_PRIMITIVE);
                        String[] stringIds = new String[ids.length];
                        for (int j = 0; j < ids.length; j++) {
                            stringIds[j] = "viaversion:legacy/" + ids[j];
                        }
                        wrapper.write(Type.STRING_ARRAY, stringIds);
                    }
                    if (action == 0) {
                        wrapper.create(ClientboundPackets1_13.DECLARE_RECIPES, new // Declare recipes
                        PacketHandler() {

                            @Override
                            public void handle(PacketWrapper wrapper) throws Exception {
                                wrapper.write(Type.VAR_INT, RecipeData.recipes.size());
                                for (Map.Entry<String, RecipeData.Recipe> entry : RecipeData.recipes.entrySet()) {
                                    // Id
                                    wrapper.write(Type.STRING, entry.getKey());
                                    wrapper.write(Type.STRING, entry.getValue().getType());
                                    switch(entry.getValue().getType()) {
                                        case "crafting_shapeless":
                                            {
                                                wrapper.write(Type.STRING, entry.getValue().getGroup());
                                                wrapper.write(Type.VAR_INT, entry.getValue().getIngredients().length);
                                                for (Item[] ingredient : entry.getValue().getIngredients()) {
                                                    // Clone because array and item is mutable
                                                    Item[] clone = ingredient.clone();
                                                    for (int i = 0; i < clone.length; i++) {
                                                        if (clone[i] == null)
                                                            continue;
                                                        clone[i] = new DataItem(clone[i]);
                                                    }
                                                    wrapper.write(Type.FLAT_ITEM_ARRAY_VAR_INT, clone);
                                                }
                                                wrapper.write(Type.FLAT_ITEM, new DataItem(entry.getValue().getResult()));
                                                break;
                                            }
                                        case "crafting_shaped":
                                            {
                                                wrapper.write(Type.VAR_INT, entry.getValue().getWidth());
                                                wrapper.write(Type.VAR_INT, entry.getValue().getHeight());
                                                wrapper.write(Type.STRING, entry.getValue().getGroup());
                                                for (Item[] ingredient : entry.getValue().getIngredients()) {
                                                    // Clone because array and item is mutable
                                                    Item[] clone = ingredient.clone();
                                                    for (int i = 0; i < clone.length; i++) {
                                                        if (clone[i] == null)
                                                            continue;
                                                        clone[i] = new DataItem(clone[i]);
                                                    }
                                                    wrapper.write(Type.FLAT_ITEM_ARRAY_VAR_INT, clone);
                                                }
                                                wrapper.write(Type.FLAT_ITEM, new DataItem(entry.getValue().getResult()));
                                                break;
                                            }
                                        case "smelting":
                                            {
                                                wrapper.write(Type.STRING, entry.getValue().getGroup());
                                                // Clone because array and item is mutable
                                                Item[] clone = entry.getValue().getIngredient().clone();
                                                for (int i = 0; i < clone.length; i++) {
                                                    if (clone[i] == null)
                                                        continue;
                                                    clone[i] = new DataItem(clone[i]);
                                                }
                                                wrapper.write(Type.FLAT_ITEM_ARRAY_VAR_INT, clone);
                                                wrapper.write(Type.FLAT_ITEM, new DataItem(entry.getValue().getResult()));
                                                wrapper.write(Type.FLOAT, entry.getValue().getExperience());
                                                wrapper.write(Type.VAR_INT, entry.getValue().getCookingTime());
                                                break;
                                            }
                                    }
                                }
                            }
                        }).send(Protocol1_13To1_12_2.class);
                    }
                }
            });
        }
    });
    registerClientbound(ClientboundPackets1_12_1.RESPAWN, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
                    int dimensionId = wrapper.get(Type.INT, 0);
                    clientWorld.setEnvironment(dimensionId);
                    if (Via.getConfig().isServersideBlockConnections()) {
                        ConnectionData.clearBlockStorage(wrapper.user());
                    }
                }
            });
            handler(SEND_DECLARE_COMMANDS_AND_TAGS);
        }
    });
    registerClientbound(ClientboundPackets1_12_1.SCOREBOARD_OBJECTIVE, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Objective name
            map(Type.STRING);
            // 1 - Mode
            map(Type.BYTE);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    byte mode = wrapper.get(Type.BYTE, 0);
                    // On create or update
                    if (mode == 0 || mode == 2) {
                        // Value
                        String value = wrapper.read(Type.STRING);
                        wrapper.write(Type.COMPONENT, ChatRewriter.legacyTextToJson(value));
                        String type = wrapper.read(Type.STRING);
                        // integer or hearts
                        wrapper.write(Type.VAR_INT, type.equals("integer") ? 0 : 1);
                    }
                }
            });
        }
    });
    registerClientbound(ClientboundPackets1_12_1.TEAMS, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Team Name
            map(Type.STRING);
            // 1 - Mode
            map(Type.BYTE);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    byte action = wrapper.get(Type.BYTE, 0);
                    if (action == 0 || action == 2) {
                        // Display Name
                        String displayName = wrapper.read(Type.STRING);
                        wrapper.write(Type.COMPONENT, ChatRewriter.legacyTextToJson(displayName));
                        // Prefix moved
                        String prefix = wrapper.read(Type.STRING);
                        // Suffix moved
                        String suffix = wrapper.read(Type.STRING);
                        // Flags
                        wrapper.passthrough(Type.BYTE);
                        // Name Tag Visibility
                        wrapper.passthrough(Type.STRING);
                        // Collision rule
                        wrapper.passthrough(Type.STRING);
                        // Handle new colors
                        int colour = wrapper.read(Type.BYTE).intValue();
                        if (colour == -1) {
                            // -1 changed to 21
                            colour = 21;
                        }
                        if (Via.getConfig().is1_13TeamColourFix()) {
                            char lastColorChar = getLastColorChar(prefix);
                            colour = ChatColorUtil.getColorOrdinal(lastColorChar);
                            suffix = ChatColorUtil.COLOR_CHAR + Character.toString(lastColorChar) + suffix;
                        }
                        wrapper.write(Type.VAR_INT, colour);
                        // Prefix
                        wrapper.write(Type.COMPONENT, ChatRewriter.legacyTextToJson(prefix));
                        // Suffix
                        wrapper.write(Type.COMPONENT, ChatRewriter.legacyTextToJson(suffix));
                    }
                    if (action == 0 || action == 3 || action == 4) {
                        // Entities
                        String[] names = wrapper.read(Type.STRING_ARRAY);
                        for (int i = 0; i < names.length; i++) {
                            names[i] = rewriteTeamMemberName(names[i]);
                        }
                        wrapper.write(Type.STRING_ARRAY, names);
                    }
                }
            });
        }
    });
    registerClientbound(ClientboundPackets1_12_1.UPDATE_SCORE, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    // Display Name
                    String displayName = wrapper.read(Type.STRING);
                    displayName = rewriteTeamMemberName(displayName);
                    wrapper.write(Type.STRING, displayName);
                    byte action = wrapper.read(Type.BYTE);
                    wrapper.write(Type.BYTE, action);
                    // Objective Name
                    wrapper.passthrough(Type.STRING);
                    if (action != 1) {
                        // Value
                        wrapper.passthrough(Type.VAR_INT);
                    }
                }
            });
        }
    });
    componentRewriter.registerTitle(ClientboundPackets1_12_1.TITLE);
    // New 0x4C - Stop Sound
    new SoundRewriter(this).registerSound(ClientboundPackets1_12_1.SOUND);
    registerClientbound(ClientboundPackets1_12_1.TAB_LIST, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    componentRewriter.processText(wrapper.passthrough(Type.COMPONENT));
                    componentRewriter.processText(wrapper.passthrough(Type.COMPONENT));
                }
            });
        }
    });
    registerClientbound(ClientboundPackets1_12_1.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
                            componentRewriter.processText(wrapper.passthrough(Type.COMPONENT));
                            // Description
                            componentRewriter.processText(wrapper.passthrough(Type.COMPONENT));
                            Item icon = wrapper.read(Type.ITEM);
                            itemRewriter.handleItemToClient(icon);
                            // Translate item to flat item
                            wrapper.write(Type.FLAT_ITEM, 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);
                        }
                    }
                }
            });
        }
    });
    // Incoming packets
    // New packet 0x02 - Login Plugin Message
    cancelServerbound(State.LOGIN, 0x02);
    // New 0x01 - Query Block NBT
    cancelServerbound(ServerboundPackets1_13.QUERY_BLOCK_NBT);
    // Tab-Complete
    registerServerbound(ServerboundPackets1_13.TAB_COMPLETE, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    // Disable auto-complete if configured
                    if (Via.getConfig().isDisable1_13AutoComplete()) {
                        wrapper.cancel();
                    }
                    int tid = wrapper.read(Type.VAR_INT);
                    // Save transaction id
                    wrapper.user().get(TabCompleteTracker.class).setTransactionId(tid);
                }
            });
            // Prepend /
            map(Type.STRING, new ValueTransformer<String, String>(Type.STRING) {

                @Override
                public String transform(PacketWrapper wrapper, String inputValue) {
                    wrapper.user().get(TabCompleteTracker.class).setInput(inputValue);
                    return "/" + inputValue;
                }
            });
            // Fake the end of the packet
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    wrapper.write(Type.BOOLEAN, false);
                    wrapper.write(Type.OPTIONAL_POSITION, null);
                    if (!wrapper.isCancelled() && Via.getConfig().get1_13TabCompleteDelay() > 0) {
                        TabCompleteTracker tracker = wrapper.user().get(TabCompleteTracker.class);
                        wrapper.cancel();
                        tracker.setTimeToSend(System.currentTimeMillis() + Via.getConfig().get1_13TabCompleteDelay() * 50L);
                        tracker.setLastTabComplete(wrapper.get(Type.STRING, 0));
                    }
                }
            });
        }
    });
    // New 0x0A - Edit book -> Plugin Message
    registerServerbound(ServerboundPackets1_13.EDIT_BOOK, ServerboundPackets1_12_1.PLUGIN_MESSAGE, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    Item item = wrapper.read(Type.FLAT_ITEM);
                    boolean isSigning = wrapper.read(Type.BOOLEAN);
                    itemRewriter.handleItemToServer(item);
                    // Channel
                    wrapper.write(Type.STRING, isSigning ? "MC|BSign" : "MC|BEdit");
                    wrapper.write(Type.ITEM, item);
                }
            });
        }
    });
    // New 0x0C - Query Entity NBT
    cancelServerbound(ServerboundPackets1_13.ENTITY_NBT_REQUEST);
    // New 0x15 - Pick Item -> Plugin Message
    registerServerbound(ServerboundPackets1_13.PICK_ITEM, ServerboundPackets1_12_1.PLUGIN_MESSAGE, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    // Channel
                    wrapper.write(Type.STRING, "MC|PickItem");
                }
            });
        }
    });
    registerServerbound(ServerboundPackets1_13.CRAFT_RECIPE_REQUEST, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Window id
            map(Type.BYTE);
            handler(wrapper -> {
                String s = wrapper.read(Type.STRING);
                Integer id;
                if (s.length() < 19 || (id = Ints.tryParse(s.substring(18))) == null) {
                    wrapper.cancel();
                    return;
                }
                wrapper.write(Type.VAR_INT, id);
            });
        }
    });
    registerServerbound(ServerboundPackets1_13.RECIPE_BOOK_DATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Type
            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) {
                        String s = wrapper.read(Type.STRING);
                        Integer id;
                        // Custom recipes
                        if (s.length() < 19 || (id = Ints.tryParse(s.substring(18))) == null) {
                            wrapper.cancel();
                            return;
                        }
                        wrapper.write(Type.INT, id);
                    }
                    if (type == 1) {
                        // Crafting Recipe Book Open
                        wrapper.passthrough(Type.BOOLEAN);
                        // Crafting Recipe Filter Active
                        wrapper.passthrough(Type.BOOLEAN);
                        // Smelting Recipe Book Open | IGNORE NEW 1.13 FIELD
                        wrapper.read(Type.BOOLEAN);
                        // Smelting Recipe Filter Active | IGNORE NEW 1.13 FIELD
                        wrapper.read(Type.BOOLEAN);
                    }
                }
            });
        }
    });
    // New 0x1C - Name Item -> Plugin Message
    registerServerbound(ServerboundPackets1_13.RENAME_ITEM, ServerboundPackets1_12_1.PLUGIN_MESSAGE, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                // Channel
                wrapper.write(Type.STRING, "MC|ItemName");
            });
        }
    });
    // New 0x1F - Select Trade -> Plugin Message
    registerServerbound(ServerboundPackets1_13.SELECT_TRADE, ServerboundPackets1_12_1.PLUGIN_MESSAGE, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                // Channel
                wrapper.write(Type.STRING, "MC|TrSel");
            });
            // Slot
            map(Type.VAR_INT, Type.INT);
        }
    });
    // New 0x20 - Set Beacon Effect -> Plugin Message
    registerServerbound(ServerboundPackets1_13.SET_BEACON_EFFECT, ServerboundPackets1_12_1.PLUGIN_MESSAGE, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                // Channel
                wrapper.write(Type.STRING, "MC|Beacon");
            });
            // Primary Effect
            map(Type.VAR_INT, Type.INT);
            // Secondary Effect
            map(Type.VAR_INT, Type.INT);
        }
    });
    // New 0x22 - Update Command Block -> Plugin Message
    registerServerbound(ServerboundPackets1_13.UPDATE_COMMAND_BLOCK, ServerboundPackets1_12_1.PLUGIN_MESSAGE, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> wrapper.write(Type.STRING, "MC|AutoCmd"));
            handler(POS_TO_3_INT);
            // Command
            map(Type.STRING);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int mode = wrapper.read(Type.VAR_INT);
                    byte flags = wrapper.read(Type.BYTE);
                    String stringMode = mode == 0 ? "SEQUENCE" : mode == 1 ? "AUTO" : "REDSTONE";
                    // Track output
                    wrapper.write(Type.BOOLEAN, (flags & 0x1) != 0);
                    wrapper.write(Type.STRING, stringMode);
                    // Is conditional
                    wrapper.write(Type.BOOLEAN, (flags & 0x2) != 0);
                    // Automatic
                    wrapper.write(Type.BOOLEAN, (flags & 0x4) != 0);
                }
            });
        }
    });
    // New 0x23 - Update Command Block Minecart -> Plugin Message
    registerServerbound(ServerboundPackets1_13.UPDATE_COMMAND_BLOCK_MINECART, ServerboundPackets1_12_1.PLUGIN_MESSAGE, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    wrapper.write(Type.STRING, "MC|AdvCmd");
                    // Type 1 for Entity
                    wrapper.write(Type.BYTE, (byte) 1);
                }
            });
            // Entity Id
            map(Type.VAR_INT, Type.INT);
        }
    });
    // 0x1B -> 0x24 in InventoryPackets
    // New 0x25 - Update Structure Block -> Message Channel
    registerServerbound(ServerboundPackets1_13.UPDATE_STRUCTURE_BLOCK, ServerboundPackets1_12_1.PLUGIN_MESSAGE, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                // Channel
                wrapper.write(Type.STRING, "MC|Struct");
            });
            handler(POS_TO_3_INT);
            map(Type.VAR_INT, new // Action
            ValueTransformer<Integer, Byte>(// Action
            Type.BYTE) {

                @Override
                public Byte transform(PacketWrapper wrapper, Integer action) throws Exception {
                    return (byte) (action + 1);
                }
            });
            // Action
            map(Type.VAR_INT, new ValueTransformer<Integer, String>(Type.STRING) {

                @Override
                public String transform(PacketWrapper wrapper, Integer mode) throws Exception {
                    return mode == 0 ? "SAVE" : mode == 1 ? "LOAD" : mode == 2 ? "CORNER" : "DATA";
                }
            });
            // Name
            map(Type.STRING);
            // Offset X
            map(Type.BYTE, Type.INT);
            // Offset Y
            map(Type.BYTE, Type.INT);
            // Offset Z
            map(Type.BYTE, Type.INT);
            // Size X
            map(Type.BYTE, Type.INT);
            // Size Y
            map(Type.BYTE, Type.INT);
            // Size Z
            map(Type.BYTE, Type.INT);
            map(Type.VAR_INT, new // Mirror
            ValueTransformer<Integer, String>(// Mirror
            Type.STRING) {

                @Override
                public String transform(PacketWrapper wrapper, Integer mirror) throws Exception {
                    return mirror == 0 ? "NONE" : mirror == 1 ? "LEFT_RIGHT" : "FRONT_BACK";
                }
            });
            map(Type.VAR_INT, new // Rotation
            ValueTransformer<Integer, String>(// Rotation
            Type.STRING) {

                @Override
                public String transform(PacketWrapper wrapper, Integer rotation) throws Exception {
                    return rotation == 0 ? "NONE" : rotation == 1 ? "CLOCKWISE_90" : rotation == 2 ? "CLOCKWISE_180" : "COUNTERCLOCKWISE_90";
                }
            });
            map(Type.STRING);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    float integrity = wrapper.read(Type.FLOAT);
                    long seed = wrapper.read(Type.VAR_LONG);
                    byte flags = wrapper.read(Type.BYTE);
                    // Ignore Entities
                    wrapper.write(Type.BOOLEAN, (flags & 0x1) != 0);
                    // Show air
                    wrapper.write(Type.BOOLEAN, (flags & 0x2) != 0);
                    // Show bounding box
                    wrapper.write(Type.BOOLEAN, (flags & 0x4) != 0);
                    wrapper.write(Type.FLOAT, integrity);
                    wrapper.write(Type.VAR_LONG, seed);
                }
            });
        }
    });
}
Also used : PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) ComponentRewriter(com.viaversion.viaversion.rewriter.ComponentRewriter) JsonObject(com.google.gson.JsonObject) RecipeData(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.RecipeData) BlockConnectionStorage(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.storage.BlockConnectionStorage) Entity1_13Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_13Types) Types1_13(com.viaversion.viaversion.api.type.types.version.Types1_13) MappingData(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.MappingData) BlockStorage(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.storage.BlockStorage) Item(com.viaversion.viaversion.api.minecraft.item.Item) GsonUtil(com.viaversion.viaversion.util.GsonUtil) StatisticMappings(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.StatisticMappings) StatisticData(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.StatisticData) Map(java.util.Map) UserConnection(com.viaversion.viaversion.api.connection.UserConnection) ValueTransformer(com.viaversion.viaversion.api.protocol.remapper.ValueTransformer) BlockConnectionProvider(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.blockconnections.providers.BlockConnectionProvider) ChatColorUtil(com.viaversion.viaversion.util.ChatColorUtil) ServerboundPackets1_12_1(com.viaversion.viaversion.protocols.protocol1_12_1to1_12.ServerboundPackets1_12_1) ViaProviders(com.viaversion.viaversion.api.platform.providers.ViaProviders) ConnectionData(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.blockconnections.ConnectionData) State(com.viaversion.viaversion.api.protocol.packet.State) EntityPackets(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.packets.EntityPackets) Set(java.util.Set) AbstractProtocol(com.viaversion.viaversion.api.protocol.AbstractProtocol) Sets(com.google.common.collect.Sets) List(java.util.List) MetadataRewriter1_13To1_12_2(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.metadata.MetadataRewriter1_13To1_12_2) InventoryPackets(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.packets.InventoryPackets) PaintingProvider(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.providers.PaintingProvider) JsonParseException(com.google.gson.JsonParseException) SoundRewriter(com.viaversion.viaversion.rewriter.SoundRewriter) ParticleType(com.viaversion.viaversion.api.type.types.minecraft.ParticleType) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) ComponentRewriter1_13(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.ComponentRewriter1_13) HashMap(java.util.HashMap) ClientboundPackets1_12_1(com.viaversion.viaversion.protocols.protocol1_12_1to1_12.ClientboundPackets1_12_1) PacketBlockConnectionProvider(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.blockconnections.providers.PacketBlockConnectionProvider) ArrayList(java.util.ArrayList) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) DataItem(com.viaversion.viaversion.api.minecraft.item.DataItem) EntityTrackerBase(com.viaversion.viaversion.data.entity.EntityTrackerBase) ItemRewriter(com.viaversion.viaversion.api.rewriter.ItemRewriter) BlockIdData(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.BlockIdData) Position(com.viaversion.viaversion.api.minecraft.Position) ClientWorld(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld) Via(com.viaversion.viaversion.api.Via) Ints(com.google.common.primitives.Ints) EntityRewriter(com.viaversion.viaversion.api.rewriter.EntityRewriter) WorldPackets(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.packets.WorldPackets) BlockEntityProvider(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.providers.BlockEntityProvider) TabCompleteTracker(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.storage.TabCompleteTracker) DataItem(com.viaversion.viaversion.api.minecraft.item.DataItem) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) JsonParseException(com.google.gson.JsonParseException) StatisticData(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.StatisticData) Item(com.viaversion.viaversion.api.minecraft.item.Item) DataItem(com.viaversion.viaversion.api.minecraft.item.DataItem) TabCompleteTracker(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.storage.TabCompleteTracker) ValueTransformer(com.viaversion.viaversion.api.protocol.remapper.ValueTransformer) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) SoundRewriter(com.viaversion.viaversion.rewriter.SoundRewriter) ClientWorld(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld) Map(java.util.Map) HashMap(java.util.HashMap)

Example 17 with Item

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

the class InventoryPackets method registerPackets.

@Override
public void registerPackets() {
    registerSetSlot(ClientboundPackets1_9_3.SET_SLOT, Type.ITEM);
    registerWindowItems(ClientboundPackets1_9_3.WINDOW_ITEMS, Type.ITEM_ARRAY);
    registerEntityEquipment(ClientboundPackets1_9_3.ENTITY_EQUIPMENT, Type.ITEM);
    // Plugin message Packet -> Trading
    protocol.registerClientbound(ClientboundPackets1_9_3.PLUGIN_MESSAGE, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    if (wrapper.get(Type.STRING, 0).equalsIgnoreCase("MC|TrList")) {
                        // Passthrough Window ID
                        wrapper.passthrough(Type.INT);
                        int size = wrapper.passthrough(Type.UNSIGNED_BYTE);
                        for (int i = 0; i < size; i++) {
                            // Input Item
                            handleItemToClient(wrapper.passthrough(Type.ITEM));
                            // Output Item
                            handleItemToClient(wrapper.passthrough(Type.ITEM));
                            // Has second item
                            boolean secondItem = wrapper.passthrough(Type.BOOLEAN);
                            if (secondItem) {
                                // Second Item
                                handleItemToClient(wrapper.passthrough(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_12.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 {
                    Item item = wrapper.get(Type.ITEM, 0);
                    if (!Via.getConfig().is1_12QuickMoveActionFix()) {
                        handleItemToServer(item);
                        return;
                    }
                    byte button = wrapper.get(Type.BYTE, 0);
                    int mode = wrapper.get(Type.VAR_INT, 0);
                    // QUICK_MOVE PATCH (Shift + (click/double click))
                    if (mode == 1 && button == 0 && item == null) {
                        short windowId = wrapper.get(Type.UNSIGNED_BYTE, 0);
                        short slotId = wrapper.get(Type.SHORT, 0);
                        short actionId = wrapper.get(Type.SHORT, 1);
                        InventoryQuickMoveProvider provider = Via.getManager().getProviders().get(InventoryQuickMoveProvider.class);
                        boolean succeed = provider.registerQuickMoveAction(windowId, slotId, actionId, wrapper.user());
                        if (succeed) {
                            wrapper.cancel();
                        }
                    // otherwise just pass through so the server sends the PacketPlayOutTransaction packet.
                    } else {
                        handleItemToServer(item);
                    }
                }
            });
        }
    });
    // Creative Inventory Action
    registerCreativeInvAction(ServerboundPackets1_12.CREATIVE_INVENTORY_ACTION, Type.ITEM);
}
Also used : Item(com.viaversion.viaversion.api.minecraft.item.Item) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) InventoryQuickMoveProvider(com.viaversion.viaversion.protocols.protocol1_12to1_11_1.providers.InventoryQuickMoveProvider)

Example 18 with Item

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

the class Protocol1_13_2To1_13_1 method registerPackets.

@Override
protected void registerPackets() {
    InventoryPackets.register(this);
    WorldPackets.register(this);
    EntityPackets.register(this);
    registerServerbound(ServerboundPackets1_13.EDIT_BOOK, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.FLAT_VAR_INT_ITEM, Type.FLAT_ITEM);
        }
    });
    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.read(Type.FLAT_ITEM);
                            wrapper.write(Type.FLAT_VAR_INT_ITEM, 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);
                        }
                    }
                }
            });
        }
    });
}
Also used : Item(com.viaversion.viaversion.api.minecraft.item.Item) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)

Example 19 with Item

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

the class EntityPackets method register.

public static void register(Protocol1_9To1_8 protocol) {
    // Attach Entity Packet
    protocol.registerClientbound(ClientboundPackets1_8.ATTACH_ENTITY, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.INT);
            // 1 - Vehicle
            map(Type.INT);
            // Leash boolean is removed in new versions
            map(Type.BOOLEAN, new ValueTransformer<Boolean, Void>(Type.NOTHING) {

                @Override
                public Void transform(PacketWrapper wrapper, Boolean inputValue) throws Exception {
                    EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                    if (!inputValue) {
                        int passenger = wrapper.get(Type.INT, 0);
                        int vehicle = wrapper.get(Type.INT, 1);
                        // Don't send current packet
                        wrapper.cancel();
                        PacketWrapper passengerPacket = wrapper.create(ClientboundPackets1_9.SET_PASSENGERS);
                        if (vehicle == -1) {
                            if (!tracker.getVehicleMap().containsKey(passenger))
                                // Cancel
                                return null;
                            passengerPacket.write(Type.VAR_INT, tracker.getVehicleMap().remove(passenger));
                            passengerPacket.write(Type.VAR_INT_ARRAY_PRIMITIVE, new int[] {});
                        } else {
                            passengerPacket.write(Type.VAR_INT, vehicle);
                            passengerPacket.write(Type.VAR_INT_ARRAY_PRIMITIVE, new int[] { passenger });
                            tracker.getVehicleMap().put(passenger, vehicle);
                        }
                        // Send the packet
                        passengerPacket.send(Protocol1_9To1_8.class);
                    }
                    return null;
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.ENTITY_TELEPORT, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.VAR_INT);
            // 1 - X - Needs to be divide by 32
            map(Type.INT, SpawnPackets.toNewDouble);
            // 2 - Y - Needs to be divide by 32
            map(Type.INT, SpawnPackets.toNewDouble);
            // 3 - Z - Needs to be divide by 32
            map(Type.INT, SpawnPackets.toNewDouble);
            // 4 - Pitch
            map(Type.BYTE);
            // 5 - Yaw
            map(Type.BYTE);
            // 6 - On Ground
            map(Type.BOOLEAN);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int entityID = wrapper.get(Type.VAR_INT, 0);
                    if (Via.getConfig().isHologramPatch()) {
                        EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                        if (tracker.getKnownHolograms().contains(entityID)) {
                            Double newValue = wrapper.get(Type.DOUBLE, 1);
                            newValue += (Via.getConfig().getHologramYOffset());
                            wrapper.set(Type.DOUBLE, 1, newValue);
                        }
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.ENTITY_POSITION_AND_ROTATION, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.VAR_INT);
            // 1 - X
            map(Type.BYTE, toNewShort);
            // 2 - Y
            map(Type.BYTE, toNewShort);
            // 3 - Z
            map(Type.BYTE, toNewShort);
            // 4 - Yaw
            map(Type.BYTE);
            // 5 - Pitch
            map(Type.BYTE);
            // 6 - On Ground
            map(Type.BOOLEAN);
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.ENTITY_POSITION, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.VAR_INT);
            // 1 - X
            map(Type.BYTE, toNewShort);
            // 2 - Y
            map(Type.BYTE, toNewShort);
            // 3 - Z
            map(Type.BYTE, toNewShort);
            // 4 - On Ground
            map(Type.BOOLEAN);
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.ENTITY_EQUIPMENT, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.VAR_INT);
            // 1 - Slot ID
            map(Type.SHORT, new ValueTransformer<Short, Integer>(Type.VAR_INT) {

                @Override
                public Integer transform(PacketWrapper wrapper, Short slot) throws Exception {
                    int entityId = wrapper.get(Type.VAR_INT, 0);
                    int receiverId = wrapper.user().getEntityTracker(Protocol1_9To1_8.class).clientEntityId();
                    // (In 1.9+, every client treats the received the same: 0=hand, 1=offhand, 2-5=armor)
                    if (entityId == receiverId) {
                        return slot.intValue() + 2;
                    }
                    return slot > 0 ? slot.intValue() + 1 : slot.intValue();
                }
            });
            // 2 - Item
            map(Type.ITEM);
            // Item Rewriter
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    Item stack = wrapper.get(Type.ITEM, 0);
                    ItemRewriter.toClient(stack);
                }
            });
            // Blocking
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    EntityTracker1_9 entityTracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                    int entityID = wrapper.get(Type.VAR_INT, 0);
                    Item stack = wrapper.get(Type.ITEM, 0);
                    if (stack != null) {
                        if (Protocol1_9To1_8.isSword(stack.identifier())) {
                            entityTracker.getValidBlocking().add(entityID);
                            return;
                        }
                    }
                    entityTracker.getValidBlocking().remove(entityID);
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.ENTITY_METADATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.VAR_INT);
            // 1 - Metadata List
            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 {
                        // Buffer
                        tracker.addMetadataToBuffer(entityId, metadataList);
                        wrapper.cancel();
                    }
                }
            });
            // 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);
                }
            });
            // Cancel packet if list empty
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    List<Metadata> metadataList = wrapper.get(Types1_9.METADATA_LIST, 0);
                    if (metadataList.isEmpty()) {
                        wrapper.cancel();
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.ENTITY_EFFECT, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.VAR_INT);
            // 1 - Effect ID
            map(Type.BYTE);
            // 2 - Amplifier
            map(Type.BYTE);
            // 3 - Duration
            map(Type.VAR_INT);
            handler(new // Handle effect indicator
            PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    // In 1.8 = true->Show particles : false->Hide particles
                    boolean showParticles = wrapper.read(Type.BOOLEAN);
                    boolean newEffect = Via.getConfig().isNewEffectIndicator();
                    // 0: hide, 1: shown without indictator, 2: shown with indicator, 3: hide with beacon indicator but we don't use it.
                    wrapper.write(Type.BYTE, (byte) (showParticles ? newEffect ? 2 : 1 : 0));
                }
            });
        }
    });
    protocol.cancelClientbound(ClientboundPackets1_8.UPDATE_ENTITY_NBT);
    protocol.registerClientbound(ClientboundPackets1_8.COMBAT_EVENT, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    if (wrapper.get(Type.VAR_INT, 0) == 2) {
                        // entity dead
                        // Player id
                        wrapper.passthrough(Type.VAR_INT);
                        // Entity id
                        wrapper.passthrough(Type.INT);
                        Protocol1_9To1_8.FIX_JSON.write(wrapper, wrapper.read(Type.STRING));
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.ENTITY_PROPERTIES, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    if (!Via.getConfig().isMinimizeCooldown())
                        return;
                    EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                    if (wrapper.get(Type.VAR_INT, 0) != tracker.getProvidedEntityId()) {
                        return;
                    }
                    int propertiesToRead = wrapper.read(Type.INT);
                    Map<String, Pair<Double, List<Triple<UUID, Double, Byte>>>> properties = new HashMap<>(propertiesToRead);
                    for (int i = 0; i < propertiesToRead; i++) {
                        String key = wrapper.read(Type.STRING);
                        Double value = wrapper.read(Type.DOUBLE);
                        int modifiersToRead = wrapper.read(Type.VAR_INT);
                        List<Triple<UUID, Double, Byte>> modifiers = new ArrayList<>(modifiersToRead);
                        for (int j = 0; j < modifiersToRead; j++) {
                            modifiers.add(new Triple<>(wrapper.read(Type.UUID), // Amount
                            wrapper.read(Type.DOUBLE), // Operation
                            wrapper.read(Type.BYTE)));
                        }
                        properties.put(key, new Pair<>(value, modifiers));
                    }
                    // == Why 15.9? ==
                    // Higher values hides the cooldown but it bugs visual animation on hand
                    // when removing item from hand with inventory gui
                    properties.put("generic.attackSpeed", new Pair<Double, List<Triple<UUID, Double, Byte>>>(15.9, // Neutralize modifiers
                    ImmutableList.of(// Tool and weapon modifier
                    new Triple<>(UUID.fromString("FA233E1C-4180-4865-B01B-BCCE9785ACA3"), 0.0, (byte) 0), // Dig speed
                    new Triple<>(UUID.fromString("AF8B6E3F-3328-4C0A-AA36-5BA2BB9DBEF3"), 0.0, (byte) 2), // Dig slow down
                    new Triple<>(UUID.fromString("55FCED67-E92A-486E-9800-B47F202C4386"), 0.0, (byte) 2))));
                    wrapper.write(Type.INT, properties.size());
                    for (Map.Entry<String, Pair<Double, List<Triple<UUID, Double, Byte>>>> entry : properties.entrySet()) {
                        // Key
                        wrapper.write(Type.STRING, entry.getKey());
                        // Value
                        wrapper.write(Type.DOUBLE, entry.getValue().key());
                        wrapper.write(Type.VAR_INT, entry.getValue().value().size());
                        for (Triple<UUID, Double, Byte> modifier : entry.getValue().value()) {
                            wrapper.write(Type.UUID, modifier.first());
                            // Amount
                            wrapper.write(Type.DOUBLE, modifier.second());
                            // Operation
                            wrapper.write(Type.BYTE, modifier.third());
                        }
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.ENTITY_ANIMATION, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    if (wrapper.get(Type.UNSIGNED_BYTE, 0) == 3) {
                        wrapper.cancel();
                    }
                }
            });
        }
    });
    /* Incoming Packets */
    protocol.registerServerbound(ServerboundPackets1_9.ENTITY_ACTION, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Player ID
            map(Type.VAR_INT);
            // 1 - Action
            map(Type.VAR_INT);
            // 2 - Jump
            map(Type.VAR_INT);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int action = wrapper.get(Type.VAR_INT, 1);
                    if (action == 6 || action == 8)
                        wrapper.cancel();
                    if (action == 7) {
                        wrapper.set(Type.VAR_INT, 1, 6);
                    }
                }
            });
        }
    });
    protocol.registerServerbound(ServerboundPackets1_9.INTERACT_ENTITY, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID (Target)
            map(Type.VAR_INT);
            // 1 - Action Type
            map(Type.VAR_INT);
            // Cancel second hand to prevent double interact
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int type = wrapper.get(Type.VAR_INT, 1);
                    if (type == 2) {
                        // 2 - X
                        wrapper.passthrough(Type.FLOAT);
                        // 3 - Y
                        wrapper.passthrough(Type.FLOAT);
                        // 4 - Z
                        wrapper.passthrough(Type.FLOAT);
                    }
                    if (type == 0 || type == 2) {
                        // 2/5 - Hand
                        int hand = wrapper.read(Type.VAR_INT);
                        if (hand == 1)
                            wrapper.cancel();
                    }
                }
            });
        }
    });
}
Also used : HashMap(java.util.HashMap) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata) ArrayList(java.util.ArrayList) Item(com.viaversion.viaversion.api.minecraft.item.Item) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) MetadataRewriter1_9To1_8(com.viaversion.viaversion.protocols.protocol1_9to1_8.metadata.MetadataRewriter1_9To1_8) UUID(java.util.UUID) Pair(com.viaversion.viaversion.util.Pair) ValueTransformer(com.viaversion.viaversion.api.protocol.remapper.ValueTransformer) EntityTracker1_9(com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.EntityTracker1_9) Triple(com.viaversion.viaversion.util.Triple) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) HashMap(java.util.HashMap) Map(java.util.Map) Protocol1_9To1_8(com.viaversion.viaversion.protocols.protocol1_9to1_8.Protocol1_9To1_8)

Example 20 with Item

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

the class PlayerPackets method register.

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

        @Override
        public void registerMap() {
            // 0 - Chat Message (json)
            map(Type.STRING, Protocol1_9To1_8.FIX_JSON);
            // 1 - Chat Positon
            map(Type.BYTE);
            handler(wrapper -> {
                try {
                    JsonObject obj = (JsonObject) wrapper.get(Type.COMPONENT, 0);
                    ChatRewriter.toClient(obj, wrapper.user());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.TAB_LIST, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Header
            map(Type.STRING, Protocol1_9To1_8.FIX_JSON);
            // 1 - Footer
            map(Type.STRING, Protocol1_9To1_8.FIX_JSON);
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.DISCONNECT, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Reason
            map(Type.STRING, Protocol1_9To1_8.FIX_JSON);
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.TITLE, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Action
            map(Type.VAR_INT);
            // We only handle if the title or subtitle is set then just write through.
            handler(wrapper -> {
                int action = wrapper.get(Type.VAR_INT, 0);
                if (action == 0 || action == 1) {
                    Protocol1_9To1_8.FIX_JSON.write(wrapper, wrapper.read(Type.STRING));
                }
            });
        // Everything else is handled.
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.PLAYER_POSITION, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Player X
            map(Type.DOUBLE);
            // 1 - Player Y
            map(Type.DOUBLE);
            // 2 - Player Z
            map(Type.DOUBLE);
            // 3 - Player Yaw
            map(Type.FLOAT);
            // 4 - Player Pitch
            map(Type.FLOAT);
            // 5 - Player Flags
            map(Type.BYTE);
            handler(wrapper -> {
                // 6 - Teleport ID was added
                wrapper.write(Type.VAR_INT, 0);
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.TEAMS, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Team Name
            map(Type.STRING);
            // 1 - Mode
            map(Type.BYTE);
            handler(wrapper -> {
                // Mode
                byte mode = wrapper.get(Type.BYTE, 0);
                if (mode == 0 || mode == 2) {
                    // Display Name
                    wrapper.passthrough(Type.STRING);
                    // Prefix
                    wrapper.passthrough(Type.STRING);
                    // Suffix
                    wrapper.passthrough(Type.STRING);
                    // Friendly Fire
                    wrapper.passthrough(Type.BYTE);
                    // Name tag visibility
                    wrapper.passthrough(Type.STRING);
                    wrapper.write(Type.STRING, Via.getConfig().isPreventCollision() ? "never" : "");
                    // Colour
                    wrapper.passthrough(Type.BYTE);
                }
                if (mode == 0 || mode == 3 || mode == 4) {
                    // Players
                    String[] players = wrapper.passthrough(Type.STRING_ARRAY);
                    final EntityTracker1_9 entityTracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                    String myName = wrapper.user().getProtocolInfo().getUsername();
                    String teamName = wrapper.get(Type.STRING, 0);
                    for (String player : players) {
                        if (entityTracker.isAutoTeam() && player.equalsIgnoreCase(myName)) {
                            if (mode == 4) {
                                // since removing add to auto team
                                // Workaround for packet order issue
                                wrapper.send(Protocol1_9To1_8.class);
                                wrapper.cancel();
                                entityTracker.sendTeamPacket(true, true);
                                entityTracker.setCurrentTeam("viaversion");
                            } else {
                                // since adding remove from auto team
                                entityTracker.sendTeamPacket(false, true);
                                entityTracker.setCurrentTeam(teamName);
                            }
                        }
                    }
                }
                if (mode == 1) {
                    // Remove team
                    final EntityTracker1_9 entityTracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                    String teamName = wrapper.get(Type.STRING, 0);
                    if (entityTracker.isAutoTeam() && teamName.equals(entityTracker.getCurrentTeam())) {
                        // team was removed
                        // Workaround for packet order issue
                        wrapper.send(Protocol1_9To1_8.class);
                        wrapper.cancel();
                        entityTracker.sendTeamPacket(true, true);
                        entityTracker.setCurrentTeam("viaversion");
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.JOIN_GAME, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Player ID
            map(Type.INT);
            // Parse this info
            handler(wrapper -> {
                int entityId = wrapper.get(Type.INT, 0);
                EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                tracker.addEntity(entityId, Entity1_10Types.EntityType.PLAYER);
                tracker.setClientEntityId(entityId);
            });
            // 1 - Player Gamemode
            map(Type.UNSIGNED_BYTE);
            // 2 - Player Dimension
            map(Type.BYTE);
            // 3 - World Difficulty
            map(Type.UNSIGNED_BYTE);
            // 4 - Max Players (Tab)
            map(Type.UNSIGNED_BYTE);
            // 5 - Level Type
            map(Type.STRING);
            // 6 - Reduced Debug info
            map(Type.BOOLEAN);
            handler(wrapper -> {
                EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                // Set player gamemode
                tracker.setGameMode(GameMode.getById(wrapper.get(Type.UNSIGNED_BYTE, 0)));
            });
            // Track player's dimension
            handler(wrapper -> {
                ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
                int dimensionId = wrapper.get(Type.BYTE, 0);
                clientWorld.setEnvironment(dimensionId);
            });
            // Gotta fake their op
            handler(wrapper -> {
                CommandBlockProvider provider = Via.getManager().getProviders().get(CommandBlockProvider.class);
                provider.sendPermission(wrapper.user());
            });
            // Scoreboard will be cleared when join game is received
            handler(wrapper -> {
                EntityTracker1_9 entityTracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                if (Via.getConfig().isAutoTeam()) {
                    entityTracker.setAutoTeam(true);
                    // Workaround for packet order issue
                    wrapper.send(Protocol1_9To1_8.class);
                    wrapper.cancel();
                    entityTracker.sendTeamPacket(true, true);
                    entityTracker.setCurrentTeam("viaversion");
                } else {
                    entityTracker.setAutoTeam(false);
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.PLAYER_INFO, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Action
            map(Type.VAR_INT);
            // 1 - Player Count
            map(Type.VAR_INT);
            // Due to this being a complex data structure we just use a handler.
            handler(wrapper -> {
                int action = wrapper.get(Type.VAR_INT, 0);
                int count = wrapper.get(Type.VAR_INT, 1);
                for (int i = 0; i < count; i++) {
                    // Player UUID
                    wrapper.passthrough(Type.UUID);
                    if (action == 0) {
                        // add player
                        // Player Name
                        wrapper.passthrough(Type.STRING);
                        int properties = wrapper.passthrough(Type.VAR_INT);
                        // loop through properties
                        for (int j = 0; j < properties; j++) {
                            // name
                            wrapper.passthrough(Type.STRING);
                            // value
                            wrapper.passthrough(Type.STRING);
                            boolean isSigned = wrapper.passthrough(Type.BOOLEAN);
                            if (isSigned) {
                                // signature
                                wrapper.passthrough(Type.STRING);
                            }
                        }
                        // gamemode
                        wrapper.passthrough(Type.VAR_INT);
                        // ping
                        wrapper.passthrough(Type.VAR_INT);
                        boolean hasDisplayName = wrapper.passthrough(Type.BOOLEAN);
                        if (hasDisplayName) {
                            // display name
                            Protocol1_9To1_8.FIX_JSON.write(wrapper, wrapper.read(Type.STRING));
                        }
                    } else if ((action == 1) || (action == 2)) {
                        // update gamemode || update latency
                        wrapper.passthrough(Type.VAR_INT);
                    } else if (action == 3) {
                        // update display name
                        boolean hasDisplayName = wrapper.passthrough(Type.BOOLEAN);
                        if (hasDisplayName) {
                            // display name
                            Protocol1_9To1_8.FIX_JSON.write(wrapper, wrapper.read(Type.STRING));
                        }
                    } else if (action == 4) {
                    // remove player
                    // no fields
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.PLUGIN_MESSAGE, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Channel Name
            map(Type.STRING);
            handler(wrapper -> {
                String name = wrapper.get(Type.STRING, 0);
                if (name.equalsIgnoreCase("MC|BOpen")) {
                    // Not used anymore
                    wrapper.read(Type.REMAINING_BYTES);
                    wrapper.write(Type.VAR_INT, 0);
                }
                if (name.equalsIgnoreCase("MC|TrList")) {
                    // ID
                    wrapper.passthrough(Type.INT);
                    Short size = wrapper.passthrough(Type.UNSIGNED_BYTE);
                    for (int i = 0; i < size; ++i) {
                        Item item1 = wrapper.passthrough(Type.ITEM);
                        ItemRewriter.toClient(item1);
                        Item item2 = wrapper.passthrough(Type.ITEM);
                        ItemRewriter.toClient(item2);
                        boolean present = wrapper.passthrough(Type.BOOLEAN);
                        if (present) {
                            Item item3 = wrapper.passthrough(Type.ITEM);
                            ItemRewriter.toClient(item3);
                        }
                        wrapper.passthrough(Type.BOOLEAN);
                        wrapper.passthrough(Type.INT);
                        wrapper.passthrough(Type.INT);
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.RESPAWN, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Dimension
            map(Type.INT);
            // 1 - Difficulty
            map(Type.UNSIGNED_BYTE);
            // 2 - GameMode
            map(Type.UNSIGNED_BYTE);
            // 3 - Level Type
            map(Type.STRING);
            // Track player's dimension
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
                    int dimensionId = wrapper.get(Type.INT, 0);
                    clientWorld.setEnvironment(dimensionId);
                }
            });
            handler(wrapper -> {
                // Client unloads chunks on respawn
                wrapper.user().get(ClientChunks.class).getLoadedChunks().clear();
                int gamemode = wrapper.get(Type.UNSIGNED_BYTE, 0);
                EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                tracker.setGameMode(GameMode.getById(gamemode));
            });
            // Fake permissions to get Commandblocks working
            handler(wrapper -> {
                CommandBlockProvider provider = Via.getManager().getProviders().get(CommandBlockProvider.class);
                provider.sendPermission(wrapper.user());
                provider.unloadChunks(wrapper.user());
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.GAME_EVENT, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Reason
            map(Type.UNSIGNED_BYTE);
            // 1 - Value
            map(Type.FLOAT);
            handler(wrapper -> {
                short reason = wrapper.get(Type.UNSIGNED_BYTE, 0);
                if (reason == 3) {
                    // Change gamemode
                    int gamemode = wrapper.get(Type.FLOAT, 0).intValue();
                    EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                    tracker.setGameMode(GameMode.getById(gamemode));
                } else if (reason == 4) {
                    // Open credits screen
                    wrapper.set(Type.FLOAT, 0, 1F);
                }
            });
        }
    });
    /* Removed packets */
    protocol.registerClientbound(ClientboundPackets1_8.SET_COMPRESSION, null, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                wrapper.cancel();
                CompressionProvider provider = Via.getManager().getProviders().get(CompressionProvider.class);
                provider.handlePlayCompression(wrapper.user(), wrapper.read(Type.VAR_INT));
            });
        }
    });
    /* Incoming Packets */
    protocol.registerServerbound(ServerboundPackets1_9.TAB_COMPLETE, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Requested Command
            map(Type.STRING);
            // 1 - Is Command Block
            map(Type.BOOLEAN, Type.NOTHING);
        }
    });
    protocol.registerServerbound(ServerboundPackets1_9.CLIENT_SETTINGS, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - locale
            map(Type.STRING);
            // 1 - View Distance
            map(Type.BYTE);
            // 2 - Chat Mode
            map(Type.VAR_INT, Type.BYTE);
            // 3 - If Chat Colours on
            map(Type.BOOLEAN);
            // 4 - Skin Parts
            map(Type.UNSIGNED_BYTE);
            handler(wrapper -> {
                int hand = wrapper.read(Type.VAR_INT);
                if (Via.getConfig().isLeftHandedHandling()) {
                    // Add 0x80 if left handed
                    if (hand == 0)
                        wrapper.set(Type.UNSIGNED_BYTE, 0, (short) (wrapper.get(Type.UNSIGNED_BYTE, 0).intValue() | 0x80));
                }
                wrapper.sendToServer(Protocol1_9To1_8.class);
                wrapper.cancel();
                Via.getManager().getProviders().get(MainHandProvider.class).setMainHand(wrapper.user(), hand);
            });
        }
    });
    protocol.registerServerbound(ServerboundPackets1_9.ANIMATION, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Hand
            map(Type.VAR_INT, Type.NOTHING);
        }
    });
    protocol.cancelServerbound(ServerboundPackets1_9.TELEPORT_CONFIRM);
    protocol.cancelServerbound(ServerboundPackets1_9.VEHICLE_MOVE);
    protocol.cancelServerbound(ServerboundPackets1_9.STEER_BOAT);
    protocol.registerServerbound(ServerboundPackets1_9.PLUGIN_MESSAGE, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Channel Name
            map(Type.STRING);
            handler(wrapper -> {
                String name = wrapper.get(Type.STRING, 0);
                if (name.equalsIgnoreCase("MC|BSign")) {
                    Item item = wrapper.passthrough(Type.ITEM);
                    if (item != null) {
                        // Written Book
                        item.setIdentifier(387);
                        ItemRewriter.rewriteBookToServer(item);
                    }
                }
                if (name.equalsIgnoreCase("MC|AutoCmd")) {
                    wrapper.set(Type.STRING, 0, "MC|AdvCdm");
                    wrapper.write(Type.BYTE, (byte) 0);
                    // X
                    wrapper.passthrough(Type.INT);
                    // Y
                    wrapper.passthrough(Type.INT);
                    // Z
                    wrapper.passthrough(Type.INT);
                    // Command
                    wrapper.passthrough(Type.STRING);
                    // Flag
                    wrapper.passthrough(Type.BOOLEAN);
                    wrapper.clearInputBuffer();
                }
                if (name.equalsIgnoreCase("MC|AdvCmd")) {
                    wrapper.set(Type.STRING, 0, "MC|AdvCdm");
                }
            });
        }
    });
    protocol.registerServerbound(ServerboundPackets1_9.CLIENT_STATUS, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Action ID
            map(Type.VAR_INT);
            handler(wrapper -> {
                int action = wrapper.get(Type.VAR_INT, 0);
                if (action == 2) {
                    // cancel any blocking >.>
                    EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                    if (tracker.isBlocking()) {
                        if (!Via.getConfig().isShowShieldWhenSwordInHand()) {
                            tracker.setSecondHand(null);
                        }
                        tracker.setBlocking(false);
                    }
                }
            });
        }
    });
    protocol.registerServerbound(ServerboundPackets1_9.PLAYER_POSITION, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - X
            map(Type.DOUBLE);
            // 1 - Y
            map(Type.DOUBLE);
            // 2 - Z
            map(Type.DOUBLE);
            // 3 - Ground
            map(Type.BOOLEAN);
            handler(new PlayerMovementMapper());
        }
    });
    protocol.registerServerbound(ServerboundPackets1_9.PLAYER_POSITION_AND_ROTATION, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - X
            map(Type.DOUBLE);
            // 1 - Y
            map(Type.DOUBLE);
            // 2 - Z
            map(Type.DOUBLE);
            // 3 - Yaw
            map(Type.FLOAT);
            // 4 - Pitch
            map(Type.FLOAT);
            // 5 - Ground
            map(Type.BOOLEAN);
            handler(new PlayerMovementMapper());
        }
    });
    protocol.registerServerbound(ServerboundPackets1_9.PLAYER_ROTATION, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Yaw
            map(Type.FLOAT);
            // 1 - Pitch
            map(Type.FLOAT);
            // 2 - Ground
            map(Type.BOOLEAN);
            handler(new PlayerMovementMapper());
        }
    });
    protocol.registerServerbound(ServerboundPackets1_9.PLAYER_MOVEMENT, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Ground
            map(Type.BOOLEAN);
            handler(new PlayerMovementMapper());
        }
    });
}
Also used : PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PlayerMovementMapper(com.viaversion.viaversion.protocols.protocol1_9to1_8.PlayerMovementMapper) JsonObject(com.google.gson.JsonObject) GameMode(com.viaversion.viaversion.protocols.protocol1_9to1_8.chat.GameMode) ClientWorld(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld) ChatRewriter(com.viaversion.viaversion.protocols.protocol1_9to1_8.chat.ChatRewriter) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) Via(com.viaversion.viaversion.api.Via) Entity1_10Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_10Types) Item(com.viaversion.viaversion.api.minecraft.item.Item) CommandBlockProvider(com.viaversion.viaversion.protocols.protocol1_9to1_8.providers.CommandBlockProvider) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) MainHandProvider(com.viaversion.viaversion.protocols.protocol1_9to1_8.providers.MainHandProvider) Protocol1_9To1_8(com.viaversion.viaversion.protocols.protocol1_9to1_8.Protocol1_9To1_8) EntityTracker1_9(com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.EntityTracker1_9) ServerboundPackets1_9(com.viaversion.viaversion.protocols.protocol1_9to1_8.ServerboundPackets1_9) CompressionProvider(com.viaversion.viaversion.protocols.protocol1_9to1_8.providers.CompressionProvider) ClientboundPackets1_8(com.viaversion.viaversion.protocols.protocol1_8.ClientboundPackets1_8) ItemRewriter(com.viaversion.viaversion.protocols.protocol1_9to1_8.ItemRewriter) ClientChunks(com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.ClientChunks) EntityTracker1_9(com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.EntityTracker1_9) ClientChunks(com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.ClientChunks) PlayerMovementMapper(com.viaversion.viaversion.protocols.protocol1_9to1_8.PlayerMovementMapper) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) JsonObject(com.google.gson.JsonObject) CommandBlockProvider(com.viaversion.viaversion.protocols.protocol1_9to1_8.providers.CommandBlockProvider) CompressionProvider(com.viaversion.viaversion.protocols.protocol1_9to1_8.providers.CompressionProvider) Item(com.viaversion.viaversion.api.minecraft.item.Item) 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) Protocol1_9To1_8(com.viaversion.viaversion.protocols.protocol1_9to1_8.Protocol1_9To1_8)

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