mirror of
https://github.com/MCCTeam/Minecraft-Console-Client
synced 2025-10-14 21:22:49 +00:00
Add command auto-unregister mechanism (#1492)
* Add command auto-unregister mechanism * Improve logic
This commit is contained in:
parent
4853871ea1
commit
62c985376e
2 changed files with 30 additions and 2 deletions
|
|
@ -40,6 +40,7 @@ namespace MinecraftClient
|
|||
private McClient _handler = null;
|
||||
private ChatBot master = null;
|
||||
private List<string> registeredPluginChannels = new List<String>();
|
||||
private List<string> registeredCommands = new List<string>();
|
||||
private McClient Handler
|
||||
{
|
||||
get
|
||||
|
|
@ -781,6 +782,10 @@ namespace MinecraftClient
|
|||
/// </summary>
|
||||
protected void UnloadBot()
|
||||
{
|
||||
foreach (string cmdName in registeredCommands)
|
||||
{
|
||||
Handler.UnregisterCommand(cmdName);
|
||||
}
|
||||
Handler.BotUnLoad(this);
|
||||
}
|
||||
|
||||
|
|
@ -1261,7 +1266,7 @@ namespace MinecraftClient
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// Register a command in command prompt
|
||||
/// Register a command in command prompt. Command will be automatically unregistered when unloading ChatBot
|
||||
/// </summary>
|
||||
/// <param name="cmdName">Name of the command</param>
|
||||
/// <param name="cmdDesc">Description/usage of the command</param>
|
||||
|
|
@ -1269,7 +1274,10 @@ namespace MinecraftClient
|
|||
/// <returns>True if successfully registered</returns>
|
||||
protected bool RegisterChatBotCommand(string cmdName, string cmdDesc, string cmdUsage, CommandRunner callback)
|
||||
{
|
||||
return Handler.RegisterCommand(cmdName, cmdDesc, cmdUsage, callback);
|
||||
bool result = Handler.RegisterCommand(cmdName, cmdDesc, cmdUsage, callback);
|
||||
if (result)
|
||||
registeredCommands.Add(cmdName.ToLower());
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
|||
|
|
@ -629,6 +629,26 @@ namespace MinecraftClient
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unregister a console command
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// There is no check for the command is registered by above method or is embedded command.
|
||||
/// Which mean this can unload any command
|
||||
/// </remarks>
|
||||
/// <param name="cmdName">The name of command to be unregistered</param>
|
||||
/// <returns></returns>
|
||||
public bool UnregisterCommand(string cmdName)
|
||||
{
|
||||
if (cmds.ContainsKey(cmdName.ToLower()))
|
||||
{
|
||||
cmds.Remove(cmdName.ToLower());
|
||||
cmd_names.Remove(cmdName.ToLower());
|
||||
return true;
|
||||
}
|
||||
else return false;
|
||||
}
|
||||
|
||||
#region Management: Load/Unload ChatBots and Enable/Disable settings
|
||||
|
||||
/// <summary>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue