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: docs/en/create-commands/arguments/listed-arguments.md
+24-23Lines changed: 24 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -18,26 +18,27 @@ This flag is set using the following function:
18
18
Argument setListed(boolean listed);
19
19
```
20
20
21
-
> [!TIP] Example - Setting listed arguments
22
-
>
23
-
> Say we have the following command:
24
-
>
25
-
> ```mccmd
26
-
> /mycommand <player> <value> <message>
27
-
> ```
28
-
>
29
-
> Let's also say that in our implementation of this command, we don't actually perform any processing for `<value>`. Hence, listing it in the [`CommandArguments args`](./arguments) is unnecessary.
30
-
>
31
-
> :::tabs
32
-
> ===Java
33
-
> ```java
34
-
> // todo {{#include ../../commandapi-documentation-code/src/main/java/dev/jorel/commandapi/examples/java/Examples.java:listed1}}
> In this scenario, the argument `<value>` is not present in the [`CommandArguments args`](./arguments) for the executor.
43
-
>
21
+
::::tip Example - Setting listed arguments
22
+
23
+
Say we have the following command:
24
+
25
+
```mccmd
26
+
/mycommand <player> <value> <message>
27
+
```
28
+
29
+
Let's also say that in our implementation of this command, we don't actually perform any processing for `<value>`. Hence, listing it in the [`CommandArguments args`](./arguments) is unnecessary.
30
+
31
+
:::tabs
32
+
===Java
33
+
```java
34
+
// todo {{#include ../../commandapi-documentation-code/src/main/java/dev/jorel/commandapi/examples/java/Examples.java:listed1}}
> [!TIP] Example - <code>/sayhi</code> command with an optional argument
25
-
>
26
-
> For example, say we're registering a command `/sayhi`:
27
-
>
28
-
> ```mccmd
29
-
> /sayhi - Says "Hi!" to yourself
30
-
> /sayhi <target> - Says "Hi!" to a target player
31
-
> ```
32
-
>
33
-
> For that, we are going to register a command `/sayhi`. To add optional arguments, we are going to use the `withOptionalArguments(Argument... args)` method:
34
-
>
35
-
> :::tabs
36
-
> ===Java
37
-
> ```java
38
-
> // todo {{#include ../../commandapi-documentation-code/src/main/java/dev/jorel/commandapi/examples/java/Examples.java:optionalArguments1}}
39
-
> ```
40
-
> ===Kotlin
41
-
> ```kotlin
42
-
> // todo {{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/Examples.kt:optionalArguments1}}
43
-
> ```
44
-
> ===Kotlin DSL
45
-
> ```kotlin
46
-
> // todo {{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/ExamplesKotlinDSL.kt:optionalArguments1}}
47
-
> ```
48
-
> :::
49
-
>
50
-
>
51
-
> This gives us the ability to run both `/sayhi` and `/sayhi <target>` with the same command name "sayhi", but have different results based on the arguments used.
52
-
>
53
-
> You can notice two things:
54
-
>
55
-
> - We use the `withOptionalArguments` method to add an optional argument to a command
56
-
> - We use `args.get("target")` to get our player out of the arguments
57
-
>
58
-
> With optional arguments, there is a possibility of them being not present in the arguments of a command. The reason we use `args.get("target")` is that this will just return `null` and you can handle what should happen.
24
+
::::tip Example - `/sayhi` command with an optional argument
59
25
26
+
For example, say we're registering a command `/sayhi`:
27
+
28
+
```mccmd
29
+
/sayhi - Says "Hi!" to yourself
30
+
/sayhi <target> - Says "Hi!" to a target player
31
+
```
32
+
33
+
For that, we are going to register a command `/sayhi`. To add optional arguments, we are going to use the `withOptionalArguments(Argument... args)` method:
34
+
35
+
:::tabs
36
+
===Java
37
+
```java
38
+
// todo {{#include ../../commandapi-documentation-code/src/main/java/dev/jorel/commandapi/examples/java/Examples.java:optionalArguments1}}
39
+
```
40
+
===Kotlin
41
+
```kotlin
42
+
// todo {{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/Examples.kt:optionalArguments1}}
43
+
```
44
+
===Kotlin DSL
45
+
```kotlin
46
+
// todo {{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/ExamplesKotlinDSL.kt:optionalArguments1}}
47
+
```
48
+
:::
49
+
50
+
51
+
This gives us the ability to run both `/sayhi` and `/sayhi <target>` with the same command name "sayhi", but have different results based on the arguments used.
52
+
53
+
You can notice two things:
54
+
55
+
- We use the `withOptionalArguments` method to add an optional argument to a command
56
+
- We use `args.get("target")` to get our player out of the arguments
57
+
58
+
With optional arguments, there is a possibility of them being not present in the arguments of a command. The reason we use `args.get("target")` is that this will just return `null` and you can handle what should happen.
59
+
60
+
::::
60
61
61
62
## Setting existing arguments as optional arguments
The examples will be using the `getOptional` methods but there is no downside of using the `getOrDefault` methods.
97
98
98
-
> [!TIP] Example - <code>/sayhi</code> command while using the getOptional method
99
-
>
100
-
> Let's register the `/sayhi` command from above a second time - this time using a `getOptional` method. We are using the exact same command syntax:
101
-
>
102
-
> ```mccmd
103
-
> /sayhi - Says "Hi!" to yourself
104
-
> /sayhi <target> - Says "Hi!" to a target player
105
-
> ```
106
-
>
107
-
> This is how the `getOptional` method is being implemented:
108
-
>
109
-
> :::tabs
110
-
> ===Java
111
-
> ```java
112
-
> // todo {{#include ../../commandapi-documentation-code/src/main/java/dev/jorel/commandapi/examples/java/Examples.java:optionalArguments2}}
113
-
> ```
114
-
> ===Kotlin
115
-
> ```kotlin
116
-
> // todo {{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/Examples.kt:optionalArguments2}}
117
-
> ```
118
-
> ===Kotlin DSL
119
-
> ```kotlin
120
-
> // todo {{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/ExamplesKotlinDSL.kt:optionalArguments2}}
121
-
> ```
122
-
> :::
123
-
>
99
+
:::tip Example - `/sayhi` command while using the getOptional method
100
+
101
+
Let's register the `/sayhi` command from above a second time - this time using a `getOptional` method. We are using the exact same command syntax:
102
+
103
+
```mccmd
104
+
/sayhi - Says "Hi!" to yourself
105
+
/sayhi <target> - Says "Hi!" to a target player
106
+
```
107
+
108
+
This is how the `getOptional` method is being implemented:
109
+
110
+
:::tabs
111
+
===Java
112
+
```java
113
+
// todo {{#include ../../commandapi-documentation-code/src/main/java/dev/jorel/commandapi/examples/java/Examples.java:optionalArguments2}}
114
+
```
115
+
===Kotlin
116
+
```kotlin
117
+
// todo {{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/Examples.kt:optionalArguments2}}
118
+
```
119
+
===Kotlin DSL
120
+
```kotlin
121
+
// todo {{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/ExamplesKotlinDSL.kt:optionalArguments2}}
122
+
```
123
+
:::
124
+
125
+
::::
124
126
125
127
## Implementing required arguments after optional arguments
126
128
@@ -168,30 +170,31 @@ new CommandAPICommand("mycommand")
168
170
169
171
This would result in an `OptionalArgumentException` because you are declaring a required argument after an optional argument without creating that exception for argument `C` like you do for argument `B`.
170
172
171
-
> [!TIP] Example - Required arguments after optional arguments
172
-
>
173
-
> We want to register a command `/rate` with the following syntax:
174
-
>
175
-
> ```mccmd
176
-
> /rate - Sends an information message
177
-
> /rate <topic> <rating> - Rates a topic with a rating and sends a message to the command sender
178
-
> /rate <topic> <rating> <target> - Rates a topic with a rating and sends a message to the target
179
-
> ```
180
-
>
181
-
> To implement that structure we make use of the `combineWith` method to make the argument after the optional argument \<topic> required:
182
-
>
183
-
> :::tabs
184
-
> ===Java
185
-
> ```java
186
-
> // todo {{#include ../../commandapi-documentation-code/src/main/java/dev/jorel/commandapi/examples/java/Examples.java:optionalArguments3}}
187
-
> ```
188
-
> ===Kotlin
189
-
> ```kotlin
190
-
> // todo {{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/Examples.kt:optionalArguments3}}
191
-
> ```
192
-
> ===Kotlin DSL
193
-
> ```kotlin
194
-
> // todo {{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/ExamplesKotlinDSL.kt:optionalArguments3}}
195
-
> ```
196
-
> :::
197
-
>
173
+
::::tip Example - Required arguments after optional arguments
174
+
175
+
We want to register a command `/rate` with the following syntax:
176
+
177
+
```mccmd
178
+
/rate - Sends an information message
179
+
/rate <topic> <rating> - Rates a topic with a rating and sends a message to the command sender
180
+
/rate <topic> <rating> <target> - Rates a topic with a rating and sends a message to the target
181
+
```
182
+
183
+
To implement that structure we make use of the `combineWith` method to make the argument after the optional argument \<topic> required:
184
+
185
+
:::tabs
186
+
===Java
187
+
```java
188
+
// todo {{#include ../../commandapi-documentation-code/src/main/java/dev/jorel/commandapi/examples/java/Examples.java:optionalArguments3}}
189
+
```
190
+
===Kotlin
191
+
```kotlin
192
+
// todo {{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/Examples.kt:optionalArguments3}}
193
+
```
194
+
===Kotlin DSL
195
+
```kotlin
196
+
// todo {{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/ExamplesKotlinDSL.kt:optionalArguments3}}
> [!TIP] Example - Reading keys from a config file
58
-
>
59
-
> Say you wanted to write a command to modify your plugin's config file. Since the config file is an external file, you ideally want to access the file in a separate thread to the main server thread to retain performance for players on the server. We have the following command syntax:
60
-
>
61
-
> ```mccmd
62
-
> /setconfig <key> <value>
63
-
> ```
64
-
>
65
-
> We make use of the `ArgumentSuggestions.stringsAsync` method to provide asynchronous suggestions. In our completable future implementation, we access the keys from the plugin configuration.
66
-
>
67
-
> :::tabs
68
-
> ===Java
69
-
> ```java
70
-
> // todo {{#include ../../commandapi-documentation-code/src/main/java/dev/jorel/commandapi/examples/java/Examples.java:asyncSuggestions1}}
71
-
> ```
72
-
> ===Kotlin
73
-
> ```kotlin
74
-
> // todo {{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/Examples.kt:asyncSuggestions1}}
75
-
> ```
76
-
> :::
57
+
::::tip Example - Reading keys from a config file
58
+
59
+
Say you wanted to write a command to modify your plugin's config file. Since the config file is an external file, you ideally want to access the file in a separate thread to the main server thread to retain performance for players on the server. We have the following command syntax:
60
+
61
+
```mccmd
62
+
/setconfig <key> <value>
63
+
```
64
+
65
+
We make use of the `ArgumentSuggestions.stringsAsync` method to provide asynchronous suggestions. In our completable future implementation, we access the keys from the plugin configuration.
66
+
67
+
:::tabs
68
+
===Java
69
+
```java
70
+
// todo {{#include ../../commandapi-documentation-code/src/main/java/dev/jorel/commandapi/examples/java/Examples.java:asyncSuggestions1}}
71
+
```
72
+
===Kotlin
73
+
```kotlin
74
+
// todo {{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/Examples.kt:asyncSuggestions1}}
0 commit comments