diff --git a/api/src/main/kotlin/dev/cubxity/plugins/metrics/api/platform/PlatformType.kt b/api/src/main/kotlin/dev/cubxity/plugins/metrics/api/platform/PlatformType.kt index d593e11f..c3330139 100644 --- a/api/src/main/kotlin/dev/cubxity/plugins/metrics/api/platform/PlatformType.kt +++ b/api/src/main/kotlin/dev/cubxity/plugins/metrics/api/platform/PlatformType.kt @@ -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") diff --git a/platforms/sponge/build.gradle.kts b/platforms/sponge/build.gradle.kts new file mode 100644 index 00000000..9f3124f7 --- /dev/null +++ b/platforms/sponge/build.gradle.kts @@ -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 . + */ + +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") + } + +} \ No newline at end of file diff --git a/platforms/sponge/src/main/java/dev/cubxity/metrics/sponge/SpongeDispatcher.kt b/platforms/sponge/src/main/java/dev/cubxity/metrics/sponge/SpongeDispatcher.kt new file mode 100644 index 00000000..3134cc68 --- /dev/null +++ b/platforms/sponge/src/main/java/dev/cubxity/metrics/sponge/SpongeDispatcher.kt @@ -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 . + */ + +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) { + 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() } + } +} \ No newline at end of file diff --git a/platforms/sponge/src/main/java/dev/cubxity/metrics/sponge/UnifiedMetricsSpongePlugin.kt b/platforms/sponge/src/main/java/dev/cubxity/metrics/sponge/UnifiedMetricsSpongePlugin.kt new file mode 100644 index 00000000..5c9fd77e --- /dev/null +++ b/platforms/sponge/src/main/java/dev/cubxity/metrics/sponge/UnifiedMetricsSpongePlugin.kt @@ -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 . + */ + +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)) + } + } + } + +} \ No newline at end of file diff --git a/platforms/sponge/src/main/java/dev/cubxity/metrics/sponge/bootstrap/UnifiedMetricsSpongeBootstrap.kt b/platforms/sponge/src/main/java/dev/cubxity/metrics/sponge/bootstrap/UnifiedMetricsSpongeBootstrap.kt new file mode 100644 index 00000000..12e94b99 --- /dev/null +++ b/platforms/sponge/src/main/java/dev/cubxity/metrics/sponge/bootstrap/UnifiedMetricsSpongeBootstrap.kt @@ -0,0 +1,78 @@ +/* + * 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 . + */ + +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 = configDirectory + override val logger: Logger + get() = Log4jLogger(serverLogger) + override val dispatcher: CoroutineDispatcher = SpongeDispatcher(this) + + @Listener + fun onServerStart(event: StartedEngineEvent) { + plugin.enable() + } + + @Listener + fun onServerStop(event: StoppingEngineEvent) { + plugin.disable() + } +} \ No newline at end of file diff --git a/platforms/sponge/src/main/java/dev/cubxity/metrics/sponge/logger/Log4jLogger.kt b/platforms/sponge/src/main/java/dev/cubxity/metrics/sponge/logger/Log4jLogger.kt new file mode 100644 index 00000000..f9f82234 --- /dev/null +++ b/platforms/sponge/src/main/java/dev/cubxity/metrics/sponge/logger/Log4jLogger.kt @@ -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 . + */ + +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) + } +} \ No newline at end of file diff --git a/platforms/sponge/src/main/java/dev/cubxity/metrics/sponge/metric/events/EventsCollection.kt b/platforms/sponge/src/main/java/dev/cubxity/metrics/sponge/metric/events/EventsCollection.kt new file mode 100644 index 00000000..ee3017b4 --- /dev/null +++ b/platforms/sponge/src/main/java/dev/cubxity/metrics/sponge/metric/events/EventsCollection.kt @@ -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 . + */ + +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 = 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() + } +} \ No newline at end of file diff --git a/platforms/sponge/src/main/java/dev/cubxity/metrics/sponge/metric/server/ServerCollection.kt b/platforms/sponge/src/main/java/dev/cubxity/metrics/sponge/metric/server/ServerCollection.kt new file mode 100644 index 00000000..58a49549 --- /dev/null +++ b/platforms/sponge/src/main/java/dev/cubxity/metrics/sponge/metric/server/ServerCollection.kt @@ -0,0 +1,27 @@ +/* + * 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 . + */ + +package dev.cubxity.metrics.sponge.metric.server + +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 + +class ServerCollection(bootstrap: UnifiedMetricsSpongeBootstrap): CollectorCollection { + + override val collectors: List = listOf(ServerCollector(bootstrap)) +} \ No newline at end of file diff --git a/platforms/sponge/src/main/java/dev/cubxity/metrics/sponge/metric/server/ServerCollector.kt b/platforms/sponge/src/main/java/dev/cubxity/metrics/sponge/metric/server/ServerCollector.kt new file mode 100644 index 00000000..7e82e24d --- /dev/null +++ b/platforms/sponge/src/main/java/dev/cubxity/metrics/sponge/metric/server/ServerCollector.kt @@ -0,0 +1,34 @@ +/* + * 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 . + */ + +package dev.cubxity.metrics.sponge.metric.server + +import dev.cubxity.metrics.sponge.bootstrap.UnifiedMetricsSpongeBootstrap +import dev.cubxity.plugins.metrics.api.metric.collector.Collector +import dev.cubxity.plugins.metrics.api.metric.data.GaugeMetric +import dev.cubxity.plugins.metrics.api.metric.data.Metric + +class ServerCollector(private val bootstrap: UnifiedMetricsSpongeBootstrap): Collector { + + override fun collect(): List { + return listOf( + GaugeMetric("minecraft_plugins", value = bootstrap.pluginManager.plugins().size), + GaugeMetric("minecraft_players_count", value = bootstrap.server.onlinePlayers().size), + GaugeMetric("minecraft_players_max", value = bootstrap.server.maxPlayers()) + ) + } +} \ No newline at end of file diff --git a/platforms/sponge/src/main/java/dev/cubxity/metrics/sponge/metric/tick/TickCollection.kt b/platforms/sponge/src/main/java/dev/cubxity/metrics/sponge/metric/tick/TickCollection.kt new file mode 100644 index 00000000..816d3e8b --- /dev/null +++ b/platforms/sponge/src/main/java/dev/cubxity/metrics/sponge/metric/tick/TickCollection.kt @@ -0,0 +1,53 @@ +/* + * 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 . + */ + +package dev.cubxity.metrics.sponge.metric.tick + +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.Histogram +import dev.cubxity.plugins.metrics.api.metric.store.VolatileDoubleStore +import dev.cubxity.plugins.metrics.api.metric.store.VolatileLongStore +import dev.cubxity.plugins.metrics.common.metric.Metrics + +class TickCollection(bootstrap: UnifiedMetricsSpongeBootstrap): CollectorCollection { + + private val reporter = TickReporter(this, bootstrap) + + private val tickDuration = Histogram( + Metrics.Server.TickDurationSeconds, + sumStoreFactory = VolatileDoubleStore, + countStoreFactory = VolatileLongStore + ) + + override val collectors: List = listOf(tickDuration) + + override val isAsync: Boolean = true + + override fun initialize() { + reporter.initialize() + } + + override fun dispose() { + reporter.dispose() + } + + fun onTick(duration: Double) { + tickDuration += duration + } +} \ No newline at end of file diff --git a/platforms/sponge/src/main/java/dev/cubxity/metrics/sponge/metric/tick/TickReporter.kt b/platforms/sponge/src/main/java/dev/cubxity/metrics/sponge/metric/tick/TickReporter.kt new file mode 100644 index 00000000..c68c7650 --- /dev/null +++ b/platforms/sponge/src/main/java/dev/cubxity/metrics/sponge/metric/tick/TickReporter.kt @@ -0,0 +1,53 @@ +/* + * 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 . + */ + +package dev.cubxity.metrics.sponge.metric.tick + +import dev.cubxity.metrics.sponge.bootstrap.UnifiedMetricsSpongeBootstrap +import org.spongepowered.api.Sponge +import org.spongepowered.api.scheduler.ScheduledTask +import org.spongepowered.api.scheduler.Task +import org.spongepowered.api.util.Ticks + +class TickReporter( + private val metric: TickCollection, + bootstrap: UnifiedMetricsSpongeBootstrap +) { + + private lateinit var task: ScheduledTask + + private val taskExecutor = Task.builder() + .delay(Ticks.of(1)) + .execute(Runnable { + metric.onTick(0.0) + this.startTask() + }) + .plugin(bootstrap.container) + + fun initialize() { + startTask() + } + + private fun startTask() { + task = Sponge.asyncScheduler().submit(taskExecutor.build()) + } + + fun dispose() { + task.cancel() + } + +} \ No newline at end of file diff --git a/platforms/sponge/src/main/java/dev/cubxity/metrics/sponge/metric/world/WorldCollection.kt b/platforms/sponge/src/main/java/dev/cubxity/metrics/sponge/metric/world/WorldCollection.kt new file mode 100644 index 00000000..0739de45 --- /dev/null +++ b/platforms/sponge/src/main/java/dev/cubxity/metrics/sponge/metric/world/WorldCollection.kt @@ -0,0 +1,26 @@ +/* + * 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 . + */ + +package dev.cubxity.metrics.sponge.metric.world + +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 + +class WorldCollection(bootstrap: UnifiedMetricsSpongeBootstrap): CollectorCollection { + override val collectors: List = listOf(WorldCollector(bootstrap)) +} \ No newline at end of file diff --git a/platforms/sponge/src/main/java/dev/cubxity/metrics/sponge/metric/world/WorldCollector.kt b/platforms/sponge/src/main/java/dev/cubxity/metrics/sponge/metric/world/WorldCollector.kt new file mode 100644 index 00000000..7e88b958 --- /dev/null +++ b/platforms/sponge/src/main/java/dev/cubxity/metrics/sponge/metric/world/WorldCollector.kt @@ -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 . + */ + +package dev.cubxity.metrics.sponge.metric.world + +import dev.cubxity.metrics.sponge.bootstrap.UnifiedMetricsSpongeBootstrap +import dev.cubxity.plugins.metrics.api.metric.collector.Collector +import dev.cubxity.plugins.metrics.api.metric.data.GaugeMetric +import dev.cubxity.plugins.metrics.api.metric.data.Metric +import dev.cubxity.plugins.metrics.api.util.fastForEach +import dev.cubxity.plugins.metrics.common.metric.Metrics +import org.spongepowered.api.world.server.ServerWorld + +class WorldCollector(private val bootstrap: UnifiedMetricsSpongeBootstrap): Collector { + + override fun collect(): List { + val worldManager = bootstrap.server.worldManager() + val worlds: List = worldManager.worlds().toList() + val samples = ArrayList(worlds.size * 3) + + worlds.fastForEach { + val tags = mapOf("world" to it.key().value()) + samples.add(GaugeMetric(Metrics.Server.WorldEntitiesCount, tags, it.entities().size)) + samples.add(GaugeMetric(Metrics.Server.WorldPlayersCount, tags, it.players().size)) + samples.add(GaugeMetric(Metrics.Server.WorldLoadedChunks, tags, it.loadedChunks().toSet().size)) + } + return samples + } +} \ No newline at end of file diff --git a/settings.gradle.kts b/settings.gradle.kts index 3daa9cf9..d8eb359a 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -30,6 +30,8 @@ include(modulePrefix + platformPrefix + "minestom") include(modulePrefix + platformPrefix + "velocity") include(modulePrefix + platformPrefix + "bungee") include(modulePrefix + platformPrefix + "fabric") +include(modulePrefix + platformPrefix + "sponge") + include(modulePrefix + driverPrefix + "influx") include(modulePrefix + driverPrefix + "prometheus") @@ -44,6 +46,7 @@ project(modulePrefix + platformPrefix + "minestom").projectDir = File(platformsD project(modulePrefix + platformPrefix + "velocity").projectDir = File(platformsDir, "velocity") project(modulePrefix + platformPrefix + "bungee").projectDir = File(platformsDir, "bungee") project(modulePrefix + platformPrefix + "fabric").projectDir = File(platformsDir, "fabric") +project(modulePrefix + platformPrefix + "sponge").projectDir = File(platformsDir, "sponge") val driversDir = File(rootDir, "drivers") project(modulePrefix + driverPrefix + "influx").projectDir = File(driversDir, "influx")