Skip to content

Commit

Permalink
feat: Minestom server agent support
Browse files Browse the repository at this point in the history
  • Loading branch information
TropicalShadow committed Jan 24, 2024
1 parent 2831814 commit 1e1add3
Show file tree
Hide file tree
Showing 10 changed files with 190 additions and 7 deletions.
4 changes: 3 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ subprojects {
mavenCentral()
maven(url = "https://oss.sonatype.org/content/repositories/snapshots")
maven(url = "https://repo.papermc.io/repository/maven-public/")
maven(url = "https://jitpack.io")
}

tasks {
Expand Down Expand Up @@ -243,13 +244,14 @@ subprojects {
}
} else if (project.name == "shulker-server-agent") {
val commonSourceSet = sourceSets.create("common")
setOf("paper").forEach { providerName ->
setOf("paper", "minestom").forEach { providerName ->
registerPluginProvider(providerName, commonSourceSet)
}

dependencies {
"commonCompileOnly"(libs.adventure.api)
"paperCompileOnly"(libs.folia.api)
"minestomCompileOnly"(libs.minestom.api)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/shulker-server-agent/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ dependencies {
commonImplementation(project(":packages:google-agones-sdk"))
}

setOf("paperJar").forEach { taskName ->
setOf("paperJar", "minestomJar").forEach { taskName ->
tasks.named(taskName, ShadowJar::class.java) {
mergeServiceFiles()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.agones.dev.sdk.AgonesSDK
import com.agones.dev.sdk.AgonesSDKImpl
import io.shulkermc.serveragent.api.ShulkerServerAPI
import io.shulkermc.serveragent.api.ShulkerServerAPIImpl
import io.shulkermc.serveragent.logger.AgentLoggerInterface
import io.shulkermc.serveragent.services.PlayerMovementService
import io.shulkermc.serveragent.tasks.HealthcheckTask
import java.lang.Exception
Expand All @@ -12,7 +13,7 @@ import java.util.logging.Level
import java.util.logging.Logger
import kotlin.system.exitProcess

class ShulkerServerAgentCommon(val serverInterface: ServerInterface, val logger: Logger) {
class ShulkerServerAgentCommon(val serverInterface: ServerInterface, val logger: AgentLoggerInterface) {
companion object {
private const val SUMMON_LABEL_NAME = "shulkermc.io/summoned"
private const val SUMMON_TIMEOUT_MINUTES = 5L
Expand All @@ -29,7 +30,7 @@ class ShulkerServerAgentCommon(val serverInterface: ServerInterface, val logger:

fun onServerInitialization() {
try {
this.logger.fine("Creating Agones SDK from environment")
this.logger.debug("Creating Agones SDK from environment")
this.agonesGateway = AgonesSDKImpl.createFromEnvironment()
val gameServer = this.agonesGateway.getGameServer().get()
this.logger.info(
Expand Down Expand Up @@ -58,7 +59,7 @@ class ShulkerServerAgentCommon(val serverInterface: ServerInterface, val logger:

this.agonesGateway.setReady()
} catch (@Suppress("TooGenericExceptionCaught") e: Exception) {
this.logger.log(Level.SEVERE, "Shulker Agent crashed, stopping server", e)
this.logger.severe("Shulker Agent crashed, stopping server", e)
this.shutdown()
}
}
Expand All @@ -74,8 +75,7 @@ class ShulkerServerAgentCommon(val serverInterface: ServerInterface, val logger:
try {
this.agonesGateway.askShutdown()
} catch (@Suppress("TooGenericExceptionCaught") e: Exception) {
this.logger.log(
Level.SEVERE,
this.logger.severe(
"Failed to ask Agones sidecar to shutdown properly, stopping process manually",
e
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.shulkermc.serveragent.logger

interface AgentLoggerInterface {

fun info(message: String)

fun warning(message: String)

fun severe(message: String)

fun severe(message: String, exception: Throwable)

fun debug(message: String)



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.shulkermc.serveragent.logger

import java.util.logging.Level
import java.util.logging.Logger

class JavaAgentLogger(private val logger: Logger): AgentLoggerInterface {
override fun info(message: String) {
logger.info(message)
}

override fun warning(message: String) {
logger.warning(message)
}

override fun severe(message: String) {
logger.severe(message)
}

override fun severe(message: String, exception: Throwable) {
logger.log(Level.SEVERE, message, exception)
}

override fun debug(message: String) {
logger.fine(message)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package io.shulkermc.serveragent.minestom

import io.shulkermc.serveragent.ServerInterface
import io.shulkermc.serveragent.minestom.scheduler.MinestomTask
import io.shulkermc.serveragent.platform.HookPostOrder
import io.shulkermc.serveragent.platform.PlayerDisconnectHook
import io.shulkermc.serveragent.platform.PlayerLoginHook
import net.minestom.server.MinecraftServer
import net.minestom.server.event.Event
import net.minestom.server.event.EventNode
import net.minestom.server.event.player.PlayerDisconnectEvent
import net.minestom.server.event.player.PlayerSpawnEvent
import net.minestom.server.permission.Permission
import net.minestom.server.timer.TaskSchedule
import java.time.Duration
import java.util.UUID
import java.util.concurrent.TimeUnit

class ServerInterfaceMinestom(private val plugin: ShulkerServerAgentMinestom): ServerInterface {

private val eventNode: EventNode<Event> = EventNode.all("shulker-server-agent-minestom")


override fun prepareNetworkAdminsPermissions(playerIds: List<UUID>) {
eventNode.addListener(PlayerSpawnEvent::class.java) { event ->
if (event.player.uuid in playerIds) {
event.player.permissionLevel = 4
event.player.addPermission(Permission("*"));
}
}
}

override fun addPlayerJoinHook(hook: PlayerLoginHook, postOrder: HookPostOrder) {
eventNode.addListener(PlayerSpawnEvent::class.java) { _ ->
hook()
}
}

override fun addPlayerQuitHook(hook: PlayerDisconnectHook, postOrder: HookPostOrder) {
eventNode.addListener(PlayerDisconnectEvent::class.java) { _ ->
hook()
}
}

override fun getPlayerCount(): Int {
return MinecraftServer.getConnectionManager().onlinePlayers.size
}

override fun scheduleDelayedTask(
delay: Long,
timeUnit: TimeUnit,
runnable: Runnable
): ServerInterface.ScheduledTask {
val duration = Duration.ofNanos(timeUnit.toNanos(delay))
val task = MinecraftServer.getSchedulerManager().scheduleTask(runnable,
TaskSchedule.duration(duration),
TaskSchedule.stop()
)
return MinestomTask(task)
}

override fun scheduleRepeatingTask(
delay: Long,
interval: Long,
timeUnit: TimeUnit,
runnable: Runnable
): ServerInterface.ScheduledTask {
val delayDuration = Duration.ofNanos(timeUnit.toNanos(delay))
val intervalDuration = Duration.ofNanos(timeUnit.toNanos(interval))
val task = MinecraftServer.getSchedulerManager().scheduleTask(runnable,
TaskSchedule.duration(delayDuration),
TaskSchedule.duration(intervalDuration)
)
return MinestomTask(task)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.shulkermc.serveragent.minestom

import io.shulkermc.serveragent.ShulkerServerAgentCommon
import io.shulkermc.serveragent.minestom.logger.SLF4JAgentLogger
import net.minestom.server.extensions.Extension
import org.slf4j.LoggerFactory

class ShulkerServerAgentMinestom: Extension() {

companion object{
private val logger = LoggerFactory.getLogger(ShulkerServerAgentMinestom::class.java)
}
private val agent = ShulkerServerAgentCommon(ServerInterfaceMinestom(this), SLF4JAgentLogger(ShulkerServerAgentMinestom.logger))

override fun initialize() {
agent.onServerInitialization()
}

override fun terminate() {
agent.onServerShutdown()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.shulkermc.serveragent.minestom.logger

import io.shulkermc.serveragent.logger.AgentLoggerInterface
import org.slf4j.Logger

class SLF4JAgentLogger(private val logger: Logger): AgentLoggerInterface {


override fun info(message: String) {
logger.info(message)
}

override fun warning(message: String) {
logger.warn(message)
}

override fun severe(message: String) {
logger.error(message)
}

override fun severe(message: String, exception: Throwable) {
logger.error(message, exception)
}

override fun debug(message: String) {
logger.debug(message)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.shulkermc.serveragent.minestom.scheduler

import io.shulkermc.serveragent.ServerInterface
import net.minestom.server.timer.Task

class MinestomTask(private val task: Task): ServerInterface.ScheduledTask {

override fun cancel() {
task.cancel()
}
}
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ dependencyResolutionManagement {
library("kubernetes-client-http", "io.fabric8", "kubernetes-httpclient-okhttp").versionRef("kubernetes-client")
library("protobuf", "com.google.protobuf:protobuf-java:3.25.1")
library("velocity-api", "com.velocitypowered:velocity-api:3.1.1")
library("minestom-api", "com.github.Minestom:Minestom:f6596926f6")

plugin("buildconfig", "com.github.gmazzo.buildconfig").version("4.1.2")
plugin("shadow", "com.github.johnrengelman.shadow").version("8.1.0")
Expand Down

0 comments on commit 1e1add3

Please sign in to comment.