Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/bug-report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ body:
options:
- Spigot (includes Spigot-based forks)
- Fabric
- Sponge
- Minestom
- Velocity
- Bungeecord / Waterfall
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Click [here](https://unifiedmetrics-demo.cubxity.dev) for live preview!

- 1.8+ Spigot servers *(includes Spigot-based forks)*
- 1.16+ Fabric servers
- Sponge API 12+
- Minestom
- Velocity
- BungeeCord
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ 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
102 changes: 102 additions & 0 deletions platforms/sponge/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* 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.gradle.vanilla.repository.MinecraftPlatform
import org.spongepowered.plugin.metadata.model.PluginDependency
import org.jetbrains.kotlin.gradle.dsl.JvmTarget

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

minecraft {
latestRelease()
platform(MinecraftPlatform.SERVER)
}

val mixinConfigsAttribute: String by extra { "unifiedmetrics.mixins.json" }
tasks.withType<Jar> {
manifest {
attributes(
mapOf("MixinConfigs" to mixinConfigsAttribute)
)
}
}

sponge {
apiVersion("12.0.0-SNAPSHOT")
loader {
name(PluginLoaders.JAVA_PLAIN)
version("1.0.0-SNAPSHOT")
}
plugin("unifiedmetrics") {
displayName("UnifiedMetrics")
entrypoint("dev.cubxity.plugins.metrics.sponge.bootstrap.UnifiedMetricsSpongeBootstrap")
description("Fully-featured metrics plugin for Minecraft servers")
license("LGPL-3")
dependency("spongeapi") {
loadOrder(PluginDependency.LoadOrder.AFTER)
optional(false)
}
}
}

dependencies {
api(project(":unifiedmetrics-core"))
compileOnly("org.spongepowered:mixin:0.8.6-SNAPSHOT")
}

tasks {
shadowJar {
mergeServiceFiles()
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")
relocate("com.google.gson", "dev.cubxity.plugins.metrics.libs.gson")
relocate("com.google.errorprone.annotations", "dev.cubxity.plugins.metrics.libs.errorprone")
relocate("org.apache.commons", "dev.cubxity.plugins.metrics.libs.commons")
relocate("org.intellij.lang.annotations", "dev.cubxity.plugins.metrics.libs.intellij.annotations")
relocate("org.jetbrains.annotations", "dev.cubxity.plugins.metrics.libs.jetbrains.annotations")
relocate("javax.annotation", "dev.cubxity.plugins.metrics.libs.javax.annotations")
relocate("org.reactivestreams", "dev.cubxity.plugins.metrics.libs.reactivestreams")
relocate("net.thauvin.erik.urlencoder", "dev.cubxity.plugins.metrics.libs.urlencoder")
relocate("it.krzeminski.snakeyaml", "dev.cubxity.plugins.metrics.libs.snakeyaml")
relocate("io.reactivex.rxjava3", "dev.cubxity.plugins.metrics.libs.rxjava")
relocate("kotlin", "dev.cubxity.plugins.metrics.libs.kotlin")
relocate("kotlinx", "dev.cubxity.plugins.metrics.libs.kotlinx")
}

compileKotlin {
compilerOptions.jvmTarget.set(JvmTarget.JVM_21)
}

compileJava {
options.encoding = "UTF-8"
}
}

java {
sourceCompatibility = JavaVersion.VERSION_21
targetCompatibility = JavaVersion.VERSION_21
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* 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.plugins.metrics.sponge.mixins;

import dev.cubxity.plugins.metrics.sponge.events.TickEndEvent;
import net.minecraft.server.MinecraftServer;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.event.Cause;
import org.spongepowered.api.event.EventContext;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import static dev.cubxity.plugins.metrics.api.metric.collector.CollectorKt.NANOSECONDS_PER_MILLISECOND;
import static dev.cubxity.plugins.metrics.api.metric.collector.CollectorKt.NANOSECONDS_PER_SECOND;

/**
* Designed to emulate paper's tick event as closely as possible.
*/
@Mixin(MinecraftServer.class)
public class MinecraftServerMixin {

@Unique
private long unifiedmetrics$lastTick = 0;

@Inject(
method = "runServer",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/server/MinecraftServer;buildServerStatus()Lnet/minecraft/network/protocol/status/ServerStatus;"
)
)
private void onRunServerBeforeLoop(CallbackInfo ci) {
unifiedmetrics$lastTick = System.nanoTime() - ((long) NANOSECONDS_PER_SECOND / 20);
}

@Inject(
method = "tickServer",
at = @At("HEAD")
)
private void onTickStart(CallbackInfo ci) {
unifiedmetrics$lastTick = System.nanoTime();
}

@Inject(
method = "tickServer",
at = @At(
value = "CONSTANT",
target = "Lnet/minecraft/util/profiling/ProfilerFiller;push(Ljava/lang/String;)V",
args = "stringValue=tallying")
)
private void onTickEnd(CallbackInfo ci) {
Sponge.eventManager().post(new TickEndEvent((double)(System.nanoTime() - unifiedmetrics$lastTick) / NANOSECONDS_PER_MILLISECOND, Cause.of(EventContext.empty(), Sponge.server())));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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.plugins.metrics.sponge

import dev.cubxity.plugins.metrics.sponge.bootstrap.UnifiedMetricsSpongeBootstrap
import kotlinx.coroutines.CancellableContinuation
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Delay
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.InternalCoroutinesApi
import kotlinx.coroutines.isActive
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.plugins.metrics.sponge

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

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,76 @@
/*
* 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.plugins.metrics.sponge.bootstrap

import com.google.inject.Inject
import dev.cubxity.plugins.metrics.api.logging.Logger
import dev.cubxity.plugins.metrics.api.platform.PlatformType
import dev.cubxity.plugins.metrics.common.UnifiedMetricsBootstrap
import dev.cubxity.plugins.metrics.sponge.SpongeDispatcher
import dev.cubxity.plugins.metrics.sponge.UnifiedMetricsSpongePlugin
import dev.cubxity.plugins.metrics.sponge.logger.Log4jLogger
import kotlinx.coroutines.CoroutineDispatcher
import org.spongepowered.api.Game
import org.spongepowered.api.MinecraftVersion
import org.spongepowered.api.Server
import org.spongepowered.api.config.ConfigDir
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,
@ConfigDir(sharedRoot = false) override val configDirectory: Path
): 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 dataDirectory: Path = configDirectory
override val logger: Logger
get() = Log4jLogger(serverLogger)
override val dispatcher: CoroutineDispatcher = SpongeDispatcher(this)

@Listener
fun onServerStart(event: StartedEngineEvent<Server>) {
plugin.enable()
}

@Listener
fun onServerStop(event: StoppingEngineEvent<Server>) {
plugin.disable()
}
}
Loading