Skip to content

Commit 60ba4bd

Browse files
authored
Merge pull request #2700 from breadbyte/master
2 parents ac9a70c + a72a8cf commit 60ba4bd

File tree

3 files changed

+48
-21
lines changed

3 files changed

+48
-21
lines changed

MinecraftClient/ChatBots/AutoRelog.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,10 @@ private void LaunchDelayedReconnection(string? msg)
137137
{
138138
double delay = random.NextDouble() * (Config.Delay.max - Config.Delay.min) + Config.Delay.min;
139139
LogDebugToConsole(string.Format(string.IsNullOrEmpty(msg) ? Translations.bot_autoRelog_reconnect_always : Translations.bot_autoRelog_reconnect, msg));
140-
LogToConsole(string.Format(Translations.bot_autoRelog_wait, delay));
141-
System.Threading.Thread.Sleep((int)Math.Floor(delay * 1000));
142-
ReconnectToTheServer();
140+
141+
// TODO: Change this translation string to add the retries left text
142+
LogToConsole(string.Format(Translations.bot_autoRelog_wait, delay) + $" ({Config.Retries - Configs._BotRecoAttempts} retries left)");
143+
ReconnectToTheServer(Config.Retries - Configs._BotRecoAttempts, (int)Math.Floor(delay), true);
143144
}
144145

145146
public static bool OnDisconnectStatic(DisconnectReason reason, string message)

MinecraftClient/McClient.cs

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ public enum MovementType { Sneak, Walk, Sprint }
153153

154154
private static IMinecraftComHandler? instance;
155155
public static IMinecraftComHandler? Instance => instance;
156+
156157
/// <summary>
157158
/// Starts the main chat client, wich will login to the server using the MinecraftCom class.
158159
/// </summary>
@@ -245,28 +246,48 @@ public McClient(SessionToken session, PlayerKeyPair? playerKeyPair, string serve
245246

246247
return;
247248

248-
Retry:
249+
Retry:
249250
if (timeoutdetector != null)
250251
{
251252
timeoutdetector.Item2.Cancel();
252253
timeoutdetector = null;
253254
}
254-
if (ReconnectionAttemptsLeft > 0)
255+
256+
if (!Config.ChatBot.AutoRelog.Enabled)
255257
{
256-
Log.Info(string.Format(Translations.mcc_reconnect, ReconnectionAttemptsLeft));
257-
Thread.Sleep(5000);
258-
ReconnectionAttemptsLeft--;
259-
Program.Restart();
260-
}
261-
else if (InternalConfig.InteractiveMode)
258+
if (ReconnectionAttemptsLeft > 0)
259+
{
260+
Log.Info(string.Format(Translations.mcc_reconnect, ReconnectionAttemptsLeft));
261+
Thread.Sleep(5000);
262+
ReconnectionAttemptsLeft--;
263+
Program.Restart();
264+
}
265+
else if (InternalConfig.InteractiveMode)
266+
{
267+
ConsoleInteractive.ConsoleReader.StopReadThread();
268+
ConsoleInteractive.ConsoleReader.MessageReceived -= ConsoleReaderOnMessageReceived;
269+
ConsoleInteractive.ConsoleReader.OnInputChange -= ConsoleIO.AutocompleteHandler;
270+
Program.HandleFailure();
271+
}
272+
273+
throw new Exception("Initialization failed.");
274+
}
275+
else
262276
{
263-
ConsoleInteractive.ConsoleReader.StopReadThread();
264-
ConsoleInteractive.ConsoleReader.MessageReceived -= ConsoleReaderOnMessageReceived;
265-
ConsoleInteractive.ConsoleReader.OnInputChange -= ConsoleIO.AutocompleteHandler;
266-
Program.HandleFailure();
267-
}
277+
// The AutoRelog ChatBot will handle reconnection at this point.
278+
// This is important, or else we'll have multiple instances of the client running at the same time.
268279

269-
throw new Exception("Initialization failed.");
280+
if (ReconnectionAttemptsLeft == 0)
281+
{
282+
if (InternalConfig.InteractiveMode)
283+
{
284+
ConsoleInteractive.ConsoleReader.StopReadThread();
285+
ConsoleInteractive.ConsoleReader.MessageReceived -= ConsoleReaderOnMessageReceived;
286+
ConsoleInteractive.ConsoleReader.OnInputChange -= ConsoleIO.AutocompleteHandler;
287+
Program.HandleFailure();
288+
}
289+
}
290+
}
270291
}
271292

272293
/// <summary>
@@ -572,7 +593,7 @@ public void OnConnectionLost(ChatBot.DisconnectReason reason, string message)
572593
ConsoleInteractive.ConsoleReader.StopReadThread();
573594
ConsoleInteractive.ConsoleReader.MessageReceived -= ConsoleReaderOnMessageReceived;
574595
ConsoleInteractive.ConsoleReader.OnInputChange -= ConsoleIO.AutocompleteHandler;
575-
Program.HandleFailure();
596+
Program.HandleFailure(message, false, reason);
576597
}
577598
}
578599

MinecraftClient/Program.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -398,15 +398,20 @@ private static void RequestPassword()
398398
/// </summary>
399399
private static void InitializeClient()
400400
{
401-
InternalConfig.MinecraftVersion = Config.Main.Advanced.MinecraftVersion;
401+
// Ensure that we use the provided Minecraft version if we can't connect automatically.
402+
//
403+
// useMcVersionOnce is set to true on HandleFailure()
404+
// whenever we are unable to connect to the server and the user provides a version number.
405+
if (!useMcVersionOnce)
406+
InternalConfig.MinecraftVersion = Config.Main.Advanced.MinecraftVersion;
402407

403408
SessionToken session = new();
404409
PlayerKeyPair? playerKeyPair = null;
405410

406411
ProtocolHandler.LoginResult result = ProtocolHandler.LoginResult.LoginRequired;
407412

408413
string loginLower = ToLowerIfNeed(InternalConfig.Account.Login);
409-
if (InternalConfig.Account.Password == "-")
414+
if (InternalConfig.Account.Password == "-" || InternalConfig.Account.Password == string.Empty)
410415
{
411416
ConsoleIO.WriteLineFormatted("§8" + Translations.mcc_offline, acceptnewlines: true);
412417
result = ProtocolHandler.LoginResult.Success;
@@ -749,7 +754,7 @@ public static void HandleFailure(string? errorMessage = null, bool versionError
749754
if (InternalConfig.MinecraftVersion != "")
750755
{
751756
useMcVersionOnce = true;
752-
Restart();
757+
Restart(0, true);
753758
return;
754759
}
755760
}

0 commit comments

Comments
 (0)