Core Concepts - LilBroCodes/commander GitHub Wiki
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.
Commander uses a tree-based approach to command organization. This hierarchical structure allows for intuitive command organization and navigation.
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
CommandGroupNodeinstances (for nested subcommands) -
CommandActionNodeinstances (for executable commands with parameters) -
CommandHybridNodeinstances (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>
Commander provides three main types of nodes for building your command tree:
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.
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.
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.
Commander uses a typed parameter system to automatically parse and validate command arguments. This system provides several benefits:
- Type Safety: Arguments are automatically converted to the appropriate Java types
- Validation: Invalid arguments are rejected with helpful error messages
- 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
ParameterTypeenum) - Optionally, a supplier function that provides tab completion suggestions
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.
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.
Now that you understand the core concepts of Commander, check out these pages for more detailed information:
- Command Nodes - Learn more about the different types of command nodes
- Parameter Types - Explore the parameter types and validation
- Tab Completion - Implement tab completion for your commands