From dd467f1949a6dfc2c69626bbf4e5c681e7840da8 Mon Sep 17 00:00:00 2001 From: abhiram Date: Thu, 13 May 2021 01:05:03 +0530 Subject: [PATCH] Added an api method to unload Plugins diff --git a/api/src/main/java/net/md_5/bungee/api/plugin/PluginManager.java b/api/src/main/java/net/md_5/bungee/api/plugin/PluginManager.java index 90031156..3ece27c8 100644 --- a/api/src/main/java/net/md_5/bungee/api/plugin/PluginManager.java +++ b/api/src/main/java/net/md_5/bungee/api/plugin/PluginManager.java @@ -25,6 +25,7 @@ import java.util.Set; import java.util.Stack; import java.util.jar.JarEntry; import java.util.jar.JarFile; +import java.util.logging.Handler; import java.util.logging.Level; import io.github.waterfallmc.waterfall.event.ProxyExceptionEvent; // Waterfall import io.github.waterfallmc.waterfall.exception.ProxyCommandException; // Waterfall @@ -63,6 +64,7 @@ public final class PluginManager private Map toLoad = new HashMap<>(); private final Multimap commandsByPlugin = ArrayListMultimap.create(); private final Multimap listenersByPlugin = ArrayListMultimap.create(); + private final HashMap pluginloaders = new HashMap<>(); @SuppressWarnings("unchecked") public PluginManager(ProxyServer proxy) @@ -291,6 +293,45 @@ public final class PluginManager return plugins.get( name ); } + + /** + * This will Unload the given plugin + * + * @param plugin to unload + */ + // FlameCord start - Adds a method to unload plugin from proxy + public void unloadPlugin(Plugin plugin) + { + plugin.onDisable(); + this.unregisterListeners(plugin); + this.unregisterCommands(plugin); + this.proxy.getScheduler().cancel(plugin); + + for ( Handler handler : plugin.getLogger().getHandlers()) + { + handler.close(); + } + + try + { + pluginloaders.get(plugin.getDescription().getName()).close(); + }catch (Exception exception) + { + exception.printStackTrace(); + } + + if(this.plugins.containsKey(plugin.getDescription().getName())) + { + this.plugins.remove(plugin.getDescription().getName()); + } + + if(this.pluginloaders.containsKey(plugin.getDescription().getName())) + { + this.pluginloaders.remove(plugin.getDescription().getName()); + } + } + // FlameCord end + public void loadPlugins() { Map pluginStatuses = new HashMap<>(); @@ -395,6 +436,9 @@ public final class PluginManager Plugin clazz = (Plugin) main.getDeclaredConstructor().newInstance(); plugins.put( plugin.getName(), clazz ); + // FlameCord start - cache url classloaders to unload plugins + pluginloaders.put(plugin.getName(),loader); + // FlameCord end clazz.onLoad(); ProxyServer.getInstance().getLogger().log( Level.INFO, "Loaded plugin {0} version {1} by {2}", new Object[] { -- 2.26.2.windows.1