refactor: drive subcommand registration from a data table

This commit is contained in:
2026-07-11 17:55:32 +02:00
parent 28a89056e6
commit 7d5deb5bdf
+94 -77
View File
@@ -1,3 +1,4 @@
using System;
using Vintagestory.API.Common;
using Vintagestory.API.Server;
@@ -5,6 +6,43 @@ namespace ClaimLink;
public static class ClaimLinkChatCommand
{
private readonly struct CommandSpec
{
public readonly string[] Names;
public readonly string Description;
public readonly ICommandArgumentParser[] Args;
public readonly bool IsPlayerOnly;
public readonly OnCommandDelegate Handler;
public CommandSpec(string[] names, string description, ICommandArgumentParser[] args, bool isPlayerOnly, OnCommandDelegate handler)
{
Names = names;
Description = description;
Args = args;
IsPlayerOnly = isPlayerOnly;
Handler = handler;
}
}
private static void BuildSubCommand(IChatCommand parent, CommandSpec spec)
{
var sub = spec.Names.Length > 1
? parent.BeginSubCommands(spec.Names)
: parent.BeginSubCommand(spec.Names[0]);
sub.WithDescription(spec.Description);
if (spec.Args.Length > 0)
sub.WithArgs(spec.Args);
if (spec.IsPlayerOnly)
sub.RequiresPlayer();
else
sub.RequiresPrivilege(Privilege.controlserver);
sub.HandleWith(spec.Handler).EndSubCommand();
}
public static void Register(ICoreServerAPI api)
{
var p = api.ChatCommands.Parsers;
@@ -14,101 +52,80 @@ public static class ClaimLinkChatCommand
.WithDescription("Link vanilla land claims into shared, derived protective territory.")
.RequiresPrivilege(Privilege.chat);
root.BeginSubCommands("new", "n")
.WithDescription("Promote a group you own into a claim link (enters pending state).")
.WithArgs(p.Word("groupname"))
.RequiresPlayer()
.HandleWith(_ => TextCommandResult.Success("stub: claimlink new"))
.EndSubCommand();
CommandSpec[] playerCommands =
{
new(new[] { "new", "n" }, "Promote a group you own into a claim link (enters pending state).",
new ICommandArgumentParser[] { p.Word("groupname") }, true, New),
root.BeginSubCommands("link", "l")
.WithDescription("Link a claim you own into the pending or committed claim link.")
.WithArgs(p.Word("claim"))
.RequiresPlayer()
.HandleWith(_ => TextCommandResult.Success("stub: claimlink link"))
.EndSubCommand();
new(new[] { "link", "l" }, "Link a claim you own into the pending or committed claim link.",
new ICommandArgumentParser[] { p.Word("claim") }, true, Link),
root.BeginSubCommands("confirm", "c")
.WithDescription("Commit your pending claim link.")
.RequiresPlayer()
.HandleWith(_ => TextCommandResult.Success("stub: claimlink confirm"))
.EndSubCommand();
new(new[] { "confirm", "c" }, "Commit your pending claim link.",
Array.Empty<ICommandArgumentParser>(), true, Confirm),
root.BeginSubCommand("cancel")
.WithDescription("Discard your pending claim link.")
.RequiresPlayer()
.HandleWith(_ => TextCommandResult.Success("stub: claimlink cancel"))
.EndSubCommand();
new(new[] { "cancel" }, "Discard your pending claim link.",
Array.Empty<ICommandArgumentParser>(), true, Cancel),
root.BeginSubCommands("unlink", "ul")
.WithDescription("Remove a claim you own from its claim link.")
.WithArgs(p.Word("claim"))
.RequiresPlayer()
.HandleWith(_ => TextCommandResult.Success("stub: claimlink unlink"))
.EndSubCommand();
new(new[] { "unlink", "ul" }, "Remove a claim you own from its claim link.",
new ICommandArgumentParser[] { p.Word("claim") }, true, Unlink),
root.BeginSubCommand("kick")
.WithDescription("Force-unlink another player's claim from the link (Owner/Op).")
.WithArgs(p.OnlinePlayer("playername"), p.Word("claim"))
.RequiresPlayer()
.HandleWith(_ => TextCommandResult.Success("stub: claimlink kick"))
.EndSubCommand();
new(new[] { "kick" }, "Force-unlink another player's claim from the link (Owner/Op).",
new ICommandArgumentParser[] { p.OnlinePlayer("playername"), p.Word("claim") }, true, Kick),
root.BeginSubCommand("delete")
.WithDescription("Delete the claim link entirely (Owner).")
.WithArgs(p.Word("groupname"))
.RequiresPlayer()
.HandleWith(_ => TextCommandResult.Success("stub: claimlink delete"))
.EndSubCommand();
new(new[] { "delete" }, "Delete the claim link entirely (Owner).",
new ICommandArgumentParser[] { p.Word("groupname") }, true, Delete),
root.BeginSubCommands("transferownership", "transfer", "to")
.WithDescription("Transfer ownership of the claim link to another player (Owner).")
.WithArgs(p.Word("groupname"), p.OnlinePlayer("playername"))
.RequiresPlayer()
.HandleWith(_ => TextCommandResult.Success("stub: claimlink transferownership"))
.EndSubCommand();
new(new[] { "transferownership", "transfer", "to" }, "Transfer ownership of the claim link to another player (Owner).",
new ICommandArgumentParser[] { p.Word("groupname"), p.OnlinePlayer("playername") }, true, TransferOwnership),
root.BeginSubCommands("info", "i")
.WithDescription("Show a claim link's linked claims, members, and territory (Owner/Op or admin).")
.WithArgs(p.Word("groupname"))
.RequiresPlayer()
.HandleWith(_ => TextCommandResult.Success("stub: claimlink info"))
.EndSubCommand();
new(new[] { "info", "i" }, "Show a claim link's linked claims, members, and territory (Owner/Op or admin).",
new ICommandArgumentParser[] { p.Word("groupname") }, true, Info),
};
foreach (var spec in playerCommands)
BuildSubCommand(root, spec);
var admin = root.BeginSubCommand("admin")
.WithDescription("Admin management for any claim link (op or console).")
.RequiresPrivilege(Privilege.controlserver);
admin.BeginSubCommands("delete", "del")
.WithDescription("Delete any claim link entirely (claims and group untouched).")
.WithArgs(p.Word("groupname"))
.HandleWith(_ => TextCommandResult.Success("stub: claimlink admin delete"))
.EndSubCommand();
CommandSpec[] adminCommands =
{
new(new[] { "delete", "del" }, "Delete any claim link entirely (claims and group untouched).",
new ICommandArgumentParser[] { p.Word("groupname") }, false, AdminDelete),
admin.BeginSubCommand("unlink")
.WithDescription("Force a single claim out of its link by player and claim (claim untouched).")
.WithArgs(p.OnlinePlayer("playername"), p.Word("claim"))
.HandleWith(_ => TextCommandResult.Success("stub: claimlink admin unlink"))
.EndSubCommand();
new(new[] { "unlink" }, "Force a single claim out of its link by player and claim (claim untouched).",
new ICommandArgumentParser[] { p.OnlinePlayer("playername"), p.Word("claim") }, false, AdminUnlink),
admin.BeginSubCommand("kick")
.WithDescription("Force-unlink all of a player's claims from a link (claims untouched).")
.WithArgs(p.Word("groupname"), p.OnlinePlayer("playername"))
.HandleWith(_ => TextCommandResult.Success("stub: claimlink admin kick"))
.EndSubCommand();
new(new[] { "kick" }, "Force-unlink all of a player's claims from a link (claims untouched).",
new ICommandArgumentParser[] { p.Word("groupname"), p.OnlinePlayer("playername") }, false, AdminKick),
admin.BeginSubCommands("transferownership", "transfer", "to")
.WithDescription("Transfer ownership of any claim link to another player.")
.WithArgs(p.Word("groupname"), p.OnlinePlayer("playername"))
.HandleWith(_ => TextCommandResult.Success("stub: claimlink admin transferownership"))
.EndSubCommand();
new(new[] { "transferownership", "transfer", "to" }, "Transfer ownership of any claim link to another player.",
new ICommandArgumentParser[] { p.Word("groupname"), p.OnlinePlayer("playername") }, false, AdminTransferOwnership),
admin.BeginSubCommands("info", "i")
.WithDescription("Show any claim link's linked claims, members, and territory.")
.WithArgs(p.Word("groupname"))
.HandleWith(_ => TextCommandResult.Success("stub: claimlink admin info"))
.EndSubCommand();
new(new[] { "info", "i" }, "Show any claim link's linked claims, members, and territory.",
new ICommandArgumentParser[] { p.Word("groupname") }, false, AdminInfo),
};
foreach (var spec in adminCommands)
BuildSubCommand(admin, spec);
admin.EndSubCommand();
}
public static TextCommandResult New(TextCommandCallingArgs args) => TextCommandResult.Success("stub: claimlink new");
public static TextCommandResult Link(TextCommandCallingArgs args) => TextCommandResult.Success("stub: claimlink link");
public static TextCommandResult Confirm(TextCommandCallingArgs args) => TextCommandResult.Success("stub: claimlink confirm");
public static TextCommandResult Cancel(TextCommandCallingArgs args) => TextCommandResult.Success("stub: claimlink cancel");
public static TextCommandResult Unlink(TextCommandCallingArgs args) => TextCommandResult.Success("stub: claimlink unlink");
public static TextCommandResult Kick(TextCommandCallingArgs args) => TextCommandResult.Success("stub: claimlink kick");
public static TextCommandResult Delete(TextCommandCallingArgs args) => TextCommandResult.Success("stub: claimlink delete");
public static TextCommandResult TransferOwnership(TextCommandCallingArgs args) => TextCommandResult.Success("stub: claimlink transferownership");
public static TextCommandResult Info(TextCommandCallingArgs args) => TextCommandResult.Success("stub: claimlink info");
public static TextCommandResult AdminDelete(TextCommandCallingArgs args) => TextCommandResult.Success("stub: claimlink admin delete");
public static TextCommandResult AdminUnlink(TextCommandCallingArgs args) => TextCommandResult.Success("stub: claimlink admin unlink");
public static TextCommandResult AdminKick(TextCommandCallingArgs args) => TextCommandResult.Success("stub: claimlink admin kick");
public static TextCommandResult AdminTransferOwnership(TextCommandCallingArgs args) => TextCommandResult.Success("stub: claimlink admin transferownership");
public static TextCommandResult AdminInfo(TextCommandCallingArgs args) => TextCommandResult.Success("stub: claimlink admin info");
}