Implement command completion suggestions.

This commit is contained in:
BruceChen 2022-12-06 15:50:17 +08:00
parent 5d2589b10f
commit 84cf749344
115 changed files with 4684 additions and 2695 deletions

View file

@ -1,5 +1,6 @@
using System.Collections.Generic;
using Brigadier.NET;
using Brigadier.NET;
using Brigadier.NET.Builder;
using MinecraftClient.CommandHandler;
namespace MinecraftClient.Commands
{
@ -9,27 +10,50 @@ namespace MinecraftClient.Commands
public override string CmdUsage { get { return "set varname=value"; } }
public override string CmdDesc { get { return Translations.cmd_set_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))
)
);
dispatcher.Register(l => l.Literal(CmdName)
.Then(l => l.Argument("Expression", Arguments.GreedyString())
.Executes(r => DoSetVar(r.Source, Arguments.GetString(r, "Expression"))))
.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[] temp = GetArg(command).Split('=');
if (temp.Length > 1)
#pragma warning disable format // @formatter:off
_ => GetCmdDescTranslated(),
#pragma warning restore format // @formatter:on
});
}
private int DoSetVar(CmdResult r, string command)
{
string[] temp = command.Trim().Split('=');
if (temp.Length > 1)
{
if (Settings.Config.AppVar.SetVar(temp[0], command[(temp[0].Length + 1)..]))
{
if (Settings.Config.AppVar.SetVar(temp[0], GetArg(command).Substring(temp[0].Length + 1)))
return ""; //Success
else
return Translations.cmd_set_format;
return r.SetAndReturn(CmdResult.Status.Done); //Success
}
else
return GetCmdDescTranslated();
{
return r.SetAndReturn(CmdResult.Status.Fail, Translations.cmd_set_format);
}
}
else
return GetCmdDescTranslated();
{
return r.SetAndReturn(CmdResult.Status.Fail, Translations.cmd_set_format);
}
}
}
}