10000 Merge pull request #357 from DerEchtePilz/dev/dev · CommandAPI/CommandAPI@d650fba · GitHub
[go: up one dir, main page]

Skip to content

Commit d650fba

Browse files
authored
Merge pull request #357 from DerEchtePilz/dev/dev
adding Kotlin DSL
2 parents 1d80238 + 56e9fdd commit d650fba

File tree

14 files changed

+1537
-0
lines changed

14 files changed

+1537
-0
lines changed

commandapi-kotlin/.gitignore

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# User-specific stuff
2+
.idea/
3+
4+
*.iml
5+
*.ipr
6+
*.iws
7+
8+
# IntelliJ
9+
out/
10+
11+
# Compiled class file
12+
*.class
13+
14+
# Log file
15+
*.log
16+
17+
# BlueJ files
18+
*.ctxt
19+
20+
# Package Files #
21+
*.jar
22+
*.war
23+
*.nar
24+
*.ear
25+
*.zip
26+
*.tar.gz
27+
*.rar
28+
29+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
30+
hs_err_pid*
31+
32+
*~
33+
34+
# temporary files which can be created if a process still has a handle open of a deleted file
35+
.fuse_hidden*
36+
37+
# KDE directory preferences
38+
.directory
39+
40+
# Linux trash folder which might appear on any partition or disk
41+
.Trash-*
42+
43+
# .nfs files are created when an open file is removed but is still being accessed
44+
.nfs*
45+
46+
# General
47+
.DS_Store
48+
.AppleDouble
49+
.LSOverride
50+
51+
# Icon must end with two \r
52+
Icon
53+
54+
# Thumbnails
55+
._*
56+
57+
# Files that might appear in the root of a volume
58+
.DocumentRevisions-V100
59+
.fseventsd
60+
.Spotlight-V100
61+
.TemporaryItems
62+
.Trashes
63+
.VolumeIcon.icns
64+
.com.apple.timemachine.donotpresent
65+
66+
# Directories potentially created on remote AFP share
67+
.AppleDB
68+
.AppleDesktop
69+
Network Trash Folder
70+
Temporary Items
71+
.apdisk
72+
73+
# Windows thumbnail cache files
74+
Thumbs.db
75+
Thumbs.db:encryptable
76+
ehthumbs.db
77+
ehthumbs_vista.db
78+
79+
# Dump file
80+
*.stackdump
81+
82+
# Folder config file
83+
[Dd]esktop.ini
84+
85+
# Recycle Bin used on file shares
86+
$RECYCLE.BIN/
87+
88+
# Windows Installer files
89+
*.cab
90+
*.msi
91+
*.msix
92+
*.msm
93+
*.msp
94+
95+
# Windows shortcuts
96+
*.lnk
97+
98+
target/
99+
100+
pom.xml.tag
101+
pom.xml.releaseBackup
102+
pom.xml.versionsBackup
103+
pom.xml.next
104+
105+
release.properties
106+
dependency-reduced-pom.xml
107+
buildNumber.properties
108+
.mvn/timing.properties
109+
.mvn/wrapper/maven-wrapper.jar
110+
.flattened-pom.xml
111+
112+
# Common working directory
113+
run/

commandapi-kotlin/pom.xml

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<groupId>dev.jorel</groupId>
9+
<artifactId>commandapi</artifactId>
10+
<version>8.6.0-SNAPSHOT</version>
11+
</parent>
12+
13+
<artifactId>commandapi-kotlin</artifactId>
14+
15+
<properties>
16+
<kotlin.version>1.7.20</kotlin.version>
17+
</properties>
18+
19+
<build>
20+
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
21+
<plugins>
22+
<plugin>
23+
<groupId>org.jetbrains.kotlin</groupId>
24+
<artifactId>kotlin-maven-plugin</artifactId>
25+
<version>${kotlin.version}</version>
26+
27+
<executions>
28+
<execution>
29+
<id>compile</id>
30+
<goals>
31+
<goal>compile</goal>
32+
</goals>
33+
</execution>
34+
35+
<execution>
36+
<id>test-compile</id>
37+
<goals>
38+
<goal>test-compile</goal>
39+
</goals>
40+
</execution>
41+
</executions>
42+
</plugin>
43+
</plugins>
44+
</build>
45+
46+
<repositories>
47+
<repository>
48+
<id>papermc</id>
49+
<url>https://papermc.io/repo/repository/maven-public/</url>
50+
</repository>
51+
<repository>
52+
<id>sonatype</id>
53+
<url>https://oss.sonatype.org/content/groups/public/</url>
54+
</repository>
55+
</repositories>
56+
57+
<dependencies>
58+
<dependency>
59+
<groupId>io.papermc.paper</groupId>
60+
<artifactId>paper-api</artifactId>
61+
<version>${paper.version}</version>
62+
<scope>provided</scope>
63+
</dependency>
64+
<dependency>
65+
<groupId>org.jetbrains.kotlin</groupId>
66+
<artifactId>kotlin-stdlib</artifactId>
67+
<version>${kotlin.version}</version>
68+
<scope>provided</scope>
69+
</dependency>
70+
<dependency>
71+
<groupId>org.jetbrains.kotlin</groupId>
72+
<artifactId>kotlin-test</artifactId>
73+
<version>${kotlin.version}</version>
74+
<scope>test</scope>
75+
</dependency>
76+
<dependency>
77+
<groupId>dev.jorel</groupId>
78+
<artifactId>commandapi-core</artifactId>
79+
<version>${project.version}</version>
80+
<scope>provided</scope>
81+
</dependency>
82+
</dependencies>
83+
</project>

commandapi-kotlin/src/main/kotlin/dev/jorel/commandapi/kotlindsl/CommandAPICommandDSL.kt

Lines changed: 203 additions & 0 deletions
Large diffs are not rendered by default.

commandapi-kotlin/src/main/kotlin/dev/jorel/commandapi/kotlindsl/CommandTreeDSL.kt

Lines changed: 341 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
import dev.jorel.commandapi.*
2+
import dev.jorel.commandapi.kotlindsl.*
3+
import dev.jorel.commandapi.arguments.ArgumentSuggestions
4+
import dev.jorel.commandapi.arguments.LiteralArgument.of
5+
import dev.jorel.commandapi.arguments.StringArgument
6+
import org.bukkit.Bukkit
7+
import org.bukkit.command.CommandSender
8+
import org.bukkit.entity.Player
9+
import org.bukkit.inventory.ItemStack
10+
11+
fun sendMessageToCommand() {
12+
/* ANCHOR: dslSendMessageToCommand */
13+
commandTree("sendmessageto") {
14+
playerArgument("player") { // Defines a new PlayerArgument("player")
15+
greedyArgument("msg") { // Defines a new GreedyStringArgument("msg)
16+
anyExecutor { _, args -> // Command can be executed by anyone and anything (such as entities, the console, etc.)
17+
val player: Player = args[0] as Player
18+
val message: String = args[1] as String
19+
player.sendMessage(message)
20+
}
21+
}
22+
}
23+
}
24+
/* ANCHOR_END: dslSendMessageToCommand */
25+
26+
/* ANCHOR: dslSendMessageToCommand2 */
27+
commandAPICommand("sendmessageto") {
28+
playerArgument("player") // Defines a new PlayerArgument("player")
29+
greedyArgument("msg") // Defines a new GreedyStringArgument("msg)
30+
anyExecutor { _, args -> // Command can be executed by anyone and anything (such as entities, the console, etc.)
31+
val player: Player = args[0] as Player
32+
val message: String = args[1] as String
33+
player.sendMessage(message)
34+
}
35+
}
36+
/* ANCHOR_END: dslSendMessageToCommand2 */
37+
38+
/* ANCHOR: dslSendMessageToCommandRequirement */
39+
commandTree("sendMessageTo") {
40+
playerArgument("player") {
41+
greedyArgument("msg") {
42+
playerExecutor { _, args ->
43+
val player: Player = args[0] as Player
44+
val message: String = args[1] as String
45+
player.sendMessage(message)
46+
}
47+
}
48+
}
49+
requirement(of("broadcast"), { sender: CommandSender -> sender.isOp }) { // Define a new LiteralArgument("broadcast") that requires the CommandSender to be a player who is a server operator
50+
greedyArgument("msg") {
51+
playerExecutor { _, args ->
52+
val message: String = args[0] as String
53+
Bukkit.broadcastMessage(message)
54+
}
55+
}
56+
}
57+
}
58+
/* ANCHOR_END: dslSendMessageToCommandRequirement */
59+
60+
/* ANCHOR: dslSendMessageToCommandRequirement2 */
61+
commandAPICommand("sendMessageTo") {
62+
playerArgument("player")
63+
greedyArgument("msg")
64+
playerExecutor { _, args ->
65+
val player: Player = args[0] as Player
66+
val message: String = args[1] as String
67+
player.sendMessage(message)
68+
}
69+
}
70+
71+
commandAPICommand("sendMessageTo") {
72+
requirement(of("broadcast"), { sender: CommandSender -> sender.isOp }) // Define a new LiteralArgument("broadcast") that requires the CommandSender to be a player who is a server operator
73+
greedyArgument("msg")
74+
playerExecutor { _, args ->
75+
val message: String = args[0] as String
76+
Bukkit.broadcastMessage(message)
77+
}
78+
}
79+
/* ANCHOR_END: dslSendMessageToCommandRequirement2 */
80+
81+
/* ANCHOR: dslCommandRequirements */
82+
commandTree("commandRequirement", {sender: CommandSender -> sender.isOp}) {
83+
playerExecutor { player, _ ->
84+
player.sendMessage("This command can only be executed by players who are server operators.")
85+
}
86+
}
87+
/* ANCHOR_END: dslCommandRequirements */
88+
89+
/* ANCHOR: dslCommandRequirements2 */
90+
commandAPICommand("commandRequirement", {sender: CommandSender -> sender.isOp}) {
91+
playerExecutor { player, _ ->
92+
player.sendMessage("This command can only be executed by players who are server operators.")
93+
}
94+
}
95+
/* ANCHOR_END: dslCommandRequirements2 */
96+
}
97+
98+
fun moreExamples() {
99+
/* ANCHOR: optionalArgument */
100+
commandTree("optionalArgument") {
101+
literalArgument("give") {
102+
itemStackArgument("item") {
103+
playerExecutor { player, args -> // This will let you execute "/optionalArgument give minecraft:stick"
104+
val itemStack: ItemStack = args[0] as ItemStack
105+
player.inventory.addItem(itemStack)
106+
}
107+
integerArgument("amount") {
108+
playerExecutor { player, args -> // This will let you execute "/optionalArgument give minecraft:stick 5"
109+
val itemStack: ItemStack = args[0] as ItemStack
110+
val amount: Int = args[1] as Int
111+
itemStack.amount = amount
112+
player.inventory.addItem(itemStack)
113+
}
114+
}
115+
}
116+
}
117+
}
118+
/* ANCHOR_END: optionalArgument */
119+
120+
/* ANCHOR: optionalArgument2 */
121+
commandAPICommand("optionalArgument") {
122+
literalArgument("give")
123+
itemStackArgument("item")
124+
playerExecutor { player, args -> // This will let you execute "/optionalArgument give minecraft:stick"
125+
val itemStack: ItemStack = args[0] as ItemStack
126+
player.inventory.addItem(itemStack)
127+
}
128+
}
129+
130+
commandAPICommand("optionalArgument") {
131+
literalArgument("give")
132+
itemStackArgument("item")
133+
integerArgument("amount")
134+
playerExecutor { player, args -> // This will let you execute "/optionalArgument give minecraft:stick 5"
135+
val itemStack: ItemStack = args[0] as ItemStack
136+
val amount: Int = args[1] as Int
137+
itemStack.amount = amount
138+
player.inventory.addItem(itemStack)
139+
}
140+
}
141+
/* ANCHOR_END: optionalArgument2 */
142+
143+
/* ANCHOR: replaceSuggestions */
144+
commandTree("replaceSuggestions") {
145+
argument(StringArgument("strings").replaceSuggestions(ArgumentSuggestions.strings("one", "two", "three"))) { // Implement an argument that has suggestions
146+
playerExecutor { player, args ->
147+
player.sendMessage("You chose option ${args[0] as String}!")
148+
}
149+
}
150+
}
151+
/* ANCHOR_END: replaceSuggestions */
152+
153+
/* ANCHOR: replaceSuggestions2 */
154+
commandAPICommand("replaceSuggestions") {
155+
argument(StringArgument("strings").replaceSuggestions(ArgumentSuggestions.strings("one", "two", "three"))) // Implement an argument that has suggestions
156+
playerExecutor { player, args ->
157+
player.sendMessage("You chose option ${args[0] as String}!")
158+
}
159+
}
160+
/* ANCHOR_END: replaceSuggestions2 */
161+
}

docssrc/src/SUMMARY.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@
100100
- [Annotations](./annotations.md)
101101
- [Registering annotation-based commands](./registeringannotations.md)
102102

103+
# Kotlin-based Commands
104+
- [Kotlin-based commands](./kotlinintro.md)
105+
- [Using the DSL](./kotlindsl.md)
106+
103107
# CommandAPI Utilities
104108

105109
- [Command conversion](./conversion.md)

0 commit comments

Comments
 (0)
0