From 3e68af06ea06320e598e198eac993cb7f963f091 Mon Sep 17 00:00:00 2001 From: alexcrea Date: Mon, 2 Mar 2026 21:58:09 +0100 Subject: [PATCH] progress on new metrics --- defaultconfigs/1.18/config.yml | 8 ++ defaultconfigs/1.21.11/config.yml | 8 ++ defaultconfigs/1.21.9/config.yml | 8 ++ defaultconfigs/1.21/config.yml | 8 ++ src/main/kotlin/io/delilaheve/CustomAnvil.kt | 10 ++- .../io/delilaheve/util/ConfigOptions.kt | 2 + .../cuanvil/command/CustomAnvilCmd.kt | 1 - .../alexcrea/cuanvil/command/DebugExecutor.kt | 25 ------- .../cuanvil/command/DiagnosticExecutor.kt | 35 +++++---- .../xyz/alexcrea/cuanvil/util/MetricsUtil.kt | 73 +++++++++++++++++++ src/main/resources/config.yml | 10 ++- 11 files changed, 141 insertions(+), 47 deletions(-) delete mode 100644 src/main/kotlin/xyz/alexcrea/cuanvil/command/DebugExecutor.kt create mode 100644 src/main/kotlin/xyz/alexcrea/cuanvil/util/MetricsUtil.kt diff --git a/defaultconfigs/1.18/config.yml b/defaultconfigs/1.18/config.yml index 0b543cd..69a0e18 100644 --- a/defaultconfigs/1.18/config.yml +++ b/defaultconfigs/1.18/config.yml @@ -3,6 +3,14 @@ # You can still manually edit here if you like to. but if you do, don't forget to /anvilconfigreload after you changes ! # +# What service of metric should custom anvil use +# Custom anvil collect generic information like server minecraft version, type, etc... +# It can also collect error information if error is happening (currently faststats only) +# It can also be disabled +# Please refer to README for public metric link +# possible options: auto, bstat, faststats, disabled +metric_type: auto + # All anvil cost will be capped to limit_repair_value if enabled. # # In other words: diff --git a/defaultconfigs/1.21.11/config.yml b/defaultconfigs/1.21.11/config.yml index 9a8eb9c..c281de8 100644 --- a/defaultconfigs/1.21.11/config.yml +++ b/defaultconfigs/1.21.11/config.yml @@ -3,6 +3,14 @@ # You can still manually edit here if you like to. but if you do, don't forget to /anvilconfigreload after you changes ! # +# What service of metric should custom anvil use +# Custom anvil collect generic information like server minecraft version, type, etc... +# It can also collect error information if error is happening (currently faststats only) +# It can also be disabled +# Please refer to README for public metric link +# possible options: auto, bstat, faststats, disabled +metric_type: auto + # All anvil cost will be capped to limit_repair_value if enabled. # # In other words: diff --git a/defaultconfigs/1.21.9/config.yml b/defaultconfigs/1.21.9/config.yml index 809bab9..de6f020 100644 --- a/defaultconfigs/1.21.9/config.yml +++ b/defaultconfigs/1.21.9/config.yml @@ -3,6 +3,14 @@ # You can still manually edit here if you like to. but if you do, don't forget to /anvilconfigreload after you changes ! # +# What service of metric should custom anvil use +# Custom anvil collect generic information like server minecraft version, type, etc... +# It can also collect error information if error is happening (currently faststats only) +# It can also be disabled +# Please refer to README for public metric link +# possible options: auto, bstat, faststats, disabled +metric_type: auto + # All anvil cost will be capped to limit_repair_value if enabled. # # In other words: diff --git a/defaultconfigs/1.21/config.yml b/defaultconfigs/1.21/config.yml index 88eff1a..ca4f248 100644 --- a/defaultconfigs/1.21/config.yml +++ b/defaultconfigs/1.21/config.yml @@ -3,6 +3,14 @@ # You can still manually edit here if you like to. but if you do, don't forget to /anvilconfigreload after you changes ! # +# What service of metric should custom anvil use +# Custom anvil collect generic information like server minecraft version, type, etc... +# It can also collect error information if error is happening (currently faststats only) +# It can also be disabled +# Please refer to README for public metric link +# possible options: auto, bstat, faststats, disabled +metric_type: auto + # All anvil cost will be capped to limit_repair_value if enabled. # # In other words: diff --git a/src/main/kotlin/io/delilaheve/CustomAnvil.kt b/src/main/kotlin/io/delilaheve/CustomAnvil.kt index 902b76d..e95622d 100644 --- a/src/main/kotlin/io/delilaheve/CustomAnvil.kt +++ b/src/main/kotlin/io/delilaheve/CustomAnvil.kt @@ -23,6 +23,7 @@ import xyz.alexcrea.cuanvil.update.ModrinthUpdateChecker import xyz.alexcrea.cuanvil.update.PluginSetDefault import xyz.alexcrea.cuanvil.update.UpdateHandler import xyz.alexcrea.cuanvil.util.Metrics +import xyz.alexcrea.cuanvil.util.MetricsUtil import java.io.File import java.io.FileReader import java.util.logging.Level @@ -34,7 +35,6 @@ open class CustomAnvil : JavaPlugin() { companion object { // pluginIDS - private const val bstatsPluginId = 20923 private const val modrinthPluginID = "S75Ueiq9" // Permission string required to use the plugin's features @@ -156,15 +156,17 @@ open class CustomAnvil : JavaPlugin() { } // Load metrics - try { - Metrics(this, bstatsPluginId) - } catch (_: Exception) {} + MetricsUtil.loadMetrics(this) // Load other thing later. // It is so other dependent plugins can implement there event listener before we fire them. DependencyManager.scheduler.scheduleGlobally(this) { loadEnchantmentSystemDirty() } } + override fun onDisable() { + MetricsUtil.shutdownMetrics() + } + private fun loadEnchantmentSystemDirty() { try { loadEnchantmentSystem() diff --git a/src/main/kotlin/io/delilaheve/util/ConfigOptions.kt b/src/main/kotlin/io/delilaheve/util/ConfigOptions.kt index afb009f..37ec7c0 100644 --- a/src/main/kotlin/io/delilaheve/util/ConfigOptions.kt +++ b/src/main/kotlin/io/delilaheve/util/ConfigOptions.kt @@ -21,6 +21,8 @@ object ConfigOptions { // Path for config values // ---------------------- + const val METRIC_TYPE = "metric_type" + const val CAP_ANVIL_COST = "limit_repair_cost" const val MAX_ANVIL_COST = "limit_repair_value" const val REMOVE_ANVIL_COST_LIMIT = "remove_repair_limit" diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/command/CustomAnvilCmd.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/command/CustomAnvilCmd.kt index 2a46fa8..e1c2b34 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/command/CustomAnvilCmd.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/command/CustomAnvilCmd.kt @@ -20,7 +20,6 @@ class CustomAnvilCmd(plugin: CustomAnvil) : CommandExecutor, TabCompleter { "gui", editConfigCommand, "reload", ReloadExecutor(), "diagnostic", DiagnosticExecutor(), - //"debug", DebugExecutor(), ) init { diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/command/DebugExecutor.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/command/DebugExecutor.kt deleted file mode 100644 index e7cbe9e..0000000 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/command/DebugExecutor.kt +++ /dev/null @@ -1,25 +0,0 @@ -package xyz.alexcrea.cuanvil.command - -import io.delilaheve.CustomAnvil -import org.bukkit.command.Command -import org.bukkit.command.CommandSender -import java.util.logging.Level - -class DebugExecutor : CASubCommand() { - - override fun executeCommand( - sender: CommandSender, - cmd: Command, - cmdstr: String, - args: Array - ): Boolean { - CustomAnvil.instance.logger.log(Level.SEVERE, "aaaaaaaaaaaaaaaaaaa"); - return true - } - - override fun tabCompleter(sender: CommandSender, args: Array, list: MutableList) { - //TODO - - } - -} diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/command/DiagnosticExecutor.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/command/DiagnosticExecutor.kt index 9459444..6ddc8df 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/command/DiagnosticExecutor.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/command/DiagnosticExecutor.kt @@ -37,6 +37,21 @@ class DiagnosticExecutor: CASubCommand() { companion object{ private const val NO_DIAG_PERM = "You do not have permission to diagnostic this server" + + fun fetchNMSType(): String { + val packetManager = DependencyManager.packetManager + val packetManagerClass = packetManager.javaClass + + val result = when (packetManagerClass) { + PaperPacketManager::class.java -> "Paper NMS" + ProtocoLibWrapper::class.java -> "Protocolib" + NoPacketManager::class.java -> "None" + else -> "Version Specific" + + } + + return "$result ${if(packetManager.canSetInstantBuild) '✅' else '❌'}" + } } enum class DiagParams(val value: String) { @@ -124,7 +139,10 @@ class DiagnosticExecutor: CASubCommand() { fun diagnostic(sender: CommandSender, stb: StringBuilder, params: Set){ stb.append("Server Info\n") - stb.append("\nPlugin Version: ").append(CustomAnvil.instance.description.version) + val version = CustomAnvil.instance.description.version + stb.append("\nPlugin Version: ").append(version) + if(version.contains("dev")) stb.append(" (alpha)") + stb.append("\nLatest Update: ").append(CustomAnvil.latestVer) stb.append("\nServer Version: ").append(Bukkit.getVersion()).append(" (").append(Bukkit.getName()).append(')') stb.append("\nPlugin Enabled: ").append(if(CustomAnvil.instance.isEnabled) "Yes" else "No") @@ -181,21 +199,6 @@ class DiagnosticExecutor: CASubCommand() { simulateAnvil(player, stb, sword, enchantedBook, enchantedSword) } - private fun fetchNMSType(): String { - val packetManager = DependencyManager.packetManager - val packetManagerClass = packetManager.javaClass - - val result = when (packetManagerClass) { - PaperPacketManager::class.java -> "Paper NMS" - ProtocoLibWrapper::class.java -> "Protocolib" - NoPacketManager::class.java -> "None" - else -> "Version Specific" - - } - - return "$result ${if(packetManager.canSetInstantBuild) '✅' else '❌'}" - } - private val Plugin.pluginNameDisplay: String get() { return this.name + " v" + this.description.version diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/util/MetricsUtil.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/util/MetricsUtil.kt new file mode 100644 index 0000000..3b912bf --- /dev/null +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/util/MetricsUtil.kt @@ -0,0 +1,73 @@ +package xyz.alexcrea.cuanvil.util + +import dev.faststats.bukkit.BukkitMetrics +import dev.faststats.core.ErrorTracker +import dev.faststats.core.data.Metric +import io.delilaheve.CustomAnvil +import io.delilaheve.util.ConfigOptions +import xyz.alexcrea.cuanvil.command.DiagnosticExecutor +import xyz.alexcrea.cuanvil.config.ConfigHolder + +object MetricsUtil { + + private const val BSTATS_PLUGIN_ID = 20923 + private const val FASTSTATS_TOKEN = "fc282b048adcc71a77bc00ace49e8a81" + + private var ERROR_TRACKER: ErrorTracker? = null + private var FAST_STATS_METRICS: BukkitMetrics? = null + + fun loadMetrics(plugin: CustomAnvil) { + val metricString = ConfigHolder.DEFAULT_CONFIG.config.getString(ConfigOptions.METRIC_TYPE, MetricType.AUTO.value)!! + val metricType = MetricType.from(metricString) + + val nmsType = DiagnosticExecutor.fetchNMSType() + val isAlpha = CustomAnvil.instance.description.version.contains("dev") + if(metricType.allowBStats) { + try { + val metric = Metrics(plugin, BSTATS_PLUGIN_ID) + //TODO nms type custom chart + } catch (_: Exception) {} + } + + if(metricType.allowFastStats) { + ERROR_TRACKER = ErrorTracker.contextAware(); + FAST_STATS_METRICS = BukkitMetrics.factory() + .addMetric(Metric.string("nms_type") { nmsType }) + .addMetric(Metric.bool("using_alpha") { isAlpha }) + .errorTracker(ERROR_TRACKER) + .token(FASTSTATS_TOKEN) + .create(plugin) + + FAST_STATS_METRICS!!.ready() + } + } + + fun shutdownMetrics() { + FAST_STATS_METRICS?.shutdown() + } + + fun trackError(e: Throwable) { + ERROR_TRACKER?.trackError(e) + } + + fun trackError(message: String) { + ERROR_TRACKER?.trackError(message) + } +} + +enum class MetricType( + val value: String, + val allowBStats: Boolean, + val allowFastStats: Boolean, +) { + AUTO("auto", true, true), + BSTATS("bstat", true, false), + FAST_STATS("faststats", false, true), + DISABLED("disabled", false, false), + ; + + companion object { + fun from(value: String): MetricType = entries.find { it.value == value } ?: AUTO + } + +} \ No newline at end of file diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index c1fe214..88ed56b 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,8 +1,16 @@ # -# It is recommended that you use /configanvil to edit theses config. +# It is recommended that you use /configanvil to edit most of these config. # You can still manually edit here if you like to. but if you do, don't forget to /anvilconfigreload after you changes ! # +# What service of metric should custom anvil use +# Custom anvil collect generic information like server minecraft version, type, etc... +# It can also collect error information if error is happening (currently faststats only) +# It can also be disabled +# Please refer to README for public metric link +# possible options: auto, bstat, faststats, disabled +metric_type: auto + # All anvil cost will be capped to limit_repair_value if enabled. # # In other words: