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,6 +1,8 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Brigadier.NET;
|
||||
using Brigadier.NET.Builder;
|
||||
using MinecraftClient.CommandHandler;
|
||||
|
||||
namespace MinecraftClient.Commands
|
||||
{
|
||||
|
|
@ -11,65 +13,61 @@ namespace MinecraftClient.Commands
|
|||
public override string CmdDesc { get { return Translations.cmd_setrnd_desc; } }
|
||||
private static readonly Random rand = new();
|
||||
|
||||
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("range")
|
||||
.Executes(r => GetUsage(r.Source, "range")))
|
||||
)
|
||||
);
|
||||
|
||||
dispatcher.Register(l => l.Literal(CmdName)
|
||||
.Then(l => l.Argument("VarName", Arguments.String())
|
||||
.Then(l => l.Argument("Min", Arguments.Long())
|
||||
.Then(l => l.Literal("to")
|
||||
.Then(l => l.Argument("Max", Arguments.Long())
|
||||
.Executes(r => DoSetRnd(r.Source, Arguments.GetString(r, "VarName"), Arguments.GetLong(r, "Min"), Arguments.GetLong(r, "Max"))))))
|
||||
.Then(l => l.Argument("Expression", Arguments.GreedyString())
|
||||
.Executes(r => DoSetRnd(r.Source, Arguments.GetString(r, "VarName"), Arguments.GetString(r, "Expression")))))
|
||||
);
|
||||
}
|
||||
|
||||
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 = GetArg(command).Split(' ');
|
||||
#pragma warning disable format // @formatter:off
|
||||
"range" => GetCmdDescTranslated(),
|
||||
_ => GetCmdDescTranslated(),
|
||||
#pragma warning restore format // @formatter:on
|
||||
});
|
||||
}
|
||||
|
||||
if (args.Length > 1)
|
||||
{
|
||||
// detect "to" keyword in string
|
||||
if (args.Length == 2 && args[1].Contains("to"))
|
||||
{
|
||||
int num1;
|
||||
int num2;
|
||||
private int DoSetRnd(CmdResult r, string var, string argString)
|
||||
{
|
||||
// process all arguments similar to regular terminals with quotes and escaping
|
||||
List<string> values = ParseCommandLine(argString);
|
||||
|
||||
// try to extract the two numbers from the string
|
||||
try
|
||||
{
|
||||
num1 = Convert.ToInt32(args[1][..args[1].IndexOf('t')]);
|
||||
num2 = Convert.ToInt32(args[1].Substring(args[1].IndexOf('o') + 1, args[1].Length - 1 - args[1].IndexOf('o')));
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return Translations.cmd_setrndnum_format;
|
||||
}
|
||||
// create a variable or set it to one of the values
|
||||
if (values.Count > 0 && Settings.Config.AppVar.SetVar(var, values[rand.Next(0, values.Count)]))
|
||||
return r.SetAndReturn(CmdResult.Status.Done, string.Format("Set %{0}% to {1}.", var, Settings.Config.AppVar.GetVar(var)));
|
||||
else
|
||||
return r.SetAndReturn(CmdResult.Status.Fail, Translations.cmd_setrndstr_format);
|
||||
}
|
||||
|
||||
// switch the values if they were entered in the wrong way
|
||||
if (num2 < num1)
|
||||
(num2, num1) = (num1, num2);
|
||||
private int DoSetRnd(CmdResult r, string var, long min, long max)
|
||||
{
|
||||
// switch the values if they were entered in the wrong way
|
||||
if (max < min)
|
||||
(max, min) = (min, max);
|
||||
|
||||
// create a variable or set it to num1 <= varlue < num2
|
||||
if (Settings.Config.AppVar.SetVar(args[0], rand.Next(num1, num2)))
|
||||
{
|
||||
return string.Format("Set %{0}% to {1}.", args[0], Settings.Config.AppVar.GetVar(args[0])); //Success
|
||||
}
|
||||
else return Translations.cmd_setrndnum_format;
|
||||
}
|
||||
else
|
||||
{
|
||||
// extract all arguments of the command
|
||||
string argString = command[(8 + command.Split(' ')[1].Length)..];
|
||||
|
||||
// process all arguments similar to regular terminals with quotes and escaping
|
||||
List<string> values = ParseCommandLine(argString);
|
||||
|
||||
// create a variable or set it to one of the values
|
||||
if (values.Count > 0 && Settings.Config.AppVar.SetVar(args[0], values[rand.Next(0, values.Count)]))
|
||||
{
|
||||
return string.Format("Set %{0}% to {1}.", args[0], Settings.Config.AppVar.GetVar(args[0])); //Success
|
||||
}
|
||||
else return Translations.cmd_setrndstr_format;
|
||||
}
|
||||
}
|
||||
else return GetCmdDescTranslated();
|
||||
}
|
||||
else return GetCmdDescTranslated();
|
||||
// create a variable or set it to num1 <= varlue < num2
|
||||
if (Settings.Config.AppVar.SetVar(var, rand.NextInt64(min, max)))
|
||||
return r.SetAndReturn(CmdResult.Status.Fail, string.Format("Set %{0}% to {1}.", var, Settings.Config.AppVar.GetVar(var)));
|
||||
else
|
||||
return r.SetAndReturn(CmdResult.Status.Fail, Translations.cmd_setrndstr_format);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue