From 33decadee5d503eaadf14de24a92949c2274f7ae Mon Sep 17 00:00:00 2001 From: Travis CI Date: Sun, 23 Jun 2019 18:28:42 +0200 Subject: [PATCH] check if bungeecore is loaded --- src/de/steamwar/bungeecore/Persistent.java | 102 +++++++++++---------- 1 file changed, 52 insertions(+), 50 deletions(-) diff --git a/src/de/steamwar/bungeecore/Persistent.java b/src/de/steamwar/bungeecore/Persistent.java index 5204a2c..1f45561 100644 --- a/src/de/steamwar/bungeecore/Persistent.java +++ b/src/de/steamwar/bungeecore/Persistent.java @@ -32,62 +32,64 @@ public class Persistent extends Plugin { // Copied from https://www.spigotmc.org/resources/bungeepluginmanager-manage-your-bungee-plugin-at-runtime.63861/ PluginManager pluginManager = getProxy().getPluginManager(); Plugin bungeecore = pluginManager.getPlugin("BungeeCore"); - ClassLoader pluginClassLoader = bungeecore.getClass().getClassLoader(); + if(bungeecore != null) { + ClassLoader pluginClassLoader = bungeecore.getClass().getClassLoader(); - bungeecore.onDisable(); - for(Handler h : bungeecore.getLogger().getHandlers()){ - h.close(); - } + bungeecore.onDisable(); + for (Handler h : bungeecore.getLogger().getHandlers()) { + h.close(); + } - pluginManager.unregisterCommands(bungeecore); - pluginManager.unregisterListeners(bungeecore); - getProxy().getScheduler().cancel(bungeecore); - bungeecore.getExecutorService().shutdownNow(); - Thread.getAllStackTraces().keySet().stream() - .filter(thread -> (thread.getClass().getClassLoader() == pluginClassLoader)) - .forEach(thread -> { - try { - thread.interrupt(); - thread.join(2000); - if (thread.isAlive()) { - throw new IllegalStateException("Thread " + thread.getName() + " still running"); + pluginManager.unregisterCommands(bungeecore); + pluginManager.unregisterListeners(bungeecore); + getProxy().getScheduler().cancel(bungeecore); + bungeecore.getExecutorService().shutdownNow(); + Thread.getAllStackTraces().keySet().stream() + .filter(thread -> (thread.getClass().getClassLoader() == pluginClassLoader)) + .forEach(thread -> { + try { + thread.interrupt(); + thread.join(2000); + if (thread.isAlive()) { + throw new IllegalStateException("Thread " + thread.getName() + " still running"); + } + } catch (Exception t) { + getProxy().getLogger().log(Level.SEVERE, "Failed to stop thread that belong to plugin", t); } - } catch (Exception t) { - getProxy().getLogger().log(Level.SEVERE, "Failed to stop thread that belong to plugin", t); - } - }); - //remove commands that were registered by plugin not through normal means - try { - Map commandMap = ReflectionUtils.getFieldValue(pluginManager, "commandMap"); - if (commandMap != null) { - commandMap.entrySet().removeIf(entry -> entry.getValue().getClass().getClassLoader() == pluginClassLoader); - } - } catch (Exception t) { - getLogger().log(Level.SEVERE, "Failed to cleanup commandMap", t); - } - try { - Map pluginsMap = ReflectionUtils.getFieldValue(pluginManager, "plugins"); - Multimap commands = ReflectionUtils.getFieldValue(pluginManager, "commandsByPlugin"); - Multimap listeners = ReflectionUtils.getFieldValue(pluginManager, "listenersByPlugin"); - - if (pluginsMap != null && commands != null && listeners != null) { - pluginsMap.values().remove(bungeecore); - commands.removeAll(bungeecore); - listeners.removeAll(bungeecore); - } - } catch (Exception t) { - getLogger().log(Level.SEVERE, "Failed to cleanup bungee internal maps from plugin refs", t); - } - if (pluginClassLoader instanceof URLClassLoader) { + }); + //remove commands that were registered by plugin not through normal means try { - ((URLClassLoader) pluginClassLoader).close(); + Map commandMap = ReflectionUtils.getFieldValue(pluginManager, "commandMap"); + if (commandMap != null) { + commandMap.entrySet().removeIf(entry -> entry.getValue().getClass().getClassLoader() == pluginClassLoader); + } } catch (Exception t) { - getLogger().log(Level.SEVERE, "Failed to close classloader", t); + getLogger().log(Level.SEVERE, "Failed to cleanup commandMap", t); + } + try { + Map pluginsMap = ReflectionUtils.getFieldValue(pluginManager, "plugins"); + Multimap commands = ReflectionUtils.getFieldValue(pluginManager, "commandsByPlugin"); + Multimap listeners = ReflectionUtils.getFieldValue(pluginManager, "listenersByPlugin"); + + if (pluginsMap != null && commands != null && listeners != null) { + pluginsMap.values().remove(bungeecore); + commands.removeAll(bungeecore); + listeners.removeAll(bungeecore); + } + } catch (Exception t) { + getLogger().log(Level.SEVERE, "Failed to cleanup bungee internal maps from plugin refs", t); + } + if (pluginClassLoader instanceof URLClassLoader) { + try { + ((URLClassLoader) pluginClassLoader).close(); + } catch (Exception t) { + getLogger().log(Level.SEVERE, "Failed to close classloader", t); + } + } + Set allLoaders = ReflectionUtils.getStaticFieldValue(PluginClassloader.class, "allLoaders"); + if (allLoaders != null) { + allLoaders.remove(pluginClassLoader); } - } - Set allLoaders = ReflectionUtils.getStaticFieldValue(PluginClassloader.class, "allLoaders"); - if (allLoaders != null) { - allLoaders.remove(pluginClassLoader); } File pluginFile = new File(getProxy().getPluginsFolder(), "BungeeCore.jar");