2022-12-06 15:50:17 +08:00
|
|
|
|
using Brigadier.NET;
|
|
|
|
|
|
using Brigadier.NET.Builder;
|
|
|
|
|
|
using MinecraftClient.CommandHandler;
|
|
|
|
|
|
using static MinecraftClient.CommandHandler.CmdResult;
|
2014-06-18 13:32:17 +02:00
|
|
|
|
|
|
|
|
|
|
namespace MinecraftClient.Commands
|
|
|
|
|
|
{
|
|
|
|
|
|
public class Connect : Command
|
|
|
|
|
|
{
|
2020-10-17 19:41:31 +08:00
|
|
|
|
public override string CmdName { get { return "connect"; } }
|
|
|
|
|
|
public override string CmdUsage { get { return "connect <server> [account]"; } }
|
2022-10-28 11:13:20 +08:00
|
|
|
|
public override string CmdDesc { get { return Translations.cmd_connect_desc; } }
|
2014-06-18 13:32:17 +02:00
|
|
|
|
|
2022-12-11 16:30:45 +08:00
|
|
|
|
public override void RegisterCommand(CommandDispatcher<CmdResult> dispatcher)
|
2022-10-26 08:54:54 +08:00
|
|
|
|
{
|
2022-12-06 15:50:17 +08:00
|
|
|
|
dispatcher.Register(l => l.Literal("help")
|
|
|
|
|
|
.Then(l => l.Literal(CmdName)
|
|
|
|
|
|
.Executes(r => GetUsage(r.Source, string.Empty))
|
|
|
|
|
|
)
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
dispatcher.Register(l => l.Literal(CmdName)
|
|
|
|
|
|
.Then(l => l.Argument("ServerNick", MccArguments.ServerNick())
|
2022-12-11 16:30:45 +08:00
|
|
|
|
.Executes(r => DoConnect(r.Source, Arguments.GetString(r, "ServerNick"), string.Empty))
|
2022-12-06 15:50:17 +08:00
|
|
|
|
.Then(l => l.Argument("AccountNick", MccArguments.AccountNick())
|
2022-12-11 16:30:45 +08:00
|
|
|
|
.Executes(r => DoConnect(r.Source, Arguments.GetString(r, "ServerNick"), Arguments.GetString(r, "AccountNick")))))
|
2022-12-06 15:50:17 +08:00
|
|
|
|
.Then(l => l.Literal("_help")
|
2022-12-11 17:31:37 +08:00
|
|
|
|
.Executes(r => GetUsage(r.Source, string.Empty))
|
2022-12-06 15:50:17 +08:00
|
|
|
|
.Redirect(dispatcher.GetRoot().GetChild("help").GetChild(CmdName)))
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private int GetUsage(CmdResult r, string? cmd)
|
|
|
|
|
|
{
|
|
|
|
|
|
return r.SetAndReturn(cmd switch
|
|
|
|
|
|
{
|
|
|
|
|
|
#pragma warning disable format // @formatter:off
|
|
|
|
|
|
_ => GetCmdDescTranslated(),
|
|
|
|
|
|
#pragma warning restore format // @formatter:on
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-11 16:30:45 +08:00
|
|
|
|
private int DoConnect(CmdResult r, string server, string account)
|
2022-12-06 15:50:17 +08:00
|
|
|
|
{
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(account) && !Settings.Config.Main.Advanced.SetAccount(account))
|
|
|
|
|
|
return r.SetAndReturn(Status.Fail, string.Format(Translations.cmd_connect_unknown, account));
|
|
|
|
|
|
|
|
|
|
|
|
if (Settings.Config.Main.SetServerIP(new Settings.MainConfigHealper.MainConfig.ServerInfoConfig(server), true))
|
|
|
|
|
|
{
|
|
|
|
|
|
Program.Restart(keepAccountAndServerSettings: true);
|
|
|
|
|
|
return r.SetAndReturn(Status.Done);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
return r.SetAndReturn(Status.Fail, string.Format(Translations.cmd_connect_invalid_ip, server));
|
|
|
|
|
|
}
|
2022-10-26 08:54:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-06 15:50:17 +08:00
|
|
|
|
internal static string DoConnect(string command)
|
2014-06-18 13:32:17 +02:00
|
|
|
|
{
|
2022-12-06 15:50:17 +08:00
|
|
|
|
string[] args = GetArgs(command);
|
|
|
|
|
|
if (args.Length > 1 && !Settings.Config.Main.Advanced.SetAccount(args[1]))
|
|
|
|
|
|
return string.Format(Translations.cmd_connect_unknown, args[1]);
|
|
|
|
|
|
|
|
|
|
|
|
if (Settings.Config.Main.SetServerIP(new Settings.MainConfigHealper.MainConfig.ServerInfoConfig(args[0]), true))
|
|
|
|
|
|
{
|
|
|
|
|
|
Program.Restart(keepAccountAndServerSettings: true);
|
|
|
|
|
|
return string.Empty;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
2014-06-18 13:32:17 +02:00
|
|
|
|
{
|
2022-12-06 15:50:17 +08:00
|
|
|
|
return string.Format(Translations.cmd_connect_invalid_ip, args[0]);
|
2014-06-18 13:32:17 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|