check if bungeecore is loaded
Dieser Commit ist enthalten in:
Ursprung
b8d343f3f1
Commit
33decadee5
@ -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<String, Command> 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<String, Plugin> pluginsMap = ReflectionUtils.getFieldValue(pluginManager, "plugins");
|
||||
Multimap<Plugin, Command> commands = ReflectionUtils.getFieldValue(pluginManager, "commandsByPlugin");
|
||||
Multimap<Plugin, Listener> 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<String, Command> 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<String, Plugin> pluginsMap = ReflectionUtils.getFieldValue(pluginManager, "plugins");
|
||||
Multimap<Plugin, Command> commands = ReflectionUtils.getFieldValue(pluginManager, "commandsByPlugin");
|
||||
Multimap<Plugin, Listener> 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<PluginClassloader> allLoaders = ReflectionUtils.getStaticFieldValue(PluginClassloader.class, "allLoaders");
|
||||
if (allLoaders != null) {
|
||||
allLoaders.remove(pluginClassLoader);
|
||||
}
|
||||
}
|
||||
Set<PluginClassloader> allLoaders = ReflectionUtils.getStaticFieldValue(PluginClassloader.class, "allLoaders");
|
||||
if (allLoaders != null) {
|
||||
allLoaders.remove(pluginClassLoader);
|
||||
}
|
||||
|
||||
File pluginFile = new File(getProxy().getPluginsFolder(), "BungeeCore.jar");
|
||||
|
In neuem Issue referenzieren
Einen Benutzer sperren