From bf86f2bf72420a2329188d2b9722bc9e3ac93e7c Mon Sep 17 00:00:00 2001 From: DerEchtePilz Date: Thu, 17 Aug 2023 14:28:54 +0200 Subject: [PATCH 01/14] Add Kotlin DSL support for delegations --- .../examples/kotlin/ExamplesKotlinDSL.kt | 3 - .../commandapi-bukkit-kotlin/pom.xml | 7 +- .../kotlindsl/CommandAPICommandDSL.kt | 0 .../commandapi/kotlindsl/CommandTreeDSL.kt | 0 .../jorel/commandapi/kotlindsl/ExecutorDSL.kt | 0 .../commandapi-core-kotlin/pom.xml | 75 ++++++++++++ .../commandapi/kotlindsl/CommandAPIDSL.kt | 7 ++ .../commandapi-velocity-kotlin/pom.xml | 16 ++- .../kotlindsl/CommandAPICommandDSL.kt | 0 .../commandapi/kotlindsl/CommandTreeDSL.kt | 0 .../jorel/commandapi/kotlindsl/ExecutorDSL.kt | 0 commandapi-kotlin/pom.xml | 15 +++ .../commandapi-bukkit-kotlin/.gitignore | 113 ------------------ .../commandapi-bukkit/pom.xml | 1 - .../commandapi-velocity-kotlin/.gitignore | 113 ------------------ .../commandapi-velocity/pom.xml | 1 - commandapi-platforms/pom.xml | 4 + pom.xml | 1 + 18 files changed, 115 insertions(+), 241 deletions(-) rename {commandapi-platforms/commandapi-bukkit => commandapi-kotlin}/commandapi-bukkit-kotlin/pom.xml (92%) rename {commandapi-platforms/commandapi-bukkit => commandapi-kotlin}/commandapi-bukkit-kotlin/src/main/kotlin/dev/jorel/commandapi/kotlindsl/CommandAPICommandDSL.kt (100%) rename {commandapi-platforms/commandapi-bukkit => commandapi-kotlin}/commandapi-bukkit-kotlin/src/main/kotlin/dev/jorel/commandapi/kotlindsl/CommandTreeDSL.kt (100%) rename {commandapi-platforms/commandapi-bukkit => commandapi-kotlin}/commandapi-bukkit-kotlin/src/main/kotlin/dev/jorel/commandapi/kotlindsl/ExecutorDSL.kt (100%) create mode 100644 commandapi-kotlin/commandapi-core-kotlin/pom.xml create mode 100644 commandapi-kotlin/commandapi-core-kotlin/src/main/kotlin/dev/jorel/commandapi/kotlindsl/CommandAPIDSL.kt rename {commandapi-platforms/commandapi-velocity => commandapi-kotlin}/commandapi-velocity-kotlin/pom.xml (87%) rename {commandapi-platforms/commandapi-velocity => commandapi-kotlin}/commandapi-velocity-kotlin/src/main/kotlin/dev/jorel/commandapi/kotlindsl/CommandAPICommandDSL.kt (100%) rename {commandapi-platforms/commandapi-velocity => commandapi-kotlin}/commandapi-velocity-kotlin/src/main/kotlin/dev/jorel/commandapi/kotlindsl/CommandTreeDSL.kt (100%) rename {commandapi-platforms/commandapi-velocity => commandapi-kotlin}/commandapi-velocity-kotlin/src/main/kotlin/dev/jorel/commandapi/kotlindsl/ExecutorDSL.kt (100%) create mode 100644 commandapi-kotlin/pom.xml delete mode 100644 commandapi-platforms/commandapi-bukkit/commandapi-bukkit-kotlin/.gitignore delete mode 100644 commandapi-platforms/commandapi-velocity/commandapi-velocity-kotlin/.gitignore diff --git a/commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/ExamplesKotlinDSL.kt b/commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/ExamplesKotlinDSL.kt index bd5efa909d..7ebf364388 100644 --- a/commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/ExamplesKotlinDSL.kt +++ b/commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/ExamplesKotlinDSL.kt @@ -1,10 +1,7 @@ package dev.jorel.commandapi.examples.kotlin import de.tr7zw.changeme.nbtapi.NBTContainer -import dev.jorel.commandapi.* import dev.jorel.commandapi.arguments.* -import dev.jorel.commandapi.arguments.LiteralArgument.of -import dev.jorel.commandapi.executors.* import dev.jorel.commandapi.kotlindsl.* import dev.jorel.commandapi.wrappers.* import dev.jorel.commandapi.wrappers.Rotation diff --git a/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-kotlin/pom.xml b/commandapi-kotlin/commandapi-bukkit-kotlin/pom.xml similarity index 92% rename from commandapi-platforms/commandapi-bukkit/commandapi-bukkit-kotlin/pom.xml rename to commandapi-kotlin/commandapi-bukkit-kotlin/pom.xml index e077d9f619..94e04ef446 100644 --- a/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-kotlin/pom.xml +++ b/commandapi-kotlin/commandapi-bukkit-kotlin/pom.xml @@ -6,7 +6,7 @@ dev.jorel - commandapi-bukkit + commandapi-kotlin 9.2.0-SNAPSHOT @@ -89,5 +89,10 @@ ${project.version} provided + + dev.jorel + commandapi-core-kotlin + 9.2.0-SNAPSHOT + diff --git a/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-kotlin/src/main/kotlin/dev/jorel/commandapi/kotlindsl/CommandAPICommandDSL.kt b/commandapi-kotlin/commandapi-bukkit-kotlin/src/main/kotlin/dev/jorel/commandapi/kotlindsl/CommandAPICommandDSL.kt similarity index 100% rename from commandapi-platforms/commandapi-bukkit/commandapi-bukkit-kotlin/src/main/kotlin/dev/jorel/commandapi/kotlindsl/CommandAPICommandDSL.kt rename to commandapi-kotlin/commandapi-bukkit-kotlin/src/main/kotlin/dev/jorel/commandapi/kotlindsl/CommandAPICommandDSL.kt diff --git a/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-kotlin/src/main/kotlin/dev/jorel/commandapi/kotlindsl/CommandTreeDSL.kt b/commandapi-kotlin/commandapi-bukkit-kotlin/src/main/kotlin/dev/jorel/commandapi/kotlindsl/CommandTreeDSL.kt similarity index 100% rename from commandapi-platforms/commandapi-bukkit/commandapi-bukkit-kotlin/src/main/kotlin/dev/jorel/commandapi/kotlindsl/CommandTreeDSL.kt rename to commandapi-kotlin/commandapi-bukkit-kotlin/src/main/kotlin/dev/jorel/commandapi/kotlindsl/CommandTreeDSL.kt diff --git a/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-kotlin/src/main/kotlin/dev/jorel/commandapi/kotlindsl/ExecutorDSL.kt b/commandapi-kotlin/commandapi-bukkit-kotlin/src/main/kotlin/dev/jorel/commandapi/kotlindsl/ExecutorDSL.kt similarity index 100% rename from commandapi-platforms/commandapi-bukkit/commandapi-bukkit-kotlin/src/main/kotlin/dev/jorel/commandapi/kotlindsl/ExecutorDSL.kt rename to commandapi-kotlin/commandapi-bukkit-kotlin/src/main/kotlin/dev/jorel/commandapi/kotlindsl/ExecutorDSL.kt diff --git a/commandapi-kotlin/commandapi-core-kotlin/pom.xml b/commandapi-kotlin/commandapi-core-kotlin/pom.xml new file mode 100644 index 0000000000..0ea9f69396 --- /dev/null +++ b/commandapi-kotlin/commandapi-core-kotlin/pom.xml @@ -0,0 +1,75 @@ + + + 4.0.0 + + + dev.jorel + commandapi-kotlin + 9.2.0-SNAPSHOT + + + commandapi-core-kotlin + + + src/main/kotlin + + + org.jetbrains.kotlin + kotlin-maven-plugin + ${kotlin.version} + + + compile + compile + + compile + + + + test-compile + test-compile + + test-compile + + + + + 16 + + + + org.apache.maven.plugins + maven-jar-plugin + + + empty-javadoc-jar + package + + jar + + + javadoc + ${basedir}/javadoc + + + + + + + + + + org.jetbrains.kotlin + kotlin-stdlib + ${kotlin.version} + provided + + + dev.jorel + commandapi-core + 9.2.0-SNAPSHOT + + + diff --git a/commandapi-kotlin/commandapi-core-kotlin/src/main/kotlin/dev/jorel/commandapi/kotlindsl/CommandAPIDSL.kt b/commandapi-kotlin/commandapi-core-kotlin/src/main/kotlin/dev/jorel/commandapi/kotlindsl/CommandAPIDSL.kt new file mode 100644 index 0000000000..c882d075b2 --- /dev/null +++ b/commandapi-kotlin/commandapi-core-kotlin/src/main/kotlin/dev/jorel/commandapi/kotlindsl/CommandAPIDSL.kt @@ -0,0 +1,7 @@ +package dev.jorel.commandapi.kotlindsl + +import dev.jorel.commandapi.executors.CommandArguments +import kotlin.reflect.KProperty + +// CommandArguments DSL +inline operator fun CommandArguments.getValue(nothing: Nothing?, property: KProperty<*>) = this[property.name] as T \ No newline at end of file diff --git a/commandapi-platforms/commandapi-velocity/commandapi-velocity-kotlin/pom.xml b/commandapi-kotlin/commandapi-velocity-kotlin/pom.xml similarity index 87% rename from commandapi-platforms/commandapi-velocity/commandapi-velocity-kotlin/pom.xml rename to commandapi-kotlin/commandapi-velocity-kotlin/pom.xml index e4d7384c08..5ff03cea33 100644 --- a/commandapi-platforms/commandapi-velocity/commandapi-velocity-kotlin/pom.xml +++ b/commandapi-kotlin/commandapi-velocity-kotlin/pom.xml @@ -6,12 +6,11 @@ dev.jorel - commandapi-velocity + commandapi-kotlin 9.2.0-SNAPSHOT commandapi-velocity-kotlin - jar src/main/kotlin @@ -59,13 +58,18 @@ provided - + dev.jorel commandapi-velocity-core 9.2.0-SNAPSHOT provided + + dev.jorel + commandapi-core-kotlin + 9.2.0-SNAPSHOT + @@ -74,11 +78,5 @@ ${kotlin.version} provided - - org.jetbrains.kotlin - kotlin-test - ${kotlin.version} - test - diff --git a/commandapi-platforms/commandapi-velocity/commandapi-velocity-kotlin/src/main/kotlin/dev/jorel/commandapi/kotlindsl/CommandAPICommandDSL.kt b/commandapi-kotlin/commandapi-velocity-kotlin/src/main/kotlin/dev/jorel/commandapi/kotlindsl/CommandAPICommandDSL.kt similarity index 100% rename from commandapi-platforms/commandapi-velocity/commandapi-velocity-kotlin/src/main/kotlin/dev/jorel/commandapi/kotlindsl/CommandAPICommandDSL.kt rename to commandapi-kotlin/commandapi-velocity-kotlin/src/main/kotlin/dev/jorel/commandapi/kotlindsl/CommandAPICommandDSL.kt diff --git a/commandapi-platforms/commandapi-velocity/commandapi-velocity-kotlin/src/main/kotlin/dev/jorel/commandapi/kotlindsl/CommandTreeDSL.kt b/commandapi-kotlin/commandapi-velocity-kotlin/src/main/kotlin/dev/jorel/commandapi/kotlindsl/CommandTreeDSL.kt similarity index 100% rename from commandapi-platforms/commandapi-velocity/commandapi-velocity-kotlin/src/main/kotlin/dev/jorel/commandapi/kotlindsl/CommandTreeDSL.kt rename to commandapi-kotlin/commandapi-velocity-kotlin/src/main/kotlin/dev/jorel/commandapi/kotlindsl/CommandTreeDSL.kt diff --git a/commandapi-platforms/commandapi-velocity/commandapi-velocity-kotlin/src/main/kotlin/dev/jorel/commandapi/kotlindsl/ExecutorDSL.kt b/commandapi-kotlin/commandapi-velocity-kotlin/src/main/kotlin/dev/jorel/commandapi/kotlindsl/ExecutorDSL.kt similarity index 100% rename from commandapi-platforms/commandapi-velocity/commandapi-velocity-kotlin/src/main/kotlin/dev/jorel/commandapi/kotlindsl/ExecutorDSL.kt rename to commandapi-kotlin/commandapi-velocity-kotlin/src/main/kotlin/dev/jorel/commandapi/kotlindsl/ExecutorDSL.kt diff --git a/commandapi-kotlin/pom.xml b/commandapi-kotlin/pom.xml new file mode 100644 index 0000000000..242135ae6f --- /dev/null +++ b/commandapi-kotlin/pom.xml @@ -0,0 +1,15 @@ + + + 4.0.0 + + dev.jorel + commandapi + 9.2.0-SNAPSHOT + + + commandapi-kotlin + pom + + \ No newline at end of file diff --git a/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-kotlin/.gitignore b/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-kotlin/.gitignore deleted file mode 100644 index 4788b4b454..0000000000 --- a/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-kotlin/.gitignore +++ /dev/null @@ -1,113 +0,0 @@ -# User-specific stuff -.idea/ - -*.iml -*.ipr -*.iws - -# IntelliJ -out/ - -# Compiled class file -*.class - -# Log file -*.log - -# BlueJ files -*.ctxt - -# Package Files # -*.jar -*.war -*.nar -*.ear -*.zip -*.tar.gz -*.rar - -# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml -hs_err_pid* - -*~ - -# temporary files which can be created if a process still has a handle open of a deleted file -.fuse_hidden* - -# KDE directory preferences -.directory - -# Linux trash folder which might appear on any partition or disk -.Trash-* - -# .nfs files are created when an open file is removed but is still being accessed -.nfs* - -# General -.DS_Store -.AppleDouble -.LSOverride - -# Icon must end with two \r -Icon - -# Thumbnails -._* - -# Files that might appear in the root of a volume -.DocumentRevisions-V100 -.fseventsd -.Spotlight-V100 -.TemporaryItems -.Trashes -.VolumeIcon.icns -.com.apple.timemachine.donotpresent - -# Directories potentially created on remote AFP share -.AppleDB -.AppleDesktop -Network Trash Folder -Temporary Items -.apdisk - -# Windows thumbnail cache files -Thumbs.db -Thumbs.db:encryptable -ehthumbs.db -ehthumbs_vista.db - -# Dump file -*.stackdump - -# Folder config file -[Dd]esktop.ini - -# Recycle Bin used on file shares -$RECYCLE.BIN/ - -# Windows Installer files -*.cab -*.msi -*.msix -*.msm -*.msp - -# Windows shortcuts -*.lnk - -target/ - -pom.xml.tag -pom.xml.releaseBackup -pom.xml.versionsBackup -pom.xml.next - -release.properties -dependency-reduced-pom.xml -buildNumber.properties -.mvn/timing.properties -.mvn/wrapper/maven-wrapper.jar -.flattened-pom.xml - -# Common working directory -run/ diff --git a/commandapi-platforms/commandapi-bukkit/pom.xml b/commandapi-platforms/commandapi-bukkit/pom.xml index ff2f6e5edd..2b4409c156 100644 --- a/commandapi-platforms/commandapi-bukkit/pom.xml +++ b/commandapi-platforms/commandapi-bukkit/pom.xml @@ -27,6 +27,5 @@ commandapi-bukkit-plugin commandapi-bukkit-test commandapi-bukkit-shade - commandapi-bukkit-kotlin \ No newline at end of file diff --git a/commandapi-platforms/commandapi-velocity/commandapi-velocity-kotlin/.gitignore b/commandapi-platforms/commandapi-velocity/commandapi-velocity-kotlin/.gitignore deleted file mode 100644 index 4788b4b454..0000000000 --- a/commandapi-platforms/commandapi-velocity/commandapi-velocity-kotlin/.gitignore +++ /dev/null @@ -1,113 +0,0 @@ -# User-specific stuff -.idea/ - -*.iml -*.ipr -*.iws - -# IntelliJ -out/ - -# Compiled class file -*.class - -# Log file -*.log - -# BlueJ files -*.ctxt - -# Package Files # -*.jar -*.war -*.nar -*.ear -*.zip -*.tar.gz -*.rar - -# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml -hs_err_pid* - -*~ - -# temporary files which can be created if a process still has a handle open of a deleted file -.fuse_hidden* - -# KDE directory preferences -.directory - -# Linux trash folder which might appear on any partition or disk -.Trash-* - -# .nfs files are created when an open file is removed but is still being accessed -.nfs* - -# General -.DS_Store -.AppleDouble -.LSOverride - -# Icon must end with two \r -Icon - -# Thumbnails -._* - -# Files that might appear in the root of a volume -.DocumentRevisions-V100 -.fseventsd -.Spotlight-V100 -.TemporaryItems -.Trashes -.VolumeIcon.icns -.com.apple.timemachine.donotpresent - -# Directories potentially created on remote AFP share -.AppleDB -.AppleDesktop -Network Trash Folder -Temporary Items -.apdisk - -# Windows thumbnail cache files -Thumbs.db -Thumbs.db:encryptable -ehthumbs.db -ehthumbs_vista.db - -# Dump file -*.stackdump - -# Folder config file -[Dd]esktop.ini - -# Recycle Bin used on file shares -$RECYCLE.BIN/ - -# Windows Installer files -*.cab -*.msi -*.msix -*.msm -*.msp - -# Windows shortcuts -*.lnk - -target/ - -pom.xml.tag -pom.xml.releaseBackup -pom.xml.versionsBackup -pom.xml.next - -release.properties -dependency-reduced-pom.xml -buildNumber.properties -.mvn/timing.properties -.mvn/wrapper/maven-wrapper.jar -.flattened-pom.xml - -# Common working directory -run/ diff --git a/commandapi-platforms/commandapi-velocity/pom.xml b/commandapi-platforms/commandapi-velocity/pom.xml index 1d856d7901..46695454fd 100644 --- a/commandapi-platforms/commandapi-velocity/pom.xml +++ b/commandapi-platforms/commandapi-velocity/pom.xml @@ -21,6 +21,5 @@ commandapi-velocity-plugin commandapi-velocity-plugin-test commandapi-velocity-shade - commandapi-velocity-kotlin \ No newline at end of file diff --git a/commandapi-platforms/pom.xml b/commandapi-platforms/pom.xml index a1fd767190..f5303071eb 100644 --- a/commandapi-platforms/pom.xml +++ b/commandapi-platforms/pom.xml @@ -19,6 +19,8 @@ Platform.Bukkit commandapi-bukkit + ../commandapi-kotlin/commandapi-core-kotlin + ../commandapi-kotlin/commandapi-bukkit-kotlin @@ -31,6 +33,8 @@ Platform.Velocity commandapi-velocity + ../commandapi-kotlin/commandapi-core-kotlin + ../commandapi-kotlin/commandapi-velocity-kotlin diff --git a/pom.xml b/pom.xml index 6d69ce1a71..8cf9795596 100644 --- a/pom.xml +++ b/pom.xml @@ -63,6 +63,7 @@ commandapi-platforms + commandapi-kotlin commandapi-annotations From c8ac59c4d9dd9d7e170080c22b246f221a2ceaaf Mon Sep 17 00:00:00 2001 From: DerEchtePilz Date: Thu, 17 Aug 2023 17:10:36 +0200 Subject: [PATCH 02/14] Set scope to provided for commandapi-core dependency --- commandapi-kotlin/commandapi-core-kotlin/pom.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/commandapi-kotlin/commandapi-core-kotlin/pom.xml b/commandapi-kotlin/commandapi-core-kotlin/pom.xml index 0ea9f69396..bd2a44aa20 100644 --- a/commandapi-kotlin/commandapi-core-kotlin/pom.xml +++ b/commandapi-kotlin/commandapi-core-kotlin/pom.xml @@ -70,6 +70,7 @@ dev.jorel commandapi-core 9.2.0-SNAPSHOT + provided From 0c728a06a847c5f06dd67071d465ca88fb47329e Mon Sep 17 00:00:00 2001 From: DerEchtePilz Date: Tue, 22 Aug 2023 14:10:34 +0200 Subject: [PATCH 03/14] Change version to use project.version --- commandapi-kotlin/commandapi-bukkit-kotlin/pom.xml | 2 +- commandapi-kotlin/commandapi-core-kotlin/pom.xml | 2 +- commandapi-kotlin/commandapi-velocity-kotlin/pom.xml | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/commandapi-kotlin/commandapi-bukkit-kotlin/pom.xml b/commandapi-kotlin/commandapi-bukkit-kotlin/pom.xml index 94e04ef446..d3246a41ce 100644 --- a/commandapi-kotlin/commandapi-bukkit-kotlin/pom.xml +++ b/commandapi-kotlin/commandapi-bukkit-kotlin/pom.xml @@ -92,7 +92,7 @@ dev.jorel commandapi-core-kotlin - 9.2.0-SNAPSHOT + ${project.version} diff --git a/commandapi-kotlin/commandapi-core-kotlin/pom.xml b/commandapi-kotlin/commandapi-core-kotlin/pom.xml index bd2a44aa20..45e7006a6c 100644 --- a/commandapi-kotlin/commandapi-core-kotlin/pom.xml +++ b/commandapi-kotlin/commandapi-core-kotlin/pom.xml @@ -69,7 +69,7 @@ dev.jorel commandapi-core - 9.2.0-SNAPSHOT + ${project.version} provided diff --git a/commandapi-kotlin/commandapi-velocity-kotlin/pom.xml b/commandapi-kotlin/commandapi-velocity-kotlin/pom.xml index 5ff03cea33..440380e5b0 100644 --- a/commandapi-kotlin/commandapi-velocity-kotlin/pom.xml +++ b/commandapi-kotlin/commandapi-velocity-kotlin/pom.xml @@ -62,13 +62,13 @@ dev.jorel commandapi-velocity-core - 9.2.0-SNAPSHOT + ${project.version} provided dev.jorel commandapi-core-kotlin - 9.2.0-SNAPSHOT + ${project.version} From 3f6ae42c4fa2b689df16be578fcfc8ae04fb1c6f Mon Sep 17 00:00:00 2001 From: DerEchtePilz Date: Tue, 22 Aug 2023 14:44:14 +0200 Subject: [PATCH 04/14] Adds tests for Kotlin DSL delegations --- .../test/dsltests/DelegationTests.kt | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-kotlin-test/src/test/kotlin/dev/jorel/commandapi/test/dsltests/DelegationTests.kt diff --git a/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-kotlin-test/src/test/kotlin/dev/jorel/commandapi/test/dsltests/DelegationTests.kt b/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-kotlin-test/src/test/kotlin/dev/jorel/commandapi/test/dsltests/DelegationTests.kt new file mode 100644 index 0000000000..64692744b8 --- /dev/null +++ b/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-kotlin-test/src/test/kotlin/dev/jorel/commandapi/test/dsltests/DelegationTests.kt @@ -0,0 +1,79 @@ +package dev.jorel.commandapi.test.dsltests + +import be.seeseemelk.mockbukkit.entity.PlayerMock +import dev.jorel.commandapi.executors.CommandArguments +import dev.jorel.commandapi.kotlindsl.* +import dev.jorel.commandapi.test.Mut +import dev.jorel.commandapi.test.TestBase +import org.bukkit.entity.Player +import org.junit.jupiter.api.AfterEach +import org.junit.jupiter.api.Assertions.assertDoesNotThrow +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows +import java.lang.NullPointerException + +class DelegationTests : TestBase() { + + /********* + * Setup * + *********/ + + @BeforeEach + override fun setUp() { + super.setUp() + } + + @AfterEach + override fun tearDown() { + super.tearDown() + } + + /********* + * Tests * + *********/ + + @Test + fun failingDelegationTestWithKotlinDSL() { + val results: Mut = Mut.of() + + commandAPICommand("test") { + stringArgument("string") + playerArgument("target") + playerExecutor { _, args -> + val string: String by args + val player: Player by args + results.set(string) + results.set(player) + } + } + + val player: PlayerMock = server.addPlayer("Player1") + + // This should throw an exception because the 'Player1' is assigned + // to a variable that does not match the node name + assertThrows { + server.dispatchCommand(player, "test testString Player1") + } + } + + @Test + fun passingDelegationTestWithKotlinDSL() { + commandAPICommand("test") { + stringArgument("string") + playerArgument("target") + playerExecutor { _, args -> + val string: String by args + val target: Player by args + } + } + + val player: PlayerMock = server.addPlayer("Player1") + + assertDoesNotThrow { + server.dispatchCommand(player, "test testString Player1") + } + } + +} \ No newline at end of file From 40e2a64ed8b5519de55f8327e7f4bb6e05c78764 Mon Sep 17 00:00:00 2001 From: DerEchtePilz Date: Thu, 24 Aug 2023 17:30:18 +0200 Subject: [PATCH 05/14] Improve delegation test by checking for correct values (just to be sure) --- .../commandapi/test/dsltests/DelegationTests.kt | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-kotlin-test/src/test/kotlin/dev/jorel/commandapi/test/dsltests/DelegationTests.kt b/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-kotlin-test/src/test/kotlin/dev/jorel/commandapi/test/dsltests/DelegationTests.kt index 64692744b8..dcb50eac79 100644 --- a/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-kotlin-test/src/test/kotlin/dev/jorel/commandapi/test/dsltests/DelegationTests.kt +++ b/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-kotlin-test/src/test/kotlin/dev/jorel/commandapi/test/dsltests/DelegationTests.kt @@ -1,10 +1,10 @@ package dev.jorel.commandapi.test.dsltests import be.seeseemelk.mockbukkit.entity.PlayerMock -import dev.jorel.commandapi.executors.CommandArguments import dev.jorel.commandapi.kotlindsl.* import dev.jorel.commandapi.test.Mut import dev.jorel.commandapi.test.TestBase +import net.kyori.adventure.text.Component import org.bukkit.entity.Player import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.Assertions.assertDoesNotThrow @@ -12,7 +12,6 @@ import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test import org.junit.jupiter.api.assertThrows -import java.lang.NullPointerException class DelegationTests : TestBase() { @@ -53,19 +52,24 @@ class DelegationTests : TestBase() { // This should throw an exception because the 'Player1' is assigned // to a variable that does not match the node name - assertThrows { + val exception = assertThrows { server.dispatchCommand(player, "test testString Player1") } + assertEquals("null cannot be cast to non-null type org.bukkit.entity.Player", exception.message) } @Test fun passingDelegationTestWithKotlinDSL() { + val results: Mut = Mut.of() + commandAPICommand("test") { stringArgument("string") playerArgument("target") playerExecutor { _, args -> val string: String by args val target: Player by args + results.set(string) + results.set(target) } } @@ -74,6 +78,11 @@ class DelegationTests : TestBase() { assertDoesNotThrow { server.dispatchCommand(player, "test testString Player1") } + + assertEquals("testString", results.get()) + assertEquals(player, results.get()) + + assertNoMoreResults(results) } } \ No newline at end of file From c2a5cdd5e3e05074f48d7607e2a38e16795e0707 Mon Sep 17 00:00:00 2001 From: DerEchtePilz Date: Thu, 21 Sep 2023 22:38:00 +0200 Subject: [PATCH 06/14] Update global changelog --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 7c095e19e9..4fc55655ab 100644 --- a/README.md +++ b/README.md @@ -376,6 +376,7 @@ This is the current roadmap for the CommandAPI (as of 11th May 2023):
  • https://github.com/JorelAli/CommandAPI/issues/487 Added support for disabling integer centering for location arguments
  • +
  • https://github.com/JorelAli/CommandAPI/pull/482 Adds Kotlin DSL support for delegations
From a1898ad2571d1bbd32c39684d52d0f03501bfc03 Mon Sep 17 00:00:00 2001 From: DerEchtePilz Date: Thu, 21 Sep 2023 23:03:22 +0200 Subject: [PATCH 07/14] Add documentation for Kotlin DSL delegations --- .../examples/kotlin/ExamplesKotlinDSL.kt | 26 +++++++++++---- docssrc/src/intro.md | 1 + docssrc/src/kotlindsl.md | 33 ++++++++++++++++--- 3 files changed, 49 insertions(+), 11 deletions(-) diff --git a/commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/ExamplesKotlinDSL.kt b/commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/ExamplesKotlinDSL.kt index 7ebf364388..1b0d96e032 100644 --- a/commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/ExamplesKotlinDSL.kt +++ b/commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/ExamplesKotlinDSL.kt @@ -1008,6 +1008,18 @@ commandAPICommand("commandRequirement") { /* ANCHOR_END: kotlindsl6 */ /* ANCHOR: kotlindsl7 */ +commandAPICommand("mycommand") { + stringArgument("string") + playerArgument("target") + playerExecutor { player, args -> + val string: String by args + val target: Player by args + // Implementation... + } +} +/* ANCHOR_END: kotlindsl7 */ + +/* ANCHOR: kotlindsl8 */ commandTree("optionalArgument") { literalArgument("give") { itemStackArgument("item") { @@ -1025,9 +1037,9 @@ commandTree("optionalArgument") { } } } -/* ANCHOR_END: kotlindsl7 */ +/* ANCHOR_END: kotlindsl8 */ -/* ANCHOR: kotlindsl8 */ +/* ANCHOR: kotlindsl9 */ commandAPICommand("optionalArgument") { literalArgument("give") itemStackArgument("item") @@ -1042,9 +1054,9 @@ commandAPICommand("optionalArgument") { player.inventory.addItem(itemStack) } } -/* ANCHOR_END: kotlindsl8 */ +/* ANCHOR_END: kotlindsl9 */ -/* ANCHOR: kotlindsl9 */ +/* ANCHOR: kotlindsl10 */ commandTree("replaceSuggestions") { stringArgument("strings") { replaceSuggestions(ArgumentSuggestions.strings("one", "two", "three")) // Replaces the suggestions for the "strings" StringArgument @@ -1053,9 +1065,9 @@ commandTree("replaceSuggestions") { } } } -/* ANCHOR_END: kotlindsl9 */ +/* ANCHOR_END: kotlindsl10 */ -/* ANCHOR: kotlindsl10 */ +/* ANCHOR: kotlindsl11 */ commandAPICommand("replaceSuggestions") { stringArgument("strings") { replaceSuggestions(ArgumentSuggestions.strings("one", "two", "three")) // Replaces the suggestions for the "strings" StringArgument @@ -1064,7 +1076,7 @@ commandAPICommand("replaceSuggestions") { player.sendMessage("You chose option ${args["strings"] as String}!") } } -/* ANCHOR_END: kotlindsl10 */ +/* ANCHOR_END: kotlindsl11 */ } fun native() { diff --git a/docssrc/src/intro.md b/docssrc/src/intro.md index 0d0f7076cc..5b8c449897 100644 --- a/docssrc/src/intro.md +++ b/docssrc/src/intro.md @@ -40,6 +40,7 @@ Here's the list of changes to the documentation between each update. You can vie ### Documentation changes 9.1.0 \\(\rightarrow\\) 9.2.0 - Updates [Location arguments](./argument_locations.md) page to mention the new `centerPosition` argument to center the position of integer values for location arguments. +- Updates [Kotlin documentation](./kotlindsl.md) page to mention the added support of delegations. ### Documentation changes 9.0.3 \\(\rightarrow\\) 9.1.0 diff --git a/docssrc/src/kotlindsl.md b/docssrc/src/kotlindsl.md index 7bb6f4bdd4..aeb390ecc5 100644 --- a/docssrc/src/kotlindsl.md +++ b/docssrc/src/kotlindsl.md @@ -127,6 +127,31 @@ This works similar to how argument behaviour is modified in a CommandTree: +## Delegations + +The Kotlin DSL offers an additional way to access arguments: delegations. With delegations, there are two possible dependencies you can use: + +1. `commandapi-core-kotlin` + + Support for delegations in the Kotlin DSL has been added to the `commandapi-core-kotlin` module. If you want to use delegations, you need to add this dependency. + + +2. `commandapi-bukkit-kotlin` + + If you are already using the Kotlin DSL to create your commands, you can already use delegations. `commandapi-core-kotlin` is included in `commandapi-bukkit-kotlin`. + +### Access arguments using delegations + +To be able to access arguments by using delegations, your variable name needs to match the node name of the argument. This could look like this: + +
+ +```kotlin,Delegation_example +{{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/ExamplesKotlinDSL.kt:kotlindsl7}} +``` + +
+ ----- ## More examples @@ -149,11 +174,11 @@ To declare an argument as optional you need to set the `optional` value to `true
```kotlin,CommandTree -{{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/ExamplesKotlinDSL.kt:kotlindsl7}} +{{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/ExamplesKotlinDSL.kt:kotlindsl8}} ``` ```kotlin,CommandAPICommand -{{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/ExamplesKotlinDSL.kt:kotlindsl8}} +{{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/ExamplesKotlinDSL.kt:kotlindsl9}} ```
@@ -177,11 +202,11 @@ You just have to use the `replaceSuggestions` method this time:
```kotlin,CommandTree -{{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/ExamplesKotlinDSL.kt:kotlindsl9}} +{{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/ExamplesKotlinDSL.kt:kotlindsl10}} ``` ```kotlin,CommandAPICommand -{{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/ExamplesKotlinDSL.kt:kotlindsl10}} +{{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/ExamplesKotlinDSL.kt:kotlindsl11}} ```
From 3bbc1c97e79aebf70b70b4880334ea53e7d021dd Mon Sep 17 00:00:00 2001 From: DerEchtePilz Date: Thu, 21 Sep 2023 23:12:15 +0200 Subject: [PATCH 08/14] Provide additional information about delegation usage --- docssrc/src/kotlindsl.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/docssrc/src/kotlindsl.md b/docssrc/src/kotlindsl.md index aeb390ecc5..4c1cfe02a6 100644 --- a/docssrc/src/kotlindsl.md +++ b/docssrc/src/kotlindsl.md @@ -152,6 +152,12 @@ To be able to access arguments by using delegations, your variable name needs to +> **Developer's Note:** +> +> Because delegations target the way you can access arguments, you can also use delegations when using Kotlin, but not the Kotlin DSL to create your commands. +> +> Just keep in mind that you need to at least add `commandapi-core-kotlin` as a dependency. + ----- ## More examples From 331d30232a35e226d04dccf9fb4944841d3daa05 Mon Sep 17 00:00:00 2001 From: DerEchtePilz Date: Thu, 21 Sep 2023 23:22:07 +0200 Subject: [PATCH 09/14] Fix MarkdownLint complaint --- docssrc/src/kotlindsl.md | 1 - 1 file changed, 1 deletion(-) diff --git a/docssrc/src/kotlindsl.md b/docssrc/src/kotlindsl.md index 4c1cfe02a6..ca8030ce09 100644 --- a/docssrc/src/kotlindsl.md +++ b/docssrc/src/kotlindsl.md @@ -134,7 +134,6 @@ The Kotlin DSL offers an additional way to access arguments: delegations. With d 1. `commandapi-core-kotlin` Support for delegations in the Kotlin DSL has been added to the `commandapi-core-kotlin` module. If you want to use delegations, you need to add this dependency. - 2. `commandapi-bukkit-kotlin` From c93b9ce926c8734a032a18ef6d0c23b4e5e366e5 Mon Sep 17 00:00:00 2001 From: DerEchtePilz Date: Thu, 21 Sep 2023 23:39:29 +0200 Subject: [PATCH 10/14] Fix new MarkdownLint complaint --- docssrc/src/kotlindsl.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docssrc/src/kotlindsl.md b/docssrc/src/kotlindsl.md index ca8030ce09..12ad128f51 100644 --- a/docssrc/src/kotlindsl.md +++ b/docssrc/src/kotlindsl.md @@ -152,7 +152,7 @@ To be able to access arguments by using delegations, your variable name needs to > **Developer's Note:** -> +> > Because delegations target the way you can access arguments, you can also use delegations when using Kotlin, but not the Kotlin DSL to create your commands. > > Just keep in mind that you need to at least add `commandapi-core-kotlin` as a dependency. From 3e08f473ae816e2dcd1b8a90ba951f8242938428 Mon Sep 17 00:00:00 2001 From: DerEchtePilz Date: Fri, 22 Sep 2023 16:10:37 +0200 Subject: [PATCH 11/14] Change term 'delegated' to 'delegated properties'. Provide a link to Kotlin's documentation for delegated properties --- docssrc/src/kotlindsl.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/docssrc/src/kotlindsl.md b/docssrc/src/kotlindsl.md index 12ad128f51..c6d8182001 100644 --- a/docssrc/src/kotlindsl.md +++ b/docssrc/src/kotlindsl.md @@ -129,23 +129,23 @@ This works similar to how argument behaviour is modified in a CommandTree: ## Delegations -The Kotlin DSL offers an additional way to access arguments: delegations. With delegations, there are two possible dependencies you can use: +The Kotlin DSL offers an additional way to access arguments: [delegated properties](https://kotlinlang.org/docs/delegated-properties.html). With delegated properties, there are two possible dependencies you can use: 1. `commandapi-core-kotlin` - Support for delegations in the Kotlin DSL has been added to the `commandapi-core-kotlin` module. If you want to use delegations, you need to add this dependency. + Support for delegated properties in the Kotlin DSL has been added to the `commandapi-core-kotlin` module. If you want to use delegated properties, you need to add this dependency. 2. `commandapi-bukkit-kotlin` - If you are already using the Kotlin DSL to create your commands, you can already use delegations. `commandapi-core-kotlin` is included in `commandapi-bukkit-kotlin`. + If you are already using the Kotlin DSL to create your commands, you can already use delegated properties. `commandapi-core-kotlin` is included in `commandapi-bukkit-kotlin`. -### Access arguments using delegations +### Access arguments using delegated properties -To be able to access arguments by using delegations, your variable name needs to match the node name of the argument. This could look like this: +To be able to access arguments by using delegated properties, your variable name needs to match the node name of the argument. This could look like this:
-```kotlin,Delegation_example +```kotlin,Delegated_properties_example {{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/ExamplesKotlinDSL.kt:kotlindsl7}} ``` @@ -153,7 +153,7 @@ To be able to access arguments by using delegations, your variable name needs to > **Developer's Note:** > -> Because delegations target the way you can access arguments, you can also use delegations when using Kotlin, but not the Kotlin DSL to create your commands. +> Because delegated properties target the way you can access arguments, you can also use delegated properties when using Kotlin, but not the Kotlin DSL to create your commands. > > Just keep in mind that you need to at least add `commandapi-core-kotlin` as a dependency. From a654182c27563331f1bfae89fb4d7c7ca729295b Mon Sep 17 00:00:00 2001 From: DerEchtePilz Date: Fri, 22 Sep 2023 16:17:32 +0200 Subject: [PATCH 12/14] Remove Velocity dependency information in kotlinintro.md --- docssrc/src/kotlinintro.md | 34 ++++------------------------------ 1 file changed, 4 insertions(+), 30 deletions(-) diff --git a/docssrc/src/kotlinintro.md b/docssrc/src/kotlinintro.md index 9c50cb7191..598eeb2a84 100644 --- a/docssrc/src/kotlinintro.md +++ b/docssrc/src/kotlinintro.md @@ -8,13 +8,11 @@ This DSL provides many methods to easily add arguments to your command structure ## Installing the DSL -To install the DSL, you need to add the `commandapi-kotlin` dependency into your `pom.xml` or your `build.gradle`, making sure to specify the server flavor you are developing for: +To install the DSL, you need to add the `commandapi-bukkit-kotlin` dependency into your `pom.xml` or your `build.gradle`, making sure to specify the server flavor you are developing for: ### Adding the dependency with Maven -
- -```xml,Bukkit +```xml dev.jorel @@ -24,18 +22,6 @@ To install the DSL, you need to add the `commandapi-kotlin` dependency into your ``` -```xml,Velocity - - - dev.jorel - commandapi-velocity-kotlin - 9.2.0-SNAPSHOT - - -``` - -
- Next, you need to add Kotlin to your project. For this, you first need to add the dependency: ```xml @@ -105,30 +91,18 @@ Next, you need to add the dependency:
-```groovy,Bukkit_build.gradle +```groovy,build.gradle dependencies { implementation "dev.jorel:commandapi-bukkit-kotlin:9.2.0-SNAPSHOT" } ``` -```kotlin,Bukkit_build.gradle.kts +```kotlin,build.gradle.kts dependencies { implementation("dev.jorel:commandapi-bukkit-kotlin:9.2.0-SNAPSHOT") } ``` -```groovy,Velocity_build.gradle -dependencies { - implementation "dev.jorel:commandapi-velocity-kotlin:9.2.0-SNAPSHOT" -} -``` - -```kotlin,Velocity_build.gradle.kts -dependencies { - implementation("dev.jorel:commandapi-velocity-kotlin:9.2.0-SNAPSHOT") -} -``` -
You also need to add Kotlin to your project. For this, you first need to add the Kotlin plugin: From 6992a422bfa0d1933cad187ae8af3e04c0689c0a Mon Sep 17 00:00:00 2001 From: DerEchtePilz Date: Fri, 22 Sep 2023 16:45:37 +0200 Subject: [PATCH 13/14] Move delegated properties documentation to a new page --- .../commandapi/examples/kotlin/Examples.kt | 19 +++++++++ .../examples/kotlin/ExamplesKotlinDSL.kt | 40 ++++++++++--------- docssrc/src/SUMMARY.md | 1 + docssrc/src/delegated_properties.md | 27 +++++++++++++ docssrc/src/kotlindsl.md | 38 ++---------------- 5 files changed, 72 insertions(+), 53 deletions(-) create mode 100644 docssrc/src/delegated_properties.md diff --git a/commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/Examples.kt b/commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/Examples.kt index 34555ef3e9..02c6f91063 100644 --- a/commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/Examples.kt +++ b/commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/Examples.kt @@ -14,6 +14,11 @@ import dev.jorel.commandapi.arguments.CustomArgument.CustomArgumentException import dev.jorel.commandapi.arguments.CustomArgument.MessageBuilder import dev.jorel.commandapi.exceptions.WrapperCommandSyntaxException import dev.jorel.commandapi.executors.* +import dev.jorel.commandapi.kotlindsl.commandAPICommand +import dev.jorel.commandapi.kotlindsl.getValue +import dev.jorel.commandapi.kotlindsl.playerArgument +import dev.jorel.commandapi.kotlindsl.playerExecutor +import dev.jorel.commandapi.kotlindsl.stringArgument import dev.jorel.commandapi.wrappers.* import dev.jorel.commandapi.wrappers.Rotation import net.kyori.adventure.inventory.Book @@ -1526,6 +1531,20 @@ Converter.convert(essentials, "speed", } } +fun delegatedProperties() { +/* ANCHOR: delegatedProperties1 */ +commandAPICommand("mycommand") { + stringArgument("string") + playerArgument("target") + playerExecutor { player, args -> + val string: String by args + val target: Player by args + // Implementation... + } +} +/* ANCHOR_END: delegatedProperties1 */ +} + class functions { /* ANCHOR: functions1 */ class Main : JavaPlugin() { diff --git a/commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/ExamplesKotlinDSL.kt b/commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/ExamplesKotlinDSL.kt index 1b0d96e032..5e196f48a1 100644 --- a/commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/ExamplesKotlinDSL.kt +++ b/commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/ExamplesKotlinDSL.kt @@ -892,6 +892,20 @@ commandAPICommand("cmd") { /* ANCHOR_END: arguments4 */ } +fun delegatedProperties() { +/* ANCHOR: delegatedProperties1 */ +commandAPICommand("mycommand") { + stringArgument("string") + playerArgument("target") + playerExecutor { player, args -> + val string: String by args + val target: Player by args + // Implementation... + } +} +/* ANCHOR_END: delegatedProperties1 */ +} + fun help() { /* ANCHOR: help1 */ commandAPICommand("mycmd") { @@ -1008,18 +1022,6 @@ commandAPICommand("commandRequirement") { /* ANCHOR_END: kotlindsl6 */ /* ANCHOR: kotlindsl7 */ -commandAPICommand("mycommand") { - stringArgument("string") - playerArgument("target") - playerExecutor { player, args -> - val string: String by args - val target: Player by args - // Implementation... - } -} -/* ANCHOR_END: kotlindsl7 */ - -/* ANCHOR: kotlindsl8 */ commandTree("optionalArgument") { literalArgument("give") { itemStackArgument("item") { @@ -1037,9 +1039,9 @@ commandTree("optionalArgument") { } } } -/* ANCHOR_END: kotlindsl8 */ +/* ANCHOR_END: kotlindsl7 */ -/* ANCHOR: kotlindsl9 */ +/* ANCHOR: kotlindsl8 */ commandAPICommand("optionalArgument") { literalArgument("give") itemStackArgument("item") @@ -1054,9 +1056,9 @@ commandAPICommand("optionalArgument") { player.inventory.addItem(itemStack) } } -/* ANCHOR_END: kotlindsl9 */ +/* ANCHOR_END: kotlindsl8 */ -/* ANCHOR: kotlindsl10 */ +/* ANCHOR: kotlindsl9 */ commandTree("replaceSuggestions") { stringArgument("strings") { replaceSuggestions(ArgumentSuggestions.strings("one", "two", "three")) // Replaces the suggestions for the "strings" StringArgument @@ -1065,9 +1067,9 @@ commandTree("replaceSuggestions") { } } } -/* ANCHOR_END: kotlindsl10 */ +/* ANCHOR_END: kotlindsl9 */ -/* ANCHOR: kotlindsl11 */ +/* ANCHOR: kotlindsl10 */ commandAPICommand("replaceSuggestions") { stringArgument("strings") { replaceSuggestions(ArgumentSuggestions.strings("one", "two", "three")) // Replaces the suggestions for the "strings" StringArgument @@ -1076,7 +1078,7 @@ commandAPICommand("replaceSuggestions") { player.sendMessage("You chose option ${args["strings"] as String}!") } } -/* ANCHOR_END: kotlindsl11 */ +/* ANCHOR_END: kotlindsl10 */ } fun native() { diff --git a/docssrc/src/SUMMARY.md b/docssrc/src/SUMMARY.md index 35733a9f85..e2f2e06a01 100644 --- a/docssrc/src/SUMMARY.md +++ b/docssrc/src/SUMMARY.md @@ -109,6 +109,7 @@ - [Kotlin-based commands](./kotlinintro.md) - [Using the DSL](./kotlindsl.md) +- [Delegated properties](./delegated_properties.md) # CommandAPI Utilities diff --git a/docssrc/src/delegated_properties.md b/docssrc/src/delegated_properties.md new file mode 100644 index 0000000000..d74555628e --- /dev/null +++ b/docssrc/src/delegated_properties.md @@ -0,0 +1,27 @@ +# Delegated properties + +The CommandAPI offers an additional way to access arguments when using Kotlin: [delegated properties](https://kotlinlang.org/docs/delegated-properties.html). With delegated properties, there are two possible dependencies you can use: + +1. `commandapi-core-kotlin` + + Support for delegated properties has been added to the `commandapi-core-kotlin` module. If you want to use delegated properties, you need to add this dependency. + +2. `commandapi-bukkit-kotlin` + + If you are already using the Kotlin DSL to create your commands, you can already use delegated properties. `commandapi-core-kotlin` is included in `commandapi-bukkit-kotlin`. + +### Access arguments using delegated properties + +To be able to access arguments by using delegated properties, your variable name needs to match the node name of the argument. This could look like this: + +
+ +```kotlin,Kotlin +{{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/Examples.kt:delegatedProperties1}} +``` + +```kotlin,Kotlin_DSL +{{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/ExamplesKotlinDSL.kt:delegatedProperties1}} +``` + +
diff --git a/docssrc/src/kotlindsl.md b/docssrc/src/kotlindsl.md index c6d8182001..7bb6f4bdd4 100644 --- a/docssrc/src/kotlindsl.md +++ b/docssrc/src/kotlindsl.md @@ -127,36 +127,6 @@ This works similar to how argument behaviour is modified in a CommandTree:
-## Delegations - -The Kotlin DSL offers an additional way to access arguments: [delegated properties](https://kotlinlang.org/docs/delegated-properties.html). With delegated properties, there are two possible dependencies you can use: - -1. `commandapi-core-kotlin` - - Support for delegated properties in the Kotlin DSL has been added to the `commandapi-core-kotlin` module. If you want to use delegated properties, you need to add this dependency. - -2. `commandapi-bukkit-kotlin` - - If you are already using the Kotlin DSL to create your commands, you can already use delegated properties. `commandapi-core-kotlin` is included in `commandapi-bukkit-kotlin`. - -### Access arguments using delegated properties - -To be able to access arguments by using delegated properties, your variable name needs to match the node name of the argument. This could look like this: - -
- -```kotlin,Delegated_properties_example -{{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/ExamplesKotlinDSL.kt:kotlindsl7}} -``` - -
- -> **Developer's Note:** -> -> Because delegated properties target the way you can access arguments, you can also use delegated properties when using Kotlin, but not the Kotlin DSL to create your commands. -> -> Just keep in mind that you need to at least add `commandapi-core-kotlin` as a dependency. - ----- ## More examples @@ -179,11 +149,11 @@ To declare an argument as optional you need to set the `optional` value to `true
```kotlin,CommandTree -{{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/ExamplesKotlinDSL.kt:kotlindsl8}} +{{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/ExamplesKotlinDSL.kt:kotlindsl7}} ``` ```kotlin,CommandAPICommand -{{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/ExamplesKotlinDSL.kt:kotlindsl9}} +{{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/ExamplesKotlinDSL.kt:kotlindsl8}} ```
@@ -207,11 +177,11 @@ You just have to use the `replaceSuggestions` method this time:
```kotlin,CommandTree -{{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/ExamplesKotlinDSL.kt:kotlindsl10}} +{{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/ExamplesKotlinDSL.kt:kotlindsl9}} ``` ```kotlin,CommandAPICommand -{{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/ExamplesKotlinDSL.kt:kotlindsl11}} +{{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/ExamplesKotlinDSL.kt:kotlindsl10}} ```
From 0fb9cd2b243edbd4283507f6e3bdadf46751fb3c Mon Sep 17 00:00:00 2001 From: DerEchtePilz Date: Fri, 22 Sep 2023 18:15:30 +0200 Subject: [PATCH 14/14] Make delegated properties Kotlin example not use the Kotlin DSL example too --- .../dev/jorel/commandapi/examples/kotlin/Examples.kt | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/Examples.kt b/commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/Examples.kt index 02c6f91063..2aab4c9523 100644 --- a/commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/Examples.kt +++ b/commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/Examples.kt @@ -1533,15 +1533,15 @@ Converter.convert(essentials, "speed", fun delegatedProperties() { /* ANCHOR: delegatedProperties1 */ -commandAPICommand("mycommand") { - stringArgument("string") - playerArgument("target") - playerExecutor { player, args -> +CommandAPICommand("mycommand") + .withArguments(StringArgument("string")) + .withArguments(PlayerArgument("target")) + .executesPlayer(PlayerCommandExecutor { player, args -> val string: String by args val target: Player by args // Implementation... - } -} + }) + .register() /* ANCHOR_END: delegatedProperties1 */ }