mirror of
https://github.com/MCCTeam/Minecraft-Console-Client
synced 2025-10-14 21:22:49 +00:00
Implement command completion suggestions.
This commit is contained in:
parent
5d2589b10f
commit
84cf749344
115 changed files with 4684 additions and 2695 deletions
|
|
@ -1,7 +1,9 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using Brigadier.NET;
|
||||
using Brigadier.NET.Builder;
|
||||
using MinecraftClient.CommandHandler;
|
||||
using MinecraftClient.Scripting;
|
||||
|
||||
namespace MinecraftClient.Commands
|
||||
{
|
||||
|
|
@ -11,69 +13,82 @@ namespace MinecraftClient.Commands
|
|||
public override string CmdUsage { get { return "bots [list|unload <bot name|all>]"; } }
|
||||
public override string CmdDesc { get { return Translations.cmd_bots_desc; } }
|
||||
|
||||
public override void RegisterCommand(McClient handler, CommandDispatcher<CommandSource> dispatcher)
|
||||
public override void RegisterCommand(McClient handler, CommandDispatcher<CmdResult> dispatcher)
|
||||
{
|
||||
dispatcher.Register(l => l.Literal("help")
|
||||
.Then(l => l.Literal(CmdName)
|
||||
.Executes(r => GetUsage(r.Source, string.Empty))
|
||||
.Then(l => l.Literal("list")
|
||||
.Executes(r => GetUsage(r.Source, "list")))
|
||||
.Then(l => l.Literal("unload")
|
||||
.Executes(r => GetUsage(r.Source, "unload")))
|
||||
)
|
||||
);
|
||||
|
||||
dispatcher.Register(l => l.Literal(CmdName)
|
||||
.Executes(r => DoListBot(r.Source, handler))
|
||||
.Then(l => l.Literal("list")
|
||||
.Executes(r => DoListBot(r.Source, handler)))
|
||||
.Then(l => l.Literal("unload")
|
||||
.Then(l => l.Argument("BotName", MccArguments.BotName())
|
||||
.Executes(r => DoUnloadBot(r.Source, handler, Arguments.GetString(r, "BotName")))))
|
||||
.Then(l => l.Literal("_help")
|
||||
.Redirect(dispatcher.GetRoot().GetChild("help").GetChild(CmdName)))
|
||||
);
|
||||
}
|
||||
|
||||
public override string Run(McClient handler, string command, Dictionary<string, object>? localVars)
|
||||
private int GetUsage(CmdResult r, string? cmd)
|
||||
{
|
||||
if (HasArg(command))
|
||||
return r.SetAndReturn(cmd switch
|
||||
{
|
||||
string[] args = GetArgs(command);
|
||||
#pragma warning disable format // @formatter:off
|
||||
"list" => GetCmdDescTranslated(),
|
||||
"unload" => GetCmdDescTranslated(),
|
||||
_ => GetCmdDescTranslated(),
|
||||
#pragma warning restore format // @formatter:on
|
||||
});
|
||||
}
|
||||
|
||||
if (args.Length == 1)
|
||||
{
|
||||
if (args[0].Equals("list", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
StringBuilder sb = new();
|
||||
private int DoListBot(CmdResult r, McClient handler)
|
||||
{
|
||||
int length = handler.GetLoadedChatBots().Count;
|
||||
if (length == 0)
|
||||
return r.SetAndReturn(CmdResult.Status.Fail, Translations.cmd_bots_noloaded);
|
||||
|
||||
int length = handler.GetLoadedChatBots().Count;
|
||||
|
||||
if (length == 0)
|
||||
return Translations.cmd_bots_noloaded;
|
||||
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
sb.Append(handler.GetLoadedChatBots()[i].GetType().Name);
|
||||
|
||||
if (i != length - 1)
|
||||
sb.Append(" ,");
|
||||
|
||||
}
|
||||
|
||||
return Translations.cmd_bots_list + ": " + sb.ToString();
|
||||
}
|
||||
|
||||
}
|
||||
else if (args.Length == 2)
|
||||
{
|
||||
if (args[0].Equals("unload", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
string botName = args[1].Trim();
|
||||
|
||||
if (botName.ToLower().Equals("all", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
if (handler.GetLoadedChatBots().Count == 0)
|
||||
return Translations.cmd_bots_noloaded;
|
||||
|
||||
handler.UnloadAllBots();
|
||||
return Translations.cmd_bots_unloaded_all;
|
||||
}
|
||||
else
|
||||
{
|
||||
ChatBot? bot = handler.GetLoadedChatBots().Find(bot => bot.GetType().Name.ToLower() == botName.ToLower());
|
||||
|
||||
if (bot == null)
|
||||
return string.Format(Translations.cmd_bots_notfound, botName);
|
||||
|
||||
handler.BotUnLoad(bot);
|
||||
return string.Format(Translations.cmd_bots_unloaded, botName);
|
||||
}
|
||||
}
|
||||
}
|
||||
StringBuilder sb = new();
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
sb.Append(handler.GetLoadedChatBots()[i].GetType().Name);
|
||||
if (i != length - 1)
|
||||
sb.Append(" ,");
|
||||
}
|
||||
|
||||
return GetCmdDescTranslated();
|
||||
return r.SetAndReturn(CmdResult.Status.Done, Translations.cmd_bots_list + ": " + sb.ToString());
|
||||
}
|
||||
|
||||
private int DoUnloadBot(CmdResult r, McClient handler, string botName)
|
||||
{
|
||||
if (botName.ToLower().Equals("all", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
if (handler.GetLoadedChatBots().Count == 0)
|
||||
return r.SetAndReturn(CmdResult.Status.Fail, Translations.cmd_bots_noloaded);
|
||||
else
|
||||
{
|
||||
handler.UnloadAllBots();
|
||||
return r.SetAndReturn(CmdResult.Status.Done, Translations.cmd_bots_unloaded_all);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ChatBot? bot = handler.GetLoadedChatBots().Find(bot => bot.GetType().Name.ToLower() == botName.ToLower());
|
||||
if (bot == null)
|
||||
return r.SetAndReturn(CmdResult.Status.Fail, string.Format(Translations.cmd_bots_notfound, botName));
|
||||
else
|
||||
{
|
||||
handler.BotUnLoad(bot);
|
||||
return r.SetAndReturn(CmdResult.Status.Done, string.Format(Translations.cmd_bots_unloaded, botName));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue