Core Concepts - LilBroCodes/commander GitHub Wiki

Core Concepts of Commander

Commander is built around a few key concepts that make it powerful and flexible. Understanding these concepts will help you make the most of the library.

Command Tree Structure

Commander uses a tree-based approach to command organization. This hierarchical structure allows for intuitive command organization and navigation.

The Command Tree

At the root of every Commander command is a CommandGroupNode. This node serves as the entry point for your command and can contain multiple child nodes, which can be either:

  • More CommandGroupNode instances (for nested subcommands)
  • CommandActionNode instances (for executable commands with parameters)
  • CommandHybridNode instances (a combination that can both execute and contain subcommands)

This tree structure allows you to create complex command hierarchies like:

/mycommand
├─ subcommand1
│  ├─ action1 <param1> <param2>
│  └─ action2 <param1>
└─ subcommand2
   └─ action3 <param1>

Node Types

Commander provides three main types of nodes for building your command tree:

CommandGroupNode

A CommandGroupNode is a parent node that can contain multiple child nodes but doesn't execute any command logic itself. It's used to create command categories and organize subcommands.

When a user executes a command that corresponds to a CommandGroupNode, Commander will look for a subcommand in the arguments. If no subcommand is provided, it will display an error message suggesting the user try the help command.

CommandActionNode

A CommandActionNode is a leaf node that executes command logic based on typed parameters. It doesn't have any child nodes.

When a user executes a command that corresponds to a CommandActionNode, Commander will parse and validate the provided arguments according to the expected parameter types, then execute the command logic with the parsed values.

CommandHybridNode

A CommandHybridNode is a combination of both CommandGroupNode and CommandActionNode. It can both execute command logic and contain child nodes.

When a user executes a command that corresponds to a CommandHybridNode:

  • If no additional arguments are provided, it will execute its command logic
  • If additional arguments are provided, it will treat them as subcommands and delegate to the appropriate child node

This is useful for commands that can both perform an action and have subcommands, like a /home command that teleports the player to their default home when used alone, but can also accept subcommands like /home set or /home delete.

Typed Parameters

Commander uses a typed parameter system to automatically parse and validate command arguments. This system provides several benefits:

  1. Type Safety: Arguments are automatically converted to the appropriate Java types
  2. Validation: Invalid arguments are rejected with helpful error messages
  3. Tab Completion: Parameter types can provide suggestions for tab completion

Each parameter is defined with a TypedParameter object that specifies:

  • The parameter name (used in help messages)
  • The parameter type (from the ParameterType enum)
  • Optionally, a supplier function that provides tab completion suggestions

Automatic Help System

Commander automatically generates help commands for your command tree. When a user types /yourcommand help, they'll see a list of all available subcommands with their descriptions.

For a more detailed view, users can type /yourcommand help tree to see the full command tree structure.

The help system is context-aware and only shows commands that the user has permission to use.

Permissions

Commander has a built-in permission system that allows you to restrict access to commands. By default, each command node uses a permission in the format pluginName.commandName.

You can customize the permission for any node using the withPermission() method:

CommandActionNode myCommand = new CommandActionNode("mycommand", "Description", "MyPlugin", params, executor)
    .withPermission("myplugin.custom.permission");

Commander will automatically check permissions before executing commands or showing them in help listings.

Next Steps

Now that you understand the core concepts of Commander, check out these pages for more detailed information:

⚠️ **GitHub.com Fallback** ⚠️