Added access to App Vars inside the Chat Bot API via 3 new methods. Added access to Random class inside the execif command

This commit is contained in:
Anon 2023-03-31 18:17:03 +02:00
parent beabe14c92
commit 0a57c927d7
2 changed files with 33 additions and 22 deletions

View file

@ -45,35 +45,15 @@ namespace MinecraftClient.Commands
{ {
var interpreter = new Interpreter(); var interpreter = new Interpreter();
interpreter.SetVariable("MCC", handler); interpreter.SetVariable("MCC", handler);
interpreter.SetVariable("random", new Random());
foreach (KeyValuePair<string, object> entry in Settings.Config.AppVar.GetVariables()) foreach (KeyValuePair<string, object> entry in Settings.Config.AppVar.GetVariables())
interpreter.SetVariable(entry.Key, entry.Value); interpreter.SetVariable(entry.Key, entry.Value);
var result = interpreter.Eval<bool>(expressionText); 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); handler.Log.Debug("[Execif] Result Type: " + result.GetType().Name);
if (shouldExec) if (result)
{ {
CmdResult output = new(); CmdResult output = new();
handler.PerformInternalCommand(resultCommand, ref output); handler.PerformInternalCommand(resultCommand, ref output);

View file

@ -978,6 +978,37 @@ namespace MinecraftClient.Scripting
Handler.BotLoad(chatBot); Handler.BotLoad(chatBot);
} }
/// <summary>
/// Set an App Variable
/// </summary>
/// <param name="name">App variable name</param>
/// <param name="value">App variable value</param>
/// <returns>void</returns>
protected void SetAppVar(string name, object value)
{
Config.AppVar.SetVar(name, value);
}
/// <summary>
/// Get a value from an App Variable
/// </summary>
/// <param name="name">App variable name</param>
/// <returns>App Variable value</returns>
protected object? GetAppVar(string name)
{
return Config.AppVar.GetVar(name);
}
/// <summary>
/// Replaces variables in text with their values from the App Var registry
/// </summary>
/// <param name="text">Your text with variables</param>
/// <returns>text with variables replaced with their values</returns>
protected string ExpandAppVars(string text)
{
return Config.AppVar.ExpandVars(text);
}
/// <summary> /// <summary>
/// Check whether Terrain and Movements is enabled. /// Check whether Terrain and Movements is enabled.
/// </summary> /// </summary>