Kotlin-based commands

The CommandAPI also provides an alternative way of making commands when using Kotlin to develop your plugins: A DSL!

This DSL provides many methods to easily add arguments to your command structure. Examples of the DSL can be found here.


Installing the DSL

To install the DSL, you need to add the commandapi-kotlin dependency into your pom.xml or your build.gradle:

Adding the dependency with Maven

<dependencies>
    <dependency>
        <groupId>dev.jorel</groupId>
        <artifactId>commandapi-kotlin</artifactId>
        <version>8.7.0</version>
    </dependency>
</dependencies>

Next, to shade it into your project easily, you need to add the maven-shade-plugin:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.3.0</version>
            <executions>
                <execution>
                    <id>shade</id>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <relocations>
                    <relocation>
                        <pattern>dev.jorel.commandapi.kotlindsl</pattern>
                        <!-- TODO: Change this to my own package name -->
                        <shadedPattern>my.custom.package.commandapi.kotlindsl</shadedPattern>
                    </relocation>
                </relocations>
            </configuration>
        </plugin>
    </plugins>
</build>

Next, you need to add Kotlin to your project. For this, you first need to add the dependency:

<dependencies>
    <dependency>
        <groupId>org.jetbrains.kotlin</groupId>
        <artifactId>kotlin-stdlib</artifactId>
        <version>1.7.20</version>
    </dependency>
</dependencies>

Finally, you need to add the kotlin-maven-plugin:

<build>
    <plugins>
        <plugin>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-maven-plugin</artifactId>
            <version>1.7.20</version>
            <executions>
                <execution>
                    <id>compile</id>
                    <phase>compile</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
                <execution>
                    <id>test-compile</id>
                    <phase>test-compile</phase>
                    <goals>
                        <goal>test-compile</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <jvmTarget>16</jvmTarget>
            </configuration>
        </plugin>
    </plugins>
</build>

Adding the dependency with Gradle

First, you need to add the repository:

repositories {
    mavenCentral()
}
repositories {
    mavenCentral()
}

Next, you need to add the dependency:

dependencies {
    implementation "dev.jorel:commandapi-kotlin:8.7.0"
}
dependencies {
    implementation("dev.jorel:commandapi-kotlin:8.7.0")
}

Finally, you need to add it to the shadowJar configuration task and relocate it to your desired location:

shadowJar {
    dependencies {
        include dependency("dev.jorel:commandapi-kotlin:8.7.0")
    }
    
    // TODO: Change this to my own package name
    relocate("dev.jorel.commandapi", "my.custom.package.commandapi")
}
shadowJar {
    dependencies {
        include dependency("dev.jorel:commandapi-kotlin:8.7.0")
    }
    
    // TODO: Change this to my own package name
    relocate("dev.jorel.commandapi", "my.custom.package.commandapi")
}

You also need to add Kotlin to your project. For this, you first need to add the Kotlin plugin:

plugins {
    id "org.jetbrains.kotlin.jvm" version "1.7.20"
}
plugins {
    kotlin("jvm") version "1.7.20"
}

Next, you need to add the dependency (you should already have added the mavenCentral() repository to your project):

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib"
}
dependencies {
    implementation("org.jetbrains.kotlin:kotlin-stdlib")
}

Then, you need to add the compileKotlin task:

compileKotlin {
    kotlinOptions {
        jvmTarget = "16"
    }
}
tasks.withType<KotlinCompile> {
    kotlinOptions.jvmTarget = "16"
}

Finally, you need to add it to the shadowJar configuration task:

shadowJar {
    dependencies {
        include dependency("org.jetbrains.kotlin:kotlin-stdlib")
    }
}
shadowJar {
    dependencies {
        include dependency("org.jetbrains.kotlin:kotlin-stdlib")
    }
}