Add setrnd command (#1846)

* Add setrnd command

* Make rand private

* Add console-like argument behaviour

* Remove unused using declarations

* Add german and english translations

* Improve translation, Add notice to config README, Add check wheather arguments are provided

* Add some comments

* Remove typo

* Add %player% constant to setrnd

* Remove test variable and add comments

* Remove typo

* Reverse changes

* Remove translations
This commit is contained in:
Daenges 2021-12-15 11:51:16 +01:00 committed by GitHub
parent 333358c73b
commit 381b8ea7d1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 149 additions and 0 deletions

View file

@ -0,0 +1,136 @@
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
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"; } }
private static readonly Random rand = new Random();
public override string Run(McClient handler, string command, Dictionary<string, object> localVars)
{
if (hasArg(command))
{
string[] args = getArg(command).Split(' ');
if (args.Length > 1)
{
// 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
{
num1 = Convert.ToInt32(args[1].Substring(0, 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.Get("cmd.setrndnum.format");
}
// switch the values if they were entered in the wrong way
if (num2 < num1)
{
int temp = num1;
num1 = num2;
num2 = temp;
}
// create a variable or set it to num1 <= varlue < num2
if (Settings.SetVar(args[0], rand.Next(num1, num2)))
{
return string.Format("Set %{0}% to {1}.", args[0], Settings.GetVar(args[0])); //Success
}
else return Translations.Get("cmd.setrndnum.format");
}
else
{
// extract all arguments of the command
string argString = command.Substring(command.IndexOf(args[0]) + args[0].Length, command.Length - 8 - args[0].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.SetVar(args[0], values[rand.Next(0, values.Count)]))
{
return string.Format("Set %{0}% to {1}.", args[0], Settings.GetVar(args[0])); //Success
}
else return Translations.Get("cmd.setrndstr.format");
}
}
else return GetCmdDescTranslated();
}
else return GetCmdDescTranslated();
}
/// <summary>
/// Extract arguments from a given string. Allows quotines and escaping them.
/// Similar to command line arguments in regular terminals.
/// </summary>
/// <param name="cmdLine">Provided arguments as a string</param>
/// <returns>All extracted arguments in a string list</returns>
private static List<string> parseCommandLine(string cmdLine)
{
var args = new List<string>();
if (string.IsNullOrWhiteSpace(cmdLine)) return args;
var currentArg = new StringBuilder();
bool inQuotedArg = false;
for (int i = 0; i < cmdLine.Length; i++)
{
if (cmdLine[i] == '"' && cmdLine[i-1] != '\\')
{
if (inQuotedArg)
{
args.Add(currentArg.ToString());
currentArg = new StringBuilder();
inQuotedArg = false;
}
else
{
inQuotedArg = true;
}
}
else if (cmdLine[i] == ' ')
{
if (inQuotedArg)
{
currentArg.Append(cmdLine[i]);
}
else if (currentArg.Length > 0)
{
args.Add(currentArg.ToString());
currentArg = new StringBuilder();
}
}
else
{
if (cmdLine[i] == '\\' && cmdLine[i + 1] == '\"')
{
currentArg.Append("\"");
i += 1;
}
else
{
currentArg.Append(cmdLine[i]);
}
}
}
if (currentArg.Length > 0) args.Add(currentArg.ToString());
return args;
}
}
}

View file

@ -75,6 +75,7 @@
<Compile Include="AutoTimeout.cs" />
<Compile Include="ChatBots\AutoDrop.cs" />
<Compile Include="ChatBots\Mailer.cs" />
<Compile Include="Commands\SetRnd.cs" />
<Compile Include="Protocol\JwtPayloadDecode.cs" />
<Compile Include="Protocol\Handlers\PacketPalettes\PacketPalette118.cs" />
<Compile Include="Protocol\MojangAPI.cs" />

View file

@ -328,6 +328,11 @@ cmd.send.desc=Sende eine Chatnachricht oder einen Command an den Server.
cmd.set.desc=Setze eine benutzerdefinierte %variable%.
cmd.set.format=Variable muss aus A-Za-z0-9 bestehen.
# SetRnd
cmd.setrnd.desc=Setze eine benutzerdefinierte %variable% zufällig auf einen der angegebenen Werte.
cmd.setrndnum.format=setrnd variable -7to17
cmd.setrndstr.format=setrnd variable string1 "\"string2\" string3"
# Sneak
cmd.sneak.desc=Schaltet Ducken AN/AUS
cmd.sneak.on=Du duckst dich jetzt.

View file

@ -328,6 +328,11 @@ cmd.send.desc=send a chat message or command.
cmd.set.desc=set a custom %variable%.
cmd.set.format=variable name must be A-Za-z0-9.
# SetRnd
cmd.setrnd.desc=set a custom %variable% randomly to a given value.
cmd.setrndnum.format=setrnd variable -7to17
cmd.setrndstr.format=setrnd variable string1 "\"string2\" string3"
# Sneak
cmd.sneak.desc=Toggles sneaking
cmd.sneak.on=You are sneaking now

View file

@ -120,6 +120,8 @@ In scripts and remote control, no slash is needed to perform the command, eg. `q
- `log <text>`: display some text in the console (useful for scripts)
- `list`: list players logged in to the server (uses tab list info sent by server)
- `set varname=value`: set a value which can be used as `%varname%` in further commands
- `setrnd variable string1 "\"string2\" string3"`: set a `%variable%` to one of the provided values
- `setrnd variable -7to10`: set a `%variable%` to a number from -7 to 9
- `wait <time>`: wait X ticks (10 ticks = ~1 second. Only for scripts)
- `move`: used for moving when terrain and movements feature is enabled
- `look`: used for looking at direction when terrain and movements is enabled