From 518a5c240c82f87a8e38d3444ddc0363e1519420 Mon Sep 17 00:00:00 2001 From: Goodlyay Date: Sun, 18 Aug 2024 16:03:10 -0700 Subject: [PATCH] Allow custom help behavior in SubCommand --- MCGalaxy/Commands/Overseer.cs | 22 ++++++------- MCGalaxy/Commands/SubCommand.cs | 43 ++++++++++++++++++++------ MCGalaxy/Commands/World/CmdOverseer.cs | 1 - 3 files changed, 44 insertions(+), 22 deletions(-) diff --git a/MCGalaxy/Commands/Overseer.cs b/MCGalaxy/Commands/Overseer.cs index c4e8a07de..53c72d43a 100644 --- a/MCGalaxy/Commands/Overseer.cs +++ b/MCGalaxy/Commands/Overseer.cs @@ -270,15 +270,15 @@ static void Moved(Player p, string message, string name, string newName = null) } static SubCommandGroup mapSubCommandGroup = new SubCommandGroup(commandShortcut + " map", new List() { - new SubCommand("Physics", (p, arg) => { Moved(p, arg, "physics"); }, null), - new SubCommand("Add", (p, arg) => { Moved(p, arg, "add"); }, null, false, new string[] { "create", "new" } ), - new SubCommand("Delete", (p, arg) => { Moved(p, arg, "delete"); }, null, false , new string[] { "del", "remove" } ), - new SubCommand("Save", (p, arg) => { Moved(p, arg, "save"); }, null), - new SubCommand("Restore", (p, arg) => { Moved(p, arg, "restore"); }, null), - new SubCommand("Resize", (p, arg) => { Moved(p, arg, "resize"); }, null), - new SubCommand("PerVisit", (p, arg) => { Moved(p, arg, "pervisit"); }, null), - new SubCommand("PerBuild", (p, arg) => { Moved(p, arg, "perbuild"); }, null), - new SubCommand("Texture", (p, arg) => { Moved(p, arg, "texture"); }, null, false, new string[] { "texturezip", "texturepack" } ), + new SubCommand("Physics", (p, arg) => { Moved(p, arg, "physics"); }), + new SubCommand("Add", (p, arg) => { Moved(p, arg, "add"); }, false, new string[] { "create", "new" } ), + new SubCommand("Delete", (p, arg) => { Moved(p, arg, "delete"); }, false, new string[] { "del", "remove" } ), + new SubCommand("Save", (p, arg) => { Moved(p, arg, "save"); }), + new SubCommand("Restore", (p, arg) => { Moved(p, arg, "restore"); }), + new SubCommand("Resize", (p, arg) => { Moved(p, arg, "resize"); }), + new SubCommand("PerVisit", (p, arg) => { Moved(p, arg, "pervisit"); }), + new SubCommand("PerBuild", (p, arg) => { Moved(p, arg, "perbuild"); }), + new SubCommand("Texture", (p, arg) => { Moved(p, arg, "texture"); }, false, new string[] { "texturezip", "texturepack" } ), } ); @@ -472,8 +472,8 @@ static void HandleZones(Player p, string raw) { internal static SubCommandGroup deprecatedSubCommandGroup = new SubCommandGroup(commandShortcut, new List() { - new SubCommand("Zone", HandleZone, null), - new SubCommand("Zones", HandleZones, null), + new SubCommand("Zone", HandleZone , false), + new SubCommand("Zones", HandleZones, false), } ); } diff --git a/MCGalaxy/Commands/SubCommand.cs b/MCGalaxy/Commands/SubCommand.cs index 7a9dbab93..5ec6f151f 100644 --- a/MCGalaxy/Commands/SubCommand.cs +++ b/MCGalaxy/Commands/SubCommand.cs @@ -21,25 +21,44 @@ permissions and limitations under the Licenses. namespace MCGalaxy.Commands { /// - /// Represents the name, behavior, and help text for a subcommand. Used with SubCommandGroup to offer a variety of subcommands to run based on user input. + /// Represents the name, behavior, and help for a subcommand. Used with SubCommandGroup to offer a variety of subcommands to run based on user input. /// public class SubCommand { public delegate void Behavior(Player p, string arg); + public delegate void HelpBehavior(Player p, string message); public readonly string Name; public readonly Behavior behavior; - string[] Help; + readonly HelpBehavior helpBehavior = null; readonly bool MapOnly; string[] Aliases; /// + /// Construct a SubCommand with simple style help. /// When mapOnly is true, the subcommand can only be used when the player is the realm owner. /// - public SubCommand(string name, Behavior behavior, string[] help, bool mapOnly = true, string[] aliases = null) { + public SubCommand(string name, Behavior behavior, string[] help, bool mapOnly = true, string[] aliases = null) + : this (name, behavior, mapOnly, aliases) + { + if (help != null && help.Length > 0) helpBehavior = (p, arg) => { p.MessageLines(help); }; + } + + /// + /// Construct a SubCommand with custom help behavior (e.g. this subcommand needs help that changes based on help args) + /// + public SubCommand(string name, Behavior behavior, HelpBehavior helpBehavior, bool mapOnly = true, string[] aliases = null) + : this(name, behavior, mapOnly, aliases) + { + this.helpBehavior = helpBehavior; + } + + /// + /// Construct a SubCommand without help + /// + public SubCommand(string name, Behavior behavior, bool mapOnly = true, string[] aliases = null) { Name = name; this.behavior = behavior; - Help = help; MapOnly = mapOnly; Aliases = aliases; } @@ -72,12 +91,12 @@ public bool Allowed(Player p, string parentCommandName) { return true; } - public void DisplayHelp(Player p) { - if (Help == null || Help.Length == 0) { + public void DisplayHelp(Player p, string message) { + if (helpBehavior == null) { p.Message("No help is available for {0}", Name); return; } - p.MessageLines(Help); + helpBehavior(p, message); } } @@ -137,14 +156,18 @@ public void DisplayAvailable(Player p) { p.Message("&HUse &T/Help {0} [command] &Hfor more details", parentCommandName); } - public void DisplayHelpFor(Player p, string subCmdName) { + public void DisplayHelpFor(Player p, string message) { + string[] words = message.SplitSpaces(2); + string subCmdName = words[0]; + string helpArgs = words.Length == 2 ? words[1] : ""; + foreach (SubCommand subCmd in subCommands) { if (!subCmd.Match(subCmdName)) { continue; } - subCmd.DisplayHelp(p); + subCmd.DisplayHelp(p, helpArgs); return; } - p.Message("There is no {0} command {1} to display help for.", parentCommandName, subCmdName); + p.Message("There is no {0} command \"{1}\".", parentCommandName, subCmdName); } } } diff --git a/MCGalaxy/Commands/World/CmdOverseer.cs b/MCGalaxy/Commands/World/CmdOverseer.cs index 2716f66b1..a73e36405 100644 --- a/MCGalaxy/Commands/World/CmdOverseer.cs +++ b/MCGalaxy/Commands/World/CmdOverseer.cs @@ -38,7 +38,6 @@ public override void Use(Player p, string message, CommandData data) { } public override void Help(Player p, string message) { - message = message.SplitSpaces()[0]; // only first argument Overseer.subCommandGroup.DisplayHelpFor(p, message); }