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.Common;
using Vintagestory.API.Server; using Vintagestory.API.Server;
@@ -5,6 +6,43 @@ namespace ClaimLink;
public static class ClaimLinkChatCommand 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) public static void Register(ICoreServerAPI api)
{ {
var p = api.ChatCommands.Parsers; var p = api.ChatCommands.Parsers;
@@ -14,101 +52,80 @@ public static class ClaimLinkChatCommand
.WithDescription("Link vanilla land claims into shared, derived protective territory.") .WithDescription("Link vanilla land claims into shared, derived protective territory.")
.RequiresPrivilege(Privilege.chat); .RequiresPrivilege(Privilege.chat);
root.BeginSubCommands("new", "n") CommandSpec[] playerCommands =
.WithDescription("Promote a group you own into a claim link (enters pending state).") {
.WithArgs(p.Word("groupname")) new(new[] { "new", "n" }, "Promote a group you own into a claim link (enters pending state).",
.RequiresPlayer() new ICommandArgumentParser[] { p.Word("groupname") }, true, New),
.HandleWith(_ => TextCommandResult.Success("stub: claimlink new"))
.EndSubCommand();
root.BeginSubCommands("link", "l") new(new[] { "link", "l" }, "Link a claim you own into the pending or committed claim link.",
.WithDescription("Link a claim you own into the pending or committed claim link.") new ICommandArgumentParser[] { p.Word("claim") }, true, Link),
.WithArgs(p.Word("claim"))
.RequiresPlayer()
.HandleWith(_ => TextCommandResult.Success("stub: claimlink link"))
.EndSubCommand();
root.BeginSubCommands("confirm", "c") new(new[] { "confirm", "c" }, "Commit your pending claim link.",
.WithDescription("Commit your pending claim link.") Array.Empty<ICommandArgumentParser>(), true, Confirm),
.RequiresPlayer()
.HandleWith(_ => TextCommandResult.Success("stub: claimlink confirm"))
.EndSubCommand();
root.BeginSubCommand("cancel") new(new[] { "cancel" }, "Discard your pending claim link.",
.WithDescription("Discard your pending claim link.") Array.Empty<ICommandArgumentParser>(), true, Cancel),
.RequiresPlayer()
.HandleWith(_ => TextCommandResult.Success("stub: claimlink cancel"))
.EndSubCommand();
root.BeginSubCommands("unlink", "ul") new(new[] { "unlink", "ul" }, "Remove a claim you own from its claim link.",
.WithDescription("Remove a claim you own from its claim link.") new ICommandArgumentParser[] { p.Word("claim") }, true, Unlink),
.WithArgs(p.Word("claim"))
.RequiresPlayer()
.HandleWith(_ => TextCommandResult.Success("stub: claimlink unlink"))
.EndSubCommand();
root.BeginSubCommand("kick") new(new[] { "kick" }, "Force-unlink another player's claim from the link (Owner/Op).",
.WithDescription("Force-unlink another player's claim from the link (Owner/Op).") new ICommandArgumentParser[] { p.OnlinePlayer("playername"), p.Word("claim") }, true, Kick),
.WithArgs(p.OnlinePlayer("playername"), p.Word("claim"))
.RequiresPlayer()
.HandleWith(_ => TextCommandResult.Success("stub: claimlink kick"))
.EndSubCommand();
root.BeginSubCommand("delete") new(new[] { "delete" }, "Delete the claim link entirely (Owner).",
.WithDescription("Delete the claim link entirely (Owner).") new ICommandArgumentParser[] { p.Word("groupname") }, true, Delete),
.WithArgs(p.Word("groupname"))
.RequiresPlayer()
.HandleWith(_ => TextCommandResult.Success("stub: claimlink delete"))
.EndSubCommand();
root.BeginSubCommands("transferownership", "transfer", "to") new(new[] { "transferownership", "transfer", "to" }, "Transfer ownership of the claim link to another player (Owner).",
.WithDescription("Transfer ownership of the claim link to another player (Owner).") new ICommandArgumentParser[] { p.Word("groupname"), p.OnlinePlayer("playername") }, true, TransferOwnership),
.WithArgs(p.Word("groupname"), p.OnlinePlayer("playername"))
.RequiresPlayer()
.HandleWith(_ => TextCommandResult.Success("stub: claimlink transferownership"))
.EndSubCommand();
root.BeginSubCommands("info", "i") new(new[] { "info", "i" }, "Show a claim link's linked claims, members, and territory (Owner/Op or admin).",
.WithDescription("Show a claim link's linked claims, members, and territory (Owner/Op or admin).") new ICommandArgumentParser[] { p.Word("groupname") }, true, Info),
.WithArgs(p.Word("groupname")) };
.RequiresPlayer()
.HandleWith(_ => TextCommandResult.Success("stub: claimlink info")) foreach (var spec in playerCommands)
.EndSubCommand(); BuildSubCommand(root, spec);
var admin = root.BeginSubCommand("admin") var admin = root.BeginSubCommand("admin")
.WithDescription("Admin management for any claim link (op or console).") .WithDescription("Admin management for any claim link (op or console).")
.RequiresPrivilege(Privilege.controlserver); .RequiresPrivilege(Privilege.controlserver);
admin.BeginSubCommands("delete", "del") CommandSpec[] adminCommands =
.WithDescription("Delete any claim link entirely (claims and group untouched).") {
.WithArgs(p.Word("groupname")) new(new[] { "delete", "del" }, "Delete any claim link entirely (claims and group untouched).",
.HandleWith(_ => TextCommandResult.Success("stub: claimlink admin delete")) new ICommandArgumentParser[] { p.Word("groupname") }, false, AdminDelete),
.EndSubCommand();
admin.BeginSubCommand("unlink") new(new[] { "unlink" }, "Force a single claim out of its link by player and claim (claim untouched).",
.WithDescription("Force a single claim out of its link by player and claim (claim untouched).") new ICommandArgumentParser[] { p.OnlinePlayer("playername"), p.Word("claim") }, false, AdminUnlink),
.WithArgs(p.OnlinePlayer("playername"), p.Word("claim"))
.HandleWith(_ => TextCommandResult.Success("stub: claimlink admin unlink"))
.EndSubCommand();
admin.BeginSubCommand("kick") new(new[] { "kick" }, "Force-unlink all of a player's claims from a link (claims untouched).",
.WithDescription("Force-unlink all of a player's claims from a link (claims untouched).") new ICommandArgumentParser[] { p.Word("groupname"), p.OnlinePlayer("playername") }, false, AdminKick),
.WithArgs(p.Word("groupname"), p.OnlinePlayer("playername"))
.HandleWith(_ => TextCommandResult.Success("stub: claimlink admin kick"))
.EndSubCommand();
admin.BeginSubCommands("transferownership", "transfer", "to") new(new[] { "transferownership", "transfer", "to" }, "Transfer ownership of any claim link to another player.",
.WithDescription("Transfer ownership of any claim link to another player.") new ICommandArgumentParser[] { p.Word("groupname"), p.OnlinePlayer("playername") }, false, AdminTransferOwnership),
.WithArgs(p.Word("groupname"), p.OnlinePlayer("playername"))
.HandleWith(_ => TextCommandResult.Success("stub: claimlink admin transferownership"))
.EndSubCommand();
admin.BeginSubCommands("info", "i") new(new[] { "info", "i" }, "Show any claim link's linked claims, members, and territory.",
.WithDescription("Show any claim link's linked claims, members, and territory.") new ICommandArgumentParser[] { p.Word("groupname") }, false, AdminInfo),
.WithArgs(p.Word("groupname")) };
.HandleWith(_ => TextCommandResult.Success("stub: claimlink admin info"))
.EndSubCommand(); foreach (var spec in adminCommands)
BuildSubCommand(admin, spec);
admin.EndSubCommand(); 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");
} }