From 146f1b0dbf0dbca9b5ebd9315a0d15145cab191c Mon Sep 17 00:00:00 2001 From: Eric Date: Sat, 29 Feb 2020 17:23:21 +0100 Subject: [PATCH] Unregister command on disable --- .../java/de/epiceric/shopchest/ShopChest.java | 4 ++ .../shopchest/command/ShopCommand.java | 57 +++++++++++++++---- 2 files changed, 51 insertions(+), 10 deletions(-) diff --git a/src/main/java/de/epiceric/shopchest/ShopChest.java b/src/main/java/de/epiceric/shopchest/ShopChest.java index fc93a02..9aa1801 100644 --- a/src/main/java/de/epiceric/shopchest/ShopChest.java +++ b/src/main/java/de/epiceric/shopchest/ShopChest.java @@ -229,6 +229,10 @@ public class ShopChest extends JavaPlugin { return; } + if (getShopCommand() != null) { + getShopCommand().unregister(); + } + ClickType.clear(); if (updater != null) { diff --git a/src/main/java/de/epiceric/shopchest/command/ShopCommand.java b/src/main/java/de/epiceric/shopchest/command/ShopCommand.java index 619c112..1fa5b10 100644 --- a/src/main/java/de/epiceric/shopchest/command/ShopCommand.java +++ b/src/main/java/de/epiceric/shopchest/command/ShopCommand.java @@ -21,17 +21,20 @@ import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.List; +import java.util.Locale; +import java.util.Map; public class ShopCommand { private static boolean commandCreated = false; - private ShopChest plugin; - private String name; - private PluginCommand pluginCommand; - private ShopCommandExecutor executor; + private final ShopChest plugin; + private final String name; + private final String fallbackPrefix; + private final PluginCommand pluginCommand; + private final ShopCommandExecutor executor; - private List subCommands = new ArrayList<>(); + private final List subCommands = new ArrayList<>(); public ShopCommand(final ShopChest plugin) { if (commandCreated) { @@ -41,7 +44,8 @@ public class ShopCommand { } this.plugin = plugin; - this.name = Config.mainCommandName; + this.name = Config.mainCommandName.toLowerCase(Locale.ENGLISH).trim(); + this.fallbackPrefix = plugin.getName().toLowerCase(Locale.ENGLISH).trim(); this.pluginCommand = createPluginCommand(); this.executor = new ShopCommandExecutor(plugin); @@ -198,13 +202,13 @@ public class ShopCommand { plugin.debug("Registering command " + name); try { - Field f = Bukkit.getPluginManager().getClass().getDeclaredField("commandMap"); - f.setAccessible(true); + Field fCommandMap = Bukkit.getPluginManager().getClass().getDeclaredField("commandMap"); + fCommandMap.setAccessible(true); - Object commandMapObject = f.get(Bukkit.getPluginManager()); + Object commandMapObject = fCommandMap.get(Bukkit.getPluginManager()); if (commandMapObject instanceof CommandMap) { CommandMap commandMap = (CommandMap) commandMapObject; - commandMap.register(plugin.getName(), pluginCommand); + commandMap.register(fallbackPrefix, pluginCommand); } } catch (NoSuchFieldException | IllegalAccessException e) { plugin.getLogger().severe("Failed to register command"); @@ -213,6 +217,39 @@ public class ShopCommand { } } + public void unregister() { + if (pluginCommand == null) return; + + plugin.debug("Unregistering command " + name); + + try { + Field fCommandMap = Bukkit.getPluginManager().getClass().getDeclaredField("commandMap"); + fCommandMap.setAccessible(true); + + Object commandMapObject = fCommandMap.get(Bukkit.getPluginManager()); + if (commandMapObject instanceof CommandMap) { + CommandMap commandMap = (CommandMap) commandMapObject; + pluginCommand.unregister(commandMap); + + Field fKnownCommands = commandMap.getClass().getDeclaredField("knownCommands"); + fKnownCommands.setAccessible(true); + + Object knownCommandsObject = fKnownCommands.get(commandMap); + if (knownCommandsObject instanceof Map) { + Map knownCommands = (Map) knownCommandsObject; + knownCommands.remove(fallbackPrefix + ":" + name); + if (pluginCommand.equals(knownCommands.get(name))) { + knownCommands.remove(name); + } + } + } + } catch (NoSuchFieldException | IllegalAccessException e) { + plugin.getLogger().severe("Failed to unregister command"); + plugin.debug("Failed to unregister plugin command"); + plugin.debug(e); + } + } + /** * Sends the basic help message *