# Project Setup

## &#x20;Project Example

This page shows the recommended project structure and setup for KXAPI-based BotWithUs scripts, following the standard layout used in the BotWithUs ecosystem.

### 📁 Recommended Project Structure

```
MyKXAPIScript/
├── build.gradle.kts                # Root build configuration
├── settings.gradle.kts             # Gradle settings
├── v2/                             # Version 2 module
│   ├── build.gradle.kts            # Module build configuration
│   └── testing/                    # Testing submodule
│       ├── build.gradle.kts        # Testing build configuration
│       └── src/
│           └── main/
│               ├── java/            # Java source code
│               │   └── botwithus/
│               │   └── module-info.java
│               └── kotlin/          # Kotlin source code
│                   └── botwithus/
│                       └── TestScript.kt
```

### 🔧 Build Configuration

<details>

<summary><strong>Root build.gradle.kts</strong></summary>

```kotlin
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    kotlin("jvm") version "2.2.0"
}

group = "botwithus"
version = "1.0.0"

allprojects {
    apply(plugin = "kotlin")
    repositories {
        mavenLocal()
        mavenCentral()
        maven("https://nexus.botwithus.net/repository/maven-releases/")
        maven("https://nexus.botwithus.net/repository/maven-snapshots/")
    }
}

```

</details>

<details>

<summary><strong>v2 Module build.gradle.kts</strong></summary>

```kotlin
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

subprojects {

    configurations {
        create("includeInJar") {
            isTransitive = false
        }
    }

    extensions.configure<JavaPluginExtension> {
        val javaModuleInfo = file("src/main/java/module-info.java")
        if (javaModuleInfo.exists()) {
            modularity.inferModulePath.set(true)
        } else if (project != rootProject) {
            throw GradleException("ERROR: No module-info.java found in src/main/java/ or src/main/kotlin/ for project ${project.name}")
        }

        toolchain {
            languageVersion.set(JavaLanguageVersion.of(24))
        }
    }

    dependencies {
        implementation("org.slf4j:slf4j-api:2.0.9")
        implementation("net.botwithus.api:api:1.0.+")
        implementation("net.botwithus.imgui:imgui:1.0.+")
        implementation("net.botwithus.xapi:xapi:2.0.+")
        implementation("net.botwithus.kxapi:kxapi:0.1-SNAPSHOT")
        implementation("botwithus.navigation:nav-api:1.+")
        add("includeInJar", "net.botwithus.xapi:xapi:2.0.+")
        add("includeInJar", "net.botwithus.kxapi:kxapi:0.1-SNAPSHOT+")
        add("includeInJar", "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.1")
        implementation("org.jetbrains.kotlin:kotlin-stdlib:2.2.0")
        implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.1")
        testImplementation("org.jetbrains.kotlin:kotlin-test:2.2.0")
    }

    java {
        modularity.inferModulePath.set(true)
    }

    tasks.compileKotlin {
        destinationDirectory.set(tasks.compileJava.get().destinationDirectory)
    }

    tasks.withType<KotlinCompile>().configureEach {
        compilerOptions {
            jvmTarget.set(JvmTarget.JVM_24)
            freeCompilerArgs.add("-Xjdk-release=24")
        }
    }

    tasks.withType<Jar>().configureEach {
        duplicatesStrategy = DuplicatesStrategy.EXCLUDE
        from(configurations["includeInJar"].map { zipTree(it) })
    }

    val copyJar by tasks.register<Copy>("copyJar") {
        from(tasks.named("jar"))
        into("${System.getProperty("user.home")}\\.BotWithUs\\scripts")
    }

    tasks.named<Jar>("jar") {
        duplicatesStrategy = DuplicatesStrategy.EXCLUDE
        finalizedBy(copyJar)
    }
}
```

</details>

<details>

<summary><strong>testing Module build.gradle.kts</strong></summary>

```kotlin
group = "com.botwithus.script.testing"
version = "1.0.0-SNAPSHOT"

dependencies {
    implementation("com.google.code.gson:gson:2.10.1")
}
```

</details>

<details>

<summary><strong>settings.gradle.kts</strong></summary>

```kotlin
rootProject.name = "my-kxapi-script"

fun includeModulesFrom(rootDir: File, parent: String = "") {
    fileTree(rootDir).matching {
        include("**/build.gradle.kts")
    }.forEach { gradleFile ->
        val moduleDir = gradleFile.parentFile
        val moduleName = ":$parent${moduleDir.relativeTo(rootDir).path}".replace(File.separator, ":")
        include(moduleName)
    }
}

includeModulesFrom(rootDir)
```

</details>

### 📝 Example Script

```kotlin
// src/main/kotlin/botwithus/TestScript.kt
package botwithus

import net.botwithus.kxapi.script.SuspendableScript

class TestScript : SuspendableScript() {

    val product = this.skilling.production {
        itemName("Clean ranarr")
        category("Clean Herbs")
    }

    override suspend fun onLoop() {
        product.produceItem(onProgress = { message, prog1, prog2, prog3, xp ->
           this.println("${message.message}, ${prog1} , ${prog2}, ${prog3}, ${xp}")
        }, onFinished = {
            println("FINSHED")
        })
    }
}
```

### 📝 Module Info

```java
// src/main/java/botwithus/module-info.java
module HauntedAshes.main {
    requires kotlin.stdlib;
    requires BotWithUs.api;
    requires BotWithUs.imgui;
    requires BotWithUs.navigation.api;
    requires org.slf4j;
    requires static xapi;
    requires static kxapi;

    opens botwithus to BotWithUs.api;

    provides net.botwithus.scripts.Script with botwithus.TestScript;
}

```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://kxapi.gitbook.io/kxapi/getting-started/project-setup.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
