Fix plugin for Java 8 while still having Java 14 work

This commit is contained in:
SeanOMik 2020-09-10 11:29:47 -05:00
parent 7a0f783393
commit 7b10885dc3
No known key found for this signature in database
GPG Key ID: FA4D55AC05268A88
2 changed files with 40 additions and 13 deletions

11
pom.xml
View File

@ -6,13 +6,13 @@
<groupId>net.seanomik</groupId>
<artifactId>tamablefoxes</artifactId>
<version>1.7.5-SNAPSHOT</version>
<version>1.7.6-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Tamablefoxes</name>
<properties>
<java.version>1.14</java.version>
<java.version>8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
@ -24,8 +24,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>9</source>
<target>9</target>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
@ -41,9 +41,10 @@
<configuration>
<!--<outputFile>D:\Code\java\spigotPlugins\_TEST_SERVER_1.14.4_\plugins\TamableFoxes_v${project.version}.jar</outputFile>-->
<!--<outputFile>D:\Code\java\spigotPlugins\_TEST_SERVER_1.15.2_\plugins\TamableFoxes_v${project.version}.jar</outputFile>-->
<outputFile>D:\Code\java\spigotPlugins\_TEST_SERVER_PAPER_1.16.1_\plugins\TamableFoxes_v${project.version}.jar</outputFile>
<!--<outputFile>D:\Code\java\spigotPlugins\_TEST_SERVER_PAPER_1.16.1_\plugins\TamableFoxes_v${project.version}.jar</outputFile>-->
<!--<outputFile>D:\Code\java\spigotPlugins\_TEST_SERVER_1.16.1_\plugins\TamableFoxes_v${project.version}.jar</outputFile>-->
<!--<outputFile>D:\Code\java\spigotPlugins\_TEST_SERVER_1.16.2_\plugins\TamableFoxes_v${project.version}.jar</outputFile>-->
<outputFile>D:\Code\java\spigotPlugins\_TEST_SERVER_PAPER_1.16.2_\plugins\TamableFoxes_v${project.version}.jar</outputFile>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
</execution>

View File

@ -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);
}
}
}