|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 GeyserMC. http://geysermc.org |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"), to deal |
| 6 | + * in the Software without restriction, including without limitation the rights |
| 7 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | + * copies of the Software, and to permit persons to whom the Software is |
| 9 | + * furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in |
| 12 | + * all copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | + * THE SOFTWARE. |
| 21 | + * |
| 22 | + * @author GeyserMC |
| 23 | + * @link https://github.com/GeyserMC/Geyser |
| 24 | + */ |
| 25 | + |
| 26 | +package org.geysermc.geyser.network.codec; |
| 27 | + |
| 28 | +import io.netty.buffer.ByteBuf; |
| 29 | +import org.cloudburstmc.protocol.bedrock.codec.BedrockCodecHelper; |
| 30 | +import org.cloudburstmc.protocol.bedrock.codec.v897.serializer.TextSerializer_v897; |
| 31 | +import org.cloudburstmc.protocol.bedrock.packet.TextPacket; |
| 32 | +import org.cloudburstmc.protocol.common.util.TextConverter; |
| 33 | + |
| 34 | +// Sending empty messages kicks clients with 1.21.130... |
| 35 | +public class CustomTextPacketSerializer extends TextSerializer_v897 { |
| 36 | + |
| 37 | + public static final CustomTextPacketSerializer INSTANCE = new CustomTextPacketSerializer(); |
| 38 | + |
| 39 | + @Override |
| 40 | + public void serialize(ByteBuf buffer, BedrockCodecHelper helper, TextPacket packet) { |
| 41 | + TextPacket.Type type = packet.getType(); |
| 42 | + TextConverter converter = helper.getTextConverter(); |
| 43 | + CharSequence message = packet.getMessage(CharSequence.class); |
| 44 | + Boolean needsTranslation = converter.needsTranslation(message); |
| 45 | + |
| 46 | + buffer.writeBoolean(needsTranslation != null ? needsTranslation : packet.isNeedsTranslation()); |
| 47 | + String msg; |
| 48 | + |
| 49 | + switch (type) { |
| 50 | + case RAW: |
| 51 | + case TIP: |
| 52 | + case SYSTEM: |
| 53 | + buffer.writeByte(0); // MessageOnly |
| 54 | + helper.writeString(buffer, "raw"); |
| 55 | + helper.writeString(buffer, "tip"); |
| 56 | + helper.writeString(buffer, "systemMessage"); |
| 57 | + helper.writeString(buffer, "textObjectWhisper"); |
| 58 | + helper.writeString(buffer, "textObjectAnnouncement"); |
| 59 | + helper.writeString(buffer, "textObject"); |
| 60 | + buffer.writeByte(type.ordinal()); |
| 61 | + msg = converter.serialize(message); |
| 62 | + if (msg.isEmpty()) msg = " "; |
| 63 | + helper.writeString(buffer, msg); |
| 64 | + break; |
| 65 | + case JSON: |
| 66 | + case WHISPER_JSON: |
| 67 | + case ANNOUNCEMENT_JSON: |
| 68 | + buffer.writeByte(0); // MessageOnly |
| 69 | + helper.writeString(buffer, "raw"); |
| 70 | + helper.writeString(buffer, "tip"); |
| 71 | + helper.writeString(buffer, "systemMessage"); |
| 72 | + helper.writeString(buffer, "textObjectWhisper"); |
| 73 | + helper.writeString(buffer, "textObjectAnnouncement"); |
| 74 | + helper.writeString(buffer, "textObject"); |
| 75 | + buffer.writeByte(type.ordinal()); |
| 76 | + msg = converter.serializeJson(message); |
| 77 | + if (msg.isEmpty()) msg = " "; |
| 78 | + helper.writeString(buffer, msg); |
| 79 | + break; |
| 80 | + case CHAT: |
| 81 | + case WHISPER: |
| 82 | + case ANNOUNCEMENT: |
| 83 | + buffer.writeByte(1); // AuthorAndMessage |
| 84 | + helper.writeString(buffer, "chat"); |
| 85 | + helper.writeString(buffer, "whisper"); |
| 86 | + helper.writeString(buffer, "announcement"); |
| 87 | + buffer.writeByte(type.ordinal()); |
| 88 | + helper.writeString(buffer, packet.getSourceName()); |
| 89 | + msg = converter.serialize(message); |
| 90 | + if (msg.isEmpty()) msg = " "; |
| 91 | + helper.writeString(buffer, msg); |
| 92 | + break; |
| 93 | + case TRANSLATION: |
| 94 | + case POPUP: |
| 95 | + case JUKEBOX_POPUP: |
| 96 | + buffer.writeByte(2); // MessageAndParams |
| 97 | + helper.writeString(buffer, "translate"); |
| 98 | + helper.writeString(buffer, "popup"); |
| 99 | + helper.writeString(buffer, "jukeboxPopup"); |
| 100 | + buffer.writeByte(type.ordinal()); |
| 101 | + String text = converter.serializeWithArguments(message, packet.getParameters()); |
| 102 | + if (text.isEmpty()) text = " "; |
| 103 | + helper.writeString(buffer, text); |
| 104 | + helper.writeArray(buffer, packet.getParameters(), helper::writeString); |
| 105 | + break; |
| 106 | + default: |
| 107 | + throw new UnsupportedOperationException("Unsupported TextType " + type); |
| 108 | + } |
| 109 | + |
| 110 | + helper.writeString(buffer, packet.getXuid()); |
| 111 | + helper.writeString(buffer, packet.getPlatformChatId()); |
| 112 | + String filtered = converter.serialize(packet.getFilteredMessage(CharSequence.class)); |
| 113 | + helper.writeOptional(buffer, (s -> !s.isEmpty()), filtered, (buf, codecHelper, s) -> codecHelper.writeString(buf, s)); |
| 114 | + } |
| 115 | +} |
0 commit comments