Skip to content

Commit

Permalink
Make AutoRelog more reliable
Browse files Browse the repository at this point in the history
Makes AutoRelog more hands on with the  relogging rather than have the general relogging handler handle it.

Fallback to the general handler only when the AutoRelog module is disabled.
  • Loading branch information
breadbyte committed Mar 7, 2024
1 parent c78245c commit a72a8cf
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 18 deletions.
7 changes: 4 additions & 3 deletions MinecraftClient/ChatBots/AutoRelog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,10 @@ private void LaunchDelayedReconnection(string? msg)
{
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));
LogToConsole(string.Format(Translations.bot_autoRelog_wait, delay));
System.Threading.Thread.Sleep((int)Math.Floor(delay * 1000));
ReconnectToTheServer();

// 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);
}

public static bool OnDisconnectStatic(DisconnectReason reason, string message)
Expand Down
51 changes: 36 additions & 15 deletions MinecraftClient/McClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ public enum MovementType { Sneak, Walk, Sprint }

private static IMinecraftComHandler? instance;
public static IMinecraftComHandler? Instance => instance;

/// <summary>
/// Starts the main chat client, wich will login to the server using the MinecraftCom class.
/// </summary>
Expand Down Expand Up @@ -245,28 +246,48 @@ public McClient(SessionToken session, PlayerKeyPair? playerKeyPair, string serve

return;

Retry:
Retry:
if (timeoutdetector != null)
{
timeoutdetector.Item2.Cancel();
timeoutdetector = null;
}
if (ReconnectionAttemptsLeft > 0)

if (!Config.ChatBot.AutoRelog.Enabled)
{
Log.Info(string.Format(Translations.mcc_reconnect, ReconnectionAttemptsLeft));
Thread.Sleep(5000);
ReconnectionAttemptsLeft--;
Program.Restart();
}
else if (InternalConfig.InteractiveMode)
if (ReconnectionAttemptsLeft > 0)
{
Log.Info(string.Format(Translations.mcc_reconnect, ReconnectionAttemptsLeft));
Thread.Sleep(5000);
ReconnectionAttemptsLeft--;
Program.Restart();
}
else if (InternalConfig.InteractiveMode)
{
ConsoleInteractive.ConsoleReader.StopReadThread();
ConsoleInteractive.ConsoleReader.MessageReceived -= ConsoleReaderOnMessageReceived;
ConsoleInteractive.ConsoleReader.OnInputChange -= ConsoleIO.AutocompleteHandler;
Program.HandleFailure();
}

throw new Exception("Initialization failed.");
}
else
{
ConsoleInteractive.ConsoleReader.StopReadThread();
ConsoleInteractive.ConsoleReader.MessageReceived -= ConsoleReaderOnMessageReceived;
ConsoleInteractive.ConsoleReader.OnInputChange -= ConsoleIO.AutocompleteHandler;
Program.HandleFailure();
}
// The AutoRelog ChatBot will handle reconnection at this point.
// This is important, or else we'll have multiple instances of the client running at the same time.

throw new Exception("Initialization failed.");
if (ReconnectionAttemptsLeft == 0)
{
if (InternalConfig.InteractiveMode)
{
ConsoleInteractive.ConsoleReader.StopReadThread();
ConsoleInteractive.ConsoleReader.MessageReceived -= ConsoleReaderOnMessageReceived;
ConsoleInteractive.ConsoleReader.OnInputChange -= ConsoleIO.AutocompleteHandler;
Program.HandleFailure();
}
}
}
}

/// <summary>
Expand Down Expand Up @@ -572,7 +593,7 @@ public void OnConnectionLost(ChatBot.DisconnectReason reason, string message)
ConsoleInteractive.ConsoleReader.StopReadThread();
ConsoleInteractive.ConsoleReader.MessageReceived -= ConsoleReaderOnMessageReceived;
ConsoleInteractive.ConsoleReader.OnInputChange -= ConsoleIO.AutocompleteHandler;
Program.HandleFailure();
Program.HandleFailure(message, false, reason);
}
}

Expand Down

0 comments on commit a72a8cf

Please sign in to comment.