|
1 | 1 | package org.cloudburstmc.protocol.bedrock.codec.compat;
|
2 | 2 |
|
3 | 3 | import org.cloudburstmc.protocol.bedrock.codec.BedrockCodec;
|
4 |
| -import org.cloudburstmc.protocol.bedrock.codec.compat.serializer.DisconnectSerializerCompat; |
5 | 4 | import org.cloudburstmc.protocol.bedrock.codec.compat.serializer.LoginSerializerCompat;
|
6 | 5 | import org.cloudburstmc.protocol.bedrock.codec.compat.serializer.PlayStatusSerializerCompat;
|
7 | 6 | import org.cloudburstmc.protocol.bedrock.codec.compat.serializer.RequestNetworkSettingsSerializerCompat;
|
| 7 | +import org.cloudburstmc.protocol.bedrock.codec.v291.serializer.DisconnectSerializer_v291; |
| 8 | +import org.cloudburstmc.protocol.bedrock.codec.v622.Bedrock_v622; |
| 9 | +import org.cloudburstmc.protocol.bedrock.codec.v622.serializer.DisconnectSerializer_v622; |
| 10 | +import org.cloudburstmc.protocol.bedrock.codec.v712.Bedrock_v712; |
| 11 | +import org.cloudburstmc.protocol.bedrock.codec.v712.serializer.DisconnectSerializer_v712; |
8 | 12 | import org.cloudburstmc.protocol.bedrock.data.PacketRecipient;
|
9 | 13 | import org.cloudburstmc.protocol.bedrock.packet.DisconnectPacket;
|
10 | 14 | import org.cloudburstmc.protocol.bedrock.packet.LoginPacket;
|
|
14 | 18 | public class BedrockCompat {
|
15 | 19 | /**
|
16 | 20 | * This is for servers when figuring out the protocol of a client joining.
|
| 21 | + * Certain protocols that are older will need to use {@link #disconnectCompat(int)} before disconnecting, |
| 22 | + * in order for the client to see a proper disconnect message. |
17 | 23 | */
|
18 | 24 | public static BedrockCodec CODEC = BedrockCodec.builder()
|
19 | 25 | .helper(() -> NoopBedrockCodecHelper.INSTANCE)
|
20 | 26 | .registerPacket(LoginPacket::new, LoginSerializerCompat.INSTANCE, 1, PacketRecipient.SERVER)
|
21 | 27 | .registerPacket(PlayStatusPacket::new, PlayStatusSerializerCompat.INSTANCE, 2, PacketRecipient.CLIENT)
|
22 |
| - .registerPacket(DisconnectPacket::new, new DisconnectSerializerCompat(true), 5, PacketRecipient.BOTH) |
| 28 | + .registerPacket(DisconnectPacket::new, DisconnectSerializer_v712.INSTANCE, 5, PacketRecipient.BOTH) |
23 | 29 | .registerPacket(RequestNetworkSettingsPacket::new, RequestNetworkSettingsSerializerCompat.INSTANCE, 193, PacketRecipient.SERVER)
|
24 | 30 | .protocolVersion(0)
|
25 | 31 | .minecraftVersion("0.0.0")
|
26 | 32 | .build();
|
27 | 33 |
|
28 | 34 | /**
|
29 |
| - * This is legacy version of the compat codec which does not use DisconnectFailReason in DisconnectPacket. |
30 |
| - * Use this for servers that do not support Minecraft: Bedrock Edition 1.20.40 and above. |
| 35 | + * A legacy version of the compat codec which does not use DisconnectFailReason in DisconnectPacket. |
| 36 | + * Use this for protocols lower than v622 |
31 | 37 | */
|
32 |
| - public static BedrockCodec CODEC_LEGACY = CODEC.toBuilder() |
33 |
| - .updateSerializer(DisconnectPacket.class, new DisconnectSerializerCompat(false)) |
| 38 | + public static BedrockCodec CODEC_v291 = CODEC.toBuilder() |
| 39 | + .updateSerializer(DisconnectPacket.class, DisconnectSerializer_v291.INSTANCE) |
34 | 40 | .build();
|
| 41 | + |
| 42 | + /** |
| 43 | + * A legacy version of the compat codec which does not use filteredMessage in DisconnectPacket. |
| 44 | + * Use this for protocols equal to or greater than v622, but less than v712 |
| 45 | + */ |
| 46 | + public static BedrockCodec CODEC_v622 = CODEC.toBuilder() |
| 47 | + .updateSerializer(DisconnectPacket.class, DisconnectSerializer_v622.INSTANCE) |
| 48 | + .build(); |
| 49 | + |
| 50 | + /** |
| 51 | + * Certain older protocols have outdated {@link DisconnectPacket} formats. |
| 52 | + * Using the codec provided by this method before disconnecting a client will always |
| 53 | + * ensure they receive a well-formed DisconnectPacket. |
| 54 | + * <p> |
| 55 | + * Note that this compat codec should only be used if disconnecting the client upon receiving |
| 56 | + * its protocol version. Otherwise, use a proper codec that targets the specific protocol. |
| 57 | + * |
| 58 | + * @param protocolVersion the client's protocol version |
| 59 | + * @return a compat codec suitable for disconnecting |
| 60 | + */ |
| 61 | + public static BedrockCodec disconnectCompat(int protocolVersion) { |
| 62 | + if (protocolVersion < Bedrock_v622.CODEC.getProtocolVersion()) { |
| 63 | + return CODEC_v291; |
| 64 | + } else if (protocolVersion < Bedrock_v712.CODEC.getProtocolVersion()) { |
| 65 | + return CODEC_v622; |
| 66 | + } |
| 67 | + return CODEC; // v712 or above |
| 68 | + } |
35 | 69 | }
|
0 commit comments