From 0a57c927d7322888b04a7e8b1125fdf46489e520 Mon Sep 17 00:00:00 2001 From: Anon Date: Fri, 31 Mar 2023 18:17:03 +0200 Subject: [PATCH] Added access to App Vars inside the Chat Bot API via 3 new methods. Added access to Random class inside the execif command --- MinecraftClient/Commands/ExecIf.cs | 24 ++------------------- MinecraftClient/Scripting/ChatBot.cs | 31 ++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 22 deletions(-) diff --git a/MinecraftClient/Commands/ExecIf.cs b/MinecraftClient/Commands/ExecIf.cs index 1e441794..ca985bf4 100644 --- a/MinecraftClient/Commands/ExecIf.cs +++ b/MinecraftClient/Commands/ExecIf.cs @@ -45,35 +45,15 @@ namespace MinecraftClient.Commands { var interpreter = new Interpreter(); interpreter.SetVariable("MCC", handler); + interpreter.SetVariable("random", new Random()); foreach (KeyValuePair entry in Settings.Config.AppVar.GetVariables()) interpreter.SetVariable(entry.Key, entry.Value); var result = interpreter.Eval(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); - if (shouldExec) + if (result) { CmdResult output = new(); handler.PerformInternalCommand(resultCommand, ref output); diff --git a/MinecraftClient/Scripting/ChatBot.cs b/MinecraftClient/Scripting/ChatBot.cs index 67bc3cb5..f8ca97cd 100644 --- a/MinecraftClient/Scripting/ChatBot.cs +++ b/MinecraftClient/Scripting/ChatBot.cs @@ -977,6 +977,37 @@ namespace MinecraftClient.Scripting { Handler.BotLoad(chatBot); } + + /// + /// Set an App Variable + /// + /// App variable name + /// App variable value + /// void + protected void SetAppVar(string name, object value) + { + Config.AppVar.SetVar(name, value); + } + + /// + /// Get a value from an App Variable + /// + /// App variable name + /// App Variable value + protected object? GetAppVar(string name) + { + return Config.AppVar.GetVar(name); + } + + /// + /// Replaces variables in text with their values from the App Var registry + /// + /// Your text with variables + /// text with variables replaced with their values + protected string ExpandAppVars(string text) + { + return Config.AppVar.ExpandVars(text); + } /// /// Check whether Terrain and Movements is enabled.