rubenjisan / jisancommandbuilder
Libreria para crear comandos con subcomandos de forma rapida
Requires
- php: ^8.1
Requires (Dev)
- axolotl-pm/pocketmine-mp: ^5.51.0
- sof3/pharynx: ^0.3.6
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
A lightweight library for building commands with nested subcommands, typed arguments, permissions and automatic help using a fluent API. It sits on top of PocketMine's command map, so there is nothing to configure outside your plugin: you describe the command tree in code and the library handles parsing, validation and error messages.
Built for the plugins of the SlimeGames network, with a focus on what server owners and plugin developers actually need: predictable argument parsing, permission-aware help and error messages that tell players exactly what went wrong.
Table of contents
- Features
- Engineering highlights
- How it works
- Installation
- Quick start
- Argument types
- CommandContext API
- Permissions
- Automatic help
- Customizing messages
- Class-based commands
- API overview
- Project structure
- Troubleshooting
- Author
- License
Features
| # | Feature | Description |
|---|---|---|
| 1 | Fluent command builder | Define a command, its aliases, description, permission and behavior in a single chained expression |
| 2 | Nested subcommands | Every node can have its own subcommands, aliases, permission, arguments and executor, at any depth |
| 3 | Typed arguments | STRING, INT, FLOAT, BOOL, PLAYER and TEXT, validated and converted before your code runs |
| 4 | Optional arguments | Optional arguments with default values; the builder rejects invalid orderings at definition time |
| 5 | Permissions | Permissions are registered automatically with a default of op, user or console and checked at every level of the tree |
| 6 | Player-only nodes | Mark any node with playerOnly() and console senders are rejected with a clear message |
| 7 | Automatic help | /command and /command help list the available subcommands, hiding those the sender can't use |
| 8 | Customizable messages | Every user-facing text lives in Messages and supports {placeholders} |
| 9 | Class-based commands | ICommand, ISubCommand and IExecutable let you keep each command in its own class |
Engineering highlights
- Validation before execution. Arguments are parsed and converted first; your executor only runs with values that are already valid. Failures produce a specific message (missing argument, wrong type, unknown player, too many arguments) together with the correct usage line.
- Errors caught at definition time. A
TEXTargument that isn't last, or a required argument placed after an optional one, throws anInvalidArgumentExceptionwhen the command is built, not when a player runs it. - Permission-aware at every level. Each node checks its own permission before dispatching, and the help screen only lists subcommands the sender is allowed to use.
- Self-registering permissions.
permission()creates the permission node if it doesn't exist and attaches it to the operator, user or console root, so you don't need to declare it inplugin.yml. - Subcommand aliases. Subcommands can define aliases that resolve to the same node, case-insensitively.
- Embeddable as a virion. Distributed as a Composer library: it is compiled into each plugin with a renamed namespace, so two plugins can ship different versions without conflicts and customize
Messagesindependently.
How it works
flowchart TD
A["Player runs /clan create Foo"] --> B["JisanCommand.execute"]
B --> C["RootNode.run"]
C --> D{"Has permission?"}
D -->|no| X["No-permission message"]
D -->|yes| E{"First argument is a subcommand or alias?"}
E -->|yes| F["Child node.run (recursive)"]
E -->|no| G{"playerOnly and sender is console?"}
G -->|yes| Y["Player-only message"]
G -->|no| H{"Has an executor?"}
H -->|no| I["Show help"]
H -->|yes| J["Parse and convert arguments"]
J -->|invalid| Z["Specific error + usage"]
J -->|valid| K["Run executor with CommandContext"]
Loading
Installation
Requirements
- PHP 8.1 or later
- PocketMine-MP 5 or AxolotlPM 5 (API
5.0.0) - Composer, and Pharynx to build your plugin
With Composer (Packagist)
composer require rubenjisan/jisancommandbuilder
Keep PocketMine/AxolotlPM and Pharynx as development dependencies of your plugin so they are not bundled into your .phar:
{
"require": {
"rubenjisan/jisancommandbuilder": "^0.1"
},
"require-dev": {
"axolotl-pm/pocketmine-mp": "^5.51.0",
"sof3/pharynx": "^0.3.6"
}
}
Directly from GitHub
If you prefer not to use Packagist, add the repository to your plugin's composer.json:
{
"repositories": [
{ "type": "vcs", "url": "https://github.com/RubenJisan/JisanCommandBuilder" }
]
}
Then run composer require rubenjisan/jisancommandbuilder.
Building your plugin
Build your plugin with Pharynx so the library is embedded with a renamed namespace:
php -dphar.readonly=0 vendor/bin/pharynx -i=. -c -p=MyPlugin.phar
Quick start
<?php declare(strict_types=1); namespace myplugin; use pocketmine\plugin\PluginBase; use rubenjisan\JisanCommandBuilder\arguments\ArgType; use rubenjisan\JisanCommandBuilder\commands\CommandBuilder; use rubenjisan\JisanCommandBuilder\commands\CommandContext; use rubenjisan\JisanCommandBuilder\commands\commandTree\CommandNode; class Main extends PluginBase { protected function onEnable(): void { CommandBuilder::create($this, "clan", "Manage clans") ->aliases(["c"]) ->permission("myplugin.clan", "user") ->subcommand("create", function(CommandNode $n): void { $n->description("Create a clan") ->playerOnly() ->argument("name", ArgType::STRING) ->executes(function(CommandContext $ctx): void { $ctx->reply("Clan created: " . $ctx->getString("name")); }); }) ->subcommand("invite", function(CommandNode $n): void { $n->description("Invite a player") ->argument("player", ArgType::PLAYER) ->executes(function(CommandContext $ctx): void { $ctx->reply("You invited " . $ctx->getTarget("player")?->getName()); }); }) ->register(); } }
register() adds the command to the server's command map under your plugin's name and returns the registered JisanCommand.
Optional arguments and free text
->subcommand("kick", function(CommandNode $n): void { $n->description("Kick a member") ->permission("myplugin.clan.kick") ->argument("player", ArgType::PLAYER) ->argument("reason", ArgType::TEXT, false, "No reason given") ->executes(function(CommandContext $ctx): void { $ctx->reply($ctx->getTarget("player")?->getName() . ": " . $ctx->getString("reason")); }); })
Nested subcommands
->subcommand("admin", function(CommandNode $n): void { $n->description("Administration tools") ->permission("myplugin.clan.admin") ->subcommand("disband", function(CommandNode $s): void { $s->description("Disband a clan") ->argument("name", ArgType::STRING) ->executes(fn(CommandContext $ctx) => $ctx->reply("Disbanded " . $ctx->getString("name"))); }); })
Argument types
| Type | Accepts | Value in CommandContext |
|---|---|---|
ArgType::STRING |
A single word | string |
ArgType::INT |
A valid integer | int |
ArgType::FLOAT |
Any numeric value | float |
ArgType::BOOL |
true, 1, on, si, sí, yes, y / false, 0, off, no, n |
bool |
ArgType::PLAYER |
Name or name prefix of an online player | Player (use getTarget()) |
ArgType::TEXT |
The rest of the line, spaces included. Must be the last argument | string |
Arguments are declared with:
->argument(string $name, ArgType $type = ArgType::STRING, bool $required = true, mixed $default = null)
CommandContext API
Your executor receives a CommandContext with the sender and the parsed values.
| Method | Returns | Description |
|---|---|---|
getSender() |
CommandSender |
Whoever ran the command |
getLabel() |
string |
The full command path that was used, e.g. clan create |
getPlayer() |
Player |
The sender as a player. Throws LogicException if it isn't one, so mark the node with playerOnly() |
has($name) |
bool |
Whether the argument has a non-null value |
get($name, $default) |
mixed |
Raw value of an argument |
getString($name, $default) |
string |
Argument as a string |
getInt($name, $default) |
int |
Argument as an integer |
getFloat($name, $default) |
float |
Argument as a float |
getBool($name, $default) |
bool |
Argument as a boolean |
getTarget($name) |
?Player |
The resolved player for a PLAYER argument |
reply($message) |
void |
Sends a message to the sender |
Permissions
->permission("myplugin.clan", "user")
The second parameter sets who has the permission by default:
| Value | Granted to |
|---|---|
"op" (default) |
Operators |
"user", "true", "all" |
Everyone |
"console" |
The console only |
- If the permission already exists, it is left untouched.
- Permissions are checked on every node as the command is dispatched, so a restricted subcommand can live under a public root command.
- Permissions are registered when
permission()is called, so define them inonEnable()before the command is registered.
Automatic help
Nodes with subcommands show help automatically:
--- Help for /clan ---
/clan create <name> - Create a clan
/clan invite <player> - Invite a player
/clan kick <player> [reason] - Kick a member
- Help is shown with
/clan, when the node has no executor, or with/clan help. - Subcommands the sender has no permission for are hidden.
- Usage lines are generated from the declared arguments:
<name>for required,[name]for optional.
Customizing messages
Every text sent by the library is a public static property of Messages. Override any of them in onEnable():
use pocketmine\utils\TextFormat; use rubenjisan\JisanCommandBuilder\utils\Messages; Messages::$noPermission = TextFormat::RED . "You can't use this command."; Messages::$missingArg = TextFormat::RED . "Missing <{arg}>. Usage: {usage}";
| Property | Placeholders |
|---|---|
$noPermission |
— |
$playerOnly |
— |
$missingArg |
{arg}, {usage} |
$tooManyArgs |
{usage} |
$invalidInt |
{arg}, {value} |
$invalidFloat |
{arg}, {value} |
$invalidBool |
{arg}, {value} |
$playerNotFound |
{value} |
$helpHeader |
{command} |
$helpLine |
{usage}, {description} |
Class-based commands
To keep large command trees organized, each subcommand can live in its own class by implementing ISubCommand:
use rubenjisan\JisanCommandBuilder\arguments\ArgType; use rubenjisan\JisanCommandBuilder\commands\CommandContext; use rubenjisan\JisanCommandBuilder\commands\ISubCommand; use rubenjisan\JisanCommandBuilder\commands\commandTree\CommandNode; final class ClanCreate implements ISubCommand { public static function register(CommandNode $node): void { $node->description("Create a clan") ->playerOnly() ->argument("name", ArgType::STRING) ->executes(self::onExecute(...)); } public static function onExecute(CommandContext $context): void { $context->reply("Clan created: " . $context->getString("name")); } }
Then attach it with a first-class callable:
CommandBuilder::create($this, "clan", "Manage clans") ->subcommand("create", ClanCreate::register(...)) ->register();
| Interface | Contract |
|---|---|
IExecutable |
public static function onExecute(CommandContext $context): void |
ISubCommand |
Extends IExecutable; adds public static function register(CommandNode $node): void |
ICommand |
Extends IExecutable; adds public static function register(PluginBase $plugin): void |
API overview
| Class | Namespace | Purpose |
|---|---|---|
CommandBuilder |
commands |
Entry point: CommandBuilder::create($plugin, $name, $description) |
RootNode |
commands\commandTree |
Root of the tree; register() adds it to the command map |
CommandNode |
commands\commandTree |
A node: description, permission, aliases, playerOnly, argument, executes, subcommand |
JisanCommand |
commands |
The PocketMine Command that delegates to the tree |
CommandContext |
commands |
Sender and parsed values passed to executors |
ArgType |
arguments |
Enum of argument types |
Argument |
arguments |
Immutable argument definition |
ArgumentException |
arguments |
Thrown internally on invalid values; reported to the sender |
Messages |
utils |
All customizable texts and the format() helper |
PermissionHelper |
utils |
Registers permissions with a default scope |
Utils |
utils |
getRunnableCommand() replaces {player}, {x}, {y}, {z} and {world} in a command string |
All namespaces are under rubenjisan\JisanCommandBuilder\.
Project structure
JisanCommandBuilder/
├── composer.json
├── README.md
├── LICENSE
└── src/rubenjisan/JisanCommandBuilder/
├── arguments/
│ ├── ArgType.php
│ ├── Argument.php
│ └── ArgumentException.php
├── commands/
│ ├── CommandBuilder.php
│ ├── CommandContext.php
│ ├── JisanCommand.php
│ ├── ICommand.php
│ ├── ISubCommand.php
│ ├── IExecutable.php
│ └── commandTree/
│ ├── CommandNode.php
│ └── RootNode.php
└── utils/
├── Messages.php
├── PermissionHelper.php
└── Utils.php
Troubleshooting
| Symptom | Cause and fix |
|---|---|
El argumento TEXT debe ser el ultimo exception on startup |
A TEXT argument was followed by another argument. Move it to the end of the node |
No puedes poner un argumento obligatorio despues de uno opcional exception |
Declare required arguments before optional ones |
LogicException when calling getPlayer() |
The node can be run from the console. Add playerOnly() to the node |
getTarget() returns null |
The argument isn't of type PLAYER, or an optional player argument was omitted |
has() returns false for an optional argument |
The argument was omitted and its default is null |
| Command doesn't appear | Make sure register() is called on the builder chain, in onEnable() |
| Subcommand not found | Subcommand names and aliases are matched in lowercase; check for typos in the registered name |
| "No permission" for everyone | The permission was created with the default op. Pass "user" as the second argument of permission() |
| Class not found after installing | Run composer install and build the plugin with Pharynx so the library is embedded |
Author
Rubén Jiménez — Software Solutions Developer GitHub · LinkedIn
License
Licensed under the PolyForm Noncommercial License 1.0.0. You may use, copy and modify this library for noncommercial purposes only. Commercial use, including selling it, requires a separate license from the author.
Required Notice: Copyright (c) 2026 RubenJisan (https://github.com/RubenJisan)