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> <groupId>net.seanomik</groupId>
<artifactId>tamablefoxes</artifactId> <artifactId>tamablefoxes</artifactId>
<version>1.9.2-SNAPSHOT</version> <version>1.9.2.1-SNAPSHOT</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>Tamablefoxes</name> <name>Tamablefoxes</name>

View File

@ -6,22 +6,49 @@ import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import java.lang.invoke.MethodHandles; 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.Field;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
public final class FieldHelper { public final class FieldHelper {
public static void makeNonFinal(Field field) {
try { private static final VarHandle MODIFIERS;
if ((field.getModifiers() & Modifier.FINAL) == Modifier.FINAL) {
Field fieldMutable = field.getClass().getDeclaredField("modifiers"); static {
fieldMutable.setAccessible(true); String version = System.getProperty("java.version");
fieldMutable.set(field, fieldMutable.getInt(field) & ~Modifier.FINAL); if (!version.startsWith("1.8")) {
fieldMutable.setAccessible(false); 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");
fieldMutable.setAccessible(true);
fieldMutable.set(field, fieldMutable.getInt(field) & ~Modifier.FINAL);
fieldMutable.setAccessible(false);
}
} catch (NoSuchFieldException | IllegalAccessException e) {
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);
} }
} catch (NoSuchFieldException | IllegalAccessException e) {
Bukkit.getServer().getConsoleSender().sendMessage(Utils.getPrefix() + ChatColor.RED + LanguageConfig.getFailureReplace());
e.printStackTrace();
} }
} }