Skip to content

Commit

Permalink
Rework ReflectionUtil versioning
Browse files Browse the repository at this point in the history
1.20.6 paper no longer remaps CraftBukkit classes which caused the plugin to fail on load. So we use the paper method for getting the Minecraft server's version if it exists. We also do a try catch for getting CraftBukkit classes so we can get support relocated and non-relocated classes.
  • Loading branch information
virustotalop committed Jun 5, 2024
1 parent acf7359 commit 7509994
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/main/java/dev/magicmq/pyspigot/util/ReflectionUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
package dev.magicmq.pyspigot.util;

import org.bukkit.Bukkit;
import org.bukkit.Server;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
Expand All @@ -29,7 +31,16 @@ public final class ReflectionUtils {
private static final String MC_VERSION;

static {
MC_VERSION = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
Server server = Bukkit.getServer();
Class<?> serverClass = server.getClass();
Method getMinecraftVersion = getMethod(serverClass, "getMinecraftVersion");
try {
MC_VERSION = getMinecraftVersion != null ?
(String) getMinecraftVersion.invoke(server) :
serverClass.getPackage().getName().split("\\.")[3];
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}

private ReflectionUtils() {}
Expand All @@ -43,11 +54,19 @@ public static Class<?> getNMSClass(String packageName, String className) throws
}

public static Class<?> getCraftBukkitClass(String className) throws ClassNotFoundException {
return Class.forName("org.bukkit.craftbukkit." + MC_VERSION + "." + className);
try {
return Class.forName("org.bukkit.craftbukkit." + className);
} catch (ClassNotFoundException ignored) {
return Class.forName("org.bukkit.craftbukkit." + MC_VERSION + "." + className);
}
}

public static Class<?> getCraftBukkitClass(String packageName, String className) throws ClassNotFoundException {
return Class.forName("org.bukkit.craftbukkit." + MC_VERSION + "." + packageName + "." + className);
try {
return Class.forName("org.bukkit.craftbukkit." + packageName + "." + className);
} catch (ClassNotFoundException ignored) {
return Class.forName("org.bukkit.craftbukkit." + MC_VERSION + "." + packageName + "." + className);
}
}

public static Method getMethod(Class<?> clazz, String methodName) {
Expand Down

0 comments on commit 7509994

Please sign in to comment.