Merge pull request #2700 from breadbyte/master

This commit is contained in:
breadbyte 2024-03-12 23:43:52 +08:00 committed by GitHub
commit 60ba4bd6c2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 50 additions and 23 deletions

View file

@ -137,9 +137,10 @@ namespace MinecraftClient.ChatBots
{ {
double delay = random.NextDouble() * (Config.Delay.max - Config.Delay.min) + Config.Delay.min; 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)); 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)); // TODO: Change this translation string to add the retries left text
ReconnectToTheServer(); 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) public static bool OnDisconnectStatic(DisconnectReason reason, string message)

View file

@ -153,6 +153,7 @@ namespace MinecraftClient
private static IMinecraftComHandler? instance; private static IMinecraftComHandler? instance;
public static IMinecraftComHandler? Instance => instance; public static IMinecraftComHandler? Instance => instance;
/// <summary> /// <summary>
/// Starts the main chat client, wich will login to the server using the MinecraftCom class. /// Starts the main chat client, wich will login to the server using the MinecraftCom class.
/// </summary> /// </summary>
@ -245,28 +246,48 @@ namespace MinecraftClient
return; return;
Retry: Retry:
if (timeoutdetector != null) if (timeoutdetector != null)
{ {
timeoutdetector.Item2.Cancel(); timeoutdetector.Item2.Cancel();
timeoutdetector = null; timeoutdetector = null;
} }
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."); if (!Config.ChatBot.AutoRelog.Enabled)
{
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
{
// 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.
if (ReconnectionAttemptsLeft == 0)
{
if (InternalConfig.InteractiveMode)
{
ConsoleInteractive.ConsoleReader.StopReadThread();
ConsoleInteractive.ConsoleReader.MessageReceived -= ConsoleReaderOnMessageReceived;
ConsoleInteractive.ConsoleReader.OnInputChange -= ConsoleIO.AutocompleteHandler;
Program.HandleFailure();
}
}
}
} }
/// <summary> /// <summary>
@ -572,7 +593,7 @@ namespace MinecraftClient
ConsoleInteractive.ConsoleReader.StopReadThread(); ConsoleInteractive.ConsoleReader.StopReadThread();
ConsoleInteractive.ConsoleReader.MessageReceived -= ConsoleReaderOnMessageReceived; ConsoleInteractive.ConsoleReader.MessageReceived -= ConsoleReaderOnMessageReceived;
ConsoleInteractive.ConsoleReader.OnInputChange -= ConsoleIO.AutocompleteHandler; ConsoleInteractive.ConsoleReader.OnInputChange -= ConsoleIO.AutocompleteHandler;
Program.HandleFailure(); Program.HandleFailure(message, false, reason);
} }
} }

View file

@ -398,7 +398,12 @@ namespace MinecraftClient
/// </summary> /// </summary>
private static void InitializeClient() private static void InitializeClient()
{ {
InternalConfig.MinecraftVersion = Config.Main.Advanced.MinecraftVersion; // Ensure that we use the provided Minecraft version if we can't connect automatically.
//
// useMcVersionOnce is set to true on HandleFailure()
// whenever we are unable to connect to the server and the user provides a version number.
if (!useMcVersionOnce)
InternalConfig.MinecraftVersion = Config.Main.Advanced.MinecraftVersion;
SessionToken session = new(); SessionToken session = new();
PlayerKeyPair? playerKeyPair = null; PlayerKeyPair? playerKeyPair = null;
@ -406,7 +411,7 @@ namespace MinecraftClient
ProtocolHandler.LoginResult result = ProtocolHandler.LoginResult.LoginRequired; ProtocolHandler.LoginResult result = ProtocolHandler.LoginResult.LoginRequired;
string loginLower = ToLowerIfNeed(InternalConfig.Account.Login); string loginLower = ToLowerIfNeed(InternalConfig.Account.Login);
if (InternalConfig.Account.Password == "-") if (InternalConfig.Account.Password == "-" || InternalConfig.Account.Password == string.Empty)
{ {
ConsoleIO.WriteLineFormatted("§8" + Translations.mcc_offline, acceptnewlines: true); ConsoleIO.WriteLineFormatted("§8" + Translations.mcc_offline, acceptnewlines: true);
result = ProtocolHandler.LoginResult.Success; result = ProtocolHandler.LoginResult.Success;
@ -749,7 +754,7 @@ namespace MinecraftClient
if (InternalConfig.MinecraftVersion != "") if (InternalConfig.MinecraftVersion != "")
{ {
useMcVersionOnce = true; useMcVersionOnce = true;
Restart(); Restart(0, true);
return; return;
} }
} }