diff --git a/MinecraftClient/Commands/Connect.cs b/MinecraftClient/Commands/Connect.cs index 7a131fa4..5fd5c669 100644 --- a/MinecraftClient/Commands/Connect.cs +++ b/MinecraftClient/Commands/Connect.cs @@ -23,7 +23,7 @@ namespace MinecraftClient.Commands if (Settings.Config.Main.SetServerIP(new Settings.MainConfigHealper.MainConfig.ServerInfoConfig(args[0]), true)) { - Program.Restart(); + Program.Restart(keepAccountAndServerSettings: true); return ""; } else return string.Format(Translations.cmd_connect_invalid_ip, args[0]); diff --git a/MinecraftClient/Commands/Reco.cs b/MinecraftClient/Commands/Reco.cs index 267573f4..53688d3e 100644 --- a/MinecraftClient/Commands/Reco.cs +++ b/MinecraftClient/Commands/Reco.cs @@ -18,7 +18,7 @@ namespace MinecraftClient.Commands return string.Format(Translations.cmd_connect_unknown, args[0]); } } - Program.Restart(); + Program.Restart(keepAccountAndServerSettings: true); return ""; } } diff --git a/MinecraftClient/McClient.cs b/MinecraftClient/McClient.cs index 3cbbe868..d29187f8 100644 --- a/MinecraftClient/McClient.cs +++ b/MinecraftClient/McClient.cs @@ -710,7 +710,7 @@ namespace MinecraftClient /// Marks if bots need to be hard reloaded public void ReloadSettings() { - Program.ReloadSettings(); + Program.ReloadSettings(true); ReloadBots(); } diff --git a/MinecraftClient/Program.cs b/MinecraftClient/Program.cs index ac82421e..2e184a7f 100644 --- a/MinecraftClient/Program.cs +++ b/MinecraftClient/Program.cs @@ -674,9 +674,9 @@ namespace MinecraftClient /// /// Reloads settings /// - 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)); } @@ -692,7 +692,7 @@ namespace MinecraftClient /// Disconnect the current client from the server and restart it /// /// Optional delay, in seconds, before restarting - public static void Restart(int delaySeconds = 0) + public static void Restart(int delaySeconds = 0, bool keepAccountAndServerSettings = false) { ConsoleInteractive.ConsoleReader.StopReadThread(); new Thread(new ThreadStart(delegate @@ -705,7 +705,7 @@ namespace MinecraftClient Thread.Sleep(delaySeconds * 1000); } ConsoleIO.WriteLine(Translations.mcc_restart); - ReloadSettings(); + ReloadSettings(keepAccountAndServerSettings); InitializeClient(); })).Start(); } diff --git a/MinecraftClient/Scripting/CSharpRunner.cs b/MinecraftClient/Scripting/CSharpRunner.cs index fa4226dd..026a29fa 100644 --- a/MinecraftClient/Scripting/CSharpRunner.cs +++ b/MinecraftClient/Scripting/CSharpRunner.cs @@ -247,12 +247,12 @@ namespace MinecraftClient /// /// If connection fails, the client will make X extra attempts /// Optional delay, in seconds, before restarting - 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) - base.ReconnectToTheServer(); + base.ReconnectToTheServer(delaySeconds: delaySeconds, keepAccountAndServerSettings: keepAccountAndServerSettings); else - base.ReconnectToTheServer(extraAttempts); + base.ReconnectToTheServer(extraAttempts, delaySeconds, keepAccountAndServerSettings); } /// @@ -361,7 +361,7 @@ namespace MinecraftClient { bool result = Settings.Config.Main.Advanced.SetAccount(accountAlias); if (result && andReconnect) - ReconnectToTheServer(); + ReconnectToTheServer(keepAccountAndServerSettings: true); return result; } @@ -374,7 +374,7 @@ namespace MinecraftClient { bool result = Settings.Config.Main.SetServerIP(new MainConfigHealper.MainConfig.ServerInfoConfig(server), true); if (result && andReconnect) - ReconnectToTheServer(); + ReconnectToTheServer(keepAccountAndServerSettings: true); return result; } diff --git a/MinecraftClient/Scripting/ChatBot.cs b/MinecraftClient/Scripting/ChatBot.cs index 06f76a04..e7ab3cde 100644 --- a/MinecraftClient/Scripting/ChatBot.cs +++ b/MinecraftClient/Scripting/ChatBot.cs @@ -920,7 +920,7 @@ namespace MinecraftClient /// /// In case of failure, maximum extra attempts before aborting /// Optional delay, in seconds, before restarting - 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) { @@ -928,7 +928,7 @@ namespace MinecraftClient ConsoleIO.WriteLogLine(string.Format(Translations.chatbot_reconnect, botName)); } McClient.ReconnectionAttemptsLeft = ExtraAttempts; - Program.Restart(delaySeconds); + Program.Restart(delaySeconds, keepAccountAndServerSettings); } /// diff --git a/MinecraftClient/Settings.cs b/MinecraftClient/Settings.cs index f56f1979..445054f0 100644 --- a/MinecraftClient/Settings.cs +++ b/MinecraftClient/Settings.cs @@ -58,6 +58,10 @@ namespace MinecraftClient public static bool InteractiveMode = true; public static bool GravityEnabled = true; + + public static bool KeepAccountSettings = false; + + public static bool KeepServerSettings = false; } public class GlobalConfig @@ -125,8 +129,13 @@ namespace MinecraftClient } - public static Tuple LoadFromFile(string filepath) + public static Tuple 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; TomlDocument document; try @@ -157,6 +166,13 @@ namespace MinecraftClient ConsoleIO.WriteLine(ex.GetFullMessage()); return new(false, false); } + finally + { + if (!keepAccountSettings) + InternalConfig.KeepAccountSettings = false; + if (!keepServerSettings) + InternalConfig.KeepServerSettings = false; + } return new(true, false); } @@ -257,12 +273,14 @@ namespace MinecraftClient { case 0: InternalConfig.Login = argument; + InternalConfig.KeepAccountSettings = true; break; case 1: InternalConfig.Password = argument; break; case 2: Config.Main.SetServerIP(new MainConfig.ServerInfoConfig(argument), true); + InternalConfig.KeepServerSettings = true; break; case 3: // SingleCommand = argument; @@ -373,7 +391,8 @@ namespace MinecraftClient General.Account.Login ??= string.Empty; General.Account.Password ??= string.Empty; - InternalConfig.Login = General.Account.Login; + if (!InternalConfig.KeepAccountSettings) + InternalConfig.Login = General.Account.Login; General.Server.Host ??= string.Empty; @@ -404,23 +423,26 @@ namespace MinecraftClient ConsoleIO.WriteLogLine("[Settings] " + Translations.config_Main_Advanced_language_invaild); } - if (!string.IsNullOrWhiteSpace(General.Server.Host)) + if (!InternalConfig.KeepServerSettings) { - string[] sip = General.Server.Host.Split(new[] { ":", ":" }, StringSplitOptions.None); - General.Server.Host = sip[0]; - InternalConfig.ServerIP = General.Server.Host; - - if (sip.Length > 1) + if (!string.IsNullOrWhiteSpace(General.Server.Host)) { - try { General.Server.Port = Convert.ToUInt16(sip[1]); } - catch (FormatException) { } - } - } + string[] sip = General.Server.Host.Split(new[] { ":", ":" }, StringSplitOptions.None); + General.Server.Host = sip[0]; + InternalConfig.ServerIP = General.Server.Host; - if (General.Server.Port.HasValue) - InternalConfig.ServerPort = General.Server.Port.Value; - else - SetServerIP(General.Server, true); + if (sip.Length > 1) + { + try { General.Server.Port = Convert.ToUInt16(sip[1]); } + catch (FormatException) { } + } + } + + if (General.Server.Port.HasValue) + InternalConfig.ServerPort = General.Server.Port.Value; + else + SetServerIP(General.Server, true); + } for (int i = 0; i < Advanced.BotOwners.Count; ++i) Advanced.BotOwners[i] = ToLowerIfNeed(Advanced.BotOwners[i]); @@ -579,7 +601,8 @@ namespace MinecraftClient { if (AccountList.TryGetValue(accountAlias, out AccountInfoConfig accountInfo)) { - Settings.Config.Main.General.Account = accountInfo; + InternalConfig.Login = accountInfo.Login; + InternalConfig.Password = accountInfo.Password; return true; } else