mirror of
https://github.com/alexcrea/CustomAnvil.git
synced 2026-08-28 08:25:56 +02:00
96 lines
2.9 KiB
Kotlin
96 lines
2.9 KiB
Kotlin
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 xyz.alexcrea.cuanvil.util.MetricsUtil
|
|
|
|
class CustomAnvilCommand(plugin: CustomAnvil) : CommandExecutor, TabCompleter {
|
|
|
|
// Name of the generic command
|
|
companion object {
|
|
private const val genericCommandName = "customanvil"
|
|
}
|
|
|
|
private val editConfigCommand = EditConfigExecutor()
|
|
private val helpCommand = HelpExecutor()
|
|
private val commands = ImmutableMap.of(
|
|
"gui", editConfigCommand,
|
|
"reload", ReloadExecutor(),
|
|
"diagnostic", DiagnosticExecutor(),
|
|
"debug", DebugToggleExecutor(),
|
|
"help", helpCommand,
|
|
)
|
|
|
|
init {
|
|
val self = plugin.getCommand(genericCommandName)!!
|
|
self.setExecutor(this)
|
|
self.tabCompleter = this
|
|
|
|
helpCommand.commands = commands
|
|
}
|
|
|
|
override fun onCommand(
|
|
sender: CommandSender,
|
|
cmd: Command,
|
|
cmdstr: String,
|
|
args: Array<out String>
|
|
): Boolean {
|
|
// Find sub command to execute based on the provided command name
|
|
val subcmd: CASubCommand?
|
|
val newargs: Array<out String>
|
|
if (args.isEmpty()) {
|
|
subcmd = editConfigCommand
|
|
newargs = args
|
|
} else {
|
|
subcmd = commands[args[0].lowercase()]
|
|
newargs = args.copyOfRange(1, args.size)
|
|
}
|
|
|
|
if (subcmd == null || !subcmd.allowed(sender)) {
|
|
sender.sendMessage("Invalid subcommand. run `$cmdstr help` to see available commands")
|
|
return true
|
|
}
|
|
|
|
try {
|
|
return subcmd.executeCommand(sender, cmd, cmdstr, newargs)
|
|
} catch (e: Throwable) {
|
|
MetricsUtil.trackError(e)
|
|
sender.sendMessage("§cError running this command")
|
|
return false
|
|
}
|
|
}
|
|
|
|
override fun onTabComplete(
|
|
sender: CommandSender,
|
|
cmd: Command,
|
|
cmdstr: String,
|
|
args: Array<out String>
|
|
): MutableList<String> {
|
|
val result = ArrayList<String>()
|
|
if (args.size < 2) {
|
|
for ((key, cmd) in commands) {
|
|
if (!cmd.allowed(sender)) continue
|
|
result.add(key)
|
|
}
|
|
} else {
|
|
val subcmd = commands[args[0].lowercase()]
|
|
|
|
if (subcmd != null) {
|
|
val newArgs = args.copyOfRange(1, args.size)
|
|
if (!subcmd.allowed(sender)) return result
|
|
|
|
subcmd.tabCompleter(sender, newArgs, result)
|
|
}
|
|
}
|
|
|
|
//assumed all provided tab completed string are lowercase
|
|
return result.stream()
|
|
.filter { it.startsWith(args[args.size - 1]) }
|
|
.sorted()
|
|
.toList()
|
|
}
|
|
}
|