From e744ff206b929e9b3bf9f9bef35e86cfa1653a9f Mon Sep 17 00:00:00 2001 From: Anon Date: Wed, 10 Jun 2026 17:16:30 +0200 Subject: [PATCH] hypixel book fixes --- MinecraftClient/McClient.cs | 3 +- .../Protocol/Handlers/DataTypes.cs | 27 ++- .../Protocol/Handlers/Protocol18.cs | 196 ++++++++++++++++-- .../1_20_6/WrittenBlookContentComponent.cs | 32 ++- .../StructuredComponentsRegistry12111.cs | 5 +- .../Translations/Translations.Designer.cs | 44 ++++ .../Resources/Translations/Translations.resx | 33 +++ 7 files changed, 302 insertions(+), 38 deletions(-) diff --git a/MinecraftClient/McClient.cs b/MinecraftClient/McClient.cs index a0a0ca4d..0c7665fd 100644 --- a/MinecraftClient/McClient.cs +++ b/MinecraftClient/McClient.cs @@ -3435,7 +3435,8 @@ namespace MinecraftClient if (!String.IsNullOrWhiteSpace(bandString)) handler.SendBrandInfo(bandString.Trim()); - if (Config.MCSettings.Enabled) + // 1.20.2+ expects ClientInformation during configuration; older servers still want it here. + if (Config.MCSettings.Enabled && protocolversion < Protocol18Handler.MC_1_20_2_Version) handler.SendClientSettings( Config.MCSettings.Locale, Config.MCSettings.RenderDistance, diff --git a/MinecraftClient/Protocol/Handlers/DataTypes.cs b/MinecraftClient/Protocol/Handlers/DataTypes.cs index 182944a6..b5bea717 100644 --- a/MinecraftClient/Protocol/Handlers/DataTypes.cs +++ b/MinecraftClient/Protocol/Handlers/DataTypes.cs @@ -1424,17 +1424,24 @@ namespace MinecraftClient.Protocol.Handlers { if (protocolversion >= Protocol18Handler.MC_1_20_4_Version) { - // Read as NBT - var r = ReadNextNbt(cache); - var msg = ChatParser.ParseText(r); - return msg; - } - else - { - // Read as String - var json = ReadNextString(cache); - return ChatParser.ParseText(json); + // Vanilla 1.20.4+ uses NBT here, but Hypixel exposed a JSON-string fallback on the same path. + Queue fallbackCache = new(cache); + try + { + var r = ReadNextNbt(cache); + return ChatParser.ParseText(r); + } + catch (System.IO.InvalidDataException) + { + cache.Clear(); + foreach (var b in fallbackCache) + cache.Enqueue(b); + } } + + // Read as String + var json = ReadNextString(cache); + return ChatParser.ParseText(json); } /// diff --git a/MinecraftClient/Protocol/Handlers/Protocol18.cs b/MinecraftClient/Protocol/Handlers/Protocol18.cs index bfb7faa3..bc77463d 100644 --- a/MinecraftClient/Protocol/Handlers/Protocol18.cs +++ b/MinecraftClient/Protocol/Handlers/Protocol18.cs @@ -288,6 +288,7 @@ namespace MinecraftClient.Protocol.Handlers private void Updater(object? o) { var cancelToken = (CancellationToken)o!; + var exitReason = Translations.debug_packet_loop_reason_queue_completed; if (cancelToken.IsCancellationRequested) return; @@ -322,23 +323,29 @@ namespace MinecraftClient.Protocol.Handlers } catch (ObjectDisposedException) { + exitReason = nameof(ObjectDisposedException); } catch (OperationCanceledException) { + exitReason = nameof(OperationCanceledException); } catch (NullReferenceException) { + exitReason = nameof(NullReferenceException); } catch (SocketException) { + exitReason = nameof(SocketException); } catch (System.IO.IOException) { + exitReason = nameof(System.IO.IOException); } if (cancelToken.IsCancellationRequested) return; + LogNetworkLoopExit(nameof(Updater), exitReason); handler.OnConnectionLost(ChatBot.DisconnectReason.ConnectionLost, ""); } @@ -348,6 +355,7 @@ namespace MinecraftClient.Protocol.Handlers internal void PacketReader(object? o) { var cancelToken = (CancellationToken)o!; + var exitReason = Translations.debug_packet_loop_reason_socket_closed; while (socketWrapper.IsConnected() && !cancelToken.IsCancellationRequested) { try @@ -362,31 +370,43 @@ namespace MinecraftClient.Protocol.Handlers } catch (OperationCanceledException) { + exitReason = nameof(OperationCanceledException); break; } catch (System.IO.IOException) { + exitReason = nameof(System.IO.IOException); break; } catch (SocketException) { + exitReason = nameof(SocketException); break; } catch (NullReferenceException) { + exitReason = nameof(NullReferenceException); break; } catch (System.IO.InvalidDataException) { + exitReason = nameof(System.IO.InvalidDataException); break; } if (cancelToken.IsCancellationRequested) + { + exitReason = Translations.debug_packet_loop_reason_cancelled; break; + } Thread.Sleep(10); } + if (cancelToken.IsCancellationRequested) + exitReason = Translations.debug_packet_loop_reason_cancelled; + + LogNetworkLoopExit(nameof(PacketReader), exitReason); packetQueue.CompleteAdding(); } @@ -399,21 +419,25 @@ namespace MinecraftClient.Protocol.Handlers { var size = dataTypes.ReadNextVarIntRAW(socketWrapper); //Packet size Queue packetData = new(socketWrapper.ReadDataRAW(size)); //Packet contents + var compressed = false; + var sizeUncompressed = 0; //Handle packet decompression if (protocolVersion >= MC_1_8_Version && compression_treshold >= 0) { - var sizeUncompressed = dataTypes.ReadNextVarInt(packetData); + sizeUncompressed = dataTypes.ReadNextVarInt(packetData); if (sizeUncompressed != 0) // != 0 means compressed, let's decompress { var toDecompress = packetData.ToArray(); var uncompressed = ZlibUtils.Decompress(toDecompress, sizeUncompressed); packetData = new Queue(uncompressed); + compressed = true; } } var packetId = dataTypes.ReadNextVarInt(packetData); // Packet ID + LogIncomingPacket(packetId, packetData.Count, size, compressed, sizeUncompressed); if (handler.GetNetworkPacketCaptureEnabled()) handler.OnNetworkPacket(packetId, packetData.ToList(), currentState == CurrentState.Login, true); @@ -476,7 +500,7 @@ namespace MinecraftClient.Protocol.Handlers { if (packetPalette.GetMappingIn().ContainsKey(packetId)) { - currentState = CurrentState.Play; + SetCurrentState(CurrentState.Play); return HandlePlayPackets(packetId, packetData); } @@ -499,7 +523,7 @@ namespace MinecraftClient.Protocol.Handlers return false; case ConfigurationPacketTypesIn.FinishConfiguration: - currentState = CurrentState.Play; + SetCurrentState(CurrentState.Play); SendPacket(ConfigurationPacketTypesOut.FinishConfiguration, new List()); break; @@ -1260,7 +1284,7 @@ namespace MinecraftClient.Protocol.Handlers chunkBatchStartTime = GetNanos(); break; case PacketTypesIn.StartConfiguration: - currentState = CurrentState.Configuration; + SetCurrentState(CurrentState.Configuration); SendAcknowledgeConfiguration(); break; case PacketTypesIn.HideMessage: @@ -4017,7 +4041,7 @@ namespace MinecraftClient.Protocol.Handlers /// packet Data private void SendPacket(PacketTypesOut packet, IEnumerable packetData) { - SendPacket(packetPalette.GetOutgoingIdByType(packet), packetData); + SendPacket(packetPalette.GetOutgoingIdByType(packet), packetData, packet.ToString()); } private void ProcessChunkBlockEntityData(int chunkX, int chunkZ, Queue packetData) @@ -4060,7 +4084,7 @@ namespace MinecraftClient.Protocol.Handlers /// packet Data private void SendPacket(ConfigurationPacketTypesOut packet, IEnumerable packetData) { - SendPacket(packetPalette.GetOutgoingIdByTypeConfiguration(packet), packetData); + SendPacket(packetPalette.GetOutgoingIdByTypeConfiguration(packet), packetData, packet.ToString()); } /// @@ -4068,9 +4092,10 @@ namespace MinecraftClient.Protocol.Handlers /// /// packet ID /// packet Data - private void SendPacket(int packetId, IEnumerable packetData) + private void SendPacket(int packetId, IEnumerable packetData, string? packetType = null) { byte[] payload = packetData as byte[] ?? packetData.ToArray(); + LogOutgoingPacket(packetId, payload.Length, packetType); if (handler.GetNetworkPacketCaptureEnabled()) { @@ -4108,6 +4133,109 @@ namespace MinecraftClient.Protocol.Handlers socketWrapper.SendDataRAW(fullPacket); } + private void SetCurrentState(CurrentState newState) + { + if (currentState == newState) + return; + + var previousState = currentState; + currentState = newState; + + if (!log.DebugEnabled) + return; + + log.Debug(string.Format(Translations.debug_packet_state_change, previousState, newState)); + } + + private void LogIncomingPacket(int packetId, int payloadLength, int frameLength, bool compressed, int uncompressedLength) + { + if (!log.DebugEnabled) + return; + + var packetType = ResolveIncomingPacketType(packetId); + var compressionInfo = compression_treshold < 0 + ? Translations.debug_packet_compression_disabled + : compressed + ? string.Format(Translations.debug_packet_compression_compressed, uncompressedLength) + : Translations.debug_packet_compression_uncompressed; + + log.Debug(string.Format(Translations.debug_packet_incoming, + currentState, + packetId, + packetType, + payloadLength, + frameLength, + compressionInfo)); + } + + private void LogOutgoingPacket(int packetId, int payloadLength, string? packetType) + { + if (!log.DebugEnabled) + return; + + log.Debug(string.Format(Translations.debug_packet_outgoing, + currentState, + packetId, + packetType ?? ResolveOutgoingPacketType(packetId), + payloadLength, + compression_treshold)); + } + + private void LogNetworkLoopExit(string loopName, string reason) + { + if (!log.DebugEnabled) + return; + + log.Debug(string.Format(Translations.debug_packet_loop_exit, + loopName, + reason, + currentState, + socketWrapper.IsConnected())); + } + + private string ResolveIncomingPacketType(int packetId) + { + return currentState switch + { + CurrentState.Login => packetId switch + { + 0x00 => "Disconnect", + 0x01 => "EncryptionRequest", + 0x02 => "LoginSuccess", + 0x03 => "SetCompression", + 0x04 => "LoginPluginRequest", + 0x05 => "CookieRequest", + _ => string.Format(Translations.debug_packet_unknown_type, packetId) + }, + CurrentState.Configuration when packetPalette.GetMappingInConfiguration().TryGetValue(packetId, out var configurationPacket) => + configurationPacket.ToString(), + CurrentState.Play when packetPalette.GetMappingIn().TryGetValue(packetId, out var playPacket) => + playPacket.ToString(), + _ => string.Format(Translations.debug_packet_unknown_type, packetId) + }; + } + + private string ResolveOutgoingPacketType(int packetId) + { + return currentState switch + { + CurrentState.Login => packetId switch + { + 0x00 => "LoginStart", + 0x01 => "EncryptionResponse", + 0x02 => "LoginPluginResponse", + 0x03 => "LoginAcknowledged", + 0x04 => "CookieResponse", + _ => string.Format(Translations.debug_packet_unknown_type, packetId) + }, + CurrentState.Configuration when packetPalette.GetMappingOutConfiguration().TryGetValue(packetId, out var configurationPacket) => + configurationPacket.ToString(), + CurrentState.Play when packetPalette.GetMappingOut().TryGetValue(packetId, out var playPacket) => + playPacket.ToString(), + _ => string.Format(Translations.debug_packet_unknown_type, packetId) + }; + } + /// /// Do the Minecraft login. /// @@ -4131,7 +4259,8 @@ namespace MinecraftClient.Protocol.Handlers dataTypes.GetUShort((ushort)handler.GetServerPort()), // Next State - DataTypes.GetVarInt(nextState)) // 2 is Login, 3 is Transfer + DataTypes.GetVarInt(nextState)), // 2 is Login, 3 is Transfer + "Handshake" ); // 2. Send the Login Start packet @@ -4186,7 +4315,7 @@ namespace MinecraftClient.Protocol.Handlers break; } - SendPacket(0x00, fullLoginPacket); + SendPacket(0x00, fullLoginPacket, "LoginStart"); // 3. Encryption Request - 9. Login Acknowledged while (true) @@ -4223,12 +4352,16 @@ namespace MinecraftClient.Protocol.Handlers case 0x02: { log.Info($"ยง8{Translations.mcc_server_offline}"); - currentState = protocolVersion < MC_1_20_2_Version + SetCurrentState(protocolVersion < MC_1_20_2_Version ? CurrentState.Play - : CurrentState.Configuration; + : CurrentState.Configuration); if (protocolVersion >= MC_1_20_2_Version) - SendPacket(0x03, new List()); + { + // Hypixel and other 1.20.2+ servers stay in configuration until ClientInformation is sent. + SendPacket(0x03, new List(), "LoginAcknowledged"); + SendConfiguredClientSettings(); + } if (!pForge.CompleteForgeHandshake()) { @@ -4371,12 +4504,16 @@ namespace MinecraftClient.Protocol.Handlers if (protocolVersion >= MC_1_20_6_Version && protocolVersion < MC_1_21_2_Version) dataTypes.ReadNextBool(packetData); - currentState = protocolVersion < MC_1_20_2_Version + SetCurrentState(protocolVersion < MC_1_20_2_Version ? CurrentState.Play - : CurrentState.Configuration; + : CurrentState.Configuration); if (protocolVersion >= MC_1_20_2_Version) - SendPacket(0x03, new List()); + { + // Hypixel and other 1.20.2+ servers stay in configuration until ClientInformation is sent. + SendPacket(0x03, new List(), "LoginAcknowledged"); + SendConfiguredClientSettings(); + } handler.OnLoginSuccess(uuidReceived, userName, playerProperty); @@ -5082,7 +5219,13 @@ namespace MinecraftClient.Protocol.Handlers if (protocolVersion >= MC_1_21_2_Version) fields.AddRange(DataTypes.GetVarInt(0)); // 1.21.2+ Particle status: 0=All, 1=Decreased, 2=Minimal - SendPacket(PacketTypesOut.ClientSettings, fields); + + if (currentState == CurrentState.Configuration) + SendPacket(ConfigurationPacketTypesOut.ClientInformation, fields); + else + SendPacket(PacketTypesOut.ClientSettings, fields); + + return true; } catch (SocketException) { @@ -5099,6 +5242,22 @@ namespace MinecraftClient.Protocol.Handlers return false; } + private bool SendConfiguredClientSettings() + { + if (!Config.MCSettings.Enabled) + return true; + + // Keep the configuration-phase send separate so modern servers receive ClientInformation, not play-era ClientSettings. + return SendClientSettings( + Config.MCSettings.Locale, + Config.MCSettings.RenderDistance, + (byte)Config.MCSettings.Difficulty, + (byte)Config.MCSettings.ChatMode, + Config.MCSettings.ChatColors, + Config.MCSettings.Skin.GetByte(), + (byte)Config.MCSettings.MainHand); + } + /// /// Send a location update to the server @@ -5350,7 +5509,8 @@ namespace MinecraftClient.Protocol.Handlers try { SendPacket(0x02, - dataTypes.ConcatBytes(DataTypes.GetVarInt(messageId), dataTypes.GetBool(understood), data)); + dataTypes.ConcatBytes(DataTypes.GetVarInt(messageId), dataTypes.GetBool(understood), data), + "LoginPluginResponse"); return true; } catch (SocketException) @@ -6318,7 +6478,7 @@ namespace MinecraftClient.Protocol.Handlers switch (currentState) { case CurrentState.Login: - SendPacket(0x04, packet); + SendPacket(0x04, packet, "CookieResponse"); break; case CurrentState.Configuration: diff --git a/MinecraftClient/Protocol/Handlers/StructuredComponents/Components/1_20_6/WrittenBlookContentComponent.cs b/MinecraftClient/Protocol/Handlers/StructuredComponents/Components/1_20_6/WrittenBlookContentComponent.cs index 4f42d19f..79e58af8 100644 --- a/MinecraftClient/Protocol/Handlers/StructuredComponents/Components/1_20_6/WrittenBlookContentComponent.cs +++ b/MinecraftClient/Protocol/Handlers/StructuredComponents/Components/1_20_6/WrittenBlookContentComponent.cs @@ -32,17 +32,13 @@ public class WrittenBlookContentComponent(DataTypes dataTypes, ItemPalette itemP for (var i = 0; i < NumberOfPages; i++) { - var rawContentNbt = DataTypes.ReadNextNbt(data); - var rawContent = ChatParser.ParseText(rawContentNbt); + var (rawContent, rawContentNbt) = ReadPageComponent(data); var hasFilteredContent = DataTypes.ReadNextBool(data); Dictionary? filteredContentNbt = null; string? filteredContent = null; if (hasFilteredContent) - { - filteredContentNbt = DataTypes.ReadNextNbt(data); - filteredContent = ChatParser.ParseText(filteredContentNbt); - } + (filteredContent, filteredContentNbt) = ReadPageComponent(data); Pages.Add(new BookPage(rawContent, hasFilteredContent, filteredContent, rawContentNbt, filteredContentNbt)); } @@ -50,6 +46,28 @@ public class WrittenBlookContentComponent(DataTypes dataTypes, ItemPalette itemP Resolved = DataTypes.ReadNextBool(data); } + private (string Content, Dictionary Nbt) ReadPageComponent(Queue data) + { + // Hypixel sent page payloads in the string-shaped form on this structured-book path, + // so keep the parser tolerant while still preserving the raw data for serialization. + Queue fallbackData = new(data); + + try + { + var nbt = DataTypes.ReadNextNbt(data); + return (ChatParser.ParseText(nbt), nbt); + } + catch (System.IO.InvalidDataException) + { + data.Clear(); + foreach (var b in fallbackData) + data.Enqueue(b); + + var json = DataTypes.ReadNextString(data); + return (ChatParser.ParseText(json), new Dictionary { [""] = json }); + } + } + public override Queue Serialize() { var data = new List(); @@ -80,4 +98,4 @@ public class WrittenBlookContentComponent(DataTypes dataTypes, ItemPalette itemP data.AddRange(DataTypes.GetBool(Resolved)); return new Queue(data); } -} \ No newline at end of file +} diff --git a/MinecraftClient/Protocol/Handlers/StructuredComponents/Registries/StructuredComponentsRegistry12111.cs b/MinecraftClient/Protocol/Handlers/StructuredComponents/Registries/StructuredComponentsRegistry12111.cs index 7788f0c2..de00bbe2 100644 --- a/MinecraftClient/Protocol/Handlers/StructuredComponents/Registries/StructuredComponentsRegistry12111.cs +++ b/MinecraftClient/Protocol/Handlers/StructuredComponents/Registries/StructuredComponentsRegistry12111.cs @@ -7,6 +7,7 @@ using MinecraftClient.Protocol.Handlers.StructuredComponents.Components._1_21_5; using MinecraftClient.Protocol.Handlers.StructuredComponents.Components._1_21_8; using MinecraftClient.Protocol.Handlers.StructuredComponents.Components._1_21_9; using MinecraftClient.Protocol.Handlers.StructuredComponents.Components._1_21_11; +using MinecraftClient.Protocol.Handlers.StructuredComponents.Components._26_1; using MinecraftClient.Protocol.Handlers.StructuredComponents.Core; namespace MinecraftClient.Protocol.Handlers.StructuredComponents.Registries; @@ -72,9 +73,9 @@ public class StructuredComponentsRegistry12111 : StructuredComponentRegistry RegisterComponent(53, "minecraft:written_book_content"); RegisterComponent(54, "minecraft:trim"); RegisterComponent(55, "minecraft:debug_stick_state"); - RegisterComponent(56, "minecraft:entity_data"); + RegisterComponent(56, "minecraft:entity_data"); RegisterComponent(57, "minecraft:bucket_entity_data"); - RegisterComponent(58, "minecraft:block_entity_data"); + RegisterComponent(58, "minecraft:block_entity_data"); RegisterComponent(59, "minecraft:instrument"); RegisterComponent(60, "minecraft:provides_trim_material"); RegisterComponent(61, "minecraft:ominous_bottle_amplifier"); diff --git a/MinecraftClient/Resources/Translations/Translations.Designer.cs b/MinecraftClient/Resources/Translations/Translations.Designer.cs index 2330a891..ab775990 100644 --- a/MinecraftClient/Resources/Translations/Translations.Designer.cs +++ b/MinecraftClient/Resources/Translations/Translations.Designer.cs @@ -7757,5 +7757,49 @@ namespace MinecraftClient { get { return ResourceManager.GetString("tui.book.page_shortcut_tip", resourceCulture); } } + internal static string debug_packet_incoming { + get { return ResourceManager.GetString("debug.packet.incoming", resourceCulture); } + } + + internal static string debug_packet_outgoing { + get { return ResourceManager.GetString("debug.packet.outgoing", resourceCulture); } + } + + internal static string debug_packet_state_change { + get { return ResourceManager.GetString("debug.packet.state_change", resourceCulture); } + } + + internal static string debug_packet_unknown_type { + get { return ResourceManager.GetString("debug.packet.unknown_type", resourceCulture); } + } + + internal static string debug_packet_compression_disabled { + get { return ResourceManager.GetString("debug.packet.compression.disabled", resourceCulture); } + } + + internal static string debug_packet_compression_uncompressed { + get { return ResourceManager.GetString("debug.packet.compression.uncompressed", resourceCulture); } + } + + internal static string debug_packet_compression_compressed { + get { return ResourceManager.GetString("debug.packet.compression.compressed", resourceCulture); } + } + + internal static string debug_packet_loop_exit { + get { return ResourceManager.GetString("debug.packet.loop_exit", resourceCulture); } + } + + internal static string debug_packet_loop_reason_queue_completed { + get { return ResourceManager.GetString("debug.packet.loop_reason.queue_completed", resourceCulture); } + } + + internal static string debug_packet_loop_reason_socket_closed { + get { return ResourceManager.GetString("debug.packet.loop_reason.socket_closed", resourceCulture); } + } + + internal static string debug_packet_loop_reason_cancelled { + get { return ResourceManager.GetString("debug.packet.loop_reason.cancelled", resourceCulture); } + } + } } diff --git a/MinecraftClient/Resources/Translations/Translations.resx b/MinecraftClient/Resources/Translations/Translations.resx index cb2dae94..5494dafc 100644 --- a/MinecraftClient/Resources/Translations/Translations.resx +++ b/MinecraftClient/Resources/Translations/Translations.resx @@ -2794,4 +2794,37 @@ see item details. PageUp/PageDown: previous/next page + + [S -> C] state={0} id=0x{1:X2} type={2} payload={3} frame={4} compression={5} + + + [C -> S] state={0} id=0x{1:X2} type={2} payload={3} compression_threshold={4} + + + Protocol state changed: {0} -> {1} + + + Unknown(0x{0:X2}) + + + disabled + + + uncompressed + + + compressed, uncompressed={0} + + + {0} exited: reason={1}, state={2}, socket_connected={3} + + + packet queue completed + + + socket closed + + + cancelled +