Skip to content

Commit b82aa9f

Browse files
committed
feat: finish executors
1 parent fa3eda8 commit b82aa9f

File tree

14 files changed

+416
-145
lines changed

14 files changed

+416
-145
lines changed

docs/en/create-commands/executors/handle-failures.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,9 @@ Therefore, to gracefully handle this with a proper error message, we use one of
2929

3030
:::tabs
3131
===Java
32-
```java
33-
// todo {{#include ../../commandapi-documentation-code/src/main/java/dev/jorel/commandapi/examples/java/Examples.java:commandFailures1}}
34-
```
32+
<<< @/../reference-code/src/main/java/createcommands/executors/HandleFailures.java#handleFailuresExample
3533
===Kotlin
36-
```kotlin
37-
// todo {{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/Examples.kt:commandFailures1}}
38-
```
34+
<<< @/../reference-code/src/main/kotlin/createcommands/executors/HandleFailures.kt#handleFailuresExample
3935
:::
4036

4137
::::

docs/en/create-commands/executors/native-sender.md

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,11 @@ As you can see, this command takes no arguments. This is fine, since our "argume
5252

5353
:::tabs
5454
===Java
55-
```java
56-
// todo {{#include ../../commandapi-documentation-code/src/main/java/dev/jorel/commandapi/examples/java/Examples.java:native1}}
57-
```
55+
<<< @/../reference-code/src/main/java/createcommands/executors/NativeSender.java#breakCommandExample
5856
===Kotlin
59-
```kotlin
60-
// todo {{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/Examples.kt:native1}}
61-
```
57+
<<< @/../reference-code/src/main/kotlin/createcommands/executors/NativeSender.kt#breakCommandExample
6258
===Kotlin DSL
63-
```kotlin
64-
// todo {{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/ExamplesKotlinDSL.kt:native1}}
65-
```
59+
<<< @/../reference-code/src/main/kotlin/createcommands/executors/NativeSender.kt#breakCommandExampleDSL
6660
:::
6761

6862
This can now be used via the following command examples:

docs/en/create-commands/executors/proxy-sender.md

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,11 @@ Say we have a command which kills the sender of a command. This is easily implem
1616

1717
:::tabs
1818
===Java
19-
```java
20-
// todo {{#include ../../commandapi-documentation-code/src/main/java/dev/jorel/commandapi/examples/java/Examples.java:proxySender1}}
21-
```
19+
<<< @/../reference-code/src/main/java/createcommands/executors/ProxySender.java#simpleKillCommandExample
2220
===Kotlin
23-
```kotlin
24-
// todo {{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/Examples.kt:proxySender1}}
25-
```
21+
<<< @/../reference-code/src/main/kotlin/createcommands/executors/ProxySender.kt#simpleKillCommandExample
2622
===Kotlin DSL
27-
```kotlin
28-
// todo {{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/ExamplesKotlinDSL.kt:proxySender1}}
29-
```
23+
<<< @/../reference-code/src/main/kotlin/createcommands/executors/ProxySender.kt#simpleKillCommandExampleDSL
3024
:::
3125

3226
But what if the sender of the command is _not_ a player? By using Minecraft's `/execute` command, we could execute the command as _any_ arbitrary entity, as shown with the command below:
@@ -39,17 +33,11 @@ To handle this case, we can use the `.executesProxy()` method to ensure that the
3933

4034
:::tabs
4135
===Java
42-
```java
43-
// todo {{#include ../../commandapi-documentation-code/src/main/java/dev/jorel/commandapi/examples/java/Examples.java:proxySender2}}
44-
```
36+
<<< @/../reference-code/src/main/java/createcommands/executors/ProxySender.java#proxyKillCommandExample
4537
===Kotlin
46-
```kotlin
47-
// todo {{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/Examples.kt:proxySender2}}
48-
```
38+
<<< @/../reference-code/src/main/kotlin/createcommands/executors/ProxySender.kt#proxyKillCommandExample
4939
===Kotlin DSL
50-
```kotlin
51-
// todo {{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/ExamplesKotlinDSL.kt:proxySender2}}
52-
```
40+
<<< @/../reference-code/src/main/kotlin/createcommands/executors/ProxySender.kt#proxyKillCommandExampleDSL
5341
:::
5442

5543
This allows the command above to run successfully, killing all chickens it can find.

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

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,11 @@ Say we want a command that returns a random number as a result. This can then be
3232

3333
:::tabs
3434
===Java
35-
```java
36-
// todo {{#include ../../commandapi-documentation-code/src/main/java/dev/jorel/commandapi/examples/java/Examples.java:resultingCommandExecutors1}}
37-
```
35+
<<< @/../reference-code/src/main/java/createcommands/executors/ResultingExecutors.java#randomResultCommandExample
3836
===Kotlin
39-
```kotlin
40-
// todo {{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/Examples.kt:resultingCommandExecutors1}}
41-
```
37+
<<< @/../reference-code/src/main/kotlin/createcommands/executors/ResultingExecutors.kt#randomResultCommandExample
4238
===Kotlin DSL
43-
```kotlin
44-
// todo {{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/ExamplesKotlinDSL.kt:resultingCommandExecutors1}}
45-
```
39+
<<< @/../reference-code/src/main/kotlin/createcommands/executors/ResultingExecutors.kt#randomResultCommandExampleDSL
4640
:::
4741

4842
This returns a **success value of 1** _(Because no errors or `CommandAPI.failWithString(String)` was thrown)_ and a **result value of a random number**.
@@ -64,34 +58,22 @@ Since we're declaring commands that are to be used in `/execute`, we must ensure
6458

6559
:::tabs
6660
===Java
67-
```java
68-
// todo {{#include ../../commandapi-documentation-code/src/main/java/dev/jorel/commandapi/examples/java/Examples.java:resultingCommandExecutors2}}
69-
```
61+
<<< @/../reference-code/src/main/java/createcommands/executors/ResultingExecutors.java#randomNumberCommandExample
7062
===Kotlin
71-
```kotlin
72-
// todo {{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/Examples.kt:resultingCommandExecutors2}}
73-
```
63+
<<< @/../reference-code/src/main/kotlin/createcommands/executors/ResultingExecutors.kt#randomNumberCommandExample
7464
===Kotlin DSL
75-
```kotlin
76-
// todo {{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/ExamplesKotlinDSL.kt:resultingCommandExecutors2}}
77-
```
65+
<<< @/../reference-code/src/main/kotlin/createcommands/executors/ResultingExecutors.kt#randomNumberCommandExampleDSL
7866
:::
7967

8068
Now we write our implementation for `/givereward`. In this example, we use the `EntitySelectorArgument` to select a single player. We cast it to `Player` and then add the items to their inventory.
8169

8270
:::tabs
8371
===Java
84-
```java
85-
// todo {{#include ../../commandapi-documentation-code/src/main/java/dev/jorel/commandapi/examples/java/Examples.java:resultingCommandExecutors3}}
86-
```
72+
<<< @/../reference-code/src/main/java/createcommands/executors/ResultingExecutors.java#giverewardCommandExample
8773
===Kotlin
88-
```kotlin
89-
// todo {{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/Examples.kt:resultingCommandExecutors3}}
90-
```
74+
<<< @/../reference-code/src/main/kotlin/createcommands/executors/ResultingExecutors.kt#giverewardCommandExample
9175
===Kotlin DSL
92-
```kotlin
93-
// todo {#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/ExamplesKotlinDSL.kt:resultingCommandExecutors3}}
94-
```
76+
<<< @/../reference-code/src/main/kotlin/createcommands/executors/ResultingExecutors.kt#giverewardCommandExampleDSL
9577
:::
9678

9779
Now that we've declared these commands, we can now use them in practice. We can use a command block to store a random number under the scoreboard score `randVal` for a player called `SomePlayer`, by executing the command `/randomnumber`. Since `/randomnumber` **returns** an integer, this value is stored in the scoreboard score:
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package createcommands.executors;
2+
3+
import dev.jorel.commandapi.CommandAPI;
4+
import dev.jorel.commandapi.CommandAPICommand;
5+
import dev.jorel.commandapi.arguments.ArgumentSuggestions;
6+
import dev.jorel.commandapi.arguments.StringArgument;
7+
8+
import java.util.Arrays;
9+
10+
class HandleFailures {
11+
{
12+
// #region handleFailuresExample
13+
// Array of fruit
14+
String[] fruit = new String[]{"banana", "apple", "orange"};
15+
16+
// Register the command
17+
new CommandAPICommand("getfruit")
18+
.withArguments(new StringArgument("item").replaceSuggestions(ArgumentSuggestions.strings(fruit)))
19+
.executes((sender, args) -> {
20+
String inputFruit = (String) args.get("item");
21+
22+
if (Arrays.asList(fruit).contains(inputFruit)) {
23+
// Do something with inputFruit
24+
} else {
25+
// The sender's input is not in the list of fruit
26+
throw CommandAPI.failWithString("That fruit doesn't exist!");
27+
}
28+
})
29+
.register();
30+
// #endregion handleFailuresExample
31+
}
32+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package createcommands.executors;
2+
3+
import dev.jorel.commandapi.CommandAPICommand;
4+
import org.bukkit.Location;
5+
6+
class NativeSender {
7+
{
8+
// #region breakCommandExample
9+
new CommandAPICommand("break")
10+
.executesNative((sender, args) -> {
11+
Location location = sender.getLocation();
12+
if (location != null) {
13+
location.getBlock().breakNaturally();
14+
}
15+
})
16+
.register();
17+
// #endregion breakCommandExample
18+
}
19+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package createcommands.executors;
2+
3+
import dev.jorel.commandapi.CommandAPICommand;
4+
import org.bukkit.entity.LivingEntity;
5+
6+
class ProxySender {
7+
{
8+
// #region simpleKillCommandExample
9+
new CommandAPICommand("killme")
10+
.executesPlayer((player, args) -> {
11+
player.setHealth(0);
12+
})
13+
.register();
14+
// #endregion simpleKillCommandExample
15+
16+
// #region proxyKillCommandExample
17+
new CommandAPICommand("killme")
18+
.executesPlayer((player, args) -> {
19+
player.setHealth(0);
20+
})
21+
.executesProxy((proxy, args) -> {
22+
// Check if the callee (target) is an Entity and kill it
23+
if (proxy.getCallee() instanceof LivingEntity target) {
24+
target.setHealth(0);
25+
}
26+
})
27+
.register();
28+
// #endregion proxyKillCommandExample
29+
}
30+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package createcommands.executors;
2+
3+
import dev.jorel.commandapi.CommandAPICommand;
4+
import dev.jorel.commandapi.arguments.EntitySelectorArgument;
5+
import org.bukkit.Bukkit;
6+
import org.bukkit.Material;
7+
import org.bukkit.entity.Player;
8+
import org.bukkit.inventory.ItemStack;
9+
10+
import java.util.concurrent.ThreadLocalRandom;
11+
12+
class ResultingExecutors {
13+
{
14+
// #region randomResultCommandExample
15+
new CommandAPICommand("randnum")
16+
.executes((sender, args) -> {
17+
return ThreadLocalRandom.current().nextInt();
18+
})
19+
.register();
20+
// #endregion randomResultCommandExample
21+
22+
// #region randomNumberCommandExample
23+
// Register random number generator command from 1 to 99 (inclusive)
24+
new CommandAPICommand("randomnumber")
25+
.executes((sender, args) -> {
26+
return ThreadLocalRandom.current().nextInt(1, 100); // Returns random number from 1 <= x < 100
27+
})
28+
.register();
29+
// #endregion randomNumberCommandExample
30+
31+
// #region giverewardCommandExample
32+
// Register reward giving system for a target player
33+
new CommandAPICommand("givereward")
34+
.withArguments(new EntitySelectorArgument.OnePlayer("target"))
35+
.executes((sender, args) -> {
36+
Player player = (Player) args.get("target");
37+
player.getInventory().addItem(new ItemStack(Material.DIAMOND, 64));
38+
Bukkit.broadcastMessage(player.getName() + " won a rare 64 diamonds from a loot box!");
39+
})
40+
.register();
41+
// #endregion giverewardCommandExample
42+
}
43+
}

0 commit comments

Comments
 (0)