From a0f95100ac4c432f40ae3e67eed791380d7e0f9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils=20Kr=C3=BCger?= Date: Thu, 12 Dec 2024 15:42:46 +0100 Subject: [PATCH] feat: ktor support --- api/build.gradle.kts | 5 --- build.gradle.kts | 12 +++++- examples/build.gradle.kts | 8 ---- examples/http-example/build.gradle.kts | 3 -- examples/ktor-example/build.gradle.kts | 37 +++++++++++++++++++ .../inject/examples/ktor/InjectKtorExample.kt | 36 ++++++++++++++++++ .../src/main/resources/plugin.yml | 4 ++ fabric/build.gradle.kts | 3 -- http/build.gradle.kts | 5 --- javalin/build.gradle.kts | 5 --- jetty/build.gradle.kts | 5 --- ktor/build.gradle.kts | 17 +++++++++ .../net/mcbrawls/inject/ktor/InjectKtor.kt | 30 +++++++++++++++ settings.gradle.kts | 9 +++-- spigot/build.gradle.kts | 3 -- spring/build.gradle.kts | 5 --- 16 files changed, 140 insertions(+), 47 deletions(-) create mode 100644 examples/ktor-example/build.gradle.kts create mode 100644 examples/ktor-example/src/main/kotlin/net/mcbrawls/inject/examples/ktor/InjectKtorExample.kt create mode 100644 examples/ktor-example/src/main/resources/plugin.yml create mode 100644 ktor/build.gradle.kts create mode 100644 ktor/src/main/kotlin/net/mcbrawls/inject/ktor/InjectKtor.kt diff --git a/api/build.gradle.kts b/api/build.gradle.kts index 6568c7e..6bf244d 100644 --- a/api/build.gradle.kts +++ b/api/build.gradle.kts @@ -3,11 +3,6 @@ plugins { `maven-publish` } -fun prop(name: String) = project.rootProject.property(name) as String - -group = prop("group") -version = prop("version") - base { archivesName.set("${rootProject.name}-${project.name}") } diff --git a/build.gradle.kts b/build.gradle.kts index 78a52d9..df126aa 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,7 +1,15 @@ plugins { id("xyz.jpenilla.run-paper") version "2.3.1" apply false id("io.papermc.paperweight.userdev") version "1.7.4" apply false + kotlin("jvm") version "2.1.0" apply false } -version = project.property("version") as String -group = project.property("group") as String +fun prop(name: String) = property("version") as String + +version = prop("version") +group = prop("group") + +subprojects { + version = prop("version") + group = prop("version") +} diff --git a/examples/build.gradle.kts b/examples/build.gradle.kts index 2072a4c..57e57cf 100644 --- a/examples/build.gradle.kts +++ b/examples/build.gradle.kts @@ -2,17 +2,9 @@ plugins { java } -fun prop(name: String) = project.rootProject.property(name) as String - -group = prop("group") -version = prop("version") - subprojects { apply(plugin = "java") - group = prop("group") - version = prop("version") - repositories { mavenCentral() } diff --git a/examples/http-example/build.gradle.kts b/examples/http-example/build.gradle.kts index 73e9c74..7c4fe2a 100644 --- a/examples/http-example/build.gradle.kts +++ b/examples/http-example/build.gradle.kts @@ -4,9 +4,6 @@ plugins { fun prop(name: String) = project.rootProject.property(name) as String -version = prop("version") -group = prop("group") - dependencies { minecraft("com.mojang:minecraft:${prop("minecraft_version")}") mappings("net.fabricmc:yarn:${prop("yarn_mappings")}:v2") diff --git a/examples/ktor-example/build.gradle.kts b/examples/ktor-example/build.gradle.kts new file mode 100644 index 0000000..d97f5dc --- /dev/null +++ b/examples/ktor-example/build.gradle.kts @@ -0,0 +1,37 @@ +plugins { + kotlin("jvm") version "2.1.0" + id("xyz.jpenilla.run-paper") + id("com.gradleup.shadow") version "9.0.0-beta4" +} + +repositories { + mavenCentral() + maven("https://repo.papermc.io/repository/maven-public/") +} + +dependencies { + implementation(project(":spigot")) + implementation(project(":http")) + implementation(project(":api")) + implementation(project(":jetty")) + implementation(project(":ktor")) { + isTransitive = false + } + + val ktorVersion = "3.0.2" + implementation("io.ktor:ktor-server-core:$ktorVersion") + implementation("io.ktor:ktor-server-jetty-jakarta:$ktorVersion") + + compileOnly("io.netty:netty-all:4.1.97.Final") + compileOnly("org.slf4j:slf4j-api:1.7.30") + + compileOnly("io.papermc.paper:paper-api:1.21.4-R0.1-SNAPSHOT") +} + +tasks.runServer { + minecraftVersion("1.21.4") +} + +tasks.assemble { + dependsOn(tasks.shadowJar) +} diff --git a/examples/ktor-example/src/main/kotlin/net/mcbrawls/inject/examples/ktor/InjectKtorExample.kt b/examples/ktor-example/src/main/kotlin/net/mcbrawls/inject/examples/ktor/InjectKtorExample.kt new file mode 100644 index 0000000..67211e3 --- /dev/null +++ b/examples/ktor-example/src/main/kotlin/net/mcbrawls/inject/examples/ktor/InjectKtorExample.kt @@ -0,0 +1,36 @@ +package net.mcbrawls.inject.examples.ktor + +import io.ktor.server.engine.EmbeddedServer +import io.ktor.server.jetty.jakarta.JettyApplicationEngine +import io.ktor.server.jetty.jakarta.JettyApplicationEngineBase +import io.ktor.server.response.respondText +import io.ktor.server.routing.get +import io.ktor.server.routing.routing +import net.mcbrawls.inject.ktor.jettyKtorServer +import net.mcbrawls.inject.ktor.startOnNewThread +import net.mcbrawls.inject.spigot.InjectSpigot +import org.bukkit.plugin.java.JavaPlugin + +class InjectKtorExample : JavaPlugin() { + private var ktor: EmbeddedServer? = null + + private fun initKtor() { + ktor = jettyKtorServer(InjectSpigot.INSTANCE) { + routing { + get("/") { + call.respondText("Hello, from Ktor!") + } + } + } + + ktor!!.startOnNewThread() + } + + override fun onEnable() { + initKtor() + } + + override fun onDisable() { + ktor?.stop() + } +} diff --git a/examples/ktor-example/src/main/resources/plugin.yml b/examples/ktor-example/src/main/resources/plugin.yml new file mode 100644 index 0000000..c1d7aec --- /dev/null +++ b/examples/ktor-example/src/main/resources/plugin.yml @@ -0,0 +1,4 @@ +name: "inject-ktor-example" +version: "0.0" +main: net.mcbrawls.inject.examples.ktor.InjectKtorExample +api-version: "1.21" diff --git a/fabric/build.gradle.kts b/fabric/build.gradle.kts index 7937b31..7f1280d 100644 --- a/fabric/build.gradle.kts +++ b/fabric/build.gradle.kts @@ -6,9 +6,6 @@ plugins { fun prop(name: String) = project.rootProject.property(name) as String -version = prop("version") -group = prop("group") - base { archivesName.set("${rootProject.name}-${project.name}") } diff --git a/http/build.gradle.kts b/http/build.gradle.kts index 036c2c3..fefb91a 100644 --- a/http/build.gradle.kts +++ b/http/build.gradle.kts @@ -3,11 +3,6 @@ plugins { `maven-publish` } -fun prop(name: String) = project.rootProject.property(name) as String - -group = prop("group") -version = prop("version") - base { archivesName.set("${rootProject.name}-${project.name}") } diff --git a/javalin/build.gradle.kts b/javalin/build.gradle.kts index 75d7399..e99607b 100644 --- a/javalin/build.gradle.kts +++ b/javalin/build.gradle.kts @@ -3,11 +3,6 @@ plugins { `maven-publish` } -fun prop(name: String) = project.rootProject.property(name) as String - -group = prop("group") -version = prop("version") - base { archivesName.set("${rootProject.name}-${project.name}") } diff --git a/jetty/build.gradle.kts b/jetty/build.gradle.kts index e8b0f8b..3ea2d00 100644 --- a/jetty/build.gradle.kts +++ b/jetty/build.gradle.kts @@ -3,11 +3,6 @@ plugins { `maven-publish` } -fun prop(name: String) = project.rootProject.property(name) as String - -group = prop("group") -version = prop("version") - base { archivesName.set("${rootProject.name}-${project.name}") } diff --git a/ktor/build.gradle.kts b/ktor/build.gradle.kts new file mode 100644 index 0000000..c16f065 --- /dev/null +++ b/ktor/build.gradle.kts @@ -0,0 +1,17 @@ +plugins { + kotlin("jvm") version "2.1.0" +} + +repositories { + mavenCentral() +} + +dependencies { + val ktorVersion = "3.0.2" + + implementation(project(":api")) + implementation(project(":jetty")) + + compileOnly("io.ktor:ktor-server-core:$ktorVersion") + compileOnly("io.ktor:ktor-server-jetty-jakarta:$ktorVersion") +} diff --git a/ktor/src/main/kotlin/net/mcbrawls/inject/ktor/InjectKtor.kt b/ktor/src/main/kotlin/net/mcbrawls/inject/ktor/InjectKtor.kt new file mode 100644 index 0000000..238ea2a --- /dev/null +++ b/ktor/src/main/kotlin/net/mcbrawls/inject/ktor/InjectKtor.kt @@ -0,0 +1,30 @@ +package net.mcbrawls.inject.ktor + +import io.ktor.server.application.Application +import io.ktor.server.engine.EmbeddedServer +import io.ktor.server.engine.embeddedServer +import io.ktor.server.jetty.jakarta.Jetty +import io.ktor.server.jetty.jakarta.JettyApplicationEngine +import io.ktor.server.jetty.jakarta.JettyApplicationEngineBase +import net.mcbrawls.inject.api.InjectPlatform +import net.mcbrawls.inject.jetty.JettyInjector +import org.eclipse.jetty.server.LocalConnector + +fun jettyKtorServer(platform: InjectPlatform, module: Application.() -> Unit = {}) = embeddedServer( + Jetty, + configure = { + configureServer = { + val connector = LocalConnector(this) + server.connectors = arrayOf(connector) + + platform.registerInjector(JettyInjector(connector)) + } + }, + module = module, +) + +fun EmbeddedServer.startOnNewThread() { + Thread({ + start(wait = true) + }, "Ktor").start() +} diff --git a/settings.gradle.kts b/settings.gradle.kts index 3ce4e9d..4d4d31e 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -16,9 +16,12 @@ include(":fabric") include(":spigot") include(":http") include(":examples") -include(":examples:http-example") -include(":examples:javalin-example") -include(":examples:spring-example") include(":jetty") include(":javalin") include(":spring") +include(":ktor") + +include(":examples:http-example") +include(":examples:javalin-example") +include(":examples:spring-example") +include(":examples:ktor-example") diff --git a/spigot/build.gradle.kts b/spigot/build.gradle.kts index cfafa7b..d2db39d 100644 --- a/spigot/build.gradle.kts +++ b/spigot/build.gradle.kts @@ -7,9 +7,6 @@ plugins { fun prop(name: String) = project.rootProject.property(name) as String -group = prop("group") -version = prop("version") - repositories { mavenCentral() maven("https://repo.papermc.io/repository/maven-public/") diff --git a/spring/build.gradle.kts b/spring/build.gradle.kts index a909f78..42d0596 100644 --- a/spring/build.gradle.kts +++ b/spring/build.gradle.kts @@ -3,11 +3,6 @@ plugins { `maven-publish` } -fun prop(name: String) = project.rootProject.property(name) as String - -group = prop("group") -version = prop("version") - base { archivesName.set("${rootProject.name}-${project.name}") }