Class MagicCommand
- Direct Known Subclasses:
DynamicCommand, HelpCommandBase, MagicUtilsBundleCommand
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classBuilder API that produces a concreteMagicCommand.static classHolds information about a subcommand and its method. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionAdd an alias at runtime.addSubCommand(SubCommandSpec<?> subCommand) Add a dynamic subcommand (builder API).addSubCommand(SubCommandSpec<?> subCommand, boolean replaceExisting) Add a dynamic subcommand with explicit replace flag.static <S> MagicCommand.Builder<S> Creates a builder-backed command definition.copy()Creates an unfrozen duplicate of this command's declarative state so it can be modified and re-registered after the original was frozen by registration.static MethodfindExecuteMethod(Class<?> clazz) Gets the direct execute method from a command class.static MagicCommandfromSpec(CommandSpec<?> spec) Wraps a compatibility command spec into a runtimeMagicCommand.static List<CommandArgument> getArguments(Method method) Gets all arguments for a subcommand method.static Optional<CommandInfo> getCommandInfo(Class<?> clazz) Gets the CommandInfo annotation from a class.static List<MagicCommand.SubCommandInfo> getSubCommands(Class<?> clazz) Gets all subcommands from a command class.static StringGets the usage string for a command class.final booleanisFrozen()Returns whether this command is frozen for runtime use.mergeSubCommands(MagicCommand command, Consumer<String> warn) Mergescommand's sub-commands into this one so several plugins can contribute to a shared tree (e.g.mount(MagicCommand command) Mounts another command tree under this command using the child's own root label.mount(String route, MagicCommand command) Mounts another command tree under this command using a parent-local route label.mountSubCommands(MagicCommand command) Adds another command's sub-commands directly into this command tree, at the same level — without nesting them under the source command's name the waymount(MagicCommand)does.overrideInfo(CommandInfo original) Apply runtime overrides to a CommandInfo annotation.removeAlias(String alias) Remove an alias (by exact match, case-insensitive) at runtime.final CommandInfoResolves the effective command metadata after runtime overrides.setExecute(CommandExecutor<?> executor, CommandArgument... arguments) Define a dynamic execute handler (builder API).setExecute(CommandExecutor<?> executor, CommandThreading threading, CommandArgument... arguments) Define a dynamic execute handler (builder API) with explicit threading policy.setExecute(CommandExecutor<?> executor, List<CommandArgument> arguments) Define a dynamic execute handler (builder API).setExecute(CommandExecutor<?> executor, List<CommandArgument> arguments, boolean replaceExisting) Define a dynamic execute handler (builder API).setExecute(CommandExecutor<?> executor, List<CommandArgument> arguments, CommandThreading threading) Define a dynamic execute handler (builder API) with explicit threading policy.setExecute(CommandExecutor<?> executor, List<CommandArgument> arguments, CommandThreading threading, boolean replaceExisting) Define a dynamic execute handler (builder API).withInfo(CommandInfo info) Override full command info at runtime (builder API).Override command name at runtime.
-
Constructor Details
-
MagicCommand
public MagicCommand()Default constructor for MagicCommand.
-
-
Method Details
-
getCommandInfo
Gets the CommandInfo annotation from a class.- Parameters:
clazz- the command class- Returns:
- an Optional containing CommandInfo if present
-
builder
Creates a builder-backed command definition.- Type Parameters:
S- sender type- Parameters:
name- command name- Returns:
- builder instance
-
fromSpec
Wraps a compatibility command spec into a runtimeMagicCommand.- Parameters:
spec- command spec- Returns:
- built command
-
resolveInfo
Resolves the effective command metadata after runtime overrides.- Returns:
- effective command info or
nullif this command has no metadata
-
isFrozen
public final boolean isFrozen()Returns whether this command is frozen for runtime use.- Returns:
- true when mutations are no longer allowed
-
withName
Override command name at runtime.- Parameters:
name- new name- Returns:
- this
-
withInfo
Override full command info at runtime (builder API).- Parameters:
info- command metadata- Returns:
- this
-
addAlias
Add an alias at runtime.- Parameters:
alias- alias to add- Returns:
- this
-
removeAlias
Remove an alias (by exact match, case-insensitive) at runtime.- Parameters:
alias- alias to remove- Returns:
- this
-
mount
Mounts another command tree under this command using the child's own root label.The mounted command is snapshotted into dynamic subcommands. Metadata and route structure are copied at mount time, while execution still targets the mounted command instance.
- Parameters:
command- command tree to mount- Returns:
- this command
-
mount
Mounts another command tree under this command using a parent-local route label.This overrides the mounted route segment without renaming the source command definition itself. When a custom route is used, root aliases from the mounted command are not exposed automatically.
- Parameters:
route- route label inside this command treecommand- command tree to mount- Returns:
- this command
-
mountSubCommands
Adds another command's sub-commands directly into this command tree, at the same level — without nesting them under the source command's name the waymount(MagicCommand)does.Where
mount(cmd)produces/root <cmd-name> <sub>, this produces/root <sub>. Use it to let a plugin contribute top-level sub-commands to a shared root command it does not own: define them as@SubCommandmethods on a carrier class, then flat-mount that carrier into a copy of the root and re-register. The source command's own directexecute(its bare-name action) is intentionally not carried over — only its sub-commands are.- Parameters:
command- the command whose sub-commands to graft in at this level- Returns:
- this command
-
mergeSubCommands
Mergescommand's sub-commands into this one so several plugins can contribute to a shared tree (e.g. each adding under/nox) without clobbering each other. UnlikemountSubCommands(MagicCommand), which blindly appends, this deduplicates by (path + name): when a node already exists, ownership of its execute is decided byMergePolicy—MergePolicy.CONTRIBUTEkeeps the existing handler (put-if-absent),MergePolicy.OVERRIDEreplaces it (loggingwarnif the existing one was itself an explicit override), andMergePolicy.EXTENDnever claims the node. Leaf nodes that don't collide are simply added.- Parameters:
command- the carrier whose sub-commands to merge inwarn- sink for a one-line conflict warning (may benull)- Returns:
- this command
-
addSubCommand
Add a dynamic subcommand (builder API).- Parameters:
subCommand- subcommand spec- Returns:
- this
-
addSubCommand
Add a dynamic subcommand with explicit replace flag.- Parameters:
subCommand- subcommand specreplaceExisting- whether to override matching subcommands- Returns:
- this
-
setExecute
public MagicCommand setExecute(CommandExecutor<?> executor, List<CommandArgument> arguments, boolean replaceExisting) Define a dynamic execute handler (builder API).- Parameters:
executor- command executorarguments- argument definitionsreplaceExisting- whether to replace annotated execute method- Returns:
- this
-
setExecute
public MagicCommand setExecute(CommandExecutor<?> executor, List<CommandArgument> arguments, CommandThreading threading, boolean replaceExisting) Define a dynamic execute handler (builder API).- Parameters:
executor- command executorarguments- argument definitionsthreading- threading policyreplaceExisting- whether to replace annotated execute method- Returns:
- this
-
setExecute
Define a dynamic execute handler (builder API).- Parameters:
executor- command executorarguments- argument definitions- Returns:
- this
-
setExecute
public MagicCommand setExecute(CommandExecutor<?> executor, List<CommandArgument> arguments, CommandThreading threading) Define a dynamic execute handler (builder API) with explicit threading policy.- Parameters:
executor- command executorarguments- argument definitionsthreading- threading policy- Returns:
- this
-
setExecute
Define a dynamic execute handler (builder API).- Parameters:
executor- command executorarguments- argument definitions- Returns:
- this
-
setExecute
public MagicCommand setExecute(CommandExecutor<?> executor, CommandThreading threading, CommandArgument... arguments) Define a dynamic execute handler (builder API) with explicit threading policy.- Parameters:
executor- command executorthreading- threading policyarguments- argument definitions- Returns:
- this
-
overrideInfo
Apply runtime overrides to a CommandInfo annotation.- Parameters:
original- original info- Returns:
- overridden info
-
copy
Creates an unfrozen duplicate of this command's declarative state so it can be modified and re-registered after the original was frozen by registration.Registration
freezesa command, after whichmount(MagicCommand)and the other mutators throw. This is the supported way to add sub-commands to an already-registered command tree: copy it, mount into the copy, and register the copy under the same name (registration replaces the previous owner by name). Because a copy preserves themountedsub-commands of the source, multiple contributors can each copy-mount-register in turn and their additions accumulate.The copy is a
DynamicCommandcarrying the source's effectiveinfo, name/alias overrides, dynamic sub-commands (which is wheremount(MagicCommand)records mounted trees) and dynamic execute. Sub-commands declared as@SubCommandmethods on a concrete subclass are part of that subclass, not of this declarative state; a subclass that carries such methods and needs them preserved across a copy should override this method to return an instance of its own type. Commands built from a spec/builder or assembled purely bymount(MagicCommand)— such as an aggregate root command — copy losslessly with the default implementation.- Returns:
- an unfrozen copy that can be mutated and re-registered
-
getSubCommands
Gets all subcommands from a command class.- Parameters:
clazz- the command class- Returns:
- a list of SubCommandInfo
-
findExecuteMethod
-
getArguments
Gets all arguments for a subcommand method.- Parameters:
method- the subcommand method- Returns:
- a list of CommandArgument
-
getUsage
-