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,99 +1,94 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Brigadier.NET;
using Brigadier.NET.Builder;
using DynamicExpresso;
using MinecraftClient.CommandHandler;
namespace MinecraftClient.Commands
{
class ExecIf : Command
{
public override string CmdName { get { return "execif"; } }
public override string CmdUsage { get { return "execif <condition/expression> ---> <command>"; } }
public override string CmdUsage { get { return "execif \"<condition/expression>\" \"<command>\""; } }
public override string CmdDesc { get { return Translations.cmd_execif_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("Condition", Arguments.String())
.Then(l => l.Argument("Command", Arguments.String())
.Executes(r => HandleCommand(r.Source, handler, Arguments.GetString(r, "Condition"), Arguments.GetString(r, "Command")))))
);
}
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 commandsString = GetArg(command);
#pragma warning disable format // @formatter:off
_ => GetCmdDescTranslated(),
#pragma warning restore format // @formatter:on
});
}
if (!commandsString.Contains("--->"))
return GetCmdDescTranslated();
private int HandleCommand(CmdResult r, McClient handler, string expressionText, string resultCommand)
{
try
{
var interpreter = new Interpreter();
interpreter.SetVariable("MCC", handler);
string[] parts = commandsString.Split("--->", StringSplitOptions.TrimEntries)
.ToList()
.ConvertAll(command => command.Trim())
.ToArray();
foreach (KeyValuePair<string, object> entry in Settings.Config.AppVar.GetVariables())
interpreter.SetVariable(entry.Key, entry.Value);
if (parts.Length == 0)
return GetCmdDescTranslated();
var result = interpreter.Eval<bool>(expressionText);
string expressionText = parts[0];
string resultCommand = parts[1];
bool shouldExec = result;
try
/*if (result is bool)
shouldExec = (bool)result;
else if (result is string)
shouldExec = !string.IsNullOrEmpty((string)result) && ((string)result).Trim().Contains("true", StringComparison.OrdinalIgnoreCase);
else if (result is int)
shouldExec = (int)result > 0;
else if (result is double)
shouldExec = (double)result > 0;
else if (result is float)
shouldExec = (float)result > 0;
else if (result is Int16)
shouldExec = (Int16)result > 0;
else if (result is Int32)
shouldExec = (Int32)result > 0;
else if (result is Int64)
shouldExec = (Int64)result > 0;
*/
handler.Log.Debug("[Execif] Result Type: " + result.GetType().Name);
if (shouldExec)
{
var interpreter = new Interpreter();
interpreter.SetVariable("MCC", handler);
CmdResult output = new();
handler.PerformInternalCommand(resultCommand, ref output);
foreach (KeyValuePair<string, object> entry in Settings.Config.AppVar.GetVariables())
interpreter.SetVariable(entry.Key, entry.Value);
var result = interpreter.Eval<bool>(expressionText);
bool shouldExec = result;
/*if (result is bool)
shouldExec = (bool)result;
else if (result is string)
shouldExec = !string.IsNullOrEmpty((string)result) && ((string)result).Trim().Contains("true", StringComparison.OrdinalIgnoreCase);
else if (result is int)
shouldExec = (int)result > 0;
else if (result is double)
shouldExec = (double)result > 0;
else if (result is float)
shouldExec = (float)result > 0;
else if (result is Int16)
shouldExec = (Int16)result > 0;
else if (result is Int32)
shouldExec = (Int32)result > 0;
else if (result is Int64)
shouldExec = (Int64)result > 0;
*/
handler.Log.Debug("[Execif] Result Type: " + result.GetType().Name);
if (shouldExec)
{
string? output = "";
handler.PerformInternalCommand(resultCommand, ref output);
if (string.IsNullOrEmpty(output))
handler.Log.Debug(string.Format(Translations.cmd_execif_executed_no_output, expressionText, resultCommand));
else
handler.Log.Debug(string.Format(Translations.cmd_execif_executed, expressionText, resultCommand, output));
return "";
}
return "";
return r.SetAndReturn(CmdResult.Status.Done, string.Format(Translations.cmd_execif_executed, expressionText, resultCommand, output));
}
catch (Exception e)
else
{
handler.Log.Error(string.Format(Translations.cmd_execif_error_occured, command));
handler.Log.Error(string.Format(Translations.cmd_execif_error, e.Message));
return "";
return r.SetAndReturn(CmdResult.Status.Done);
}
}
return GetCmdDescTranslated();
catch (Exception e)
{
handler.Log.Error(string.Format(Translations.cmd_execif_error, e.Message));
return r.SetAndReturn(CmdResult.Status.Fail, string.Format(Translations.cmd_execif_error_occured, expressionText + " ---> " + resultCommand));
}
}
}
}