Minestom wiki
Search…
Commands
Commands are the main communication between the server and the players. In contrary to current alternatives, Minestom takes full advantage of auto-completion/suggestion and has therefore a fairly strict API.

Overview

All auto-completable commands should extend Command, each command is composed of zero or multiple syntaxes, and each syntax is composed of one or more arguments.
If you find it confusing, here are a few examples:
1
/health // This is a command
2
/health set 50; // This is a command and its syntax
3
set // This is a literal argument
4
~ ~ ~ // This is a position argument
Copied!

Create your first command

First of all, create your command class!
1
package demo.commands;
2
3
import net.minestom.server.command.builder.Command;
4
5
public class TestCommand extends Command {
6
7
public TestCommand() {
8
super("my-command", "hey");
9
// "my-command" is the main name of the command
10
// "hey" is an alias, you can have an unlimited number of those
11
}
12
}
Copied!
After this is done, you need to register the command.
1
MinecraftServer.getCommandManager().register(new TestCommand());
Copied!
Nothing crazy so far, let's create a callback once the command is run without any argument and another for our custom syntax.
1
package demo.commands;
2
3
import net.minestom.server.command.builder.Command;
4
import net.minestom.server.command.builder.arguments.ArgumentType;
5
6
public class TestCommand extends Command {
7
8
public TestCommand() {
9
super("command", "alias");
10
11
// Executed if no other executor can be used
12
setDefaultExecutor((sender, context) -> {
13
sender.sendMessage("You executed the command");
14
});
15
16
// All default arguments are available in the ArgumentType class
17
// Each argument has an identifier which should be unique. It is used internally to create the nodes
18
var numberArgument = ArgumentType.Integer("my-number");
19
20
// Finally, create the syntax with the callback, and an infinite number of arguments
21
addSyntax((sender, context) -> {
22
final int number = context.get(numberArgument);
23
sender.sendMessage("You typed the number " + number);
24
}, numberArgument);
25
26
}
27
}
Copied!
The command in action

Argument callback

Let's say you have the command "/set <number>" and the player types "/set text", you would probably like to warn the player that the argument requires a number and not text. This is where argument callbacks come in!
When the command parser detects a wrongly typed argument, it will first check if the given argument has an error callback to execute, if not, the default executor is used.
Here an example checking the correctness of an integer argument:
1
package demo.commands;
2
3
import net.minestom.server.command.builder.Command;
4
import net.minestom.server.command.builder.arguments.ArgumentType;
5
6
public class TestCommand extends Command {
7
8
public TestCommand() {
9
super("command");
10
11
setDefaultExecutor((sender, context) -> {
12
sender.sendMessage("Usage: /command <number>");
13
});
14
15
var numberArgument = ArgumentType.Integer("my-number");
16
17
// Callback executed if the argument has been wrongly used
18
numberArgument.setCallback((sender, exception) -> {
19
final String input = exception.getInput();
20
sender.sendMessage("The number " + input + " is invalid!");
21
});
22
23
addSyntax((sender, context) -> {
24
final int number = context.get(numberArgument);
25
sender.sendMessage("You typed the number " + number);
26
}, numberArgument);
27
28
}
29
}
Copied!
Argument callback detecting an invalid number

Command data

One of the very important features of the command API is the fact that every syntax can return optional data. This data is presented in a structure similar to a Map (in fact, it is only a small wrapper around it).
1
addSyntax((sender, context) -> {
2
final int number = context.get("number");
3
sender.sendMessage("You typed the number " + number);
4
5
// Put the argument data into the returned command data
6
context.setReturnData(new CommandData().set("value", number));
7
}, Integer("number"));
Copied!
The data will be created and returned every time the syntax is called. It can then be retrieved from the CommandResult. CommandManager#executeServerCommand(String) allows you to execute a command as a ServerSender (which has the benefit of not printing anything on CommandSender#sendMessage(String), and permit to differentiate this sender from a player or the console).
1
CommandResult result = MinecraftServer.getCommandManager().executeServerCommand("command 5");
2
if (result.getType() == CommandResult.Type.SUCCESS) {
3
final CommandData data = result.getCommandData();
4
if (data != null && data.has("value")) {
5
System.out.println("The command gave us the value " + data.get("value"));
6
} else {
7
System.out.println("The command didn't give us any value!");
8
}
9
} else {
10
System.out.println("The command didn't work out!");
11
}
Copied!
This tool opens a lot of possibilities, including powerful scripts, remote calls, and an overall easy-to-use interface for all your APIs.
Last modified 7mo ago