progress on new metrics

This commit is contained in:
alexcrea 2026-03-02 21:58:09 +01:00
parent d037263e3f
commit 3e68af06ea
Signed by: alexcrea
GPG key ID: E346CD16413450E3
11 changed files with 141 additions and 47 deletions

View file

@ -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()

View file

@ -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"

View file

@ -20,7 +20,6 @@ class CustomAnvilCmd(plugin: CustomAnvil) : CommandExecutor, TabCompleter {
"gui", editConfigCommand,
"reload", ReloadExecutor(),
"diagnostic", DiagnosticExecutor(),
//"debug", DebugExecutor(),
)
init {

View file

@ -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<out String>
): Boolean {
CustomAnvil.instance.logger.log(Level.SEVERE, "aaaaaaaaaaaaaaaaaaa");
return true
}
override fun tabCompleter(sender: CommandSender, args: Array<out String>, list: MutableList<String>) {
//TODO
}
}

View file

@ -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<DiagParams>){
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

View file

@ -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
}
}

View file

@ -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: