Skip to content

Commit 6f4a67b

Browse files
committed
feat: some argument page
1 parent bb96c5c commit 6f4a67b

File tree

6 files changed

+265
-33
lines changed

6 files changed

+265
-33
lines changed

docs/en/create-commands/arguments/listed-arguments.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ authors:
44
- DerEchtePilz
55
- willkroboth
66
- JorelAli
7+
- MC-XiaoHei
78
---
89

910
# Listed arguments
@@ -30,13 +31,11 @@ Let's also say that in our implementation of this command, we don't actually per
3031

3132
:::tabs
3233
===Java
33-
```java
34-
// todo {{#include ../../commandapi-documentation-code/src/main/java/dev/jorel/commandapi/examples/java/Examples.java:listed1}}
35-
```
34+
<<< @/../reference-code/src/main/java/createcommands/arguments/ListedArguments.java#listedArgumentsExample
3635
===Kotlin
37-
```kotlin
38-
{{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/Examples.kt:listed1}}
39-
```
36+
<<< @/../reference-code/src/main/kotlin/createcommands/arguments/ListedArguments.kt#listedArgumentsExample
37+
===Kotlin DSL
38+
<<< @/../reference-code/src/main/kotlin/createcommands/arguments/ListedArguments.kt#listedArgumentsExampleDSL
4039
:::
4140

4241
In this scenario, the argument `<value>` is not present in the [`CommandArguments args`](./command-arguments) for the executor.

docs/en/create-commands/arguments/optional-arguments.md

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,11 @@ For that, we’re going to register a command `/sayhi`. To add optional argument
3434

3535
:::tabs
3636
===Java
37-
```java
38-
// todo {{#include ../../commandapi-documentation-code/src/main/java/dev/jorel/commandapi/examples/java/Examples.java:optionalArguments1}}
39-
```
37+
<<< @/../reference-code/src/main/java/createcommands/arguments/OptionalArguments.java#simpleOptionalArgumentsExample
4038
===Kotlin
41-
```kotlin
42-
// todo {{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/Examples.kt:optionalArguments1}}
43-
```
39+
<<< @/../reference-code/src/main/kotlin/createcommands/arguments/OptionalArguments.kt#simpleOptionalArgumentsExample
4440
===Kotlin DSL
45-
```kotlin
46-
// todo {{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/ExamplesKotlinDSL.kt:optionalArguments1}}
47-
```
41+
<<< @/../reference-code/src/main/kotlin/createcommands/arguments/OptionalArguments.kt#simpleOptionalArgumentsExampleDSL
4842
:::
4943

5044

@@ -109,17 +103,11 @@ This is how the `getOptional` method is being implemented:
109103

110104
:::tabs
111105
===Java
112-
```java
113-
// todo {{#include ../../commandapi-documentation-code/src/main/java/dev/jorel/commandapi/examples/java/Examples.java:optionalArguments2}}
114-
```
106+
<<< @/../reference-code/src/main/java/createcommands/arguments/OptionalArguments.java#getOptionalExample
115107
===Kotlin
116-
```kotlin
117-
// todo {{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/Examples.kt:optionalArguments2}}
118-
```
108+
<<< @/../reference-code/src/main/kotlin/createcommands/arguments/OptionalArguments.kt#getOptionalExample
119109
===Kotlin DSL
120-
```kotlin
121-
// todo {{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/ExamplesKotlinDSL.kt:optionalArguments2}}
122-
```
110+
<<< @/../reference-code/src/main/kotlin/createcommands/arguments/OptionalArguments.kt#getOptionalExampleDSL
123111
:::
124112

125113
::::
@@ -188,17 +176,11 @@ To implement that structure, we make use of the `combineWith` method to make the
188176

189177
:::tabs
190178
===Java
191-
```java
192-
// todo {{#include ../../commandapi-documentation-code/src/main/java/dev/jorel/commandapi/examples/java/Examples.java:optionalArguments3}}
193-
```
179+
<<< @/../reference-code/src/main/java/createcommands/arguments/OptionalArguments.java#argumentsAfterOptionalArgumentsExample
194180
===Kotlin
195-
```kotlin
196-
// todo {{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/Examples.kt:optionalArguments3}}
197-
```
181+
<<< @/../reference-code/src/main/kotlin/createcommands/arguments/OptionalArguments.kt#argumentsAfterOptionalArgumentsExample
198182
===Kotlin DSL
199-
```kotlin
200-
// todo {{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/ExamplesKotlinDSL.kt:optionalArguments3}}
201-
```
183+
<<< @/../reference-code/src/main/kotlin/createcommands/arguments/OptionalArguments.kt#argumentsAfterOptionalArgumentsExampleDSL
202184
:::
203185

204186
::::
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package createcommands.arguments;
2+
3+
import dev.jorel.commandapi.CommandAPICommand;
4+
import dev.jorel.commandapi.arguments.GreedyStringArgument;
5+
import dev.jorel.commandapi.arguments.IntegerArgument;
6+
import dev.jorel.commandapi.arguments.PlayerArgument;
7+
import org.bukkit.entity.Player;
8+
9+
class ListedArguments {
10+
{
11+
// #region listedArgumentsExample
12+
new CommandAPICommand("mycommand")
13+
.withArguments(new PlayerArgument("player"))
14+
.withArguments(new IntegerArgument("value").setListed(false))
15+
.withArguments(new GreedyStringArgument("message"))
16+
.executes((sender, args) -> {
17+
// args == [player, message]
18+
Player player = (Player) args.get("player");
19+
String message = (String) args.get("message"); // Note that the IntegerArgument is not available in the CommandArguments
20+
player.sendMessage(message);
21+
})
22+
.register();
23+
// #endregion listedArgumentsExample
24+
}
25+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package createcommands.arguments;
2+
3+
import dev.jorel.commandapi.CommandAPICommand;
4+
import dev.jorel.commandapi.arguments.IntegerArgument;
5+
import dev.jorel.commandapi.arguments.PlayerArgument;
6+
import dev.jorel.commandapi.arguments.StringArgument;
7+
import org.bukkit.command.CommandSender;
8+
import org.bukkit.entity.Player;
9+
10+
class OptionalArguments {
11+
{
12+
// #region simpleOptionalArgumentsExample
13+
new CommandAPICommand("sayhi")
14+
.withOptionalArguments(new PlayerArgument("target"))
15+
.executesPlayer((player, args) -> {
16+
Player target = (Player) args.get("target");
17+
if (target != null) {
18+
target.sendMessage("Hi!");
19+
} else {
20+
player.sendMessage("Hi!");
21+
}
22+
})
23+
.register();
24+
// #endregion simpleOptionalArgumentsExample
25+
26+
// #region getOptionalExample
27+
new CommandAPICommand("sayhi")
28+
.withOptionalArguments(new PlayerArgument("target"))
29+
.executesPlayer((player, args) -> {
30+
Player target = (Player) args.getOptional("target").orElse(player);
31+
target.sendMessage("Hi!");
32+
})
33+
.register();
34+
// #endregion getOptionalExample
35+
36+
// #region argumentsAfterOptionalArgumentsExample
37+
new CommandAPICommand("rate")
38+
.withOptionalArguments(new StringArgument("topic").combineWith(new IntegerArgument("rating", 0, 10)))
39+
.withOptionalArguments(new PlayerArgument("target"))
40+
.executes((sender, args) -> {
41+
String topic = (String) args.get("topic");
42+
if (topic == null) {
43+
sender.sendMessage(
44+
"Usage: /rate <topic> <rating> <player>(optional)",
45+
"Select a topic to rate, then give a rating between 0 and 10",
46+
"You can optionally add a player at the end to give the rating to"
47+
);
48+
return;
49+
}
50+
51+
// We know this is not null because rating is required if topic is given
52+
int rating = (int) args.get("rating");
53+
54+
// The target player is optional, so give it a default here
55+
CommandSender target = (CommandSender) args.getOptional("target").orElse(sender);
56+
57+
target.sendMessage("Your " + topic + " was rated: " + rating + "/10");
58+
})
59+
.register();
60+
// #endregion argumentsAfterOptionalArgumentsExample
61+
}
62+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package createcommands.arguments
2+
3+
import dev.jorel.commandapi.CommandAPICommand
4+
import dev.jorel.commandapi.arguments.GreedyStringArgument
5+
import dev.jorel.commandapi.arguments.IntegerArgument
6+
import dev.jorel.commandapi.arguments.PlayerArgument
7+
import dev.jorel.commandapi.executors.CommandExecutor
8+
import dev.jorel.commandapi.kotlindsl.anyExecutor
9+
import dev.jorel.commandapi.kotlindsl.commandAPICommand
10+
import dev.jorel.commandapi.kotlindsl.greedyStringArgument
11+
import dev.jorel.commandapi.kotlindsl.integerArgument
12+
import dev.jorel.commandapi.kotlindsl.playerArgument
13+
import org.bukkit.entity.Player
14+
15+
fun listedArguments() {
16+
// #region listedArgumentsExample
17+
CommandAPICommand("mycommand")
18+
.withArguments(PlayerArgument("player"))
19+
.withArguments(IntegerArgument("value").setListed(false))
20+
.withArguments(GreedyStringArgument("message"))
21+
.executes(CommandExecutor { _, args ->
22+
// args == [player, message]
23+
val player = args["player"] as Player
24+
val message = args["message"] as String // Note that the IntegerArgument is not available in the CommandArguments
25+
player.sendMessage(message)
26+
})
27+
.register()
28+
// #endregion listedArgumentsExample
29+
}
30+
31+
fun listedArgumentsDSL() {
32+
// #region listedArgumentsExampleDSL
33+
commandAPICommand("mycommand") {
34+
playerArgument("player")
35+
integerArgument("value") { isListed = false }
36+
greedyStringArgument("message")
37+
anyExecutor { _, args ->
38+
// args == [player, message]
39+
val player = args["player"] as Player
40+
val message = args["message"] as String // Note that the IntegerArgument is not available in the CommandArguments
41+
player.sendMessage(message)
42+
}
43+
}
44+
// #endregion listedArgumentsExampleDSL
45+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package createcommands.arguments
2+
3+
import dev.jorel.commandapi.CommandAPICommand
4+
import dev.jorel.commandapi.arguments.IntegerArgument
5+
import dev.jorel.commandapi.arguments.PlayerArgument
6+
import dev.jorel.commandapi.arguments.StringArgument
7+
import dev.jorel.commandapi.executors.CommandExecutor
8+
import dev.jorel.commandapi.executors.PlayerCommandExecutor
9+
import dev.jorel.commandapi.kotlindsl.anyExecutor
10+
import dev.jorel.commandapi.kotlindsl.argument
11+
import dev.jorel.commandapi.kotlindsl.commandAPICommand
12+
import dev.jorel.commandapi.kotlindsl.playerArgument
13+
import dev.jorel.commandapi.kotlindsl.playerExecutor
14+
import org.bukkit.command.CommandSender
15+
import org.bukkit.entity.Player
16+
17+
fun optionalArguments() {
18+
// #region simpleOptionalArgumentsExample
19+
CommandAPICommand("sayhi")
20+
.withOptionalArguments(PlayerArgument("target"))
21+
.executesPlayer(PlayerCommandExecutor { player, args ->
22+
val target: Player? = args["target"] as Player?
23+
if (target != null) {
24+
target.sendMessage("Hi!")
25+
} else {
26+
player.sendMessage("Hi!")
27+
}
28+
})
29+
.register()
30+
// #endregion simpleOptionalArgumentsExample
31+
32+
// #region getOptionalExample
33+
CommandAPICommand("sayhi")
34+
.withOptionalArguments(PlayerArgument("target"))
35+
.executesPlayer(PlayerCommandExecutor { player, args ->
36+
val target: Player = args.getOptional("target").orElse(player) as Player
37+
target.sendMessage("Hi!")
38+
})
39+
.register()
40+
// #endregion getOptionalExample
41+
42+
// #region argumentsAfterOptionalArgumentsExample
43+
CommandAPICommand("rate")
44+
.withOptionalArguments(StringArgument("topic").combineWith(IntegerArgument("rating", 0, 10)))
45+
.withOptionalArguments(PlayerArgument("target"))
46+
.executes(CommandExecutor { sender, args ->
47+
val topic: String? = args["topic"] as String?
48+
if (topic == null) {
49+
sender.sendMessage(
50+
"Usage: /rate <topic> <rating> <player>(optional)",
51+
"Select a topic to rate, then give a rating between 0 and 10",
52+
"You can optionally add a player at the end to give the rating to"
53+
)
54+
return@CommandExecutor
55+
}
56+
57+
// We know this is not null because rating is required if a topic is given
58+
val rating = args["rating"] as Int
59+
60+
// The target player is optional, so give it a default here
61+
val target: CommandSender = args.getOptional("target").orElse(sender) as CommandSender
62+
63+
target.sendMessage("Your $topic was rated: $rating/10")
64+
})
65+
.register()
66+
// #endregion argumentsAfterOptionalArgumentsExample
67+
}
68+
69+
fun optionalArgumentsDSL() {
70+
// #region simpleOptionalArgumentsExampleDSL
71+
commandAPICommand("sayhi") {
72+
playerArgument("target", optional = true)
73+
playerExecutor { player, args ->
74+
val target: Player? = args["target"] as Player?
75+
if (target != null) {
76+
target.sendMessage("Hi!")
77+
} else {
78+
player.sendMessage("Hi!")
79+
}
80+
}
81+
}
82+
// #endregion simpleOptionalArgumentsExampleDSL
83+
84+
// #region getOptionalExampleDSL
85+
commandAPICommand("sayhi") {
86+
playerArgument("target", optional = true)
87+
playerExecutor { player, args ->
88+
val target: Player = args.getOptional("target").orElse(player) as Player
89+
target.sendMessage("Hi!")
90+
}
91+
}
92+
// #endregion getOptionalExampleDSL
93+
94+
// #region argumentsAfterOptionalArgumentsExampleDSL
95+
commandAPICommand("rate") {
96+
argument(StringArgument("topic").setOptional(true).combineWith(IntegerArgument("rating", 0, 10)))
97+
playerArgument("target", optional = true)
98+
anyExecutor { sender, args ->
99+
val topic: String? = args["topic"] as String?
100+
if (topic == null) {
101+
sender.sendMessage(
102+
"Usage: /rate <topic> <rating> <player>(optional)",
103+
"Select a topic to rate, then give a rating between 0 and 10",
104+
"You can optionally add a player at the end to give the rating to"
105+
)
106+
return@anyExecutor
107+
}
108+
109+
// We know this is not null because rating is required if a topic is given
110+
val rating = args["rating"] as Int
111+
112+
// The target player is optional, so give it a default here
113+
val target: CommandSender = args.getOptional("target").orElse(sender) as CommandSender
114+
115+
target.sendMessage("Your $topic was rated: $rating/10")
116+
}
117+
}
118+
// #endregion argumentsAfterOptionalArgumentsExampleDSL
119+
}

0 commit comments

Comments
 (0)