Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sponge platform #78

Open
wants to merge 6 commits into
base: dev/0.3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ sealed class PlatformType(val name: String) {
object Bukkit : PlatformType("Bukkit")
object Minestom : PlatformType("Minestom")
object Fabric : PlatformType("Fabric")
object Sponge : PlatformType("Sponge")


// Proxies
object Velocity : PlatformType("Velocity")
Expand Down
73 changes: 73 additions & 0 deletions platforms/sponge/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* This file is part of UnifiedMetrics.
*
* UnifiedMetrics is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* UnifiedMetrics is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with UnifiedMetrics. If not, see <https://www.gnu.org/licenses/>.
*/

import org.spongepowered.gradle.plugin.config.PluginLoaders
import org.spongepowered.plugin.metadata.model.PluginDependency

plugins {
id("com.github.johnrengelman.shadow")
id("org.spongepowered.gradle.plugin") version "2.0.2"
}

repositories {
mavenCentral()
maven("https://repo.spongepowered.org/repository/maven-public/")
}

dependencies {
api(project(":unifiedmetrics-core"))
}

sponge {
apiVersion("8.0.0")
license("LGPL-3.0")
loader {
name(PluginLoaders.JAVA_PLAIN)
version("1.0")
}
plugin("unifiedmetrics") {
displayName("UnifiedMetrics")
version(project.version.toString())
entrypoint("dev.cubxity.metrics.sponge.bootstrap.UnifiedMetricsSpongeBootstrap")
description("Fully-featured metrics plugin for Minecraft servers")
links {
homepage("https://github.com/Cubxity/UnifiedMetrics/wiki")
source("https://github.com/Cubxity/UnifiedMetrics/")
issues("https://github.com/Cubxity/UnifiedMetrics/issues")
}
contributor("Cubxity") {
description("Maintainer")
}
dependency("spongeapi") {
loadOrder(PluginDependency.LoadOrder.AFTER)
optional(false)
}
}
}

tasks {
shadowJar {
archiveClassifier.set("")
relocate("retrofit2", "dev.cubxity.plugins.metrics.libs.retrofit2")
relocate("com.charleskorn", "dev.cubxity.plugins.metrics.libs.com.charleskorn")
relocate("com.influxdb", "dev.cubxity.plugins.metrics.libs.com.influxdb")
relocate("okhttp", "dev.cubxity.plugins.metrics.libs.okhttp")
relocate("okio", "dev.cubxity.plugins.metrics.libs.okio")
relocate("io.prometheus", "dev.cubxity.plugins.metrics.libs.io.prometheus")
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* This file is part of UnifiedMetrics.
*
* UnifiedMetrics is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* UnifiedMetrics is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with UnifiedMetrics. If not, see <https://www.gnu.org/licenses/>.
*/

package dev.cubxity.metrics.sponge

import dev.cubxity.metrics.sponge.bootstrap.UnifiedMetricsSpongeBootstrap
import kotlinx.coroutines.*
import org.spongepowered.api.scheduler.Task
import java.util.concurrent.TimeUnit
import kotlin.coroutines.CoroutineContext

@OptIn(InternalCoroutinesApi::class)
class SpongeDispatcher(private val plugin: UnifiedMetricsSpongeBootstrap): CoroutineDispatcher(), Delay {

override fun dispatch(context: CoroutineContext, block: Runnable) {
if(!context.isActive) return
if(plugin.server.onMainThread()) {
block.run()
}else {
plugin.server.scheduler().submit(Task.builder().execute(block).plugin(plugin.container).build())
}
}

@OptIn(ExperimentalCoroutinesApi::class)
override fun scheduleResumeAfterDelay(timeMillis: Long, continuation: CancellableContinuation<Unit>) {
val task = plugin.server.scheduler().submit(
Task.builder()
.delay(timeMillis, TimeUnit.MILLISECONDS)
.execute(Runnable {
continuation.apply { resumeUndispatched(Unit) }
})
.plugin(plugin.container)
.build()
)
continuation.invokeOnCancellation { task.cancel() }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* This file is part of UnifiedMetrics.
*
* UnifiedMetrics is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* UnifiedMetrics is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with UnifiedMetrics. If not, see <https://www.gnu.org/licenses/>.
*/

package dev.cubxity.metrics.sponge

import dev.cubxity.metrics.sponge.bootstrap.UnifiedMetricsSpongeBootstrap
import dev.cubxity.metrics.sponge.metric.events.EventsCollection
import dev.cubxity.metrics.sponge.metric.server.ServerCollection
import dev.cubxity.metrics.sponge.metric.tick.TickCollection
import dev.cubxity.metrics.sponge.metric.world.WorldCollection
import dev.cubxity.plugins.metrics.api.UnifiedMetrics
import dev.cubxity.plugins.metrics.core.plugin.CoreUnifiedMetricsPlugin

class UnifiedMetricsSpongePlugin(
override val bootstrap: UnifiedMetricsSpongeBootstrap
): CoreUnifiedMetricsPlugin() {

override fun registerPlatformService(api: UnifiedMetrics) {
super.registerPlatformMetrics()

apiProvider.metricsManager.apply {
with(config.metrics.collectors) {
if(server) registerCollection(ServerCollection(bootstrap))
if(events) registerCollection(EventsCollection(bootstrap))
if(tick) registerCollection(TickCollection(bootstrap))
if(world) registerCollection(WorldCollection(bootstrap))
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* This file is part of UnifiedMetrics.
*
* UnifiedMetrics is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* UnifiedMetrics is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with UnifiedMetrics. If not, see <https://www.gnu.org/licenses/>.
*/

package dev.cubxity.metrics.sponge.bootstrap

import com.google.inject.Inject
import dev.cubxity.metrics.sponge.SpongeDispatcher
import dev.cubxity.metrics.sponge.UnifiedMetricsSpongePlugin
import dev.cubxity.metrics.sponge.logger.Log4jLogger
import dev.cubxity.plugins.metrics.api.logging.Logger
import dev.cubxity.plugins.metrics.api.platform.PlatformType
import dev.cubxity.plugins.metrics.common.UnifiedMetricsBootstrap
import kotlinx.coroutines.CoroutineDispatcher
import org.spongepowered.api.Game
import org.spongepowered.api.MinecraftVersion
import org.spongepowered.api.Server
import org.spongepowered.api.config.ConfigManager
import org.spongepowered.api.event.Listener
import org.spongepowered.api.event.lifecycle.StartedEngineEvent
import org.spongepowered.api.event.lifecycle.StoppingEngineEvent
import org.spongepowered.api.plugin.PluginManager
import org.spongepowered.plugin.PluginContainer
import org.spongepowered.plugin.builtin.jvm.Plugin
import java.nio.file.Path

@Plugin("unifiedmetrics")
class UnifiedMetricsSpongeBootstrap @Inject constructor(
val container: PluginContainer,
val pluginManager: PluginManager,
private val game: Game,
private val serverLogger: org.apache.logging.log4j.Logger,
private val serverVersion: MinecraftVersion,
private val configManager: ConfigManager
): UnifiedMetricsBootstrap {

val server: Server
get() = game.server()

private val plugin = UnifiedMetricsSpongePlugin(this)
override val type: PlatformType
get() = PlatformType.Sponge

private val containerVersion = container.metadata().version()
override val version: String
get() = "${containerVersion.majorVersion}.${containerVersion.minorVersion}.${containerVersion.incrementalVersion}#${containerVersion.buildNumber}"
override val serverBrand: String
get() = serverVersion.name()
override val configDirectory: Path
get() = configManager.pluginConfig(container).configPath()
override val dataDirectory: Path
get() = configDirectory

override val logger: Logger
Nuckerr marked this conversation as resolved.
Show resolved Hide resolved
get() = Log4jLogger(serverLogger)
override val dispatcher: CoroutineDispatcher
Nuckerr marked this conversation as resolved.
Show resolved Hide resolved
get() = SpongeDispatcher(this)

@Listener
fun onServerStart(event: StartedEngineEvent<Server>) {
plugin.enable()
Nuckerr marked this conversation as resolved.
Show resolved Hide resolved
}

@Listener
fun onServerStop(event: StoppingEngineEvent<Server>) {
plugin.disable()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* This file is part of UnifiedMetrics.
*
* UnifiedMetrics is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* UnifiedMetrics is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with UnifiedMetrics. If not, see <https://www.gnu.org/licenses/>.
*/

package dev.cubxity.metrics.sponge.logger

import dev.cubxity.plugins.metrics.api.logging.Logger
import org.apache.logging.log4j.Level

class Log4jLogger(private val logger: org.apache.logging.log4j.Logger): Logger {
override fun info(message: String) {
logger.log(Level.INFO, message)
}

override fun warn(message: String) {
logger.log(Level.WARN, message)
}

override fun warn(message: String, error: Throwable) {
logger.log(Level.WARN, message, error)
}

override fun severe(message: String) {
logger.log(Level.ERROR, message)
}

override fun severe(message: String, error: Throwable) {
logger.log(Level.ERROR, message, error)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* This file is part of UnifiedMetrics.
*
* UnifiedMetrics is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* UnifiedMetrics is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with UnifiedMetrics. If not, see <https://www.gnu.org/licenses/>.
*/

package dev.cubxity.metrics.sponge.metric.events

import dev.cubxity.metrics.sponge.bootstrap.UnifiedMetricsSpongeBootstrap
import dev.cubxity.plugins.metrics.api.metric.collector.Collector
import dev.cubxity.plugins.metrics.api.metric.collector.CollectorCollection
import dev.cubxity.plugins.metrics.api.metric.collector.Counter
import org.spongepowered.api.Sponge
import org.spongepowered.api.event.Listener
import org.spongepowered.api.event.message.PlayerChatEvent
import org.spongepowered.api.event.network.ServerSideConnectionEvent.*
import org.spongepowered.api.event.server.ClientPingServerEvent

class EventsCollection(private val bootstrap: UnifiedMetricsSpongeBootstrap) : CollectorCollection {

private val loginCounter = Counter("minecraft_events_login_total")
private val joinCounter = Counter("minecraft_events_join_total")
private val quitCounter = Counter("minecraft_events_quit_total")
private val chatCounter = Counter("minecraft_events_chat_total")
private val pingCounter = Counter("minecraft_events_ping_total")

override val collectors: List<Collector> = listOf(loginCounter, joinCounter, quitCounter, chatCounter, pingCounter)

override fun initialize() {
Sponge.eventManager().registerListeners(bootstrap.container, this)
}

override fun dispose() {
Sponge.eventManager().unregisterListeners(this)
}

@Listener
fun onLogin(event: Join) {
joinCounter.inc()
}

@Listener
fun onConnect(event: Login) {
loginCounter.inc()
}


@Listener
fun onDisconnect(event: Disconnect) {
quitCounter.inc()
}

@Listener
fun onChat(event: PlayerChatEvent) {
chatCounter.inc()
}

@Listener
fun onPing(event: ClientPingServerEvent) {
pingCounter.inc()
}
}
Loading