fix: resolve AutoRelog reconnect errors (#3036)

- Reset _BotRecoAttempts on successful game join so retry counter
  does not carry stale state across sessions
- Display "unlimited" instead of near-int.MaxValue retry count when
  Retries is set to -1 (infinite)
- Guard SendText with CanSendMessage check to prevent
  NullReferenceException when bots call send after disconnect
- Catch SocketException/IOException in Protocol18 Updater thread so
  a closed socket triggers OnConnectionLost gracefully instead of an
  unhandled exception

Made-with: Cursor
This commit is contained in:
BruceChen 2026-04-03 01:26:59 +08:00
parent 2441c67178
commit 2003786608
5 changed files with 65 additions and 5 deletions

View file

@ -1,4 +1,4 @@
using System;
using System;
using MinecraftClient.Scripting;
using Tomlet.Attributes;
@ -95,6 +95,11 @@ namespace MinecraftClient.ChatBots
_Initialize();
}
public override void AfterGameJoined()
{
Configs._BotRecoAttempts = 0;
}
private void _Initialize()
{
McClient.ReconnectionAttemptsLeft = Config.Retries;
@ -144,10 +149,17 @@ namespace MinecraftClient.ChatBots
{
double delay = random.NextDouble() * (Config.Delay.max - Config.Delay.min) + Config.Delay.min;
LogDebugToConsole(string.Format(string.IsNullOrEmpty(msg) ? Translations.bot_autoRelog_reconnect_always : Translations.bot_autoRelog_reconnect, msg));
// TODO: Change this translation string to add the retries left text
LogToConsole(string.Format(Translations.bot_autoRelog_wait, delay) + $" ({Config.Retries - Configs._BotRecoAttempts} retries left)");
ReconnectToTheServer(Config.Retries - Configs._BotRecoAttempts, (int)Math.Floor(delay), true);
int retriesLeft = Config.Retries - Configs._BotRecoAttempts;
if (retriesLeft < 0)
retriesLeft = 0;
string retriesDisplay = Config.Retries == int.MaxValue
? Translations.bot_autoRelog_retries_unlimited
: retriesLeft.ToString();
LogToConsole(string.Format(Translations.bot_autoRelog_wait_with_retries, delay, retriesDisplay));
ReconnectToTheServer(retriesLeft, (int)Math.Floor(delay), true);
}
public static bool OnDisconnectStatic(DisconnectReason reason, string message)

View file

@ -1579,6 +1579,12 @@ namespace MinecraftClient
if (String.IsNullOrEmpty(text))
return;
if (!CanSendMessage)
{
Log.Warn(Translations.mcc_send_text_not_connected);
return;
}
int maxLength = handler.GetMaxChatMessageLength();
lock (chatQueue)

View file

@ -324,6 +324,12 @@ namespace MinecraftClient.Protocol.Handlers
catch (NullReferenceException)
{
}
catch (SocketException)
{
}
catch (System.IO.IOException)
{
}
if (cancelToken.IsCancellationRequested)
return;

View file

@ -897,6 +897,24 @@ namespace MinecraftClient {
}
}
/// <summary>
/// Looks up a localized string similar to Waiting {0:0.000} seconds before reconnecting... ({1} retries left).
/// </summary>
internal static string bot_autoRelog_wait_with_retries {
get {
return ResourceManager.GetString("bot.autoRelog.wait_with_retries", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to unlimited.
/// </summary>
internal static string bot_autoRelog_retries_unlimited {
get {
return ResourceManager.GetString("bot.autoRelog.retries_unlimited", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to File not found: &apos;{0}&apos;.
/// </summary>
@ -6158,6 +6176,15 @@ namespace MinecraftClient {
}
}
/// <summary>
/// Looks up a localized string similar to Cannot send text: not connected to a server..
/// </summary>
internal static string mcc_send_text_not_connected {
get {
return ResourceManager.GetString("mcc.send_text_not_connected", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Waiting {0} seconds before restarting....
/// </summary>

View file

@ -394,6 +394,12 @@
<data name="bot.autoRelog.wait" xml:space="preserve">
<value>Waiting {0:0.000} seconds before reconnecting...</value>
</data>
<data name="bot.autoRelog.wait_with_retries" xml:space="preserve">
<value>Waiting {0:0.000} seconds before reconnecting... ({1} retries left)</value>
</data>
<data name="bot.autoRelog.retries_unlimited" xml:space="preserve">
<value>unlimited</value>
</data>
<data name="bot.autoRespond.file_not_found" xml:space="preserve">
<value>File not found: '{0}'</value>
</data>
@ -2058,6 +2064,9 @@ Type '{0}quit' to leave the server.</value>
<data name="mcc.restart" xml:space="preserve">
<value>Restarting Minecraft Console Client...</value>
</data>
<data name="mcc.send_text_not_connected" xml:space="preserve">
<value>Cannot send text: not connected to a server.</value>
</data>
<data name="mcc.restart_delay" xml:space="preserve">
<value>Waiting {0} seconds before restarting...</value>
</data>