forked from SteamWar/SteamWar
More robust version detection.
This commit is contained in:
@@ -33,8 +33,15 @@ import java.util.Arrays;
|
|||||||
@UtilityClass
|
@UtilityClass
|
||||||
public final class Reflection {
|
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();
|
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");
|
private static final String LEGACY_NET_MINECRAFT_SERVER = ORG_BUKKIT_CRAFTBUKKIT.replace("org.bukkit.craftbukkit", "net.minecraft.server");
|
||||||
|
|
||||||
public static Class<?> getClass(String name) {
|
public static Class<?> getClass(String name) {
|
||||||
@@ -53,12 +60,12 @@ public final class Reflection {
|
|||||||
|
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public static class Field<T> {
|
public static class Field<T> {
|
||||||
private final java.lang.reflect.Field field;
|
private final java.lang.reflect.Field f;
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public T get(Object target) {
|
public T get(Object target) {
|
||||||
try {
|
try {
|
||||||
return (T) field.get(target);
|
return (T) f.get(target);
|
||||||
} catch (IllegalAccessException e) {
|
} catch (IllegalAccessException e) {
|
||||||
throw new IllegalArgumentException("Cannot read field", e);
|
throw new IllegalArgumentException("Cannot read field", e);
|
||||||
}
|
}
|
||||||
@@ -66,7 +73,7 @@ public final class Reflection {
|
|||||||
|
|
||||||
public void set(Object target, Object value) {
|
public void set(Object target, Object value) {
|
||||||
try {
|
try {
|
||||||
field.set(target, value);
|
f.set(target, value);
|
||||||
} catch (IllegalAccessException e) {
|
} catch (IllegalAccessException e) {
|
||||||
throw new IllegalArgumentException("Cannot write field", e);
|
throw new IllegalArgumentException("Cannot write field", e);
|
||||||
}
|
}
|
||||||
@@ -128,13 +135,13 @@ public final class Reflection {
|
|||||||
|
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public static class Method {
|
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) {
|
public Object invoke(Object target, Object... arguments) {
|
||||||
try {
|
try {
|
||||||
return method.invoke(target, arguments);
|
return m.invoke(target, arguments);
|
||||||
} catch (Exception e) {
|
} 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
|
@AllArgsConstructor
|
||||||
public static class Constructor {
|
public static class Constructor {
|
||||||
private final java.lang.reflect.Constructor<?> constructor;
|
private final java.lang.reflect.Constructor<?> c;
|
||||||
|
|
||||||
public Object invoke(Object... arguments) {
|
public Object invoke(Object... arguments) {
|
||||||
try {
|
try {
|
||||||
return constructor.newInstance(arguments);
|
return c.newInstance(arguments);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new IllegalArgumentException("Cannot invoke constructor " + constructor, e);
|
throw new IllegalArgumentException("Cannot invoke constructor " + c, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user