Fix plugin for Java 1.16

This commit is contained in:
SeanOMik 2021-06-14 12:33:45 -04:00
parent 90f1c58670
commit 3c79bb028d
No known key found for this signature in database
GPG Key ID: CA09E5BE1F32728A
2 changed files with 39 additions and 12 deletions

View File

@ -6,7 +6,7 @@
<groupId>net.seanomik</groupId>
<artifactId>tamablefoxes</artifactId>
<version>1.9.2-SNAPSHOT</version>
<version>1.9.2.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Tamablefoxes</name>

View File

@ -6,12 +6,33 @@ import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import java.lang.invoke.MethodHandles;
//import java.lang.invoke.VarHandle;
import java.lang.invoke.MethodType;
import java.lang.invoke.VarHandle;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
public final class FieldHelper {
private static final VarHandle MODIFIERS;
static {
String version = System.getProperty("java.version");
if (!version.startsWith("1.8")) {
try {
MethodHandles.Lookup lookup = MethodHandles.privateLookupIn(Field.class, MethodHandles.lookup());
MODIFIERS = lookup.findVarHandle(Field.class, "modifiers", int.class);
} catch (IllegalAccessException | NoSuchFieldException ex) {
throw new RuntimeException(ex);
}
} else {
MODIFIERS = null;
}
}
public static void makeNonFinal(Field field) {
// Check if we're running a supported java version for this new method.
if (MODIFIERS == null) {
try {
if ((field.getModifiers() & Modifier.FINAL) == Modifier.FINAL) {
Field fieldMutable = field.getClass().getDeclaredField("modifiers");
@ -23,6 +44,12 @@ public final class FieldHelper {
Bukkit.getServer().getConsoleSender().sendMessage(Utils.getPrefix() + ChatColor.RED + LanguageConfig.getFailureReplace());
e.printStackTrace();
}
} else {
int mods = field.getModifiers();
if (Modifier.isFinal(mods)) {
MODIFIERS.set(field, mods & ~Modifier.FINAL);
}
}
}
public static void setField(Field field, Object obj, Object value) throws IllegalAccessException {