Skip to content

Commit 54e08ee

Browse files
olijeffers0nlynxplayMachine-Makerkennytv
authored
feat: Brigadier API docs (#386)
Co-authored-by: Bjarne Koll <LynxPlay101@gmail.com> Co-authored-by: Jake Potrebic <jake.m.potrebic@gmail.com> Co-authored-by: Nassim Jahnke <nassim@njahnke.dev>
1 parent 8d9e798 commit 54e08ee

File tree

3 files changed

+134
-1
lines changed

3 files changed

+134
-1
lines changed

config/sidebar.paper.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ const paper: SidebarsConfig = {
137137
},
138138
"dev/api/pdc",
139139
"dev/api/custom-inventory-holder",
140+
"dev/api/commands",
140141
"dev/api/scheduler",
141142
"dev/api/entity-teleport",
142143
"dev/api/plugin-messaging",

docs/paper/dev/api/commands.mdx

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
---
2+
slug: /dev/commands
3+
description: A guide to Paper's Brigadier command API.
4+
---
5+
6+
# Command API
7+
8+
Paper's command system is built on top of Minecraft's Brigadier command system. This system is a
9+
powerful and flexible way to define commands and arguments.
10+
11+
:::danger[Experimental]
12+
13+
Paper's command system is still experimental and may change in the future.
14+
15+
:::
16+
17+
## Defining commands
18+
19+
:::note
20+
21+
This uses the <Javadoc name={"io.papermc.paper.plugin.lifecycle.event.LifecycleEventManager"}>LifecycleEventManager</Javadoc>
22+
to register the command. See the [Lifecycle Events](./lifecycle.mdx) page for more information.
23+
24+
:::
25+
26+
### JavaPlugin
27+
28+
Commands can be registered inside the <Javadoc name={"org.bukkit.plugin.java.JavaPlugin#onEnable()"}>onEnable</Javadoc>
29+
method of the plugin. Commands registered here will not be available to datapack functions because
30+
functions are loaded by the server before plugins are loaded. To make commands available to
31+
datapacks, register them via the [PluginBootstrap](#pluginbootstrap).
32+
33+
```java
34+
class YourPluginClass extends JavaPlugin {
35+
36+
@Override
37+
public void onEnable() {
38+
LifecycleEventManager<Plugin> manager = this.getLifecycleManager();
39+
manager.registerEventHandler(LifecycleEvents.COMMANDS, event -> {
40+
final Commands commands = event.registrar();
41+
commands.register(
42+
Commands.literal("new-command")
43+
.executes(ctx -> {
44+
ctx.getSource().getSender().sendPlainMessage("some message");
45+
return Command.SINGLE_SUCCESS;
46+
})
47+
.build(),
48+
"some bukkit help description string",
49+
List.of("an-alias")
50+
);
51+
});
52+
}
53+
}
54+
```
55+
56+
### PluginBootstrap
57+
58+
:::note
59+
60+
If any plugin on the server registers a handler for the `LifecycleEvents.COMMANDS` event, the
61+
server will disable Bukkit's plugin reload functionality. This is a limitation with how
62+
the plugin reload system works in conjunction with the plugin bootstrapper system. The `/reload`
63+
command should never be used regardless.
64+
65+
:::
66+
67+
Commands are registered in the same way in a plugin's
68+
<Javadoc name={"io.papermc.paper.plugin.bootstrap.PluginBootstrap#bootstrap(io.papermc.paper.plugin.bootstrap.BootstrapContext)"}>bootstrapper</Javadoc>.
69+
The benefit of registering commands here is that they will be available to datapack functions
70+
because the command registration happens early enough.
71+
72+
```java
73+
class YourPluginBootstrap implements PluginBootstrap {
74+
75+
@Override
76+
public void bootstrap(BootstrapContext context) {
77+
LifecycleEventManager<BootstrapContext> manager = context.getLifecycleManager();
78+
// Same as for JavaPlugin
79+
}
80+
}
81+
```
82+
83+
### BasicCommand
84+
85+
Paper provides a simple <Javadoc name={"io.papermc.paper.command.brigadier.BasicCommand"}>BasicCommand</Javadoc>
86+
interface which can be used to define a command in a similar way to Bukkit's `CommandExecutor`.
87+
There are methods on the <Javadoc name={"io.papermc.paper.command.brigadier.Commands"}>Commands</Javadoc> interface
88+
to register such commands.
89+
90+
Example registration and implementation of a BasicCommand:
91+
92+
```java
93+
LifecycleEventManager<BootstrapContext> manager = context.getLifecycleManager();
94+
manager.registerEventHandler(LifecycleEvents.COMMANDS, event -> {
95+
final Commands commands = event.registrar();
96+
commands.register("fun", "some help description string", new FunCommand());
97+
});
98+
```
99+
100+
```java
101+
class FunCommand implements BasicCommand {
102+
103+
@Override
104+
public void execute(@NotNull CommandSourceStack stack, @NotNull String[] args) {
105+
if (args.length == 1 && args[0].equalsIgnoreCase("start")) {
106+
stack.getSender().sendRichMessage("<rainbow>Fun activated!");
107+
}
108+
}
109+
}
110+
```
111+
112+
## Arguments
113+
114+
### Built-in arguments
115+
116+
Vanilla has lots of built-in arguments that the client also supports. These can provide better
117+
syntax and error-checking before even executing the commands. <Javadoc name={"io.papermc.paper.command.brigadier.argument.ArgumentTypes"}>ArgumentTypes</Javadoc>
118+
has all the argument types available to the API.
119+
120+
For now, you can find specific examples of arguments, among other things, on the [Fabric Wiki](https://fabricmc.net/wiki/tutorial:commands).
121+
122+
### Custom arguments
123+
124+
Custom arguments can be created by implementing the <Javadoc name={"io.papermc.paper.command.brigadier.argument.CustomArgumentType"}>CustomArgumentType</Javadoc>
125+
interface. See the Javadocs for more information on how they work.
126+
127+
## Lifecycle
128+
129+
Commands are not just registered once at the start, but anytime a reload happens. This can be
130+
either via Bukkit's reload command (which should never be used and may not always be available) or
131+
Minecraft's `/reload` command (which can be used), the commands are re-registered by having the
132+
event handlers called again.

docs/paper/dev/getting-started/paper-plugins.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ See [declaring dependencies](#dependency-declaration) for more information on ho
187187
### Commands
188188
Paper plugins do not use the `commands` field to register commands. This means that you do not need to include all
189189
of your commands in the `paper-plugin.yml` file. Instead, you can register commands using the
190-
<Javadoc name={"org.bukkit.Server#getCommandMap()"}>command map</Javadoc>.
190+
[Brigadier Command API](../api/commands.mdx).
191191

192192
### Cyclic plugin loading
193193

0 commit comments

Comments
 (0)