[SKIP_BUILD] FIx "/reco" and "/connect" not working properly

This commit is contained in:
BruceChen 2022-11-05 20:27:10 +08:00
parent d56dd4a74a
commit ae23ead4c3
7 changed files with 54 additions and 31 deletions

View file

@ -23,7 +23,7 @@ namespace MinecraftClient.Commands
if (Settings.Config.Main.SetServerIP(new Settings.MainConfigHealper.MainConfig.ServerInfoConfig(args[0]), true)) if (Settings.Config.Main.SetServerIP(new Settings.MainConfigHealper.MainConfig.ServerInfoConfig(args[0]), true))
{ {
Program.Restart(); Program.Restart(keepAccountAndServerSettings: true);
return ""; return "";
} }
else return string.Format(Translations.cmd_connect_invalid_ip, args[0]); else return string.Format(Translations.cmd_connect_invalid_ip, args[0]);

View file

@ -18,7 +18,7 @@ namespace MinecraftClient.Commands
return string.Format(Translations.cmd_connect_unknown, args[0]); return string.Format(Translations.cmd_connect_unknown, args[0]);
} }
} }
Program.Restart(); Program.Restart(keepAccountAndServerSettings: true);
return ""; return "";
} }
} }

View file

@ -710,7 +710,7 @@ namespace MinecraftClient
/// <param name="hard">Marks if bots need to be hard reloaded</param> /// <param name="hard">Marks if bots need to be hard reloaded</param>
public void ReloadSettings() public void ReloadSettings()
{ {
Program.ReloadSettings(); Program.ReloadSettings(true);
ReloadBots(); ReloadBots();
} }

View file

@ -674,9 +674,9 @@ namespace MinecraftClient
/// <summary> /// <summary>
/// Reloads settings /// Reloads settings
/// </summary> /// </summary>
public static void ReloadSettings() public static void ReloadSettings(bool keepAccountAndServerSettings = false)
{ {
if(Settings.LoadFromFile(settingsIniPath).Item1) if(Settings.LoadFromFile(settingsIniPath, keepAccountAndServerSettings).Item1)
ConsoleIO.WriteLine(string.Format(Translations.config_load, settingsIniPath)); ConsoleIO.WriteLine(string.Format(Translations.config_load, settingsIniPath));
} }
@ -692,7 +692,7 @@ namespace MinecraftClient
/// Disconnect the current client from the server and restart it /// Disconnect the current client from the server and restart it
/// </summary> /// </summary>
/// <param name="delaySeconds">Optional delay, in seconds, before restarting</param> /// <param name="delaySeconds">Optional delay, in seconds, before restarting</param>
public static void Restart(int delaySeconds = 0) public static void Restart(int delaySeconds = 0, bool keepAccountAndServerSettings = false)
{ {
ConsoleInteractive.ConsoleReader.StopReadThread(); ConsoleInteractive.ConsoleReader.StopReadThread();
new Thread(new ThreadStart(delegate new Thread(new ThreadStart(delegate
@ -705,7 +705,7 @@ namespace MinecraftClient
Thread.Sleep(delaySeconds * 1000); Thread.Sleep(delaySeconds * 1000);
} }
ConsoleIO.WriteLine(Translations.mcc_restart); ConsoleIO.WriteLine(Translations.mcc_restart);
ReloadSettings(); ReloadSettings(keepAccountAndServerSettings);
InitializeClient(); InitializeClient();
})).Start(); })).Start();
} }

View file

@ -247,12 +247,12 @@ namespace MinecraftClient
/// </summary> /// </summary>
/// <param name="extraAttempts">If connection fails, the client will make X extra attempts</param> /// <param name="extraAttempts">If connection fails, the client will make X extra attempts</param>
/// <param name="delaySeconds">Optional delay, in seconds, before restarting</param> /// <param name="delaySeconds">Optional delay, in seconds, before restarting</param>
new public void ReconnectToTheServer(int extraAttempts = -999999, int delaySeconds = 0) new public void ReconnectToTheServer(int extraAttempts = -999999, int delaySeconds = 0, bool keepAccountAndServerSettings = false)
{ {
if (extraAttempts == -999999) if (extraAttempts == -999999)
base.ReconnectToTheServer(); base.ReconnectToTheServer(delaySeconds: delaySeconds, keepAccountAndServerSettings: keepAccountAndServerSettings);
else else
base.ReconnectToTheServer(extraAttempts); base.ReconnectToTheServer(extraAttempts, delaySeconds, keepAccountAndServerSettings);
} }
/// <summary> /// <summary>
@ -361,7 +361,7 @@ namespace MinecraftClient
{ {
bool result = Settings.Config.Main.Advanced.SetAccount(accountAlias); bool result = Settings.Config.Main.Advanced.SetAccount(accountAlias);
if (result && andReconnect) if (result && andReconnect)
ReconnectToTheServer(); ReconnectToTheServer(keepAccountAndServerSettings: true);
return result; return result;
} }
@ -374,7 +374,7 @@ namespace MinecraftClient
{ {
bool result = Settings.Config.Main.SetServerIP(new MainConfigHealper.MainConfig.ServerInfoConfig(server), true); bool result = Settings.Config.Main.SetServerIP(new MainConfigHealper.MainConfig.ServerInfoConfig(server), true);
if (result && andReconnect) if (result && andReconnect)
ReconnectToTheServer(); ReconnectToTheServer(keepAccountAndServerSettings: true);
return result; return result;
} }

View file

@ -920,7 +920,7 @@ namespace MinecraftClient
/// </summary> /// </summary>
/// <param name="ExtraAttempts">In case of failure, maximum extra attempts before aborting</param> /// <param name="ExtraAttempts">In case of failure, maximum extra attempts before aborting</param>
/// <param name="delaySeconds">Optional delay, in seconds, before restarting</param> /// <param name="delaySeconds">Optional delay, in seconds, before restarting</param>
protected void ReconnectToTheServer(int ExtraAttempts = 3, int delaySeconds = 0) protected void ReconnectToTheServer(int ExtraAttempts = 3, int delaySeconds = 0, bool keepAccountAndServerSettings = false)
{ {
if (Settings.Config.Logging.DebugMessages) if (Settings.Config.Logging.DebugMessages)
{ {
@ -928,7 +928,7 @@ namespace MinecraftClient
ConsoleIO.WriteLogLine(string.Format(Translations.chatbot_reconnect, botName)); ConsoleIO.WriteLogLine(string.Format(Translations.chatbot_reconnect, botName));
} }
McClient.ReconnectionAttemptsLeft = ExtraAttempts; McClient.ReconnectionAttemptsLeft = ExtraAttempts;
Program.Restart(delaySeconds); Program.Restart(delaySeconds, keepAccountAndServerSettings);
} }
/// <summary> /// <summary>

View file

@ -58,6 +58,10 @@ namespace MinecraftClient
public static bool InteractiveMode = true; public static bool InteractiveMode = true;
public static bool GravityEnabled = true; public static bool GravityEnabled = true;
public static bool KeepAccountSettings = false;
public static bool KeepServerSettings = false;
} }
public class GlobalConfig public class GlobalConfig
@ -125,8 +129,13 @@ namespace MinecraftClient
} }
public static Tuple<bool, bool> LoadFromFile(string filepath) public static Tuple<bool, bool> LoadFromFile(string filepath, bool keepAccountAndServerSettings = false)
{ {
bool keepAccountSettings = InternalConfig.KeepAccountSettings;
bool keepServerSettings = InternalConfig.KeepServerSettings;
if (keepAccountAndServerSettings)
InternalConfig.KeepAccountSettings = InternalConfig.KeepServerSettings = true;
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
TomlDocument document; TomlDocument document;
try try
@ -157,6 +166,13 @@ namespace MinecraftClient
ConsoleIO.WriteLine(ex.GetFullMessage()); ConsoleIO.WriteLine(ex.GetFullMessage());
return new(false, false); return new(false, false);
} }
finally
{
if (!keepAccountSettings)
InternalConfig.KeepAccountSettings = false;
if (!keepServerSettings)
InternalConfig.KeepServerSettings = false;
}
return new(true, false); return new(true, false);
} }
@ -257,12 +273,14 @@ namespace MinecraftClient
{ {
case 0: case 0:
InternalConfig.Login = argument; InternalConfig.Login = argument;
InternalConfig.KeepAccountSettings = true;
break; break;
case 1: case 1:
InternalConfig.Password = argument; InternalConfig.Password = argument;
break; break;
case 2: case 2:
Config.Main.SetServerIP(new MainConfig.ServerInfoConfig(argument), true); Config.Main.SetServerIP(new MainConfig.ServerInfoConfig(argument), true);
InternalConfig.KeepServerSettings = true;
break; break;
case 3: case 3:
// SingleCommand = argument; // SingleCommand = argument;
@ -373,6 +391,7 @@ namespace MinecraftClient
General.Account.Login ??= string.Empty; General.Account.Login ??= string.Empty;
General.Account.Password ??= string.Empty; General.Account.Password ??= string.Empty;
if (!InternalConfig.KeepAccountSettings)
InternalConfig.Login = General.Account.Login; InternalConfig.Login = General.Account.Login;
General.Server.Host ??= string.Empty; General.Server.Host ??= string.Empty;
@ -404,6 +423,8 @@ namespace MinecraftClient
ConsoleIO.WriteLogLine("[Settings] " + Translations.config_Main_Advanced_language_invaild); ConsoleIO.WriteLogLine("[Settings] " + Translations.config_Main_Advanced_language_invaild);
} }
if (!InternalConfig.KeepServerSettings)
{
if (!string.IsNullOrWhiteSpace(General.Server.Host)) if (!string.IsNullOrWhiteSpace(General.Server.Host))
{ {
string[] sip = General.Server.Host.Split(new[] { ":", "" }, StringSplitOptions.None); string[] sip = General.Server.Host.Split(new[] { ":", "" }, StringSplitOptions.None);
@ -421,6 +442,7 @@ namespace MinecraftClient
InternalConfig.ServerPort = General.Server.Port.Value; InternalConfig.ServerPort = General.Server.Port.Value;
else else
SetServerIP(General.Server, true); SetServerIP(General.Server, true);
}
for (int i = 0; i < Advanced.BotOwners.Count; ++i) for (int i = 0; i < Advanced.BotOwners.Count; ++i)
Advanced.BotOwners[i] = ToLowerIfNeed(Advanced.BotOwners[i]); Advanced.BotOwners[i] = ToLowerIfNeed(Advanced.BotOwners[i]);
@ -579,7 +601,8 @@ namespace MinecraftClient
{ {
if (AccountList.TryGetValue(accountAlias, out AccountInfoConfig accountInfo)) if (AccountList.TryGetValue(accountAlias, out AccountInfoConfig accountInfo))
{ {
Settings.Config.Main.General.Account = accountInfo; InternalConfig.Login = accountInfo.Login;
InternalConfig.Password = accountInfo.Password;
return true; return true;
} }
else else