diff --git a/build.gradle b/build.gradle index 25e3e21..8990f72 100644 --- a/build.gradle +++ b/build.gradle @@ -59,8 +59,9 @@ dependencies { include modImplementation ("com.github.yundom:kache:$kache_version") + include modImplementation ("io.javalin:javalin:$javalin_version") include modImplementation ("net.mcbrawls.inject:api:$inject_version") - include modImplementation ("net.mcbrawls.inject:http:$inject_version") + include modImplementation ("net.mcbrawls.inject:javalin:$inject_version") include modImplementation ("net.mcbrawls.inject:fabric:$inject_version") testImplementation sourceSets.main.output diff --git a/gradle.properties b/gradle.properties index 43db466..8bbe944 100644 --- a/gradle.properties +++ b/gradle.properties @@ -15,11 +15,12 @@ kotlin_version=2.0.21 fabric_kotlin_version=1.12.3 # mod properties -mod_version=3.2.1 +mod_version=3.3 maven_group=net.mcbrawls mod_id=audience # dependencies kache_version=1.0.5 +javalin_version=6.3.0 inject_version=3.0.0 diff --git a/src/main/kotlin/dev/andante/audience/AudienceInitializer.kt b/src/main/kotlin/dev/andante/audience/AudienceInitializer.kt index 31ef10e..62a6940 100644 --- a/src/main/kotlin/dev/andante/audience/AudienceInitializer.kt +++ b/src/main/kotlin/dev/andante/audience/AudienceInitializer.kt @@ -3,7 +3,6 @@ package dev.andante.audience import dev.andante.audience.resource.ResourcePackInjectHandler import net.fabricmc.api.ModInitializer import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents -import net.mcbrawls.inject.fabric.InjectFabric import net.minecraft.server.MinecraftServer object AudienceInitializer : ModInitializer { @@ -16,8 +15,11 @@ object AudienceInitializer : ModInitializer { override fun onInitialize() { // register event to capture server - ServerLifecycleEvents.SERVER_STARTING.register { _minecraftServer = it } + ServerLifecycleEvents.SERVER_STARTING.register { server -> + _minecraftServer = server - InjectFabric.INSTANCE.registerInjector(ResourcePackInjectHandler) + val javalin = ResourcePackInjectHandler.createJavalin() + javalin.start(server.serverPort) + } } } diff --git a/src/main/kotlin/dev/andante/audience/resource/ResourcePackInjectHandler.kt b/src/main/kotlin/dev/andante/audience/resource/ResourcePackInjectHandler.kt index ba249ec..f51edf9 100644 --- a/src/main/kotlin/dev/andante/audience/resource/ResourcePackInjectHandler.kt +++ b/src/main/kotlin/dev/andante/audience/resource/ResourcePackInjectHandler.kt @@ -1,30 +1,29 @@ package dev.andante.audience.resource -import io.netty.channel.ChannelHandlerContext -import net.mcbrawls.inject.http.HttpByteBuf -import net.mcbrawls.inject.http.HttpByteBuf.httpBuf -import net.mcbrawls.inject.http.HttpInjector -import net.mcbrawls.inject.http.HttpRequest +import io.javalin.Javalin +import io.javalin.http.Context +import net.mcbrawls.inject.fabric.InjectFabric +import net.mcbrawls.inject.javalin.InjectJavalinFactory -object ResourcePackInjectHandler : HttpInjector() { +object ResourcePackInjectHandler { private val resourcePacks: MutableMap = mutableMapOf() - override fun intercept(ctx: ChannelHandlerContext, request: HttpRequest): HttpByteBuf { - val response = httpBuf(ctx) + fun createJavalin(): Javalin { + return InjectJavalinFactory.create(InjectFabric.INSTANCE).apply { + get("/packs/{hash}", ::handleRequest) + } + } - val path = request.requestURI.removePrefix("/") - val pack = resourcePacks[path] ?: return response + fun handleRequest(context: Context) { + val hash = context.pathParam("hash") - if (!ctx.channel().isActive) { - return response + val pack = resourcePacks[hash] + if (pack == null) { + context.status(404) + return } - response.writeStatusLine("1.1", 200, "OK") - response.writeHeader("Content-Type", "application/zip") - response.writeHeader("Content-Length", pack.size.toString()) - response.writeBytes(pack) - - return response + context.result(pack) } /** diff --git a/src/test/kotlin/dev/andante/audience/test/AudienceTest.kt b/src/test/kotlin/dev/andante/audience/test/AudienceTest.kt index 5c40a97..2f6ca5f 100644 --- a/src/test/kotlin/dev/andante/audience/test/AudienceTest.kt +++ b/src/test/kotlin/dev/andante/audience/test/AudienceTest.kt @@ -73,7 +73,7 @@ object AudienceTest : ModInitializer { } ServerConfigurationConnectionEvents.CONFIGURE.register { handler, server -> - handler.addTask(SendResourcePackTask(MinecraftServer.ServerResourcePackProperties(UUID.randomUUID(), "http://localhost:25565/${packOne.hash}", packOne.hash, true, Text.literal("EEEE")))) + handler.addTask(SendResourcePackTask(MinecraftServer.ServerResourcePackProperties(UUID.randomUUID(), "http://localhost:25565/packs/${packOne.hash}", packOne.hash, true, Text.literal("EEEE")))) } } }