Add Java 14 support by replacing the final field access
This commit is contained in:
parent
582a675185
commit
9f8c6edabe
6
pom.xml
6
pom.xml
|
@ -12,7 +12,7 @@
|
|||
<name>Tamablefoxes</name>
|
||||
|
||||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
<java.version>1.14</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>${java.version}</source>
|
||||
<target>${java.version}</target>
|
||||
<source>9</source>
|
||||
<target>9</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
|
|
|
@ -0,0 +1,34 @@
|
|||
package net.seanomik.tamablefoxes.versions;
|
||||
|
||||
import java.lang.invoke.MethodHandles;
|
||||
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 {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
public static void makeNonFinal(Field field) {
|
||||
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 {
|
||||
makeNonFinal(field);
|
||||
field.setAccessible(true);
|
||||
field.set(obj, value);
|
||||
field.setAccessible(false);
|
||||
}
|
||||
}
|
|
@ -4,6 +4,7 @@ import net.minecraft.server.v1_14_R1.EntityFox;
|
|||
import net.minecraft.server.v1_14_R1.EntityTypes;
|
||||
import net.seanomik.tamablefoxes.Utils;
|
||||
import net.seanomik.tamablefoxes.io.LanguageConfig;
|
||||
import net.seanomik.tamablefoxes.versions.FieldHelper;
|
||||
import net.seanomik.tamablefoxes.versions.NMSInterface;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
|
@ -19,20 +20,7 @@ public class NMSInterface_1_14_R1 implements NMSInterface {
|
|||
public void registerCustomFoxEntity() {
|
||||
try { // Replace the fox entity
|
||||
Field field = EntityTypes.FOX.getClass().getDeclaredField("aZ");
|
||||
field.setAccessible(true);
|
||||
|
||||
// If the field is final, then make it non final
|
||||
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);
|
||||
}
|
||||
|
||||
field.set(EntityTypes.FOX, (EntityTypes.b<EntityFox>) (type, world) -> new EntityTamableFox(type, world));
|
||||
|
||||
field.setAccessible(false);
|
||||
|
||||
FieldHelper.setField(field, EntityTypes.FOX, (EntityTypes.b<EntityFox>) EntityTamableFox::new);
|
||||
Bukkit.getServer().getConsoleSender().sendMessage(Utils.getPrefix() + ChatColor.GREEN + LanguageConfig.getSuccessReplaced());
|
||||
} catch (Exception e) {
|
||||
Bukkit.getServer().getConsoleSender().sendMessage(Utils.getPrefix() + ChatColor.RED + LanguageConfig.getFailureReplace());
|
||||
|
|
|
@ -2,9 +2,9 @@ package net.seanomik.tamablefoxes.versions.version_1_15_R1;
|
|||
|
||||
import net.minecraft.server.v1_15_R1.EntityFox;
|
||||
import net.minecraft.server.v1_15_R1.EntityTypes;
|
||||
import net.seanomik.tamablefoxes.TamableFoxes;
|
||||
import net.seanomik.tamablefoxes.Utils;
|
||||
import net.seanomik.tamablefoxes.io.LanguageConfig;
|
||||
import net.seanomik.tamablefoxes.versions.FieldHelper;
|
||||
import net.seanomik.tamablefoxes.versions.NMSInterface;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
|
@ -20,20 +20,7 @@ public class NMSInterface_1_15_R1 implements NMSInterface {
|
|||
public void registerCustomFoxEntity() {
|
||||
try { // Replace the fox entity
|
||||
Field field = EntityTypes.FOX.getClass().getDeclaredField("ba");
|
||||
field.setAccessible(true);
|
||||
|
||||
// If the field is final, then make it non final
|
||||
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);
|
||||
}
|
||||
|
||||
field.set(EntityTypes.FOX, (EntityTypes.b<EntityFox>) (type, world) -> new EntityTamableFox(type, world));
|
||||
|
||||
field.setAccessible(false);
|
||||
|
||||
FieldHelper.setField(field, EntityTypes.FOX, (EntityTypes.b<EntityFox>) EntityTamableFox::new);
|
||||
Bukkit.getServer().getConsoleSender().sendMessage(Utils.getPrefix() + ChatColor.GREEN + LanguageConfig.getSuccessReplaced());
|
||||
} catch (Exception e) {
|
||||
Bukkit.getServer().getConsoleSender().sendMessage(Utils.getPrefix() + ChatColor.RED + LanguageConfig.getFailureReplace());
|
||||
|
|
|
@ -2,9 +2,9 @@ package net.seanomik.tamablefoxes.versions.version_1_16_R1;
|
|||
|
||||
import net.minecraft.server.v1_16_R1.EntityTypes;
|
||||
import net.minecraft.server.v1_16_R1.EntityFox;
|
||||
import net.seanomik.tamablefoxes.TamableFoxes;
|
||||
import net.seanomik.tamablefoxes.Utils;
|
||||
import net.seanomik.tamablefoxes.io.LanguageConfig;
|
||||
import net.seanomik.tamablefoxes.versions.FieldHelper;
|
||||
import net.seanomik.tamablefoxes.versions.NMSInterface;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
|
@ -21,20 +21,7 @@ public class NMSInterface_1_16_R1 implements NMSInterface {
|
|||
public void registerCustomFoxEntity() {
|
||||
try { // Replace the fox entity
|
||||
Field field = EntityTypes.FOX.getClass().getDeclaredField("be");
|
||||
field.setAccessible(true);
|
||||
|
||||
// If the field is final, then make it non final
|
||||
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);
|
||||
}
|
||||
|
||||
field.set(EntityTypes.FOX, (EntityTypes.b<EntityFox>) (type, world) -> new EntityTamableFox(type, world));
|
||||
|
||||
field.setAccessible(false);
|
||||
|
||||
FieldHelper.setField(field, EntityTypes.FOX, (EntityTypes.b<EntityFox>) EntityTamableFox::new);
|
||||
Bukkit.getServer().getConsoleSender().sendMessage(Utils.getPrefix() + ChatColor.GREEN + LanguageConfig.getSuccessReplaced());
|
||||
} catch (Exception e) {
|
||||
Bukkit.getServer().getConsoleSender().sendMessage(Utils.getPrefix() + ChatColor.RED + LanguageConfig.getFailureReplace());
|
||||
|
@ -44,6 +31,6 @@ public class NMSInterface_1_16_R1 implements NMSInterface {
|
|||
@Override
|
||||
public void spawnTamableFox(Location loc, FoxType type) {
|
||||
EntityTamableFox tamableFox = (EntityTamableFox) ((CraftEntity) loc.getWorld().spawnEntity(loc, EntityType.FOX)).getHandle();
|
||||
tamableFox.setFoxType( (type == FoxType.RED) ? EntityFox.Type.RED : EntityFox.Type.SNOW );
|
||||
tamableFox.setFoxType((type == FoxType.RED) ? EntityFox.Type.RED : EntityFox.Type.SNOW);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import net.minecraft.server.v1_16_R2.EntityFox;
|
|||
import net.minecraft.server.v1_16_R2.EntityTypes;
|
||||
import net.seanomik.tamablefoxes.Utils;
|
||||
import net.seanomik.tamablefoxes.io.LanguageConfig;
|
||||
import net.seanomik.tamablefoxes.versions.FieldHelper;
|
||||
import net.seanomik.tamablefoxes.versions.NMSInterface;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
|
@ -12,7 +13,6 @@ import org.bukkit.craftbukkit.v1_16_R2.entity.CraftEntity;
|
|||
import org.bukkit.entity.EntityType;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
|
||||
public class NMSInterface_1_16_R2 implements NMSInterface {
|
||||
|
||||
|
@ -20,20 +20,7 @@ public class NMSInterface_1_16_R2 implements NMSInterface {
|
|||
public void registerCustomFoxEntity() {
|
||||
try { // Replace the fox entity
|
||||
Field field = EntityTypes.FOX.getClass().getDeclaredField("bf");
|
||||
field.setAccessible(true);
|
||||
|
||||
// If the field is final, then make it non final
|
||||
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);
|
||||
}
|
||||
|
||||
field.set(EntityTypes.FOX, (EntityTypes.b<EntityFox>) (type, world) -> new EntityTamableFox(type, world));
|
||||
|
||||
field.setAccessible(false);
|
||||
|
||||
FieldHelper.setField(field, EntityTypes.FOX, (EntityTypes.b<EntityFox>) EntityTamableFox::new);
|
||||
Bukkit.getServer().getConsoleSender().sendMessage(Utils.getPrefix() + ChatColor.GREEN + LanguageConfig.getSuccessReplaced());
|
||||
} catch (Exception e) {
|
||||
Bukkit.getServer().getConsoleSender().sendMessage(Utils.getPrefix() + ChatColor.RED + LanguageConfig.getFailureReplace());
|
||||
|
@ -43,6 +30,6 @@ public class NMSInterface_1_16_R2 implements NMSInterface {
|
|||
@Override
|
||||
public void spawnTamableFox(Location loc, FoxType type) {
|
||||
EntityTamableFox tamableFox = (EntityTamableFox) ((CraftEntity) loc.getWorld().spawnEntity(loc, EntityType.FOX)).getHandle();
|
||||
tamableFox.setFoxType( (type == FoxType.RED) ? EntityFox.Type.RED : EntityFox.Type.SNOW );
|
||||
tamableFox.setFoxType((type == FoxType.RED) ? EntityFox.Type.RED : EntityFox.Type.SNOW);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue