You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/docs/paper/dev/api/command-api/misc/basic-command.md
+70-31Lines changed: 70 additions & 31 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,15 +2,16 @@
2
2
title: Basic commands
3
3
description: An overview of a Bukkit-style command declaration using Brigadier.
4
4
slug: paper/dev/command-api/misc/basic-command
5
+
version: 1.21.1
5
6
---
6
7
7
8
For very simple commands Paper has a way to declare Bukkit-style commands by implementing the [`BasicCommand`](jd:paper:io.papermc.paper.command.brigadier.BasicCommand) interface.
8
9
9
10
This interface has one method you have to override:
If you have seen the `CommandContext<CommandSourceStack>` class before, you might recognize the first parameter of the execute method as the generic
37
-
parameter `S` from our `CommandContext<S>`, which is also used in the `executes` method from the `ArgumentBuilder`.
38
-
39
-
With a `CommandSourceStack`, we can retrieve basic information about the sender of the command, the location the command was send from, and the executing entity.
40
-
For more information, check out [basics/command-executors](/paper/dev/command-api/basics/executors).
37
+
With a `CommandSourceStack` you can retrieve basic information about the sender of the command, the location the command was send from,
38
+
and the entity for which the command was executed for. You can find more information on [our page on command executors](/paper/dev/command-api/basics/executors).
41
39
42
40
## The optional methods
43
-
You can freely choose whether to implement either of the at the top mentioned, optional methods. Here is a quick overview on what which one does:
41
+
You can freely choose whether to implement either of the mentioned, optional methods. Here is a quick overview on what which one does:
44
42
45
43
### `suggest(CommandSourceStack, String[])`
46
44
This method returns some sort of `Collection<String>` and takes in a `CommandSourceStack` and a `String[] args` as parameters. This is similar to the
@@ -52,9 +50,52 @@ Each entry in the collection that you return will be send to the client to be sh
52
50
With this method, you can set up a basic `requires` structure from Brigadier commands. [You can read more on that here](/paper/dev/command-api/basics/requirements).
53
51
This method returns a `boolean`, which is required to return `true` in order for a command sender to be able to execute that command.
54
52
53
+
:::note
54
+
55
+
If you override this method, overriding `permission()` does nothing. This is because the default implementation
56
+
uses the return value of `permission()`, which wouldn't be used anymore if you were to override it.
As an example, we can create a simple broadcast command. We start by declaring creating a class which implements `BasicCommand` and overrides `execute` and `permission`:
@@ -88,9 +129,9 @@ operator permissions. You can also set this permission to be `true` by default.
88
129
Now, in our `execute` method, we can retrieve the name of the executor of that command. If we do not find one, we can just get the name of the command sender, like this:
89
130
90
131
```java
91
-
finalComponent name =commandSourceStack.getExecutor() !=null
92
-
?commandSourceStack.getExecutor().name()
93
-
:commandSourceStack.getSender().name();
132
+
finalComponent name =source.getExecutor() !=null
133
+
?source.getExecutor().name()
134
+
:source.getSender().name();
94
135
```
95
136
96
137
This makes sure that we cover all cases and even allow the command to work correctly with `/execute as`.
@@ -99,7 +140,7 @@ Next, we retrieve all arguments and join them to a string or tell the sender tha
99
140
arguments (meaning that `args` has a length of 0):
100
141
```java
101
142
if (args.length ==0) {
102
-
commandSourceStack.getSender().sendRichMessage("<red>You cannot send an empty broadcast!");
143
+
source.getSender().sendRichMessage("<red>You cannot send an empty broadcast!");
0 commit comments