From 04408350139044b5b01765c742e3c43696664933 Mon Sep 17 00:00:00 2001 From: alexcrea Date: Tue, 3 Mar 2026 04:08:04 +0100 Subject: [PATCH] Help Command --- src/main/kotlin/io/delilaheve/CustomAnvil.kt | 4 +-- .../alexcrea/cuanvil/command/CASubCommand.kt | 4 +++ ...ustomAnvilCmd.kt => CustomAnvilCommand.kt} | 11 +++++-- .../cuanvil/command/DiagnosticExecutor.kt | 4 +++ .../cuanvil/command/EditConfigExecutor.kt | 4 +++ .../alexcrea/cuanvil/command/HelpExecutor.kt | 32 +++++++++++++++++++ .../cuanvil/command/ReloadExecutor.kt | 4 +++ 7 files changed, 58 insertions(+), 5 deletions(-) rename src/main/kotlin/xyz/alexcrea/cuanvil/command/{CustomAnvilCmd.kt => CustomAnvilCommand.kt} (88%) create mode 100644 src/main/kotlin/xyz/alexcrea/cuanvil/command/HelpExecutor.kt diff --git a/src/main/kotlin/io/delilaheve/CustomAnvil.kt b/src/main/kotlin/io/delilaheve/CustomAnvil.kt index 9b2b696..44ca0a8 100644 --- a/src/main/kotlin/io/delilaheve/CustomAnvil.kt +++ b/src/main/kotlin/io/delilaheve/CustomAnvil.kt @@ -6,7 +6,7 @@ import org.bukkit.configuration.file.YamlConfiguration import org.bukkit.plugin.java.JavaPlugin import xyz.alexcrea.cuanvil.api.event.CAConfigReadyEvent import xyz.alexcrea.cuanvil.api.event.CAEnchantRegistryReadyEvent -import xyz.alexcrea.cuanvil.command.CustomAnvilCmd +import xyz.alexcrea.cuanvil.command.CustomAnvilCommand import xyz.alexcrea.cuanvil.command.EditConfigExecutor import xyz.alexcrea.cuanvil.command.ReloadExecutor import xyz.alexcrea.cuanvil.config.ConfigHolder @@ -309,7 +309,7 @@ open class CustomAnvil : JavaPlugin() { command = getCommand(commandConfigName) command?.setExecutor(EditConfigExecutor()) - CustomAnvilCmd(this) + CustomAnvilCommand(this) } } diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/command/CASubCommand.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/command/CASubCommand.kt index 85c1a58..b06d07f 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/command/CASubCommand.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/command/CASubCommand.kt @@ -39,4 +39,8 @@ abstract class CASubCommand: CommandExecutor { list: MutableList) { } + open fun description(): String { + return "no description" + } + } \ No newline at end of file diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/command/CustomAnvilCmd.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/command/CustomAnvilCommand.kt similarity index 88% rename from src/main/kotlin/xyz/alexcrea/cuanvil/command/CustomAnvilCmd.kt rename to src/main/kotlin/xyz/alexcrea/cuanvil/command/CustomAnvilCommand.kt index c3de43b..e5e689e 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/command/CustomAnvilCmd.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/command/CustomAnvilCommand.kt @@ -9,7 +9,7 @@ import org.bukkit.command.TabCompleter import xyz.alexcrea.cuanvil.util.MetricsUtil import java.util.ArrayList -class CustomAnvilCmd(plugin: CustomAnvil) : CommandExecutor, TabCompleter { +class CustomAnvilCommand(plugin: CustomAnvil) : CommandExecutor, TabCompleter { // Name of the generic command companion object { @@ -17,16 +17,20 @@ class CustomAnvilCmd(plugin: CustomAnvil) : CommandExecutor, TabCompleter { } private val editConfigCommand = EditConfigExecutor() + private val helpCommand = HelpExecutor() private val commands = ImmutableMap.of( "gui", editConfigCommand, "reload", ReloadExecutor(), "diagnostic", DiagnosticExecutor(), + "help", helpCommand, ) init { val self = plugin.getCommand(genericCommandName)!! self.setExecutor(this) self.tabCompleter = this + + helpCommand.commands = commands } override fun onCommand( @@ -68,8 +72,9 @@ class CustomAnvilCmd(plugin: CustomAnvil) : CommandExecutor, TabCompleter { ): MutableList { val result = ArrayList() if(args.size < 2) { - for (cmd in commands) { - result.add(cmd.key) + for ((key, cmd) in commands) { + if(!cmd.allowed(sender)) continue + result.add(key) } } else { val subcmd = commands[args[0].lowercase()] diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/command/DiagnosticExecutor.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/command/DiagnosticExecutor.kt index 6ddc8df..e550bd1 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/command/DiagnosticExecutor.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/command/DiagnosticExecutor.kt @@ -204,6 +204,10 @@ class DiagnosticExecutor: CASubCommand() { return this.name + " v" + this.description.version } + override fun description(): String { + return "Basic diagnostic of this plugin" + } + private fun pluginListDiag(sender: CommandSender, stb: StringBuilder) { val enabledPlugins: MutableList = ArrayList() val disabledPlugins: MutableList = ArrayList() diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/command/EditConfigExecutor.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/command/EditConfigExecutor.kt index d489db2..fba89b7 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/command/EditConfigExecutor.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/command/EditConfigExecutor.kt @@ -38,4 +38,8 @@ class EditConfigExecutor: CASubCommand() { return sender.hasPermission(CustomAnvil.editConfigPermission) } + override fun description(): String { + return "Gui to edit the plugin's config" + } + } diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/command/HelpExecutor.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/command/HelpExecutor.kt new file mode 100644 index 0000000..697f1ee --- /dev/null +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/command/HelpExecutor.kt @@ -0,0 +1,32 @@ +package xyz.alexcrea.cuanvil.command + +import com.google.common.collect.ImmutableMap +import org.bukkit.command.Command +import org.bukkit.command.CommandSender + +class HelpExecutor: CASubCommand() { + + lateinit var commands: ImmutableMap + + override fun executeCommand(sender: CommandSender, + cmd: Command, + cmdstr: String, + args: Array): Boolean { + + val stb = StringBuilder("List of available commands:") + for ((key, cmd) in commands) { + if(!cmd.allowed(sender)) continue + + stb.append("\n- $key: ").append(cmd.description()) + } + + sender.sendMessage(stb.toString()) + + return true + } + + override fun description(): String { + return "Help command" + } + +} \ No newline at end of file diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt index f3f97a7..ef23e7d 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt @@ -39,6 +39,10 @@ class ReloadExecutor : CASubCommand() { return sender.hasPermission(CustomAnvil.commandReloadPermission) } + override fun description(): String { + return "Reload the configuration of this plugin" + } + /** * Execute the command, return true if success or false otherwise */