[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))
{
Program.Restart();
Program.Restart(keepAccountAndServerSettings: true);
return "";
}
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]);
}
}
Program.Restart();
Program.Restart(keepAccountAndServerSettings: true);
return "";
}
}

View file

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

View file

@ -674,9 +674,9 @@ namespace MinecraftClient
/// <summary>
/// Reloads settings
/// </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));
}
@ -692,7 +692,7 @@ namespace MinecraftClient
/// Disconnect the current client from the server and restart it
/// </summary>
/// <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();
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();
}

View file

@ -247,12 +247,12 @@ namespace MinecraftClient
/// </summary>
/// <param name="extraAttempts">If connection fails, the client will make X extra attempts</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)
base.ReconnectToTheServer();
base.ReconnectToTheServer(delaySeconds: delaySeconds, keepAccountAndServerSettings: keepAccountAndServerSettings);
else
base.ReconnectToTheServer(extraAttempts);
base.ReconnectToTheServer(extraAttempts, delaySeconds, keepAccountAndServerSettings);
}
/// <summary>
@ -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;
}

View file

@ -920,7 +920,7 @@ namespace MinecraftClient
/// </summary>
/// <param name="ExtraAttempts">In case of failure, maximum extra attempts before aborting</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)
{
@ -928,7 +928,7 @@ namespace MinecraftClient
ConsoleIO.WriteLogLine(string.Format(Translations.chatbot_reconnect, botName));
}
McClient.ReconnectionAttemptsLeft = ExtraAttempts;
Program.Restart(delaySeconds);
Program.Restart(delaySeconds, keepAccountAndServerSettings);
}
/// <summary>

View file

@ -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<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;
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,6 +391,7 @@ namespace MinecraftClient
General.Account.Login ??= string.Empty;
General.Account.Password ??= string.Empty;
if (!InternalConfig.KeepAccountSettings)
InternalConfig.Login = General.Account.Login;
General.Server.Host ??= string.Empty;
@ -404,6 +423,8 @@ namespace MinecraftClient
ConsoleIO.WriteLogLine("[Settings] " + Translations.config_Main_Advanced_language_invaild);
}
if (!InternalConfig.KeepServerSettings)
{
if (!string.IsNullOrWhiteSpace(General.Server.Host))
{
string[] sip = General.Server.Host.Split(new[] { ":", "" }, StringSplitOptions.None);
@ -421,6 +442,7 @@ namespace MinecraftClient
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