diff --git a/MinecraftClient/ChatBot.cs b/MinecraftClient/ChatBot.cs index 0fafcb6b..69018281 100644 --- a/MinecraftClient/ChatBot.cs +++ b/MinecraftClient/ChatBot.cs @@ -1089,5 +1089,54 @@ namespace MinecraftClient { return Handler.UpdateSign(location, line1, line2, line3, line4); } + + /// + /// Register a command in command prompt + /// + /// Name of the command + /// Description/usage of the command + /// Method for handling the command + /// True if successfully registered + protected bool RegisterChatBotCommand(string CMDName, string CMDDesc, CommandRunner Run) + { + return Handler.RegisterCommand(CMDName, CMDDesc, Run); + } + } + + /// + /// Command runner definition. + /// Returned string will be the output of the command + /// + /// Full command + /// Arguments in the command + /// + public delegate string CommandRunner(string command, string[] args); + + /// + /// Command class with constructor for creating command for ChatBots. + /// + public class ChatBotCommand : Command + { + public CommandRunner Runner; + + public override string CMDName { get; } + public override string CMDDesc { get; } + public override string Run(McClient handler, string command, Dictionary localVars) + { + return this.Runner(command, getArgs(command)); + } + + /// + /// Constructor + /// + /// Name of the command + /// Description/usage of the command + /// Method for handling the command + public ChatBotCommand(string CMDName, string CMDDesc, CommandRunner runner) + { + this.CMDName = CMDName; + this.CMDDesc = CMDDesc; + this.Runner = runner; + } } } diff --git a/MinecraftClient/McClient.cs b/MinecraftClient/McClient.cs index 8e821a77..b8c84e0f 100644 --- a/MinecraftClient/McClient.cs +++ b/MinecraftClient/McClient.cs @@ -25,6 +25,7 @@ namespace MinecraftClient private static readonly List cmd_names = new List(); private static readonly Dictionary cmds = new Dictionary(); private readonly Dictionary onlinePlayers = new Dictionary(); + private static bool CommandLoaded = false; private readonly List bots = new List(); private static readonly List botsOnHold = new List(); @@ -338,7 +339,7 @@ namespace MinecraftClient { /* Load commands from the 'Commands' namespace */ - if (cmds.Count == 0) + if (!CommandLoaded) { Type[] cmds_classes = Program.GetTypesInNamespace("MinecraftClient.Commands"); foreach (Type type in cmds_classes) @@ -359,6 +360,7 @@ namespace MinecraftClient } } } + CommandLoaded = true; } /* Process the provided command */ @@ -572,6 +574,30 @@ namespace MinecraftClient } } + /// + /// Register a command prompt command + /// + /// Name of the command + /// Description/usage of the command + /// Method for handling the command + /// True if successfully registered + public bool RegisterCommand(string CMDName, string CMDDesc, CommandRunner runner) + { + if (cmds.ContainsKey(CMDName.ToLower())) + { + return false; + } + else + { + Command cmd = new ChatBotCommand(CMDName, CMDDesc, runner); + cmds.Add(CMDName.ToLower(), cmd); + cmd_names.Add(CMDName.ToLower()); + return true; + } + } + + + #region Management: Load/Unload ChatBots and Enable/Disable settings ///