Merge pull request #2 from KennyTV/master

Fix Java 12 reflection / 1.13 compat
This commit is contained in:
Gerrygames 2020-01-12 14:30:08 +01:00 committed by GitHub
commit 2c6a057216
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 5 deletions

View File

@ -34,7 +34,7 @@
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
<version>1.18.10</version>
<scope>provided</scope>
</dependency>
@ -42,7 +42,7 @@
<dependency>
<groupId>us.myles</groupId>
<artifactId>viaversion</artifactId>
<version>2.1.3</version>
<version>2.2.2</version>
<scope>provided</scope>
</dependency>
@ -50,7 +50,7 @@
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.12.2-R0.1-SNAPSHOT</version>
<version>1.15.1-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>

View File

@ -13,13 +13,16 @@ import us.myles.ViaVersion.api.Via;
public class EnchantingListener implements Listener {
private final boolean newMaterialNames = Material.getMaterial("LAPIS_LAZULI") != null;
private final Material lapisMaterial = newMaterialNames ? Material.LAPIS_LAZULI : Material.getMaterial("INK_SACK");
@EventHandler
public void onInventoryOpen(InventoryOpenEvent e) {
if (!(e.getInventory() instanceof EnchantingInventory)) return;
Player player = (Player) e.getPlayer();
if (Via.getAPI().getPlayerVersion(player)>5) return;
PlayerInventory playerInventory = player.getInventory();
ItemStack lapis = new ItemStack(Material.INK_SACK, 1, (short) 4);
ItemStack lapis = newMaterialNames ? new ItemStack(lapisMaterial) : new ItemStack(lapisMaterial, 1, (short) 4);
int amount = 0;
for (int i = 0; i<playerInventory.getSize(); i++) {
ItemStack item = playerInventory.getItem(i);

View File

@ -13,6 +13,15 @@ import java.util.Map;
public class ReflectionAPI {
private static Map<String, Field> fields = new HashMap<>();
private static Map<String, Method> methods = new HashMap<>();
private static boolean staticFinalModificationBlocked;
static {
try {
Field.class.getDeclaredField("modifiers");
} catch (NoSuchFieldException ex) {
staticFinalModificationBlocked = true;
}
}
public static Field getField(Class clazz, String fieldname) {
String key = clazz.getName() + ":" + fieldname;
@ -41,7 +50,25 @@ public class ReflectionAPI {
public static void setFieldNotFinal(Field field) {
int modifiers = field.getModifiers();
if (!Modifier.isFinal(modifiers)) return;
setValuePrintException(Field.class, field, "modifiers", modifiers & ~Modifier.FINAL);
if (staticFinalModificationBlocked) {
try {
Method getDeclaredFields0 = Class.class.getDeclaredMethod("getDeclaredFields0", boolean.class);
getDeclaredFields0.setAccessible(true);
Field[] fields = (Field[]) getDeclaredFields0.invoke(Field.class, false);
for (Field classField : fields) {
if ("modifiers".equals(classField.getName())) {
classField.setAccessible(true);
classField.set(field, modifiers & ~Modifier.FINAL);
break;
}
}
} catch (ReflectiveOperationException ex) {
ex.printStackTrace();
}
} else {
setValuePrintException(Field.class, field, "modifiers", modifiers & ~Modifier.FINAL);
}
}
public static <E> Constructor<E> getEmptyConstructor(Class<E> clazz) {