prepare generic command

This commit is contained in:
alexcrea 2025-09-12 12:41:46 +02:00 committed by alexcrea
parent 196392e206
commit 4023eda3e3
Signed by: alexcrea
GPG key ID: E346CD16413450E3
6 changed files with 148 additions and 8 deletions

View file

@ -6,6 +6,7 @@ import org.bukkit.configuration.file.YamlConfiguration
import org.bukkit.plugin.java.JavaPlugin import org.bukkit.plugin.java.JavaPlugin
import xyz.alexcrea.cuanvil.api.event.CAConfigReadyEvent import xyz.alexcrea.cuanvil.api.event.CAConfigReadyEvent
import xyz.alexcrea.cuanvil.api.event.CAEnchantRegistryReadyEvent import xyz.alexcrea.cuanvil.api.event.CAEnchantRegistryReadyEvent
import xyz.alexcrea.cuanvil.command.CustomAnvilCmd
import xyz.alexcrea.cuanvil.command.EditConfigExecutor import xyz.alexcrea.cuanvil.command.EditConfigExecutor
import xyz.alexcrea.cuanvil.command.ReloadExecutor import xyz.alexcrea.cuanvil.command.ReloadExecutor
import xyz.alexcrea.cuanvil.config.ConfigHolder import xyz.alexcrea.cuanvil.config.ConfigHolder
@ -290,6 +291,8 @@ open class CustomAnvil : JavaPlugin() {
command = getCommand(commandConfigName) command = getCommand(commandConfigName)
command?.setExecutor(EditConfigExecutor()) command?.setExecutor(EditConfigExecutor())
CustomAnvilCmd(this)
} }
} }

View file

@ -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<out String>
): 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<out String>): Boolean
open fun allowed(sender: CommandSender): Boolean {
return true
}
open fun tabCompleter(list: MutableList<String>) {
}
}

View file

@ -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<String, CASubCommand>
init {
commands = ImmutableMap.of<String, CASubCommand>(
"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<out String>
): 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<out String>
): MutableList<String> {
val result = ArrayList<String>()
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()
}
}

View file

@ -2,17 +2,21 @@ package xyz.alexcrea.cuanvil.command
import io.delilaheve.CustomAnvil import io.delilaheve.CustomAnvil
import org.bukkit.command.Command import org.bukkit.command.Command
import org.bukkit.command.CommandExecutor
import org.bukkit.command.CommandSender import org.bukkit.command.CommandSender
import org.bukkit.entity.HumanEntity import org.bukkit.entity.HumanEntity
import xyz.alexcrea.cuanvil.dependency.util.PlatformUtil import xyz.alexcrea.cuanvil.dependency.util.PlatformUtil
import xyz.alexcrea.cuanvil.gui.config.MainConfigGui import xyz.alexcrea.cuanvil.gui.config.MainConfigGui
import xyz.alexcrea.cuanvil.gui.util.GuiGlobalActions import xyz.alexcrea.cuanvil.gui.util.GuiGlobalActions
class EditConfigExecutor : CommandExecutor { class EditConfigExecutor: CASubCommand() {
override fun onCommand(sender: CommandSender, command: Command, label: String, args: Array<out String>): Boolean { override fun executeCommand(sender: CommandSender,
if (!sender.hasPermission(CustomAnvil.editConfigPermission)) { cmd: Command,
cmdstr: String,
args: Array<out String>): Boolean {
if (sender !is HumanEntity) return false
if (!allowed(sender)) {
sender.sendMessage(GuiGlobalActions.NO_EDIT_PERM) sender.sendMessage(GuiGlobalActions.NO_EDIT_PERM)
return false return false
} }
@ -25,10 +29,13 @@ class EditConfigExecutor : CommandExecutor {
return false return false
} }
if (sender !is HumanEntity) return false
MainConfigGui.getInstance().show(sender) MainConfigGui.getInstance().show(sender)
return true return true
} }
override fun allowed(sender: CommandSender): Boolean {
return sender.hasPermission(CustomAnvil.editConfigPermission)
}
} }

View file

@ -11,9 +11,13 @@ import xyz.alexcrea.cuanvil.dependency.DependencyManager
import xyz.alexcrea.cuanvil.gui.config.global.* import xyz.alexcrea.cuanvil.gui.config.global.*
import xyz.alexcrea.cuanvil.update.UpdateHandler import xyz.alexcrea.cuanvil.update.UpdateHandler
class ReloadExecutor : CommandExecutor { class ReloadExecutor : CASubCommand() {
override fun onCommand(sender: CommandSender, cmd: Command, cmdstr: String, args: Array<out String>): Boolean {
if (!sender.hasPermission(CustomAnvil.commandReloadPermission)) { override fun executeCommand(sender: CommandSender,
cmd: Command,
cmdstr: String,
args: Array<out String>): Boolean {
if (!allowed(sender)) {
sender.sendMessage("§cYou do not have permission to reload the config") sender.sendMessage("§cYou do not have permission to reload the config")
return false return false
} }
@ -31,6 +35,10 @@ class ReloadExecutor : CommandExecutor {
return commandSuccess return commandSuccess
} }
override fun allowed(sender: CommandSender): Boolean {
return sender.hasPermission(CustomAnvil.commandReloadPermission)
}
/** /**
* Execute the command, return true if success or false otherwise * Execute the command, return true if success or false otherwise
*/ */

View file

@ -10,6 +10,8 @@ authors: [ DelilahEve, alexcrea ]
libraries: [${libraries}] libraries: [${libraries}]
commands: commands:
customanvil:
description: Generic command for custom anvil
anvilconfigreload: anvilconfigreload:
description: Reload every config of this plugin description: Reload every config of this plugin
permission: ca.command.reload permission: ca.command.reload
@ -36,6 +38,9 @@ permissions:
ca.command.reload: ca.command.reload:
default: op default: op
description: Allow administrator to reload the plugin's configs 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: ca.config.edit:
default: op default: op
description: Allow administrator to edit the plugin's config in game description: Allow administrator to edit the plugin's config in game