Skip to content

Commit fa3eda8

Browse files
committed
feat: normal-executors page
1 parent dfd369d commit fa3eda8

File tree

3 files changed

+130
-24
lines changed

3 files changed

+130
-24
lines changed

docs/en/create-commands/executors/normal-executors.md

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,9 @@ We use an argument "message" to hold the message to broadcast, we provide some a
4141

4242
:::tabs
4343
===Java
44-
```java
45-
// todo {{#include ../../commandapi-documentation-code/src/main/java/dev/jorel/commandapi/examples/java/Examples.java:normalExecutors1}}
46-
```
44+
<<< @/../reference-code/src/main/java/createcommands/executors/NormalExecutors.java#broadcastExample
4745
===Kotlin
48-
```kotlin
49-
// todo {{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/Examples.kt:normalExecutors1}}
50-
```
46+
<<< @/../reference-code/src/main/kotlin/createcommands/executors/NormalExecutors.kt#broadcastExample
5147
:::
5248

5349
Note how when we finish up our implementation of `.executes()`, we don't return anything. This is unlike commands in the standard Bukkit API where the `onCommand` method returns a Boolean value:
@@ -80,13 +76,9 @@ Say we wanted to create a command `/suicide`, which kills the player that execut
8076

8177
:::tabs
8278
===Java
83-
```java
84-
// todo {{#include ../../commandapi-documentation-code/src/main/java/dev/jorel/commandapi/examples/java/Examples.java:normalExecutors2}}
85-
```
79+
<<< @/../reference-code/src/main/java/createcommands/executors/NormalExecutors.java#suicideExample
8680
===Kotlin
87-
```kotlin
88-
// todo {{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/Examples.kt:normalExecutors2}}
89-
```
81+
<<< @/../reference-code/src/main/kotlin/createcommands/executors/NormalExecutors.kt#suicideExample
9082
:::
9183

9284
::::
@@ -101,13 +93,9 @@ Extending on the suicide example above, we could write another implementation fo
10193

10294
:::tabs
10395
===Java
104-
```java
105-
// todo {{#include ../../commandapi-documentation-code/src/main/java/dev/jorel/commandapi/examples/java/Examples.java:normalExecutors3}}
106-
```
96+
<<< @/../reference-code/src/main/java/createcommands/executors/NormalExecutors.java#differentImplExample
10797
===Kotlin
108-
```kotlin
109-
// todo {{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/Examples.kt:normalExecutors3}}
110-
```
98+
<<< @/../reference-code/src/main/kotlin/createcommands/executors/NormalExecutors.kt#differentImplExample
11199
:::
112100

113101
This saves having to use `instanceof` multiple times to check the type of the `CommandSender`.
@@ -150,13 +138,9 @@ Expanding on the suicide example above, we can restrict the command to only play
150138

151139
:::tabs
152140
===Java
153-
```java
154-
// todo {{#include ../../commandapi-documentation-code/src/main/java/dev/jorel/commandapi/examples/java/Examples.java:normalExecutors4}}
155-
```
141+
<<< @/../reference-code/src/main/java/createcommands/executors/NormalExecutors.java#sameImplExample
156142
===Kotlin
157-
```kotlin
158-
// todo {{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/Examples.kt:normalExecutors4}}
159-
```
143+
<<< @/../reference-code/src/main/kotlin/createcommands/executors/NormalExecutors.kt#sameImplExample
160144
:::
161145

162146
::::
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package createcommands.executors;
2+
3+
import dev.jorel.commandapi.CommandAPICommand;
4+
import dev.jorel.commandapi.CommandPermission;
5+
import dev.jorel.commandapi.arguments.GreedyStringArgument;
6+
import dev.jorel.commandapi.executors.ExecutorType;
7+
import org.bukkit.Bukkit;
8+
import org.bukkit.command.ProxiedCommandSender;
9+
import org.bukkit.entity.LivingEntity;
10+
11+
class NormalExecutors {
12+
{
13+
// #region broadcastExample
14+
// Create our command
15+
new CommandAPICommand("broadcastmsg")
16+
.withArguments(new GreedyStringArgument("message")) // The arguments
17+
.withAliases("broadcast", "broadcastmessage") // Command aliases
18+
.withPermission(CommandPermission.OP) // Required permissions
19+
.executes((sender, args) -> {
20+
String message = (String) args.get("message");
21+
Bukkit.getServer().broadcastMessage(message);
22+
})
23+
.register();
24+
// #endregion broadcastExample
25+
26+
// #region suicideExample
27+
new CommandAPICommand("suicide")
28+
.executesPlayer((player, args) -> {
29+
player.setHealth(0);
30+
})
31+
.register();
32+
// #endregion suicideExample
33+
34+
// #region differentImplExample
35+
new CommandAPICommand("suicide")
36+
.executesPlayer((player, args) -> {
37+
player.setHealth(0);
38+
})
39+
.executesEntity((entity, args) -> {
40+
entity.getWorld().createExplosion(entity.getLocation(), 4);
41+
entity.remove();
42+
})
43+
.register();
44+
// #endregion differentImplExample
45+
46+
// #region sameImplExample
47+
new CommandAPICommand("suicide")
48+
.executes((sender, args) -> {
49+
LivingEntity entity;
50+
if (sender instanceof ProxiedCommandSender proxy) {
51+
entity = (LivingEntity) proxy.getCallee();
52+
} else {
53+
entity = (LivingEntity) sender;
54+
}
55+
entity.setHealth(0);
56+
}, ExecutorType.PLAYER, ExecutorType.PROXY)
57+
.register();
58+
// #endregion sameImplExample
59+
}
60+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package createcommands.executors
2+
3+
import dev.jorel.commandapi.CommandAPICommand
4+
import dev.jorel.commandapi.CommandPermission
5+
import dev.jorel.commandapi.arguments.GreedyStringArgument
6+
import dev.jorel.commandapi.executors.CommandExecutor
7+
import dev.jorel.commandapi.executors.EntityCommandExecutor
8+
import dev.jorel.commandapi.executors.ExecutorType
9+
import dev.jorel.commandapi.executors.PlayerCommandExecutor
10+
import org.bukkit.Bukkit
11+
import org.bukkit.command.ProxiedCommandSender
12+
import org.bukkit.entity.LivingEntity
13+
14+
fun broadcastExample() {
15+
// #region broadcastExample
16+
// Create our command
17+
CommandAPICommand("broadcastmsg")
18+
.withArguments(GreedyStringArgument("message")) // The arguments
19+
.withAliases("broadcast", "broadcastmessage") // Command aliases
20+
.withPermission(CommandPermission.OP) // Required permissions
21+
.executes(CommandExecutor { _, args ->
22+
val message = args["message"] as String
23+
Bukkit.getServer().broadcastMessage(message)
24+
})
25+
.register()
26+
// #endregion broadcastExample
27+
}
28+
29+
fun suicideExample() {
30+
// #region suicideExample
31+
CommandAPICommand("suicide")
32+
.executesPlayer(PlayerCommandExecutor { player, _ ->
33+
player.setHealth(0.0)
34+
})
35+
.register()
36+
// #endregion suicideExample
37+
}
38+
39+
fun differentImplExample() {
40+
// #region differentImplExample
41+
CommandAPICommand("suicide")
42+
.executesPlayer(PlayerCommandExecutor { player, _ ->
43+
player.setHealth(0.0)
44+
})
45+
.executesEntity(EntityCommandExecutor { entity, _ ->
46+
entity.world.createExplosion(entity.location, 4f)
47+
entity.remove()
48+
})
49+
.register()
50+
// #endregion differentImplExample
51+
}
52+
53+
fun sameImplExample() {
54+
// #region sameImplExample
55+
CommandAPICommand("suicide")
56+
.executes(CommandExecutor { sender, _ ->
57+
val entity = (if (sender is ProxiedCommandSender) sender.callee else sender) as LivingEntity
58+
entity.setHealth(0.0)
59+
}, ExecutorType.PLAYER, ExecutorType.PROXY)
60+
.register()
61+
// #endregion sameImplExample
62+
}

0 commit comments

Comments
 (0)