2022-01-24 16:15:44 +01:00
|
|
|
using System;
|
2021-12-15 11:51:16 +01:00
|
|
|
using System.Collections.Generic;
|
2022-10-26 08:54:54 +08:00
|
|
|
using Brigadier.NET;
|
2021-12-15 11:51:16 +01:00
|
|
|
|
|
|
|
|
namespace MinecraftClient.Commands
|
|
|
|
|
{
|
|
|
|
|
class SetRnd : Command
|
|
|
|
|
{
|
|
|
|
|
public override string CmdName { get { return "setrnd"; } }
|
|
|
|
|
public override string CmdUsage { get { return Translations.Get("cmd.setrnd.format"); } }
|
|
|
|
|
public override string CmdDesc { get { return "cmd.setrnd.desc"; } }
|
2022-10-02 18:31:08 +08:00
|
|
|
private static readonly Random rand = new();
|
2021-12-15 11:51:16 +01:00
|
|
|
|
2022-10-26 08:54:54 +08:00
|
|
|
public override void RegisterCommand(McClient handler, CommandDispatcher<CommandSource> dispatcher)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-02 18:31:08 +08:00
|
|
|
public override string Run(McClient handler, string command, Dictionary<string, object>? localVars)
|
2021-12-15 11:51:16 +01:00
|
|
|
{
|
2022-10-02 18:31:08 +08:00
|
|
|
if (HasArg(command))
|
2021-12-15 11:51:16 +01:00
|
|
|
{
|
2022-10-02 18:31:08 +08:00
|
|
|
string[] args = GetArg(command).Split(' ');
|
2021-12-15 11:51:16 +01:00
|
|
|
|
2022-10-02 18:31:08 +08:00
|
|
|
if (args.Length > 1)
|
|
|
|
|
{
|
2021-12-15 11:51:16 +01:00
|
|
|
// detect "to" keyword in string
|
|
|
|
|
if (args.Length == 2 && args[1].Contains("to"))
|
|
|
|
|
{
|
|
|
|
|
int num1;
|
|
|
|
|
int num2;
|
|
|
|
|
|
|
|
|
|
// try to extract the two numbers from the string
|
|
|
|
|
try
|
|
|
|
|
{
|
2022-10-02 18:31:08 +08:00
|
|
|
num1 = Convert.ToInt32(args[1][..args[1].IndexOf('t')]);
|
2021-12-15 11:51:16 +01:00
|
|
|
num2 = Convert.ToInt32(args[1].Substring(args[1].IndexOf('o') + 1, args[1].Length - 1 - args[1].IndexOf('o')));
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
return Translations.Get("cmd.setrndnum.format");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// switch the values if they were entered in the wrong way
|
|
|
|
|
if (num2 < num1)
|
2022-10-02 18:31:08 +08:00
|
|
|
(num2, num1) = (num1, num2);
|
2021-12-15 11:51:16 +01:00
|
|
|
|
|
|
|
|
// create a variable or set it to num1 <= varlue < num2
|
2022-10-05 15:02:30 +08:00
|
|
|
if (Settings.Config.AppVar.SetVar(args[0], rand.Next(num1, num2)))
|
2021-12-15 11:51:16 +01:00
|
|
|
{
|
2022-10-05 15:02:30 +08:00
|
|
|
return string.Format("Set %{0}% to {1}.", args[0], Settings.Config.AppVar.GetVar(args[0])); //Success
|
2021-12-15 11:51:16 +01:00
|
|
|
}
|
|
|
|
|
else return Translations.Get("cmd.setrndnum.format");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// extract all arguments of the command
|
2022-10-02 18:31:08 +08:00
|
|
|
string argString = command[(8 + command.Split(' ')[1].Length)..];
|
2021-12-15 11:51:16 +01:00
|
|
|
|
|
|
|
|
// process all arguments similar to regular terminals with quotes and escaping
|
2022-10-02 18:31:08 +08:00
|
|
|
List<string> values = ParseCommandLine(argString);
|
2021-12-15 11:51:16 +01:00
|
|
|
|
|
|
|
|
// create a variable or set it to one of the values
|
2022-10-05 15:02:30 +08:00
|
|
|
if (values.Count > 0 && Settings.Config.AppVar.SetVar(args[0], values[rand.Next(0, values.Count)]))
|
2021-12-15 11:51:16 +01:00
|
|
|
{
|
2022-10-05 15:02:30 +08:00
|
|
|
return string.Format("Set %{0}% to {1}.", args[0], Settings.Config.AppVar.GetVar(args[0])); //Success
|
2021-12-15 11:51:16 +01:00
|
|
|
}
|
|
|
|
|
else return Translations.Get("cmd.setrndstr.format");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else return GetCmdDescTranslated();
|
|
|
|
|
}
|
|
|
|
|
else return GetCmdDescTranslated();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|