Add command register for ChatBot

ChatBot can register their own command for interacting with user
This commit is contained in:
ReinforceZwei 2020-07-02 13:18:20 +08:00 committed by ORelio
parent 4f3f217c0f
commit 64643af89d
2 changed files with 76 additions and 1 deletions

View file

@ -1089,5 +1089,54 @@ namespace MinecraftClient
{ {
return Handler.UpdateSign(location, line1, line2, line3, line4); return Handler.UpdateSign(location, line1, line2, line3, line4);
} }
/// <summary>
/// Register a command in command prompt
/// </summary>
/// <param name="CMDName">Name of the command</param>
/// <param name="CMDDesc">Description/usage of the command</param>
/// <param name="Run">Method for handling the command</param>
/// <returns>True if successfully registered</returns>
protected bool RegisterChatBotCommand(string CMDName, string CMDDesc, CommandRunner Run)
{
return Handler.RegisterCommand(CMDName, CMDDesc, Run);
}
}
/// <summary>
/// Command runner definition.
/// Returned string will be the output of the command
/// </summary>
/// <param name="command">Full command</param>
/// <param name="args">Arguments in the command</param>
/// <returns></returns>
public delegate string CommandRunner(string command, string[] args);
/// <summary>
/// Command class with constructor for creating command for ChatBots.
/// </summary>
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<string, object> localVars)
{
return this.Runner(command, getArgs(command));
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="CMDName">Name of the command</param>
/// <param name="CMDDesc">Description/usage of the command</param>
/// <param name="runner">Method for handling the command</param>
public ChatBotCommand(string CMDName, string CMDDesc, CommandRunner runner)
{
this.CMDName = CMDName;
this.CMDDesc = CMDDesc;
this.Runner = runner;
}
} }
} }

View file

@ -25,6 +25,7 @@ namespace MinecraftClient
private static readonly List<string> cmd_names = new List<string>(); private static readonly List<string> cmd_names = new List<string>();
private static readonly Dictionary<string, Command> cmds = new Dictionary<string, Command>(); private static readonly Dictionary<string, Command> cmds = new Dictionary<string, Command>();
private readonly Dictionary<Guid, string> onlinePlayers = new Dictionary<Guid, string>(); private readonly Dictionary<Guid, string> onlinePlayers = new Dictionary<Guid, string>();
private static bool CommandLoaded = false;
private readonly List<ChatBot> bots = new List<ChatBot>(); private readonly List<ChatBot> bots = new List<ChatBot>();
private static readonly List<ChatBot> botsOnHold = new List<ChatBot>(); private static readonly List<ChatBot> botsOnHold = new List<ChatBot>();
@ -338,7 +339,7 @@ namespace MinecraftClient
{ {
/* Load commands from the 'Commands' namespace */ /* Load commands from the 'Commands' namespace */
if (cmds.Count == 0) if (!CommandLoaded)
{ {
Type[] cmds_classes = Program.GetTypesInNamespace("MinecraftClient.Commands"); Type[] cmds_classes = Program.GetTypesInNamespace("MinecraftClient.Commands");
foreach (Type type in cmds_classes) foreach (Type type in cmds_classes)
@ -359,6 +360,7 @@ namespace MinecraftClient
} }
} }
} }
CommandLoaded = true;
} }
/* Process the provided command */ /* Process the provided command */
@ -572,6 +574,30 @@ namespace MinecraftClient
} }
} }
/// <summary>
/// Register a command prompt command
/// </summary>
/// <param name="CMDName">Name of the command</param>
/// <param name="CMDDesc">Description/usage of the command</param>
/// <param name="runner">Method for handling the command</param>
/// <returns>True if successfully registered</returns>
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 #region Management: Load/Unload ChatBots and Enable/Disable settings
/// <summary> /// <summary>