From 965ee333257be8b6f9d4a06c1b65364c8de1581b Mon Sep 17 00:00:00 2001 From: alexcrea Date: Mon, 29 Jun 2026 00:56:54 +0200 Subject: [PATCH] add debug log command --- src/main/kotlin/io/delilaheve/CustomAnvil.kt | 16 ++- .../io/delilaheve/util/ConfigOptions.kt | 9 ++ .../cuanvil/command/CustomAnvilCommand.kt | 1 + .../cuanvil/command/DebugToggleExecutor.kt | 114 ++++++++++++++++++ .../cuanvil/command/DiagnosticExecutor.kt | 2 +- 5 files changed, 140 insertions(+), 2 deletions(-) create mode 100644 src/main/kotlin/xyz/alexcrea/cuanvil/command/DebugToggleExecutor.kt diff --git a/src/main/kotlin/io/delilaheve/CustomAnvil.kt b/src/main/kotlin/io/delilaheve/CustomAnvil.kt index 4d99faf5..e12ba4d6 100644 --- a/src/main/kotlin/io/delilaheve/CustomAnvil.kt +++ b/src/main/kotlin/io/delilaheve/CustomAnvil.kt @@ -71,21 +71,35 @@ open class CustomAnvil : JavaPlugin() { var latestVer: String? = null + // Debug + val debugStorageQueue = ArrayDeque() + + private fun addToLogQueue(message: String) { + if(debugStorageQueue.size >= 200) { + // Let not store infinite debug logs lol + debugStorageQueue.removeFirst() + } + + debugStorageQueue.addLast(message) + } + /** * Logging handler */ @JvmStatic fun log(message: String) { if (ConfigOptions.debugLog) { instance.logger.info(message) + addToLogQueue(message) } } /** * Vebose Logging handler */ - fun verboseLog(message: String) { + @JvmStatic fun verboseLog(message: String) { if (ConfigOptions.verboseDebugLog) { instance.logger.info(message) + addToLogQueue(message) } } diff --git a/src/main/kotlin/io/delilaheve/util/ConfigOptions.kt b/src/main/kotlin/io/delilaheve/util/ConfigOptions.kt index 9dc85f97..86b531ec 100644 --- a/src/main/kotlin/io/delilaheve/util/ConfigOptions.kt +++ b/src/main/kotlin/io/delilaheve/util/ConfigOptions.kt @@ -128,6 +128,9 @@ object ConfigOptions { private const val DEFAULT_DEBUG_LOG = false private const val DEFAULT_VERBOSE_DEBUG_LOG = false + var OVERRIDE_DEBUG_LOG: Boolean? = null + var OVERRIDE_VERBOSE_DEBUG_LOG: Boolean? = null + // Dialog menu rename const val DEFAULT_DIALOG_RENAME_ENABLED = false const val DEFAULT_DIALOG_MAX_SIZE = 256 @@ -436,6 +439,9 @@ object ConfigOptions { */ val debugLog: Boolean get() { + val overrider = OVERRIDE_DEBUG_LOG + if(overrider != null) return overrider + return ConfigHolder.DEFAULT_CONFIG .config .getBoolean(DEBUG_LOGGING, DEFAULT_DEBUG_LOG) @@ -446,6 +452,9 @@ object ConfigOptions { */ val verboseDebugLog: Boolean get() { + val overrider = OVERRIDE_VERBOSE_DEBUG_LOG + if(overrider != null) return overrider + return ConfigHolder.DEFAULT_CONFIG .config .getBoolean(VERBOSE_DEBUG_LOGGING, DEFAULT_VERBOSE_DEBUG_LOG) diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/command/CustomAnvilCommand.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/command/CustomAnvilCommand.kt index e5e689e2..8bd1d827 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/command/CustomAnvilCommand.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/command/CustomAnvilCommand.kt @@ -22,6 +22,7 @@ class CustomAnvilCommand(plugin: CustomAnvil) : CommandExecutor, TabCompleter { "gui", editConfigCommand, "reload", ReloadExecutor(), "diagnostic", DiagnosticExecutor(), + "debug", DebugToggleExecutor(), "help", helpCommand, ) diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/command/DebugToggleExecutor.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/command/DebugToggleExecutor.kt new file mode 100644 index 00000000..9959d269 --- /dev/null +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/command/DebugToggleExecutor.kt @@ -0,0 +1,114 @@ +package xyz.alexcrea.cuanvil.command + +import io.delilaheve.CustomAnvil +import io.delilaheve.util.ConfigOptions +import net.md_5.bungee.api.chat.ClickEvent +import net.md_5.bungee.api.chat.HoverEvent +import net.md_5.bungee.api.chat.TextComponent +import net.md_5.bungee.api.chat.hover.content.Text +import org.bukkit.ChatColor +import org.bukkit.command.Command +import org.bukkit.command.CommandSender +import xyz.alexcrea.cuanvil.command.DiagnosticExecutor.Companion.NO_DIAG_PERM + +class DebugToggleExecutor : CASubCommand() { + + override fun description(): String { + return "Used to toggle debug logs and retrieve it" + } + + override fun allowed(sender: CommandSender): Boolean { + return sender.hasPermission(CustomAnvil.diagnosticPermission) + } + + override fun executeCommand( + sender: CommandSender, + cmd: Command, + cmdstr: String, + args: Array + ): Boolean { + if (!allowed(sender)) { + sender.sendMessage(NO_DIAG_PERM) + return false + } + + if (args.isEmpty()) { + sender.sendMessage("Need to specify a subcommand: \"toggle\" or \"get\"") + return true + } + when (args[0].lowercase()) { + "toggle" -> executeToggle(sender, args) + "get" -> executeGet(sender) + "get-and-clear" -> { + executeGet(sender) + CustomAnvil.debugStorageQueue.clear() + } + + "clear" -> { + CustomAnvil.debugStorageQueue.clear() + sender.sendMessage("Log Cleared") + } + + else -> return false + } + return true + } + + private fun executeToggle(sender: CommandSender, args: Array) { + if (args.size < 2) { + sender.sendMessage("Need to specify which type of debug to toggle: \"default\" or \"verbose\"") + return + } + when (args[1].lowercase()) { + "default" -> { + ConfigOptions.OVERRIDE_DEBUG_LOG = !ConfigOptions.debugLog + sender.sendMessage("Debug toggle to: ${ConfigOptions.debugLog}") + } + + "verbose" -> { + ConfigOptions.OVERRIDE_VERBOSE_DEBUG_LOG = !ConfigOptions.verboseDebugLog + sender.sendMessage("Debug toggle to: ${ConfigOptions.verboseDebugLog}") + } + + else -> sender.sendMessage("Invalid debug type: ${args[1]}") + } + + } + + private fun executeGet(sender: CommandSender) { + val stb = StringBuilder("Debug Log data:") + if (CustomAnvil.debugStorageQueue.isEmpty()) { + sender.sendMessage("No log to show ? make sure you tried with debug log toggled (/ca debug toggle)") + return + } + + stb.append("\nFound ${CustomAnvil.debugStorageQueue.size} lines\n") + for (log in CustomAnvil.debugStorageQueue) { + stb.append('\n').append(log) + } + + val message = TextComponent(ChatColor.GREEN.toString() + "Click to copy log data") + + message.clickEvent = ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, stb.toString()) + message.hoverEvent = HoverEvent(HoverEvent.Action.SHOW_TEXT, Text("ยง7Click to copy")) + + sender.spigot().sendMessage(message); + } + + override fun tabCompleter(sender: CommandSender, args: Array, list: MutableList) { + if (!allowed(sender)) return + + list.addAll( + when (args.size) { + 1 -> listOf("toggle", "get", "get-and-clear", "clear") + 2 -> when (args[0].lowercase()) { + "toggle" -> listOf("default", "verbose") + else -> listOf() + } + + else -> listOf() + } + ) + } + +} diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/command/DiagnosticExecutor.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/command/DiagnosticExecutor.kt index 053a3fc1..b095e14c 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/command/DiagnosticExecutor.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/command/DiagnosticExecutor.kt @@ -36,7 +36,7 @@ import java.util.stream.Collectors class DiagnosticExecutor: CASubCommand() { companion object{ - private const val NO_DIAG_PERM = "You do not have permission to diagnostic this server" + const val NO_DIAG_PERM = "You do not have permission to diagnostic this server" fun fetchNMSType(): String { val packetManager = DependencyManager.packetManager