Fix Plugin class loader getter
Older Minecraft versions have different PluginClassLoader storing methods Also add method to get NMS version
This commit is contained in:
parent
fc892a6384
commit
b257272630
@ -8,7 +8,9 @@ import org.bukkit.plugin.java.PluginClassLoader;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class ServerUtils {
|
||||
/**
|
||||
@ -21,11 +23,8 @@ public class ServerUtils {
|
||||
if(Bukkit.getVersion().contains("Paper")) {
|
||||
return Bukkit.getTPS();
|
||||
} else {
|
||||
String bukkitPackageName = Bukkit.getServer().getClass().getPackage().getName();
|
||||
String nmsVer = bukkitPackageName.substring(bukkitPackageName.lastIndexOf(".") + 1);
|
||||
|
||||
Class<?> MinecraftServerClass = Reflect.getClass(
|
||||
String.format("net.minecraft.server.%s.MinecraftServer", nmsVer));
|
||||
String.format("net.minecraft.server.%s.MinecraftServer", getNmsVersion()));
|
||||
if(MinecraftServerClass != null){
|
||||
Method getServer = Reflect.getMethod(MinecraftServerClass, "getServer");
|
||||
Field recentTPS = Reflect.getField(MinecraftServerClass, "recentTps");
|
||||
@ -46,12 +45,25 @@ public class ServerUtils {
|
||||
* @param plugin Plugin which will be used to get plugin ClassLoader
|
||||
* @return Plugin ClassLoader
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
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);
|
||||
List<PluginClassLoader> loaders = null;
|
||||
switch (getNmsVersion()){
|
||||
case "v1_8_R3":
|
||||
case "v1_9_R1": // TODO: Check?
|
||||
case "v1_9_R2":
|
||||
loaders = new ArrayList<>(((Map<String, PluginClassLoader>)
|
||||
Reflect.readField(loadersField, pl)).values());
|
||||
break;
|
||||
case "v1_10_R1":
|
||||
case "v1_11_R1":
|
||||
loaders = (List<PluginClassLoader>) Reflect.readField(loadersField, pl);
|
||||
break;
|
||||
}
|
||||
|
||||
if(loaders != null) {
|
||||
for(PluginClassLoader loader : loaders) {
|
||||
if(loader.getPlugin().getName().equals(plugin.getName())){
|
||||
@ -62,4 +74,17 @@ public class ServerUtils {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get NMS version of current running server
|
||||
* @return NMS version or 0 length string
|
||||
*/
|
||||
public static String getNmsVersion(){
|
||||
try {
|
||||
String bukkitPackageName = Bukkit.getServer().getClass().getPackage().getName();
|
||||
return bukkitPackageName.substring(bukkitPackageName.lastIndexOf(".") + 1);
|
||||
} catch (NullPointerException e){
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user