Skip to content

Commit

Permalink
fix: convert resource pack injector to javalin
Browse files Browse the repository at this point in the history
  • Loading branch information
andantet committed Dec 16, 2024
1 parent c708e5d commit 0a879b9
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 24 deletions.
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 5 additions & 3 deletions src/main/kotlin/dev/andante/audience/AudienceInitializer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
}
}
Original file line number Diff line number Diff line change
@@ -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<String, ByteArray> = 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)
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/test/kotlin/dev/andante/audience/test/AudienceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"))))
}
}
}

0 comments on commit 0a879b9

Please sign in to comment.