Search in sources :

Example 1 with TranslationMappings

use of com.viaversion.viaversion.protocols.protocol1_16to1_15_2.data.TranslationMappings 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)

Aggregations

Joiner (com.google.common.base.Joiner)1 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 Via (com.viaversion.viaversion.api.Via)1 UserConnection (com.viaversion.viaversion.api.connection.UserConnection)1 RegistryType (com.viaversion.viaversion.api.minecraft.RegistryType)1 Entity1_16Types (com.viaversion.viaversion.api.minecraft.entities.Entity1_16Types)1 AbstractProtocol (com.viaversion.viaversion.api.protocol.AbstractProtocol)1 State (com.viaversion.viaversion.api.protocol.packet.State)1 PacketRemapper (com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)1 EntityRewriter (com.viaversion.viaversion.api.rewriter.EntityRewriter)1 ItemRewriter (com.viaversion.viaversion.api.rewriter.ItemRewriter)1 Type (com.viaversion.viaversion.api.type.Type)1 ParticleType (com.viaversion.viaversion.api.type.types.minecraft.ParticleType)1 Types1_16 (com.viaversion.viaversion.api.type.types.version.Types1_16)1 EntityTrackerBase (com.viaversion.viaversion.data.entity.EntityTrackerBase)1 ServerboundPackets1_14 (com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ServerboundPackets1_14)1 ClientboundPackets1_15 (com.viaversion.viaversion.protocols.protocol1_15to1_14_4.ClientboundPackets1_15)1 MappingData (com.viaversion.viaversion.protocols.protocol1_16to1_15_2.data.MappingData)1