From 7b10885dc3dbd022d8a29c5346b269c112200e61 Mon Sep 17 00:00:00 2001 From: SeanOMik Date: Thu, 10 Sep 2020 11:29:47 -0500 Subject: [PATCH] Fix plugin for Java 8 while still having Java 14 work --- pom.xml | 11 ++--- .../tamablefoxes/versions/FieldHelper.java | 42 +++++++++++++++---- 2 files changed, 40 insertions(+), 13 deletions(-) diff --git a/pom.xml b/pom.xml index 9edc7c8..c7b6f92 100644 --- a/pom.xml +++ b/pom.xml @@ -6,13 +6,13 @@ net.seanomik tamablefoxes - 1.7.5-SNAPSHOT + 1.7.6-SNAPSHOT jar Tamablefoxes - 1.14 + 8 UTF-8 @@ -24,8 +24,8 @@ maven-compiler-plugin 3.7.0 - 9 - 9 + ${java.version} + ${java.version} @@ -41,9 +41,10 @@ - D:\Code\java\spigotPlugins\_TEST_SERVER_PAPER_1.16.1_\plugins\TamableFoxes_v${project.version}.jar + + D:\Code\java\spigotPlugins\_TEST_SERVER_PAPER_1.16.2_\plugins\TamableFoxes_v${project.version}.jar false diff --git a/src/main/java/net/seanomik/tamablefoxes/versions/FieldHelper.java b/src/main/java/net/seanomik/tamablefoxes/versions/FieldHelper.java index 5f907cc..fdd80e4 100644 --- a/src/main/java/net/seanomik/tamablefoxes/versions/FieldHelper.java +++ b/src/main/java/net/seanomik/tamablefoxes/versions/FieldHelper.java @@ -1,6 +1,12 @@ package net.seanomik.tamablefoxes.versions; +import net.seanomik.tamablefoxes.Utils; +import net.seanomik.tamablefoxes.io.LanguageConfig; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; + import java.lang.invoke.MethodHandles; +import java.lang.invoke.MethodType; import java.lang.invoke.VarHandle; import java.lang.reflect.Field; import java.lang.reflect.Modifier; @@ -10,18 +16,38 @@ public final class FieldHelper { private static final VarHandle MODIFIERS; static { - 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); + 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) { - int mods = field.getModifiers(); - if (Modifier.isFinal(mods)) { - MODIFIERS.set(field, mods & ~Modifier.FINAL); + // 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); + } } }