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 Command base 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

  1. Player types command with arguments
  2. Minestom parses arguments based on defined syntaxes
  3. Matches arguments to registered syntax
  4. Validates argument types and constraints
  5. Executes callback with parsed context
  6. 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

RAW
// 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

Mode admin 2025-11-14 03:46

Amazing comments!

0
Mode admin 2025-11-14 03:56

reply

0
Mode admin 2025-11-14 03:53

test

0
Mode admin 2025-11-14 03:56

a

0
Mode admin 2025-11-14 04:02

reply

0
Mode admin 2025-11-14 03:56

test

0
Mode admin 2025-11-14 04:02

test amazing test wow

1
Mode admin 2025-11-14 04:11

test

1
Mode admin 2025-11-14 04:21

reply

1
Mode admin 2025-12-09 10:55

comme,nt

-1
Mode admin 2025-12-09 10:55

reply

-1
Mode admin 2025-12-09 10:55

re

0
Mode admin 2025-12-09 10:55

re

-1
Mode admin 2025-12-09 10:57

aa

1
Mode admin 2025-12-09 11:01

nested

-1