Add method to get Plugin ClassLoader and we're 1.3

This commit is contained in:
Mark Vainomaa 2016-12-16 17:25:08 +02:00
parent dce8edddae
commit 5ccd68db27
2 changed files with 30 additions and 3 deletions

View File

@ -10,7 +10,7 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>bukkitutils</artifactId>
<version>1.2-SNAPSHOT</version>
<version>1.3-SNAPSHOT</version>
<repositories>
<repository>
@ -27,7 +27,7 @@
<dependency>
<groupId>eu.mikroskeem</groupId>
<artifactId>reflect</artifactId>
<version>1.0-SNAPSHOT</version>
<version>1.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.destroystokyo.paper</groupId>

View File

@ -2,9 +2,13 @@ package eu.mikroskeem.utils.bukkit;
import eu.mikroskeem.utils.reflect.Reflect;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.plugin.java.JavaPluginLoader;
import org.bukkit.plugin.java.PluginClassLoader;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;
public class ServerUtils {
/**
@ -35,4 +39,27 @@ public class ServerUtils {
}
return new double[]{-1, -1, -1};
}
/**
* Get plugin class loader. Useful when you want to add new classes on runtime
*
* @param plugin Plugin which will be used to get plugin ClassLoader
* @return Plugin ClassLoader
*/
public static ClassLoader getPluginClassLoader(JavaPlugin plugin){
JavaPluginLoader pl = (JavaPluginLoader)plugin.getPluginLoader();
Field loadersField = Reflect.getField(pl.getClass(), "loaders");
if(loadersField != null) {
@SuppressWarnings("unchecked")
List<PluginClassLoader> loaders = (List<PluginClassLoader>) Reflect.readField(loadersField, pl);
if(loaders != null) {
for(PluginClassLoader loader : loaders) {
if(loader.getPlugin().getName().equals(plugin.getName())){
return loader;
}
}
}
}
return null;
}
}