Fix AutoRelog running twice on disconnection

Bug introduced in commit aaf1e8311b
Implement User-Initiated logout reason to ignore the second logout event
See #817
This commit is contained in:
ORelio 2019-12-08 22:24:20 +01:00
parent 94e96b11dd
commit 52d98538b3
3 changed files with 35 additions and 24 deletions

View file

@ -27,7 +27,7 @@ namespace MinecraftClient
/// </summary> /// </summary>
public abstract class ChatBot public abstract class ChatBot
{ {
public enum DisconnectReason { InGameKick, LoginRejected, ConnectionLost }; public enum DisconnectReason { InGameKick, LoginRejected, ConnectionLost, UserLogout };
//Handler will be automatically set on bot loading, don't worry about this //Handler will be automatically set on bot loading, don't worry about this
public void SetHandler(McTcpClient handler) { this._handler = handler; } public void SetHandler(McTcpClient handler) { this._handler = handler; }

View file

@ -58,29 +58,37 @@ namespace MinecraftClient.ChatBots
public override bool OnDisconnect(DisconnectReason reason, string message) public override bool OnDisconnect(DisconnectReason reason, string message)
{ {
message = GetVerbatim(message); if (reason == DisconnectReason.UserLogout)
string comp = message.ToLower();
if (Settings.DebugMessages)
LogToConsole("Got disconnected with message: " + message);
foreach (string msg in dictionary)
{ {
if (comp.Contains(msg)) if (Settings.DebugMessages)
{ LogToConsole("Ignoring disconnection initiated by User or MCC bot");
if (Settings.DebugMessages)
LogToConsole("Message contains '" + msg + "'. Reconnecting.");
LogToConsole("Waiting " + delay + " seconds before reconnecting...");
System.Threading.Thread.Sleep(delay * 1000);
McTcpClient.ReconnectionAttemptsLeft = attempts;
ReconnectToTheServer();
return true;
}
} }
else
{
message = GetVerbatim(message);
string comp = message.ToLower();
if (Settings.DebugMessages) if (Settings.DebugMessages)
LogToConsole("Message not containing any defined keywords. Ignoring."); LogToConsole("Got disconnected with message: " + message);
foreach (string msg in dictionary)
{
if (comp.Contains(msg))
{
if (Settings.DebugMessages)
LogToConsole("Message contains '" + msg + "'. Reconnecting.");
LogToConsole("Waiting " + delay + " seconds before reconnecting...");
System.Threading.Thread.Sleep(delay * 1000);
McTcpClient.ReconnectionAttemptsLeft = attempts;
ReconnectToTheServer();
return true;
}
}
if (Settings.DebugMessages)
LogToConsole("Message not containing any defined keywords. Ignoring.");
}
return false; return false;
} }

View file

@ -349,12 +349,12 @@ namespace MinecraftClient
} }
/// <summary> /// <summary>
/// Disconnect the client from the server /// Disconnect the client from the server (initiated from MCC)
/// </summary> /// </summary>
public void Disconnect() public void Disconnect()
{ {
foreach (ChatBot bot in bots.ToArray()) foreach (ChatBot bot in bots.ToArray())
bot.OnDisconnect(ChatBot.DisconnectReason.ConnectionLost, "Disconnected"); bot.OnDisconnect(ChatBot.DisconnectReason.UserLogout, "");
botsOnHold.Clear(); botsOnHold.Clear();
botsOnHold.AddRange(bots); botsOnHold.AddRange(bots);
@ -721,7 +721,7 @@ namespace MinecraftClient
} }
/// <summary> /// <summary>
/// When connection has been lost /// When connection has been lost, login was denied or played was kicked from the server
/// </summary> /// </summary>
public void OnConnectionLost(ChatBot.DisconnectReason reason, string message) public void OnConnectionLost(ChatBot.DisconnectReason reason, string message)
{ {
@ -751,6 +751,9 @@ namespace MinecraftClient
ConsoleIO.WriteLine("Login failed :"); ConsoleIO.WriteLine("Login failed :");
ConsoleIO.WriteLineFormatted(message); ConsoleIO.WriteLineFormatted(message);
break; break;
case ChatBot.DisconnectReason.UserLogout:
throw new InvalidOperationException("User-initiated logout should be done by calling Disconnect()");
} }
foreach (ChatBot bot in bots.ToArray()) foreach (ChatBot bot in bots.ToArray())