Fixed dynamic commands

This commit is contained in:
Auxilor
2023-02-05 16:32:48 +00:00
parent ef922f6d3f
commit 5480c70f8c
10 changed files with 38 additions and 39 deletions

View File

@@ -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.

View File

@@ -8,6 +8,9 @@ import org.bukkit.command.TabCompleter
class DelegatedBukkitCommand(
private val delegate: EcoPluginCommand
) : Command(delegate.name), TabCompleter, PluginIdentifiableCommand {
private var _aliases: List<String>? = null
private var _description: String? = null
override fun execute(sender: CommandSender, label: String, args: Array<out String>?): 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<String> = delegate.aliases
override fun getDescription() = _description ?: delegate.description ?: ""
override fun getAliases(): List<String> = _aliases ?: delegate.aliases
override fun setDescription(description: String): Command {
this._description = description
return this
}
override fun setAliases(aliases: List<String>): Command {
this._aliases = aliases
return this
}
}

View File

@@ -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()
}

View File

@@ -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}")
}

View File

@@ -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}")
}

View File

@@ -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}")
}

View File

@@ -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}")
}

View File

@@ -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}")
}

View File

@@ -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)
}

View File

@@ -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)
}