From 77225274ce1cae7bb9040af481b6cccb284a8481 Mon Sep 17 00:00:00 2001 From: jmp Date: Thu, 3 Dec 2020 20:01:03 -0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Improve=20Bukkit=20command=20regist?= =?UTF-8?q?ration=20logic?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When a new command is registered, if a command with that name already exists, Bukkit will not register the new command, ie the existing command gets priority. However, if the already existing command is an alias, it will replaced by the new command. These changes update cloud to be aware whether or not an existing command is an alias, and that if so, that Bukkit will allow us to have priority for this command. --- .../BukkitPluginRegistrationHandler.java | 30 +++++++++++++++++-- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/cloud-minecraft/cloud-bukkit/src/main/java/cloud/commandframework/bukkit/BukkitPluginRegistrationHandler.java b/cloud-minecraft/cloud-bukkit/src/main/java/cloud/commandframework/bukkit/BukkitPluginRegistrationHandler.java index f7999c98..1ce37860 100644 --- a/cloud-minecraft/cloud-bukkit/src/main/java/cloud/commandframework/bukkit/BukkitPluginRegistrationHandler.java +++ b/cloud-minecraft/cloud-bukkit/src/main/java/cloud/commandframework/bukkit/BukkitPluginRegistrationHandler.java @@ -99,12 +99,12 @@ public class BukkitPluginRegistrationHandler implements CommandRegistrationHa final String namespacedAlias = this.getNamespacedLabel(alias); this.recognizedAliases.add(namespacedAlias); - if (!this.bukkitCommands.containsKey(alias)) { + if (!this.bukkitCommandOrAliasExists(alias)) { this.recognizedAliases.add(alias); } if (this.bukkitCommandManager.getSplitAliases()) { - if (this.bukkitCommands.containsKey(alias)) { + if (this.bukkitCommandOrAliasExists(alias)) { this.registerExternal(namespacedAlias, command, bukkitCommand); } else { this.registerExternal(namespacedAlias, command, bukkitCommand); @@ -113,7 +113,7 @@ public class BukkitPluginRegistrationHandler implements CommandRegistrationHa } } - if (!this.bukkitCommands.containsKey(label)) { + if (!this.bukkitCommandExists(label)) { this.recognizedAliases.add(label); this.registerExternal(label, command, bukkitCommand); } @@ -151,4 +151,28 @@ public class BukkitPluginRegistrationHandler implements CommandRegistrationHa ) { } + /** + * Returns true if a command exists in the Bukkit command map, and is not an alias. + * + * @param commandLabel label to check + * @return whether the command exists and is not an alias + */ + private boolean bukkitCommandExists(final String commandLabel) { + final org.bukkit.command.Command existingCommand = this.bukkitCommands.get(commandLabel); + if (existingCommand == null) { + return false; + } + return existingCommand.getLabel().equals(commandLabel); + } + + /** + * Returns true if a command exists in the Bukkit command map, whether or not it is an alias. + * + * @param commandLabel label to check + * @return whether the command exists + */ + private boolean bukkitCommandOrAliasExists(final String commandLabel) { + return this.bukkitCommands.containsKey(commandLabel); + } + }