Skip to content

Commit

Permalink
Allow custom help behavior in SubCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
Goodlyay committed Aug 18, 2024
1 parent f18add8 commit 518a5c2
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 22 deletions.
22 changes: 11 additions & 11 deletions MCGalaxy/Commands/Overseer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SubCommand>() {
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" } ),
}
);

Expand Down Expand Up @@ -472,8 +472,8 @@ static void HandleZones(Player p, string raw) {

internal static SubCommandGroup deprecatedSubCommandGroup = new SubCommandGroup(commandShortcut,
new List<SubCommand>() {
new SubCommand("Zone", HandleZone, null),
new SubCommand("Zones", HandleZones, null),
new SubCommand("Zone", HandleZone , false),
new SubCommand("Zones", HandleZones, false),
}
);
}
Expand Down
43 changes: 33 additions & 10 deletions MCGalaxy/Commands/SubCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,44 @@ permissions and limitations under the Licenses.
namespace MCGalaxy.Commands {

/// <summary>
/// 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.
/// </summary>
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;

/// <summary>
/// Construct a SubCommand with simple style help.
/// When mapOnly is true, the subcommand can only be used when the player is the realm owner.
/// </summary>
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); };
}

/// <summary>
/// Construct a SubCommand with custom help behavior (e.g. this subcommand needs help that changes based on help args)
/// </summary>
public SubCommand(string name, Behavior behavior, HelpBehavior helpBehavior, bool mapOnly = true, string[] aliases = null)
: this(name, behavior, mapOnly, aliases)
{
this.helpBehavior = helpBehavior;
}

/// <summary>
/// Construct a SubCommand without help
/// </summary>
public SubCommand(string name, Behavior behavior, bool mapOnly = true, string[] aliases = null) {
Name = name;
this.behavior = behavior;
Help = help;
MapOnly = mapOnly;
Aliases = aliases;
}
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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);
}
}
}
1 change: 0 additions & 1 deletion MCGalaxy/Commands/World/CmdOverseer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit 518a5c2

Please sign in to comment.