diff --git a/src/main/kotlin/io/delilaheve/CustomAnvil.kt b/src/main/kotlin/io/delilaheve/CustomAnvil.kt index b48f4a6..7e1d959 100644 --- a/src/main/kotlin/io/delilaheve/CustomAnvil.kt +++ b/src/main/kotlin/io/delilaheve/CustomAnvil.kt @@ -6,6 +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.EditConfigExecutor import xyz.alexcrea.cuanvil.command.ReloadExecutor import xyz.alexcrea.cuanvil.config.ConfigHolder @@ -290,6 +291,8 @@ open class CustomAnvil : JavaPlugin() { command = getCommand(commandConfigName) command?.setExecutor(EditConfigExecutor()) + + CustomAnvilCmd(this) } } diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/command/CASubCommand.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/command/CASubCommand.kt new file mode 100644 index 0000000..7558127 --- /dev/null +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/command/CASubCommand.kt @@ -0,0 +1,40 @@ +package xyz.alexcrea.cuanvil.command + +import org.bukkit.ChatColor +import org.bukkit.command.Command +import org.bukkit.command.CommandExecutor +import org.bukkit.command.CommandSender + +abstract class CASubCommand: CommandExecutor { + + private var alreadySaid = false; + override fun onCommand( + sender: CommandSender, + cmd: Command, + cmdstr: String, + args: Array + ): Boolean { + if(!alreadySaid){ + sender.sendMessage(ChatColor.RED.toString() + + "Please not that this command will be replaced as a subcommand of `/customanvil`") + alreadySaid = true + } + + return executeCommand(sender, cmd, cmdstr, args) + } + + abstract fun executeCommand( + sender: CommandSender, + cmd: Command, + cmdstr: String, + args: Array): Boolean + + open fun allowed(sender: CommandSender): Boolean { + return true + } + + open fun tabCompleter(list: MutableList) { + + } + +} \ 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/CustomAnvilCmd.kt new file mode 100644 index 0000000..5333b94 --- /dev/null +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/command/CustomAnvilCmd.kt @@ -0,0 +1,77 @@ +package xyz.alexcrea.cuanvil.command + +import com.google.common.collect.ImmutableMap +import io.delilaheve.CustomAnvil +import org.bukkit.command.Command +import org.bukkit.command.CommandExecutor +import org.bukkit.command.CommandSender +import org.bukkit.command.TabCompleter +import java.util.ArrayList +import java.util.Arrays + +class CustomAnvilCmd(plugin: CustomAnvil) : CommandExecutor, TabCompleter { + + // Name of the generic command + companion object { + private const val genericCommandName = "customanvil" + } + + private val editConfigCommand = EditConfigExecutor() + private val commands: ImmutableMap + + init { + commands = ImmutableMap.of( + "gui", editConfigCommand, + "reload", ReloadExecutor() + ) + + val self = plugin.getCommand(genericCommandName)!! + self.setExecutor(this) + self.tabCompleter = this + } + + override fun onCommand( + sender: CommandSender, + cmd: Command, + cmdstr: String, + args: Array + ): Boolean { + // Find sub command to execute based on the provided command name + val subcmd: CASubCommand? = if(args.isEmpty()) { + editConfigCommand + }else { + commands[args[0].lowercase()] + } + + if(subcmd == null) { + sender.sendMessage("Invalid subcommand. run `$cmdstr help` to see available commands") + return true + } + + val newargs = args.copyOfRange(1, args.size) + return subcmd.executeCommand(sender, cmd, cmdstr, newargs) + } + + override fun onTabComplete( + sender: CommandSender, + cmd: Command, + cmdstr: String, + args: Array + ): MutableList { + val result = ArrayList() + if(args.isEmpty()) { + for (cmd in commands) { + result.add(cmd.key) + } + } else { + val subcmd = commands[args[0].lowercase()] + subcmd?.tabCompleter(result) + } + + //assumed all provided tab completed string are lowercase + return result.stream() + .filter { it.startsWith(args[args.size - 1]) } + .sorted() + .toList() + } +} diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/command/EditConfigExecutor.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/command/EditConfigExecutor.kt index f90f765..d489db2 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/command/EditConfigExecutor.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/command/EditConfigExecutor.kt @@ -2,17 +2,21 @@ package xyz.alexcrea.cuanvil.command import io.delilaheve.CustomAnvil import org.bukkit.command.Command -import org.bukkit.command.CommandExecutor import org.bukkit.command.CommandSender import org.bukkit.entity.HumanEntity import xyz.alexcrea.cuanvil.dependency.util.PlatformUtil import xyz.alexcrea.cuanvil.gui.config.MainConfigGui import xyz.alexcrea.cuanvil.gui.util.GuiGlobalActions -class EditConfigExecutor : CommandExecutor { +class EditConfigExecutor: CASubCommand() { - override fun onCommand(sender: CommandSender, command: Command, label: String, args: Array): Boolean { - if (!sender.hasPermission(CustomAnvil.editConfigPermission)) { + override fun executeCommand(sender: CommandSender, + cmd: Command, + cmdstr: String, + args: Array): Boolean { + if (sender !is HumanEntity) return false + + if (!allowed(sender)) { sender.sendMessage(GuiGlobalActions.NO_EDIT_PERM) return false } @@ -25,10 +29,13 @@ class EditConfigExecutor : CommandExecutor { return false } - if (sender !is HumanEntity) return false MainConfigGui.getInstance().show(sender) return true } + override fun allowed(sender: CommandSender): Boolean { + return sender.hasPermission(CustomAnvil.editConfigPermission) + } + } diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt index dc2fe8e..f3f97a7 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt @@ -11,9 +11,13 @@ import xyz.alexcrea.cuanvil.dependency.DependencyManager import xyz.alexcrea.cuanvil.gui.config.global.* import xyz.alexcrea.cuanvil.update.UpdateHandler -class ReloadExecutor : CommandExecutor { - override fun onCommand(sender: CommandSender, cmd: Command, cmdstr: String, args: Array): Boolean { - if (!sender.hasPermission(CustomAnvil.commandReloadPermission)) { +class ReloadExecutor : CASubCommand() { + + override fun executeCommand(sender: CommandSender, + cmd: Command, + cmdstr: String, + args: Array): Boolean { + if (!allowed(sender)) { sender.sendMessage("§cYou do not have permission to reload the config") return false } @@ -31,6 +35,10 @@ class ReloadExecutor : CommandExecutor { return commandSuccess } + override fun allowed(sender: CommandSender): Boolean { + return sender.hasPermission(CustomAnvil.commandReloadPermission) + } + /** * Execute the command, return true if success or false otherwise */ diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 9449f8b..f90f975 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -10,6 +10,8 @@ authors: [ DelilahEve, alexcrea ] libraries: [${libraries}] commands: + customanvil: + description: Generic command for custom anvil anvilconfigreload: description: Reload every config of this plugin permission: ca.command.reload @@ -36,6 +38,9 @@ permissions: ca.command.reload: default: op description: Allow administrator to reload the plugin's configs + ca.command.debug: + default: op + description: Get debug information about the plugin and server ca.config.edit: default: op description: Allow administrator to edit the plugin's config in game