mirror of
https://github.com/MCCTeam/Minecraft-Console-Client
synced 2025-11-07 17:36:07 +00:00
Added a command that executes other command if a condition is met.
This commit is contained in:
parent
59cc4cea7c
commit
77da611411
6 changed files with 126 additions and 7 deletions
98
MinecraftClient/Commands/ExecIf.cs
Normal file
98
MinecraftClient/Commands/ExecIf.cs
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using DynamicExpresso;
|
||||
|
||||
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 CmdDesc { get { return "cmd.execif.desc"; } }
|
||||
|
||||
public override string Run(McClient handler, string command, Dictionary<string, object> localVars)
|
||||
{
|
||||
if (hasArg(command))
|
||||
{
|
||||
string commandsString = getArg(command);
|
||||
|
||||
if (!commandsString.Contains("--->"))
|
||||
return GetCmdDescTranslated();
|
||||
|
||||
string[] parts = commandsString.Split("--->", StringSplitOptions.TrimEntries)
|
||||
.ToList()
|
||||
.ConvertAll(command => command.Trim())
|
||||
.ToArray();
|
||||
|
||||
if (parts.Length == 0)
|
||||
return GetCmdDescTranslated();
|
||||
|
||||
string expressionText = parts[0];
|
||||
string resultCommand = parts[1];
|
||||
|
||||
try
|
||||
{
|
||||
var interpreter = new Interpreter();
|
||||
interpreter.SetVariable("MCC", handler);
|
||||
|
||||
foreach (KeyValuePair<string, object> entry in Settings.GetVariables())
|
||||
interpreter.SetVariable(entry.Key, entry.Value);
|
||||
|
||||
Lambda parsedExpression = interpreter.Parse(expressionText);
|
||||
var result = parsedExpression.Invoke();
|
||||
|
||||
if (result != null)
|
||||
{
|
||||
bool shouldExec = false;
|
||||
|
||||
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(Translations.TryGet("cmd.execif.executed_no_output", expressionText, resultCommand));
|
||||
else handler.Log.Debug(Translations.TryGet("cmd.execif.executed", expressionText, resultCommand, output));
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
handler.Log.Error(Translations.TryGet("cmd.execif.error_occured", command));
|
||||
handler.Log.Error(Translations.TryGet("cmd.execif.error", e.Message));
|
||||
return "";
|
||||
}
|
||||
|
||||
return GetCmdDescTranslated();
|
||||
}
|
||||
|
||||
return GetCmdDescTranslated();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue