Understanding Minestom Commands
Concept explanation of Minestom's command system for creating custom server commands
java (17+)
2025-11-03
minestom
commands
concepts
minecraft
Description
Minestom’s command system provides a powerful and flexible way to create custom commands for your Minecraft server. Unlike traditional command systems, Minestom’s approach allows for complex argument parsing, automatic tab completion, and multiple command syntaxes.
Key Concepts
Command Structure
- Command class: All commands extend the
Commandbase class - Syntaxes: A single command can have multiple valid syntaxes
- Arguments: Commands accept typed arguments (String, Integer, Double, Entity, etc.)
- Callbacks: Command execution is handled through lambda expressions or method references
Argument Types
- String: Text input (can be single word or quoted string)
- Word: Single word (no spaces)
- Integer/Double: Numeric values with optional min/max constraints
- Boolean: True/false values
- Entity: Entity selectors (players, mobs, etc.)
- Player: Player-specific selectors
- Block: Block position selectors
Command Execution Flow
- Player types command with arguments
- Minestom parses arguments based on defined syntaxes
- Matches arguments to registered syntax
- Validates argument types and constraints
- Executes callback with parsed context
- Returns result or error message
Multiple Syntaxes
A single command can have multiple valid syntaxes, allowing flexibility:
/command <required>- Required argument/command [optional]- Optional argument/command <arg1> <arg2>- Multiple arguments/command <arg1> [arg2] [arg3]- Mix of required and optional
Auto-completion
- Tab completion is automatically provided for all argument types
- Word arguments can specify valid options for completion
- Entity selectors provide entity lists
- Block selectors provide block suggestions
Conditions
- Commands can have conditions that check if execution is allowed
- Common conditions: player-only commands, permission checks, instance checks
- Conditions run before syntax matching
Benefits
- Type safety: Strongly typed arguments prevent errors
- Flexibility: Multiple syntaxes for one command
- User-friendly: Automatic tab completion
- Powerful: Complex argument parsing with minimal code
- Maintainable: Clear separation of syntax and logic
Best Practices
- Use descriptive argument names
- Provide clear error messages
- Use conditions for permission checks
- Support both player and console execution when appropriate
- Document command syntaxes for users
- Use appropriate argument types (don’t use String for everything)
Code
// Simple command with one syntaxpublic class SimpleCommand extends Command { public SimpleCommand() { super("hello"); // Single string argument var nameArg = ArgumentType.String("name"); // Syntax: /hello <name> addSyntax((sender, context) -> { String name = context.get(nameArg); sender.sendMessage("Hello, " + name + "!"); }, nameArg); }}// Command with multiple syntaxespublic class MultiSyntaxCommand extends Command { public MultiSyntaxCommand() { super("teleport"); var xArg = ArgumentType.Double("x"); var yArg = ArgumentType.Double("y"); var zArg = ArgumentType.Double("z"); var playerArg = ArgumentType.Player("target"); // Syntax 1: /teleport <x> <y> <z> addSyntax((sender, context) -> { if (sender instanceof Player player) { double x = context.get(xArg); double y = context.get(yArg); double z = context.get(zArg); player.teleport(new Pos(x, y, z)); } }, xArg, yArg, zArg); // Syntax 2: /teleport <target> addSyntax((sender, context) -> { if (sender instanceof Player player) { Player target = context.get(playerArg); player.teleport(target.getPosition()); } }, playerArg); }}// Command with conditionspublic class PlayerOnlyCommand extends Command { public PlayerOnlyCommand() { super("fly"); // Only allow players to use this command setCondition((sender, commandString) -> { if (!(sender instanceof Player)) { sender.sendMessage("This command can only be used by players!"); return false; } return true; }); addSyntax((sender, context) -> { // Command logic for players only }); }}
Comments
Amazing comments!
reply
test
a
reply
test
test
amazingtest wowtest
reply
comme,nt
reply
re
re
aa
nested
Please login to leave a comment.