Unregister command on disable

This commit is contained in:
Eric 2020-02-29 17:23:21 +01:00
parent bcf3f5c9cb
commit 146f1b0dbf
2 changed files with 51 additions and 10 deletions

View File

@ -229,6 +229,10 @@ public class ShopChest extends JavaPlugin {
return; return;
} }
if (getShopCommand() != null) {
getShopCommand().unregister();
}
ClickType.clear(); ClickType.clear();
if (updater != null) { if (updater != null) {

View File

@ -21,17 +21,20 @@ import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.Map;
public class ShopCommand { public class ShopCommand {
private static boolean commandCreated = false; private static boolean commandCreated = false;
private ShopChest plugin; private final ShopChest plugin;
private String name; private final String name;
private PluginCommand pluginCommand; private final String fallbackPrefix;
private ShopCommandExecutor executor; private final PluginCommand pluginCommand;
private final ShopCommandExecutor executor;
private List<ShopSubCommand> subCommands = new ArrayList<>(); private final List<ShopSubCommand> subCommands = new ArrayList<>();
public ShopCommand(final ShopChest plugin) { public ShopCommand(final ShopChest plugin) {
if (commandCreated) { if (commandCreated) {
@ -41,7 +44,8 @@ public class ShopCommand {
} }
this.plugin = plugin; 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.pluginCommand = createPluginCommand();
this.executor = new ShopCommandExecutor(plugin); this.executor = new ShopCommandExecutor(plugin);
@ -198,13 +202,13 @@ public class ShopCommand {
plugin.debug("Registering command " + name); plugin.debug("Registering command " + name);
try { try {
Field f = Bukkit.getPluginManager().getClass().getDeclaredField("commandMap"); Field fCommandMap = Bukkit.getPluginManager().getClass().getDeclaredField("commandMap");
f.setAccessible(true); fCommandMap.setAccessible(true);
Object commandMapObject = f.get(Bukkit.getPluginManager()); Object commandMapObject = fCommandMap.get(Bukkit.getPluginManager());
if (commandMapObject instanceof CommandMap) { if (commandMapObject instanceof CommandMap) {
CommandMap commandMap = (CommandMap) commandMapObject; CommandMap commandMap = (CommandMap) commandMapObject;
commandMap.register(plugin.getName(), pluginCommand); commandMap.register(fallbackPrefix, pluginCommand);
} }
} catch (NoSuchFieldException | IllegalAccessException e) { } catch (NoSuchFieldException | IllegalAccessException e) {
plugin.getLogger().severe("Failed to register command"); 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 * Sends the basic help message
* *