More robust version detection.

This commit is contained in:
Lixfel
2025-01-06 10:25:19 +01:00
parent da148c0e9f
commit 94a1ed3569
@@ -33,8 +33,15 @@ import java.util.Arrays;
@UtilityClass
public final class Reflection {
public static final int MAJOR_VERSION;
public static final int MINOR_VERSION;
static {
String[] version = Bukkit.getServer().getBukkitVersion().split("-")[0].split("\\.");
MAJOR_VERSION = Integer.parseInt(version[1]);
MINOR_VERSION = version.length > 2 ? Integer.parseInt(version[2]) : 0;
}
private static final String ORG_BUKKIT_CRAFTBUKKIT = Bukkit.getServer().getClass().getPackage().getName();
public static final int MAJOR_VERSION = Integer.parseInt(Bukkit.getServer().getClass().getPackage().getName().split("_", 3)[1]);
private static final String LEGACY_NET_MINECRAFT_SERVER = ORG_BUKKIT_CRAFTBUKKIT.replace("org.bukkit.craftbukkit", "net.minecraft.server");
public static Class<?> getClass(String name) {
@@ -53,12 +60,12 @@ public final class Reflection {
@AllArgsConstructor
public static class Field<T> {
private final java.lang.reflect.Field field;
private final java.lang.reflect.Field f;
@SuppressWarnings("unchecked")
public T get(Object target) {
try {
return (T) field.get(target);
return (T) f.get(target);
} catch (IllegalAccessException e) {
throw new IllegalArgumentException("Cannot read field", e);
}
@@ -66,7 +73,7 @@ public final class Reflection {
public void set(Object target, Object value) {
try {
field.set(target, value);
f.set(target, value);
} catch (IllegalAccessException e) {
throw new IllegalArgumentException("Cannot write field", e);
}
@@ -128,13 +135,13 @@ public final class Reflection {
@AllArgsConstructor
public static class Method {
private final java.lang.reflect.Method method;
private final java.lang.reflect.Method m;
public Object invoke(Object target, Object... arguments) {
try {
return method.invoke(target, arguments);
return m.invoke(target, arguments);
} catch (Exception e) {
throw new IllegalArgumentException("Cannot invoke method " + method, e);
throw new IllegalArgumentException("Cannot invoke method " + m, e);
}
}
}
@@ -166,13 +173,13 @@ public final class Reflection {
@AllArgsConstructor
public static class Constructor {
private final java.lang.reflect.Constructor<?> constructor;
private final java.lang.reflect.Constructor<?> c;
public Object invoke(Object... arguments) {
try {
return constructor.newInstance(arguments);
return c.newInstance(arguments);
} catch (Exception e) {
throw new IllegalArgumentException("Cannot invoke constructor " + constructor, e);
throw new IllegalArgumentException("Cannot invoke constructor " + c, e);
}
}
}