From 5480c70f8c5f8c271ab062fe351a4f242d512c89 Mon Sep 17 00:00:00 2001 From: Auxilor Date: Sun, 5 Feb 2023 16:32:48 +0000 Subject: [PATCH] Fixed dynamic commands --- .../main/java/com/willfp/eco/core/Eco.java | 10 +--------- .../command/DelegatedBukkitCommand.kt | 17 ++++++++++++++-- .../eco/internal/command/EcoPluginCommand.kt | 20 ++++++++----------- .../spigot/proxy/v1_17_R1/BukkitCommands.kt | 4 ++-- .../spigot/proxy/v1_18_R1/BukkitCommands.kt | 4 ++-- .../spigot/proxy/v1_18_R2/BukkitCommands.kt | 4 ++-- .../spigot/proxy/v1_19_R1/BukkitCommands.kt | 4 ++-- .../spigot/proxy/v1_19_R2/BukkitCommands.kt | 4 ++-- .../com/willfp/eco/internal/spigot/EcoImpl.kt | 6 ++---- .../spigot/proxy/BukkitCommandsProxy.kt | 4 ++-- 10 files changed, 38 insertions(+), 39 deletions(-) diff --git a/eco-api/src/main/java/com/willfp/eco/core/Eco.java b/eco-api/src/main/java/com/willfp/eco/core/Eco.java index 464de76f..8acea3f1 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/Eco.java +++ b/eco-api/src/main/java/com/willfp/eco/core/Eco.java @@ -32,7 +32,6 @@ import net.kyori.adventure.platform.bukkit.BukkitAudiences; import org.apache.commons.lang.Validate; import org.bukkit.Location; import org.bukkit.NamespacedKey; -import org.bukkit.command.CommandMap; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.entity.Entity; import org.bukkit.entity.Mob; @@ -547,19 +546,12 @@ public interface Eco { */ void syncCommands(); - /** - * Get the command map. - * - * @return The command map. - */ - @NotNull CommandMap getCommandMap(); - /** * Unregister a command. * * @param command The command. */ - void unregisterCommand(@NotNull final PluginCommand command); + void unregisterCommand(@NotNull final PluginCommandBase command); /** * Get the instance of eco; the bridge between the api frontend and the implementation backend. diff --git a/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/command/DelegatedBukkitCommand.kt b/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/command/DelegatedBukkitCommand.kt index 9cf8f918..8a2ca25d 100644 --- a/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/command/DelegatedBukkitCommand.kt +++ b/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/command/DelegatedBukkitCommand.kt @@ -8,6 +8,9 @@ import org.bukkit.command.TabCompleter class DelegatedBukkitCommand( private val delegate: EcoPluginCommand ) : Command(delegate.name), TabCompleter, PluginIdentifiableCommand { + private var _aliases: List? = null + private var _description: String? = null + override fun execute(sender: CommandSender, label: String, args: Array?): Boolean { return delegate.onCommand(sender, this, label, args) } @@ -23,6 +26,16 @@ class DelegatedBukkitCommand( override fun getPlugin() = delegate.plugin override fun getPermission() = delegate.permission - override fun getDescription() = delegate.description ?: "" - override fun getAliases(): List = delegate.aliases + override fun getDescription() = _description ?: delegate.description ?: "" + override fun getAliases(): List = _aliases ?: delegate.aliases + + override fun setDescription(description: String): Command { + this._description = description + return this + } + + override fun setAliases(aliases: List): Command { + this._aliases = aliases + return this + } } diff --git a/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/command/EcoPluginCommand.kt b/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/command/EcoPluginCommand.kt index f937242c..bb0e2af1 100644 --- a/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/command/EcoPluginCommand.kt +++ b/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/command/EcoPluginCommand.kt @@ -17,29 +17,25 @@ class EcoPluginCommand( override fun register() { val command = Bukkit.getPluginCommand(name) + val knownDescription = command?.description ?: description ?: "" + val knownAliases = command?.aliases ?: aliases + if (command == null) { unregister() - commandMap.register(plugin.name.lowercase(), DelegatedBukkitCommand(this)) } else { command.setExecutor(this) - - - description?.let { - command.setDescription(it) - } - - if (aliases.isNotEmpty()) { - command.aliases = aliases - } + command.tabCompleter = this + command.description = knownDescription + command.aliases = knownAliases } Eco.get().syncCommands() } override fun unregister() { - val found = commandMap.getCommand(name) - found?.unregister(commandMap) + commandMap.getCommand(name)?.unregister(commandMap) + Eco.get().unregisterCommand(this) Eco.get().syncCommands() } diff --git a/eco-core/core-nms/v1_17_R1/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_17_R1/BukkitCommands.kt b/eco-core/core-nms/v1_17_R1/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_17_R1/BukkitCommands.kt index f0a5d404..1b75bacf 100644 --- a/eco-core/core-nms/v1_17_R1/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_17_R1/BukkitCommands.kt +++ b/eco-core/core-nms/v1_17_R1/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_17_R1/BukkitCommands.kt @@ -1,6 +1,6 @@ package com.willfp.eco.internal.spigot.proxy.v1_17_R1 -import com.willfp.eco.core.command.impl.PluginCommand +import com.willfp.eco.core.command.PluginCommandBase import com.willfp.eco.internal.spigot.proxy.BukkitCommandsProxy import org.bukkit.Bukkit import org.bukkit.command.Command @@ -28,7 +28,7 @@ class BukkitCommands : BukkitCommandsProxy { (Bukkit.getServer() as CraftServer).syncCommands() } - override fun unregisterCommand(command: PluginCommand) { + override fun unregisterCommand(command: PluginCommandBase) { knownCommands.remove(command.name) knownCommands.remove("${command.plugin.name.lowercase()}:${command.name}") } diff --git a/eco-core/core-nms/v1_18_R1/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_18_R1/BukkitCommands.kt b/eco-core/core-nms/v1_18_R1/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_18_R1/BukkitCommands.kt index b55c9abb..a2d6227f 100644 --- a/eco-core/core-nms/v1_18_R1/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_18_R1/BukkitCommands.kt +++ b/eco-core/core-nms/v1_18_R1/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_18_R1/BukkitCommands.kt @@ -1,6 +1,6 @@ package com.willfp.eco.internal.spigot.proxy.v1_18_R1 -import com.willfp.eco.core.command.impl.PluginCommand +import com.willfp.eco.core.command.PluginCommandBase import com.willfp.eco.internal.spigot.proxy.BukkitCommandsProxy import org.bukkit.Bukkit import org.bukkit.command.Command @@ -28,7 +28,7 @@ class BukkitCommands : BukkitCommandsProxy { (Bukkit.getServer() as CraftServer).syncCommands() } - override fun unregisterCommand(command: PluginCommand) { + override fun unregisterCommand(command: PluginCommandBase) { knownCommands.remove(command.name) knownCommands.remove("${command.plugin.name.lowercase()}:${command.name}") } diff --git a/eco-core/core-nms/v1_18_R2/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_18_R2/BukkitCommands.kt b/eco-core/core-nms/v1_18_R2/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_18_R2/BukkitCommands.kt index c7a30ef3..cab7e143 100644 --- a/eco-core/core-nms/v1_18_R2/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_18_R2/BukkitCommands.kt +++ b/eco-core/core-nms/v1_18_R2/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_18_R2/BukkitCommands.kt @@ -1,6 +1,6 @@ package com.willfp.eco.internal.spigot.proxy.v1_18_R2 -import com.willfp.eco.core.command.impl.PluginCommand +import com.willfp.eco.core.command.PluginCommandBase import com.willfp.eco.internal.spigot.proxy.BukkitCommandsProxy import org.bukkit.Bukkit import org.bukkit.command.Command @@ -28,7 +28,7 @@ class BukkitCommands : BukkitCommandsProxy { (Bukkit.getServer() as CraftServer).syncCommands() } - override fun unregisterCommand(command: PluginCommand) { + override fun unregisterCommand(command: PluginCommandBase) { knownCommands.remove(command.name) knownCommands.remove("${command.plugin.name.lowercase()}:${command.name}") } diff --git a/eco-core/core-nms/v1_19_R1/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_19_R1/BukkitCommands.kt b/eco-core/core-nms/v1_19_R1/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_19_R1/BukkitCommands.kt index 9687ab4f..66b2f517 100644 --- a/eco-core/core-nms/v1_19_R1/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_19_R1/BukkitCommands.kt +++ b/eco-core/core-nms/v1_19_R1/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_19_R1/BukkitCommands.kt @@ -1,6 +1,6 @@ package com.willfp.eco.internal.spigot.proxy.v1_19_R1 -import com.willfp.eco.core.command.impl.PluginCommand +import com.willfp.eco.core.command.PluginCommandBase import com.willfp.eco.internal.spigot.proxy.BukkitCommandsProxy import org.bukkit.Bukkit import org.bukkit.command.Command @@ -28,7 +28,7 @@ class BukkitCommands : BukkitCommandsProxy { (Bukkit.getServer() as CraftServer).syncCommands() } - override fun unregisterCommand(command: PluginCommand) { + override fun unregisterCommand(command: PluginCommandBase) { knownCommands.remove(command.name) knownCommands.remove("${command.plugin.name.lowercase()}:${command.name}") } diff --git a/eco-core/core-nms/v1_19_R2/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_19_R2/BukkitCommands.kt b/eco-core/core-nms/v1_19_R2/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_19_R2/BukkitCommands.kt index d7eb6631..5ac19d92 100644 --- a/eco-core/core-nms/v1_19_R2/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_19_R2/BukkitCommands.kt +++ b/eco-core/core-nms/v1_19_R2/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_19_R2/BukkitCommands.kt @@ -1,6 +1,6 @@ package com.willfp.eco.internal.spigot.proxy.v1_19_R2 -import com.willfp.eco.core.command.impl.PluginCommand +import com.willfp.eco.core.command.PluginCommandBase import com.willfp.eco.internal.spigot.proxy.BukkitCommandsProxy import org.bukkit.Bukkit import org.bukkit.command.Command @@ -28,7 +28,7 @@ class BukkitCommands : BukkitCommandsProxy { (Bukkit.getServer() as CraftServer).syncCommands() } - override fun unregisterCommand(command: PluginCommand) { + override fun unregisterCommand(command: PluginCommandBase) { knownCommands.remove(command.name) knownCommands.remove("${command.plugin.name.lowercase()}:${command.name}") } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/EcoImpl.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/EcoImpl.kt index 122e8b0b..cedc3818 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/EcoImpl.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/EcoImpl.kt @@ -5,6 +5,7 @@ import com.willfp.eco.core.EcoPlugin import com.willfp.eco.core.PluginLike import com.willfp.eco.core.PluginProps import com.willfp.eco.core.command.CommandBase +import com.willfp.eco.core.command.PluginCommandBase import com.willfp.eco.core.command.impl.PluginCommand import com.willfp.eco.core.config.ConfigType import com.willfp.eco.core.config.interfaces.Config @@ -317,9 +318,6 @@ class EcoImpl : EcoSpigotPlugin(), Eco { override fun syncCommands() = this.getProxy(BukkitCommandsProxy::class.java).syncCommands() - override fun getCommandMap(): CommandMap = - this.getProxy(BukkitCommandsProxy::class.java).getCommandMap() - - override fun unregisterCommand(command: PluginCommand) = + override fun unregisterCommand(command: PluginCommandBase) = this.getProxy(BukkitCommandsProxy::class.java).unregisterCommand(command) } diff --git a/eco-core/core-proxy/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/BukkitCommandsProxy.kt b/eco-core/core-proxy/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/BukkitCommandsProxy.kt index 96fa5b1a..c018a6b3 100644 --- a/eco-core/core-proxy/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/BukkitCommandsProxy.kt +++ b/eco-core/core-proxy/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/BukkitCommandsProxy.kt @@ -1,6 +1,6 @@ package com.willfp.eco.internal.spigot.proxy -import com.willfp.eco.core.command.impl.PluginCommand +import com.willfp.eco.core.command.PluginCommandBase import org.bukkit.command.CommandMap interface BukkitCommandsProxy { @@ -8,5 +8,5 @@ interface BukkitCommandsProxy { fun syncCommands() - fun unregisterCommand(command: PluginCommand) + fun unregisterCommand(command: PluginCommandBase) }