Search in sources :

Example 6 with StatisticsRewriter

use of com.viaversion.viaversion.rewriter.StatisticsRewriter in project ViaVersion by ViaVersion.

the class Protocol1_16To1_15_2 method registerPackets.

@Override
protected void registerPackets() {
    metadataRewriter.register();
    itemRewriter.register();
    EntityPackets.register(this);
    WorldPackets.register(this);
    tagRewriter = new TagRewriter(this);
    tagRewriter.register(ClientboundPackets1_15.TAGS, RegistryType.ENTITY);
    new StatisticsRewriter(this).register(ClientboundPackets1_15.STATISTICS);
    // Login Success
    registerClientbound(State.LOGIN, 0x02, 0x02, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                // Transform string to int array
                UUID uuid = UUID.fromString(wrapper.read(Type.STRING));
                wrapper.write(Type.UUID_INT_ARRAY, uuid);
            });
        }
    });
    // Motd Status - line breaks are no longer allowed for player samples
    registerClientbound(State.STATUS, 0x00, 0x00, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                String original = wrapper.passthrough(Type.STRING);
                JsonObject object = GsonUtil.getGson().fromJson(original, JsonObject.class);
                JsonObject players = object.getAsJsonObject("players");
                if (players == null)
                    return;
                JsonArray sample = players.getAsJsonArray("sample");
                if (sample == null)
                    return;
                JsonArray splitSamples = new JsonArray();
                for (JsonElement element : sample) {
                    JsonObject playerInfo = element.getAsJsonObject();
                    String name = playerInfo.getAsJsonPrimitive("name").getAsString();
                    if (name.indexOf('\n') == -1) {
                        splitSamples.add(playerInfo);
                        continue;
                    }
                    String id = playerInfo.getAsJsonPrimitive("id").getAsString();
                    for (String s : name.split("\n")) {
                        JsonObject newSample = new JsonObject();
                        newSample.addProperty("name", s);
                        newSample.addProperty("id", id);
                        splitSamples.add(newSample);
                    }
                }
                // Replace data if changed
                if (splitSamples.size() != sample.size()) {
                    players.add("sample", splitSamples);
                    wrapper.set(Type.STRING, 0, object.toString());
                }
            });
        }
    });
    ComponentRewriter componentRewriter = new TranslationMappings(this);
    // Handle (relevant) component cases for translatable and score changes
    registerClientbound(ClientboundPackets1_15.CHAT_MESSAGE, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.COMPONENT);
            map(Type.BYTE);
            handler(wrapper -> {
                componentRewriter.processText(wrapper.get(Type.COMPONENT, 0));
                // Sender uuid - always send as 'system'
                wrapper.write(Type.UUID, ZERO_UUID);
            });
        }
    });
    componentRewriter.registerBossBar(ClientboundPackets1_15.BOSSBAR);
    componentRewriter.registerTitle(ClientboundPackets1_15.TITLE);
    componentRewriter.registerCombatEvent(ClientboundPackets1_15.COMBAT_EVENT);
    SoundRewriter soundRewriter = new SoundRewriter(this);
    soundRewriter.registerSound(ClientboundPackets1_15.SOUND);
    soundRewriter.registerSound(ClientboundPackets1_15.ENTITY_SOUND);
    registerServerbound(ServerboundPackets1_16.INTERACT_ENTITY, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                // Entity Id
                wrapper.passthrough(Type.VAR_INT);
                int action = wrapper.passthrough(Type.VAR_INT);
                if (action == 0 || action == 2) {
                    if (action == 2) {
                        // Location
                        wrapper.passthrough(Type.FLOAT);
                        wrapper.passthrough(Type.FLOAT);
                        wrapper.passthrough(Type.FLOAT);
                    }
                    // Hand
                    wrapper.passthrough(Type.VAR_INT);
                }
                // New boolean: Whether the client is sneaking/pressing shift
                wrapper.read(Type.BOOLEAN);
            });
        }
    });
    if (Via.getConfig().isIgnoreLong1_16ChannelNames()) {
        registerServerbound(ServerboundPackets1_16.PLUGIN_MESSAGE, new PacketRemapper() {

            @Override
            public void registerMap() {
                handler(wrapper -> {
                    String channel = wrapper.passthrough(Type.STRING);
                    if (channel.length() > 32) {
                        if (!Via.getConfig().isSuppressConversionWarnings()) {
                            Via.getPlatform().getLogger().warning("Ignoring incoming plugin channel, as it is longer than 32 characters: " + channel);
                        }
                        wrapper.cancel();
                    } else if (channel.equals("minecraft:register") || channel.equals("minecraft:unregister")) {
                        String[] channels = new String(wrapper.read(Type.REMAINING_BYTES), StandardCharsets.UTF_8).split("\0");
                        List<String> checkedChannels = new ArrayList<>(channels.length);
                        for (String registeredChannel : channels) {
                            if (registeredChannel.length() > 32) {
                                if (!Via.getConfig().isSuppressConversionWarnings()) {
                                    Via.getPlatform().getLogger().warning("Ignoring incoming plugin channel register of '" + registeredChannel + "', as it is longer than 32 characters");
                                }
                                continue;
                            }
                            checkedChannels.add(registeredChannel);
                        }
                        if (checkedChannels.isEmpty()) {
                            wrapper.cancel();
                            return;
                        }
                        wrapper.write(Type.REMAINING_BYTES, Joiner.on('\0').join(checkedChannels).getBytes(StandardCharsets.UTF_8));
                    }
                });
            }
        });
    }
    registerServerbound(ServerboundPackets1_16.PLAYER_ABILITIES, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                wrapper.passthrough(Type.BYTE);
                // Flying and walking speed - not important anyways
                wrapper.write(Type.FLOAT, 0.05F);
                wrapper.write(Type.FLOAT, 0.1F);
            });
        }
    });
    cancelServerbound(ServerboundPackets1_16.GENERATE_JIGSAW);
    cancelServerbound(ServerboundPackets1_16.UPDATE_JIGSAW_BLOCK);
}
Also used : ComponentRewriter(com.viaversion.viaversion.rewriter.ComponentRewriter) JsonObject(com.google.gson.JsonObject) SoundRewriter(com.viaversion.viaversion.rewriter.SoundRewriter) ParticleType(com.viaversion.viaversion.api.type.types.minecraft.ParticleType) GsonUtil(com.viaversion.viaversion.util.GsonUtil) Types1_16(com.viaversion.viaversion.api.type.types.version.Types1_16) ArrayList(java.util.ArrayList) JsonElement(com.google.gson.JsonElement) RegistryType(com.viaversion.viaversion.api.minecraft.RegistryType) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) Entity1_16Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_16Types) EntityTrackerBase(com.viaversion.viaversion.data.entity.EntityTrackerBase) UserConnection(com.viaversion.viaversion.api.connection.UserConnection) ItemRewriter(com.viaversion.viaversion.api.rewriter.ItemRewriter) MetadataRewriter1_16To1_15_2(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.metadata.MetadataRewriter1_16To1_15_2) EntityPackets(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.packets.EntityPackets) MappingData(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.data.MappingData) InventoryPackets(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.packets.InventoryPackets) State(com.viaversion.viaversion.api.protocol.packet.State) UUID(java.util.UUID) Via(com.viaversion.viaversion.api.Via) AbstractProtocol(com.viaversion.viaversion.api.protocol.AbstractProtocol) ServerboundPackets1_14(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ServerboundPackets1_14) StandardCharsets(java.nio.charset.StandardCharsets) JsonArray(com.google.gson.JsonArray) InventoryTracker1_16(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.storage.InventoryTracker1_16) List(java.util.List) TagRewriter(com.viaversion.viaversion.rewriter.TagRewriter) StatisticsRewriter(com.viaversion.viaversion.rewriter.StatisticsRewriter) TranslationMappings(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.data.TranslationMappings) ClientboundPackets1_15(com.viaversion.viaversion.protocols.protocol1_15to1_14_4.ClientboundPackets1_15) EntityRewriter(com.viaversion.viaversion.api.rewriter.EntityRewriter) WorldPackets(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.packets.WorldPackets) Joiner(com.google.common.base.Joiner) TagRewriter(com.viaversion.viaversion.rewriter.TagRewriter) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) JsonArray(com.google.gson.JsonArray) StatisticsRewriter(com.viaversion.viaversion.rewriter.StatisticsRewriter) ComponentRewriter(com.viaversion.viaversion.rewriter.ComponentRewriter) JsonElement(com.google.gson.JsonElement) SoundRewriter(com.viaversion.viaversion.rewriter.SoundRewriter) TranslationMappings(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.data.TranslationMappings) UUID(java.util.UUID)

Example 7 with StatisticsRewriter

use of com.viaversion.viaversion.rewriter.StatisticsRewriter in project ViaBackwards by ViaVersion.

the class Protocol1_13To1_13_1 method registerPackets.

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

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

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

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

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

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

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

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

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

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

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

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

Example 8 with StatisticsRewriter

use of com.viaversion.viaversion.rewriter.StatisticsRewriter in project ViaBackwards by ViaVersion.

the class Protocol1_15_2To1_16 method registerPackets.

@Override
protected void registerPackets() {
    executeAsyncAfterLoaded(Protocol1_16To1_15_2.class, MAPPINGS::load);
    translatableRewriter.registerBossBar(ClientboundPackets1_16.BOSSBAR);
    translatableRewriter.registerCombatEvent(ClientboundPackets1_16.COMBAT_EVENT);
    translatableRewriter.registerDisconnect(ClientboundPackets1_16.DISCONNECT);
    translatableRewriter.registerTabList(ClientboundPackets1_16.TAB_LIST);
    translatableRewriter.registerTitle(ClientboundPackets1_16.TITLE);
    translatableRewriter.registerPing();
    new CommandRewriter1_16(this).registerDeclareCommands(ClientboundPackets1_16.DECLARE_COMMANDS);
    (blockItemPackets = new BlockItemPackets1_16(this)).register();
    entityRewriter.register();
    registerClientbound(State.STATUS, 0x00, 0x00, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                String original = wrapper.passthrough(Type.STRING);
                JsonObject object = GsonUtil.getGson().fromJson(original, JsonObject.class);
                JsonElement description = object.get("description");
                if (description == null)
                    return;
                translatableRewriter.processText(description);
                wrapper.set(Type.STRING, 0, object.toString());
            });
        }
    });
    registerClientbound(ClientboundPackets1_16.CHAT_MESSAGE, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> translatableRewriter.processText(wrapper.passthrough(Type.COMPONENT)));
            map(Type.BYTE);
            // Sender
            map(Type.UUID, Type.NOTHING);
        }
    });
    registerClientbound(ClientboundPackets1_16.OPEN_WINDOW, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Window Id
            map(Type.VAR_INT);
            // Window Type
            map(Type.VAR_INT);
            handler(wrapper -> translatableRewriter.processText(wrapper.passthrough(Type.COMPONENT)));
            handler(wrapper -> {
                int windowType = wrapper.get(Type.VAR_INT, 1);
                if (windowType == 20) {
                    // Smithing table
                    // Open anvil inventory
                    wrapper.set(Type.VAR_INT, 1, 7);
                } else if (windowType > 20) {
                    wrapper.set(Type.VAR_INT, 1, --windowType);
                }
            });
        }
    });
    SoundRewriter soundRewriter = new SoundRewriter(this);
    soundRewriter.registerSound(ClientboundPackets1_16.SOUND);
    soundRewriter.registerSound(ClientboundPackets1_16.ENTITY_SOUND);
    soundRewriter.registerNamedSound(ClientboundPackets1_16.NAMED_SOUND);
    soundRewriter.registerStopSound(ClientboundPackets1_16.STOP_SOUND);
    // Login success
    registerClientbound(State.LOGIN, 0x02, 0x02, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                // Transform int array to plain string
                UUID uuid = wrapper.read(Type.UUID_INT_ARRAY);
                wrapper.write(Type.STRING, uuid.toString());
            });
        }
    });
    new TagRewriter(this).register(ClientboundPackets1_16.TAGS, RegistryType.ENTITY);
    new StatisticsRewriter(this).register(ClientboundPackets1_16.STATISTICS);
    registerServerbound(ServerboundPackets1_14.ENTITY_ACTION, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                // player id
                wrapper.passthrough(Type.VAR_INT);
                int action = wrapper.passthrough(Type.VAR_INT);
                if (action == 0) {
                    wrapper.user().get(PlayerSneakStorage.class).setSneaking(true);
                } else if (action == 1) {
                    wrapper.user().get(PlayerSneakStorage.class).setSneaking(false);
                }
            });
        }
    });
    registerServerbound(ServerboundPackets1_14.INTERACT_ENTITY, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                // Entity Id
                wrapper.passthrough(Type.VAR_INT);
                int action = wrapper.passthrough(Type.VAR_INT);
                if (action == 0 || action == 2) {
                    if (action == 2) {
                        // Location
                        wrapper.passthrough(Type.FLOAT);
                        wrapper.passthrough(Type.FLOAT);
                        wrapper.passthrough(Type.FLOAT);
                    }
                    // Hand
                    wrapper.passthrough(Type.VAR_INT);
                }
                // New boolean: Whether the client is sneaking
                wrapper.write(Type.BOOLEAN, wrapper.user().get(PlayerSneakStorage.class).isSneaking());
            });
        }
    });
    registerServerbound(ServerboundPackets1_14.PLAYER_ABILITIES, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                byte flags = wrapper.read(Type.BYTE);
                // Only take the isFlying value (everything else has been removed and wasn't used anyways)
                flags &= 2;
                wrapper.write(Type.BYTE, flags);
                wrapper.read(Type.FLOAT);
                wrapper.read(Type.FLOAT);
            });
        }
    });
    cancelServerbound(ServerboundPackets1_14.UPDATE_JIGSAW_BLOCK);
}
Also used : TranslatableRewriter(com.viaversion.viabackwards.api.rewriters.TranslatableRewriter) WorldNameTracker(com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.data.WorldNameTracker) TranslatableRewriter1_16(com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.chat.TranslatableRewriter1_16) GsonUtil(com.viaversion.viaversion.util.GsonUtil) BackwardsProtocol(com.viaversion.viabackwards.api.BackwardsProtocol) Protocol1_16To1_15_2(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.Protocol1_16To1_15_2) RegistryType(com.viaversion.viaversion.api.minecraft.RegistryType) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) Entity1_16Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_16Types) EntityTrackerBase(com.viaversion.viaversion.data.entity.EntityTrackerBase) UserConnection(com.viaversion.viaversion.api.connection.UserConnection) BackwardsMappings(com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.data.BackwardsMappings) PlayerSneakStorage(com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.storage.PlayerSneakStorage) SoundRewriter(com.viaversion.viabackwards.api.rewriters.SoundRewriter) JsonElement(com.viaversion.viaversion.libs.gson.JsonElement) CommandRewriter1_16(com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.data.CommandRewriter1_16) State(com.viaversion.viaversion.api.protocol.packet.State) ClientWorld(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld) UUID(java.util.UUID) ServerboundPackets1_14(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ServerboundPackets1_14) ClientboundPackets1_16(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.ClientboundPackets1_16) JsonObject(com.viaversion.viaversion.libs.gson.JsonObject) ServerboundPackets1_16(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.ServerboundPackets1_16) TagRewriter(com.viaversion.viaversion.rewriter.TagRewriter) EntityPackets1_16(com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.packets.EntityPackets1_16) StatisticsRewriter(com.viaversion.viaversion.rewriter.StatisticsRewriter) BlockItemPackets1_16(com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.packets.BlockItemPackets1_16) ClientboundPackets1_15(com.viaversion.viaversion.protocols.protocol1_15to1_14_4.ClientboundPackets1_15) EntityRewriter(com.viaversion.viaversion.api.rewriter.EntityRewriter) BlockItemPackets1_16(com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.packets.BlockItemPackets1_16) TagRewriter(com.viaversion.viaversion.rewriter.TagRewriter) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) JsonObject(com.viaversion.viaversion.libs.gson.JsonObject) CommandRewriter1_16(com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.data.CommandRewriter1_16) StatisticsRewriter(com.viaversion.viaversion.rewriter.StatisticsRewriter) JsonElement(com.viaversion.viaversion.libs.gson.JsonElement) PlayerSneakStorage(com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.storage.PlayerSneakStorage) SoundRewriter(com.viaversion.viabackwards.api.rewriters.SoundRewriter) UUID(java.util.UUID)

Example 9 with StatisticsRewriter

use of com.viaversion.viaversion.rewriter.StatisticsRewriter in project ViaBackwards by ViaVersion.

the class Protocol1_14_4To1_15 method registerPackets.

@Override
protected void registerPackets() {
    executeAsyncAfterLoaded(Protocol1_15To1_14_4.class, MAPPINGS::load);
    translatableRewriter.registerBossBar(ClientboundPackets1_15.BOSSBAR);
    translatableRewriter.registerChatMessage(ClientboundPackets1_15.CHAT_MESSAGE);
    translatableRewriter.registerCombatEvent(ClientboundPackets1_15.COMBAT_EVENT);
    translatableRewriter.registerDisconnect(ClientboundPackets1_15.DISCONNECT);
    translatableRewriter.registerOpenWindow(ClientboundPackets1_15.OPEN_WINDOW);
    translatableRewriter.registerTabList(ClientboundPackets1_15.TAB_LIST);
    translatableRewriter.registerTitle(ClientboundPackets1_15.TITLE);
    translatableRewriter.registerPing();
    (blockItemPackets = new BlockItemPackets1_15(this)).register();
    entityRewriter.register();
    SoundRewriter soundRewriter = new SoundRewriter(this);
    soundRewriter.registerSound(ClientboundPackets1_15.SOUND);
    soundRewriter.registerSound(ClientboundPackets1_15.ENTITY_SOUND);
    soundRewriter.registerNamedSound(ClientboundPackets1_15.NAMED_SOUND);
    soundRewriter.registerStopSound(ClientboundPackets1_15.STOP_SOUND);
    // Explosion - manually send an explosion sound
    registerClientbound(ClientboundPackets1_15.EXPLOSION, new PacketRemapper() {

        @Override
        public void registerMap() {
            // x
            map(Type.FLOAT);
            // y
            map(Type.FLOAT);
            // z
            map(Type.FLOAT);
            handler(wrapper -> {
                PacketWrapper soundPacket = wrapper.create(ClientboundPackets1_14.SOUND);
                // entity.generic.explode
                soundPacket.write(Type.VAR_INT, 243);
                // blocks category
                soundPacket.write(Type.VAR_INT, 4);
                // x
                soundPacket.write(Type.INT, toEffectCoordinate(wrapper.get(Type.FLOAT, 0)));
                // y
                soundPacket.write(Type.INT, toEffectCoordinate(wrapper.get(Type.FLOAT, 1)));
                // z
                soundPacket.write(Type.INT, toEffectCoordinate(wrapper.get(Type.FLOAT, 2)));
                // volume
                soundPacket.write(Type.FLOAT, 4F);
                // pitch - usually semi randomized by the server, but we don't really have to care about that
                soundPacket.write(Type.FLOAT, 1F);
                soundPacket.send(Protocol1_14_4To1_15.class);
            });
        }

        private int toEffectCoordinate(float coordinate) {
            return (int) (coordinate * 8);
        }
    });
    new TagRewriter(this).register(ClientboundPackets1_15.TAGS, RegistryType.ENTITY);
    new StatisticsRewriter(this).register(ClientboundPackets1_15.STATISTICS);
}
Also used : BlockItemPackets1_15(com.viaversion.viabackwards.protocol.protocol1_14_4to1_15.packets.BlockItemPackets1_15) TranslatableRewriter(com.viaversion.viabackwards.api.rewriters.TranslatableRewriter) Entity1_15Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_15Types) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) ServerboundPackets1_14(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ServerboundPackets1_14) BackwardsProtocol(com.viaversion.viabackwards.api.BackwardsProtocol) RegistryType(com.viaversion.viaversion.api.minecraft.RegistryType) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) ImmediateRespawn(com.viaversion.viabackwards.protocol.protocol1_14_4to1_15.data.ImmediateRespawn) TagRewriter(com.viaversion.viaversion.rewriter.TagRewriter) StatisticsRewriter(com.viaversion.viaversion.rewriter.StatisticsRewriter) BlockItemPackets1_15(com.viaversion.viabackwards.protocol.protocol1_14_4to1_15.packets.BlockItemPackets1_15) BackwardsMappings(com.viaversion.viabackwards.api.data.BackwardsMappings) Protocol1_15To1_14_4(com.viaversion.viaversion.protocols.protocol1_15to1_14_4.Protocol1_15To1_14_4) EntityTrackerBase(com.viaversion.viaversion.data.entity.EntityTrackerBase) UserConnection(com.viaversion.viaversion.api.connection.UserConnection) ClientboundPackets1_15(com.viaversion.viaversion.protocols.protocol1_15to1_14_4.ClientboundPackets1_15) ClientboundPackets1_14(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14) EntityRewriter(com.viaversion.viaversion.api.rewriter.EntityRewriter) EntityPackets1_15(com.viaversion.viabackwards.protocol.protocol1_14_4to1_15.packets.EntityPackets1_15) SoundRewriter(com.viaversion.viabackwards.api.rewriters.SoundRewriter) StatisticsRewriter(com.viaversion.viaversion.rewriter.StatisticsRewriter) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) TagRewriter(com.viaversion.viaversion.rewriter.TagRewriter) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) SoundRewriter(com.viaversion.viabackwards.api.rewriters.SoundRewriter)

Example 10 with StatisticsRewriter

use of com.viaversion.viaversion.rewriter.StatisticsRewriter in project ViaBackwards by ViaVersion.

the class Protocol1_13_2To1_14 method registerPackets.

@Override
protected void registerPackets() {
    executeAsyncAfterLoaded(Protocol1_14To1_13_2.class, MAPPINGS::load);
    translatableRewriter.registerBossBar(ClientboundPackets1_14.BOSSBAR);
    translatableRewriter.registerChatMessage(ClientboundPackets1_14.CHAT_MESSAGE);
    translatableRewriter.registerCombatEvent(ClientboundPackets1_14.COMBAT_EVENT);
    translatableRewriter.registerDisconnect(ClientboundPackets1_14.DISCONNECT);
    translatableRewriter.registerTabList(ClientboundPackets1_14.TAB_LIST);
    translatableRewriter.registerTitle(ClientboundPackets1_14.TITLE);
    translatableRewriter.registerPing();
    new CommandRewriter1_14(this).registerDeclareCommands(ClientboundPackets1_14.DECLARE_COMMANDS);
    blockItemPackets = new BlockItemPackets1_14(this);
    blockItemPackets.register();
    entityRewriter.register();
    new PlayerPackets1_14(this).register();
    new SoundPackets1_14(this).register();
    new StatisticsRewriter(this).register(ClientboundPackets1_14.STATISTICS);
    cancelClientbound(ClientboundPackets1_14.UPDATE_VIEW_POSITION);
    cancelClientbound(ClientboundPackets1_14.UPDATE_VIEW_DISTANCE);
    cancelClientbound(ClientboundPackets1_14.ACKNOWLEDGE_PLAYER_DIGGING);
    registerClientbound(ClientboundPackets1_14.TAGS, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int blockTagsSize = wrapper.passthrough(Type.VAR_INT);
                    for (int i = 0; i < blockTagsSize; i++) {
                        wrapper.passthrough(Type.STRING);
                        int[] blockIds = wrapper.passthrough(Type.VAR_INT_ARRAY_PRIMITIVE);
                        for (int j = 0; j < blockIds.length; j++) {
                            int id = blockIds[j];
                            // Ignore new blocktags
                            int blockId = getMappingData().getNewBlockId(id);
                            blockIds[j] = blockId;
                        }
                    }
                    int itemTagsSize = wrapper.passthrough(Type.VAR_INT);
                    for (int i = 0; i < itemTagsSize; i++) {
                        wrapper.passthrough(Type.STRING);
                        int[] itemIds = wrapper.passthrough(Type.VAR_INT_ARRAY_PRIMITIVE);
                        for (int j = 0; j < itemIds.length; j++) {
                            int itemId = itemIds[j];
                            // Ignore new itemtags
                            int oldId = getMappingData().getItemMappings().get(itemId);
                            itemIds[j] = oldId;
                        }
                    }
                    // fluid tags
                    int fluidTagsSize = wrapper.passthrough(Type.VAR_INT);
                    for (int i = 0; i < fluidTagsSize; i++) {
                        wrapper.passthrough(Type.STRING);
                        wrapper.passthrough(Type.VAR_INT_ARRAY_PRIMITIVE);
                    }
                    // Eat entity tags
                    int entityTagsSize = wrapper.read(Type.VAR_INT);
                    for (int i = 0; i < entityTagsSize; i++) {
                        wrapper.read(Type.STRING);
                        wrapper.read(Type.VAR_INT_ARRAY_PRIMITIVE);
                    }
                }
            });
        }
    });
    registerClientbound(ClientboundPackets1_14.UPDATE_LIGHT, null, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int x = wrapper.read(Type.VAR_INT);
                    int z = wrapper.read(Type.VAR_INT);
                    int skyLightMask = wrapper.read(Type.VAR_INT);
                    int blockLightMask = wrapper.read(Type.VAR_INT);
                    int emptySkyLightMask = wrapper.read(Type.VAR_INT);
                    int emptyBlockLightMask = wrapper.read(Type.VAR_INT);
                    byte[][] skyLight = new byte[16][];
                    // we don't need void and +256 light
                    if (isSet(skyLightMask, 0)) {
                        wrapper.read(Type.BYTE_ARRAY_PRIMITIVE);
                    }
                    for (int i = 0; i < 16; i++) {
                        if (isSet(skyLightMask, i + 1)) {
                            skyLight[i] = wrapper.read(Type.BYTE_ARRAY_PRIMITIVE);
                        } else if (isSet(emptySkyLightMask, i + 1)) {
                            skyLight[i] = ChunkLightStorage.EMPTY_LIGHT;
                        }
                    }
                    if (isSet(skyLightMask, 17)) {
                        wrapper.read(Type.BYTE_ARRAY_PRIMITIVE);
                    }
                    byte[][] blockLight = new byte[16][];
                    if (isSet(blockLightMask, 0)) {
                        wrapper.read(Type.BYTE_ARRAY_PRIMITIVE);
                    }
                    for (int i = 0; i < 16; i++) {
                        if (isSet(blockLightMask, i + 1)) {
                            blockLight[i] = wrapper.read(Type.BYTE_ARRAY_PRIMITIVE);
                        } else if (isSet(emptyBlockLightMask, i + 1)) {
                            blockLight[i] = ChunkLightStorage.EMPTY_LIGHT;
                        }
                    }
                    if (isSet(blockLightMask, 17)) {
                        wrapper.read(Type.BYTE_ARRAY_PRIMITIVE);
                    }
                    // TODO Soft memory leak: Don't store light if chunk is already loaded
                    wrapper.user().get(ChunkLightStorage.class).setStoredLight(skyLight, blockLight, x, z);
                    wrapper.cancel();
                }

                private boolean isSet(int mask, int i) {
                    return (mask & (1 << i)) != 0;
                }
            });
        }
    });
}
Also used : BlockItemPackets1_14(com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.packets.BlockItemPackets1_14) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) CommandRewriter1_14(com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.data.CommandRewriter1_14) PlayerPackets1_14(com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.packets.PlayerPackets1_14) StatisticsRewriter(com.viaversion.viaversion.rewriter.StatisticsRewriter) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) ChunkLightStorage(com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.storage.ChunkLightStorage) SoundPackets1_14(com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.packets.SoundPackets1_14)

Aggregations

PacketRemapper (com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)13 StatisticsRewriter (com.viaversion.viaversion.rewriter.StatisticsRewriter)13 TagRewriter (com.viaversion.viaversion.rewriter.TagRewriter)11 UserConnection (com.viaversion.viaversion.api.connection.UserConnection)10 RegistryType (com.viaversion.viaversion.api.minecraft.RegistryType)10 EntityRewriter (com.viaversion.viaversion.api.rewriter.EntityRewriter)10 Type (com.viaversion.viaversion.api.type.Type)10 EntityTrackerBase (com.viaversion.viaversion.data.entity.EntityTrackerBase)10 PacketWrapper (com.viaversion.viaversion.api.protocol.packet.PacketWrapper)7 ItemRewriter (com.viaversion.viaversion.api.rewriter.ItemRewriter)7 SoundRewriter (com.viaversion.viaversion.rewriter.SoundRewriter)6 BackwardsProtocol (com.viaversion.viabackwards.api.BackwardsProtocol)5 TranslatableRewriter (com.viaversion.viabackwards.api.rewriters.TranslatableRewriter)5 BackwardsMappings (com.viaversion.viabackwards.api.data.BackwardsMappings)4 SoundRewriter (com.viaversion.viabackwards.api.rewriters.SoundRewriter)4 AbstractProtocol (com.viaversion.viaversion.api.protocol.AbstractProtocol)4 PacketHandler (com.viaversion.viaversion.api.protocol.remapper.PacketHandler)4 ServerboundPackets1_14 (com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ServerboundPackets1_14)4 ClientboundPackets1_16_2 (com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.ClientboundPackets1_16_2)3 ServerboundPackets1_16_2 (com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.ServerboundPackets1_16_2)3