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

Forge support #83

Open
wants to merge 24 commits into
base: dev/0.3.x
Choose a base branch
from
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
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 Forge : PlatformType("Forge")

// Proxies
object Velocity : PlatformType("Velocity")
Expand Down
135 changes: 135 additions & 0 deletions platforms/forge-1.18.2/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* 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/>.
*/

plugins {
id("net.kyori.blossom")
id("com.github.johnrengelman.shadow")
id("dev.architectury.loom") version "1.1.324"
}

java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

val mixinConfigName = "unifiedmetrics.mixins.json"

loom {
silentMojangMappingsLicense()

forge {
mixinConfigs(mixinConfigName)
}

mixin {
defaultRefmapName.set("mixin.refmap.json")
}
}

repositories {
maven {
name = "ParchmentMC"
url = uri("https://maven.parchmentmc.org")
}

maven {
name = "Kotlin for Forge"
url = uri("https://thedarkcolour.github.io/KotlinForForge/")
}
}

dependencies {
minecraft("com.mojang:minecraft:1.18.2")
mappings(loom.layered {
officialMojangMappings()
parchment("org.parchmentmc.data:parchment-1.18.2:2022.11.06@zip")
})

forge("net.minecraftforge:forge:1.18.2-40.2.0")

implementation("thedarkcolour:kotlinforforge:3.8.0")

shadow(project(":unifiedmetrics-core"))

forgeRuntimeLibrary(project(":unifiedmetrics-core"))
forgeRuntimeLibrary("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.8.0")
forgeRuntimeLibrary("org.jetbrains.kotlin:kotlin-reflect:1.8.0")
forgeRuntimeLibrary("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2")
forgeRuntimeLibrary("org.jetbrains.kotlinx:kotlinx-coroutines-jdk8:1.5.2")
forgeRuntimeLibrary("org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.1")
}

tasks {
compileKotlin {
kotlinOptions.jvmTarget = "17"
}

remapJar {
archiveClassifier.set("remap")
inputFile.set(jar.get().archiveFile.get())
}

shadowJar {
enabled = false // disable shadowJar for default settings can not satisfy out requirement.
}

create<com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar>("fatJar") {
archiveClassifier.set("")
dependsOn(remapJar)
mustRunAfter(remapJar)
from(zipTree(remapJar.get().archiveFile.get()))
duplicatesStrategy = DuplicatesStrategy.FAIL
configurations = listOf(project.configurations.shadow.get())
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("org.yaml.snakeyaml", "dev.cubxity.plugins.metrics.libs.org.yaml.snakeyaml")
relocate("org.snakeyaml", "dev.cubxity.plugins.metrics.libs.org.snakeyaml")
relocate("com.google.gson", "dev.cubxity.plugins.metrics.libs.org.yaml.snakeyaml")
relocate("io.reactivex", "dev.cubxity.plugins.metrics.libs.io.reactivex")
relocate("org.apache.common", "dev.cubxity.plugins.metrics.libs.org.apache.common")
relocate("org.reactivestreams", "dev.cubxity.plugins.metrics.libs.org.reactivestreams")
exclude("javax/**", "kotlin/**", "kotlinx/**", "org/jetbrains/**", "org/intellij/**")
manifest {
attributes["MixinConfigs"] = mixinConfigName
}
}

processResources {
inputs.property("version", project.version)
filesMatching("META-INF/mods.toml") {
expand("version" to project.version)
}
}

compileJava {
options.encoding = "UTF-8"
options.release.set(17)
}

assemble {
dependsOn(named("fatJar"))
}
}

blossom {
replaceTokenIn("src/main/kotlin/dev/cubxity/plugins/metrics/forge/bootstrap/UnifiedMetricsForgeBootstrap.kt")
replaceToken("@version@", version)
}
18 changes: 18 additions & 0 deletions platforms/forge-1.18.2/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# 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/>.
#

loom.platform=forge
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.forge.mixins;

import dev.cubxity.plugins.metrics.forge.events.PlayerConnectionEvent;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerLoginPacketListenerImpl;
import net.minecraftforge.common.MinecraftForge;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(ServerLoginPacketListenerImpl.class)
public abstract class ServerLoginPacketListenerImplMixin {


@Shadow
@Final
MinecraftServer server;

@Inject(method = "<init>", at = @At("RETURN"))
private void initConnection(CallbackInfo ci) {
ServerLoginPacketListenerImpl handler = (ServerLoginPacketListenerImpl) (Object) this;
MinecraftServer server = this.server;
MinecraftForge.EVENT_BUS.post(new PlayerConnectionEvent(handler, server));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.forge.mixins;

import dev.cubxity.plugins.metrics.forge.events.ServerPingEvent;
import net.minecraft.server.network.ServerStatusPacketListenerImpl;
import net.minecraftforge.common.MinecraftForge;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(ServerStatusPacketListenerImpl.class)
public abstract class ServerStatusPacketListenerImplMixin {

@Inject(method = "handleStatusRequest", at = @At("HEAD"))
private void handleHandleStatusRequest(CallbackInfo ci) {
MinecraftForge.EVENT_BUS.post(new ServerPingEvent());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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.forge

import dev.cubxity.plugins.metrics.api.UnifiedMetrics
import dev.cubxity.plugins.metrics.core.plugin.CoreUnifiedMetricsPlugin
import dev.cubxity.plugins.metrics.forge.bootstrap.UnifiedMetricsForgeBootstrap
import dev.cubxity.plugins.metrics.forge.metrics.events.EventsCollection
import dev.cubxity.plugins.metrics.forge.metrics.server.ServerCollection
import dev.cubxity.plugins.metrics.forge.metrics.tick.TickCollection
import dev.cubxity.plugins.metrics.forge.metrics.world.WorldCollection
import java.util.concurrent.Executors

class UnifiedMetricsForgePlugin(
override val bootstrap: UnifiedMetricsForgeBootstrap
): CoreUnifiedMetricsPlugin() {

override fun registerPlatformService(api: UnifiedMetrics) {

}

override fun registerPlatformMetrics() {
super.registerPlatformMetrics()

apiProvider.metricsManager.apply {
with(config.metrics.collectors) {
if (server) registerCollection(ServerCollection(bootstrap))
if (world) registerCollection(WorldCollection(bootstrap))
if (tick) registerCollection(TickCollection())
if (events) registerCollection(EventsCollection())
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* 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.forge.bootstrap

import dev.cubxity.plugins.metrics.api.platform.PlatformType
import dev.cubxity.plugins.metrics.common.UnifiedMetricsBootstrap
import dev.cubxity.plugins.metrics.common.plugin.dispatcher.CurrentThreadDispatcher
import dev.cubxity.plugins.metrics.forge.UnifiedMetricsForgePlugin
import dev.cubxity.plugins.metrics.forge.logger.Log4jLogger
import kotlinx.coroutines.CoroutineDispatcher
import net.minecraft.server.MinecraftServer
import net.minecraftforge.event.server.ServerStartedEvent
import net.minecraftforge.event.server.ServerStoppingEvent
import net.minecraftforge.fml.common.Mod
import net.minecraftforge.fml.event.lifecycle.FMLDedicatedServerSetupEvent
import net.minecraftforge.fml.loading.FMLPaths
import org.apache.logging.log4j.LogManager
import thedarkcolour.kotlinforforge.forge.FORGE_BUS
import thedarkcolour.kotlinforforge.forge.MOD_BUS
import thedarkcolour.kotlinforforge.forge.runForDist
import java.nio.file.Path

private const val pluginVersion = "@version@"

@Mod("unifiedmetrics")
class UnifiedMetricsForgeBootstrap : UnifiedMetricsBootstrap {
private val plugin = UnifiedMetricsForgePlugin(this)
lateinit var server: MinecraftServer

override val type: PlatformType
get() = PlatformType.Forge

override val version: String
get() = pluginVersion

override val serverBrand: String
get() = server.serverModName

override val dataDirectory: Path = FMLPaths.CONFIGDIR.get().resolve("unifiedmetrics")

override val configDirectory: Path = FMLPaths.CONFIGDIR.get().resolve("unifiedmetrics")

override val logger = Log4jLogger(LogManager.getLogger("UnifiedMetrics"))

override val dispatcher: CoroutineDispatcher = CurrentThreadDispatcher

init {
MOD_BUS.addListener(::onServerSetup)
}

private fun onServerSetup(event: FMLDedicatedServerSetupEvent) {
FORGE_BUS.addListener(::onServerStarted)
FORGE_BUS.addListener(::onServerStopping)
}

private fun onServerStarted(event: ServerStartedEvent) {
this.server = event.server
plugin.enable()
}


private fun onServerStopping(event: ServerStoppingEvent) {
plugin.disable()
}
}
Loading