Unregister commands by plugin instance

This commit is contained in:
Frank van der Heijden 2020-06-09 11:57:56 +02:00
parent 228d0e5a82
commit cc6b7e4b2c
No known key found for this signature in database
GPG key ID: 26DA56488D314D11

View file

@ -3,14 +3,13 @@ package net.frankheijden.serverutils.managers;
import net.frankheijden.serverutils.ServerUtils; import net.frankheijden.serverutils.ServerUtils;
import net.frankheijden.serverutils.reflection.*; import net.frankheijden.serverutils.reflection.*;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.Command; import org.bukkit.command.*;
import org.bukkit.plugin.InvalidPluginException; import org.bukkit.plugin.InvalidPluginException;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.PluginClassLoader; import org.bukkit.plugin.java.PluginClassLoader;
import java.io.File; import java.io.File;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map; import java.util.Map;
import static net.frankheijden.serverutils.reflection.ReflectionUtils.set; import static net.frankheijden.serverutils.reflection.ReflectionUtils.set;
@ -108,25 +107,25 @@ public class PluginManager {
} }
public static void unregisterCommands(Plugin plugin) { public static void unregisterCommands(Plugin plugin) {
SimpleCommandMap commandMap = RCraftServer.getCommandMap();
Map<String, Command> map; Map<String, Command> map;
try { try {
map = RCommandMap.getKnownCommands(RCraftServer.getCommandMap()); map = RCommandMap.getKnownCommands(commandMap);
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
return; return;
} }
Map<String, Map<String, Object>> commands = plugin.getDescription().getCommands(); map.values().removeIf(c -> {
if (commands == null) return; // Older versions if (c instanceof PluginCommand) {
PluginCommand pc = (PluginCommand) c;
commands.forEach((cmd, data) -> { if (pc.getPlugin() == plugin) {
map.remove(cmd); pc.unregister(RCraftServer.getCommandMap());
return true;
@SuppressWarnings("unchecked")
List<String> aliases = (List<String>) data.get("aliases");
if (aliases != null) {
aliases.forEach(map::remove);
} }
return false;
}
return false;
}); });
} }
} }