From 0ec8ea68e82c7f293cb896a5deafc25fcf4bc3cd Mon Sep 17 00:00:00 2001 From: fullwall Date: Thu, 25 Apr 2019 15:43:44 +0800 Subject: [PATCH] Upgrade to MethodHandles --- .../npc/profile/ProfileFetcher.java | 7 +- .../main/java/net/citizensnpcs/util/NMS.java | 68 ++++++- .../java/net/citizensnpcs/util/NMSBridge.java | 2 +- .../v1_14_R1/entity/ChickenController.java | 15 +- .../v1_14_R1/entity/GuardianController.java | 15 +- .../entity/GuardianElderController.java | 15 +- .../v1_14_R1/entity/PhantomController.java | 15 +- .../v1_14_R1/entity/ShulkerController.java | 62 +++---- .../v1_14_R1/entity/SnowmanController.java | 54 ++---- .../util/EmptyAdvancementDataPlayer.java | 22 +-- .../nms/v1_14_R1/util/NMSBoundingBox.java | 35 +--- .../nms/v1_14_R1/util/NMSImpl.java | 171 ++++++++---------- .../nms/v1_14_R1/util/PlayerNavigation.java | 10 - .../nms/v1_14_R1/util/PlayerlistTracker.java | 72 +++----- 14 files changed, 237 insertions(+), 326 deletions(-) diff --git a/main/src/main/java/net/citizensnpcs/npc/profile/ProfileFetcher.java b/main/src/main/java/net/citizensnpcs/npc/profile/ProfileFetcher.java index bbc13db73..cf2a1f433 100644 --- a/main/src/main/java/net/citizensnpcs/npc/profile/ProfileFetcher.java +++ b/main/src/main/java/net/citizensnpcs/npc/profile/ProfileFetcher.java @@ -79,7 +79,7 @@ public class ProfileFetcher { try { request.setResult(NMS.fillProfileProperties(profile, true), ProfileFetchResult.SUCCESS); - } catch (Exception e) { + } catch (Throwable e) { if (Messaging.isDebugging()) { Messaging.debug("Profile lookup for player '" + profile.getName() + "' failed: " + getExceptionMsg(e) + " " + isTooManyRequests(e)); @@ -134,7 +134,7 @@ public class ProfileFetcher { return null; } - private static String getExceptionMsg(Exception e) { + private static String getExceptionMsg(Throwable e) { return Throwables.getRootCause(e).getMessage(); } @@ -155,8 +155,7 @@ public class ProfileFetcher { || (cause != null && cause.contains("did not find")); } - private static boolean isTooManyRequests(Exception e) { - + private static boolean isTooManyRequests(Throwable e) { String message = e.getMessage(); String cause = e.getCause() != null ? e.getCause().getMessage() : null; diff --git a/main/src/main/java/net/citizensnpcs/util/NMS.java b/main/src/main/java/net/citizensnpcs/util/NMS.java index aae87e905..699b71601 100644 --- a/main/src/main/java/net/citizensnpcs/util/NMS.java +++ b/main/src/main/java/net/citizensnpcs/util/NMS.java @@ -1,5 +1,7 @@ package net.citizensnpcs.util; +import java.lang.invoke.MethodHandle; +import java.lang.invoke.MethodHandles; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier; @@ -55,7 +57,7 @@ public class NMS { * Yggdrasil's default implementation of this method silently fails instead of throwing * an Exception like it should. */ - public static GameProfile fillProfileProperties(GameProfile profile, boolean requireSecure) throws Exception { + public static GameProfile fillProfileProperties(GameProfile profile, boolean requireSecure) throws Throwable { return BRIDGE.fillProfileProperties(profile, requireSecure); } @@ -106,10 +108,44 @@ public class NMS { return f; } + public static MethodHandle getFinalSetter(Class clazz, String field) { + return getFinalSetter(clazz, field, true); + } + + public static MethodHandle getFinalSetter(Class clazz, String field, boolean log) { + Field f = getFinalField(clazz, field, log); + if (f == null) { + return null; + } + try { + return LOOKUP.unreflectSetter(f); + } catch (Exception e) { + if (log) { + Messaging.logTr(Messages.ERROR_GETTING_FIELD, field, e.getLocalizedMessage()); + } + } + return null; + } + public static GameProfileRepository getGameProfileRepository() { return BRIDGE.getGameProfileRepository(); } + public static MethodHandle getGetter(Class clazz, String name) { + return getGetter(clazz, name, true); + } + + public static MethodHandle getGetter(Class clazz, String name, boolean log) { + try { + return LOOKUP.unreflectGetter(getField(clazz, name, log)); + } catch (Exception e) { + if (log) { + Messaging.logTr(Messages.ERROR_GETTING_FIELD, name, e.getLocalizedMessage()); + } + } + return null; + } + public static float getHeadYaw(org.bukkit.entity.Entity entity) { return BRIDGE.getHeadYaw(entity); } @@ -133,6 +169,19 @@ public class NMS { return f; } + public static MethodHandle getMethodHandle(Class clazz, String method, boolean log, Class... params) { + if (clazz == null) + return null; + try { + return LOOKUP.unreflect(getMethod(clazz, method, log, params)); + } catch (Exception e) { + if (log) { + Messaging.logTr(Messages.ERROR_GETTING_METHOD, method, e.getLocalizedMessage()); + } + } + return null; + } + public static NPC getNPC(Entity entity) { return BRIDGE.getNPC(entity); } @@ -145,6 +194,21 @@ public class NMS { return BRIDGE.getProfile(meta); } + public static MethodHandle getSetter(Class clazz, String name) { + return getSetter(clazz, name, true); + } + + public static MethodHandle getSetter(Class clazz, String name, boolean log) { + try { + return LOOKUP.unreflectSetter(getField(clazz, name, log)); + } catch (Exception e) { + if (log) { + Messaging.logTr(Messages.ERROR_GETTING_FIELD, name, e.getLocalizedMessage()); + } + } + return null; + } + public static String getSound(String flag) throws CommandException { return BRIDGE.getSound(flag); } @@ -344,5 +408,7 @@ public class NMS { } private static NMSBridge BRIDGE; + + private static MethodHandles.Lookup LOOKUP = MethodHandles.lookup(); private static Field MODIFIERS_FIELD = NMS.getField(Field.class, "modifiers"); } diff --git a/main/src/main/java/net/citizensnpcs/util/NMSBridge.java b/main/src/main/java/net/citizensnpcs/util/NMSBridge.java index d3b7b2182..df81c9024 100644 --- a/main/src/main/java/net/citizensnpcs/util/NMSBridge.java +++ b/main/src/main/java/net/citizensnpcs/util/NMSBridge.java @@ -38,7 +38,7 @@ public interface NMSBridge { public void attack(LivingEntity attacker, LivingEntity target); - public GameProfile fillProfileProperties(GameProfile profile, boolean requireSecure) throws Exception; + public GameProfile fillProfileProperties(GameProfile profile, boolean requireSecure) throws Throwable; public BlockBreaker getBlockBreaker(Entity entity, Block targetBlock, BlockBreakerConfiguration config); diff --git a/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/entity/ChickenController.java b/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/entity/ChickenController.java index f1931b759..c61de6008 100644 --- a/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/entity/ChickenController.java +++ b/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/entity/ChickenController.java @@ -1,7 +1,5 @@ package net.citizensnpcs.nms.v1_14_R1.entity; -import java.lang.reflect.Method; - import org.bukkit.Bukkit; import org.bukkit.craftbukkit.v1_14_R1.CraftServer; import org.bukkit.craftbukkit.v1_14_R1.entity.CraftChicken; @@ -15,7 +13,6 @@ import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.nms.v1_14_R1.util.NMSImpl; import net.citizensnpcs.npc.CitizensNPC; import net.citizensnpcs.npc.ai.NPCHolder; -import net.citizensnpcs.util.NMS; import net.citizensnpcs.util.Util; import net.minecraft.server.v1_14_R1.BlockPosition; import net.minecraft.server.v1_14_R1.DamageSource; @@ -223,17 +220,7 @@ public class ChickenController extends MobEntityController { if (npc != null) { this.eggLayTime = 100; // egg timer } - try { - super.movementTick(); - } catch (NoSuchMethodError ex) { - try { - MOVEMENT_TICK.invoke(this); - } catch (Throwable ex2) { - ex2.printStackTrace(); - } - } + super.movementTick(); } - - private static final Method MOVEMENT_TICK = NMS.getMethod(EntityChicken.class, "k", false); } } diff --git a/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/entity/GuardianController.java b/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/entity/GuardianController.java index faa8ad881..c0cb5041a 100644 --- a/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/entity/GuardianController.java +++ b/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/entity/GuardianController.java @@ -1,7 +1,5 @@ package net.citizensnpcs.nms.v1_14_R1.entity; -import java.lang.reflect.Method; - import org.bukkit.Bukkit; import org.bukkit.craftbukkit.v1_14_R1.CraftServer; import org.bukkit.craftbukkit.v1_14_R1.entity.CraftEntity; @@ -15,7 +13,6 @@ import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.nms.v1_14_R1.util.NMSImpl; import net.citizensnpcs.npc.CitizensNPC; import net.citizensnpcs.npc.ai.NPCHolder; -import net.citizensnpcs.util.NMS; import net.citizensnpcs.util.Util; import net.minecraft.server.v1_14_R1.BlockPosition; import net.minecraft.server.v1_14_R1.DamageSource; @@ -185,15 +182,7 @@ public class GuardianController extends MobEntityController { @Override public void movementTick() { if (npc == null) { - try { - super.movementTick(); - } catch (NoSuchMethodError ex) { - try { - MOVEMENT_TICK.invoke(this); - } catch (Throwable ex2) { - ex2.printStackTrace(); - } - } + super.movementTick(); } else { NMSImpl.updateAI(this); npc.update(); @@ -208,8 +197,6 @@ public class GuardianController extends MobEntityController { NMSImpl.setSize(this, justCreated); } } - - private static final Method MOVEMENT_TICK = NMS.getMethod(EntityGuardian.class, "k", false); } public static class GuardianNPC extends CraftGuardian implements NPCHolder { diff --git a/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/entity/GuardianElderController.java b/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/entity/GuardianElderController.java index 0794ca5df..29dac3ffe 100644 --- a/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/entity/GuardianElderController.java +++ b/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/entity/GuardianElderController.java @@ -1,7 +1,5 @@ package net.citizensnpcs.nms.v1_14_R1.entity; -import java.lang.reflect.Method; - import org.bukkit.Bukkit; import org.bukkit.craftbukkit.v1_14_R1.CraftServer; import org.bukkit.craftbukkit.v1_14_R1.entity.CraftElderGuardian; @@ -15,7 +13,6 @@ import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.nms.v1_14_R1.util.NMSImpl; import net.citizensnpcs.npc.CitizensNPC; import net.citizensnpcs.npc.ai.NPCHolder; -import net.citizensnpcs.util.NMS; import net.citizensnpcs.util.Util; import net.minecraft.server.v1_14_R1.BlockPosition; import net.minecraft.server.v1_14_R1.DamageSource; @@ -185,15 +182,7 @@ public class GuardianElderController extends MobEntityController { @Override public void movementTick() { if (npc == null) { - try { - super.movementTick(); - } catch (NoSuchMethodError ex) { - try { - MOVEMENT_TICK.invoke(this); - } catch (Throwable ex2) { - ex2.printStackTrace(); - } - } + super.movementTick(); } else { NMSImpl.updateAI(this); npc.update(); @@ -208,8 +197,6 @@ public class GuardianElderController extends MobEntityController { NMSImpl.setSize(this, justCreated); } } - - private static final Method MOVEMENT_TICK = NMS.getMethod(EntityGuardianElder.class, "k", false); } public static class GuardianElderNPC extends CraftElderGuardian implements NPCHolder { diff --git a/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/entity/PhantomController.java b/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/entity/PhantomController.java index 06845a73d..f133d6047 100644 --- a/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/entity/PhantomController.java +++ b/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/entity/PhantomController.java @@ -1,7 +1,5 @@ package net.citizensnpcs.nms.v1_14_R1.entity; -import java.lang.reflect.Method; - import org.bukkit.Bukkit; import org.bukkit.craftbukkit.v1_14_R1.CraftServer; import org.bukkit.craftbukkit.v1_14_R1.entity.CraftEntity; @@ -15,7 +13,6 @@ import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.nms.v1_14_R1.util.NMSImpl; import net.citizensnpcs.npc.CitizensNPC; import net.citizensnpcs.npc.ai.NPCHolder; -import net.citizensnpcs.util.NMS; import net.citizensnpcs.util.Util; import net.minecraft.server.v1_14_R1.BlockPosition; import net.minecraft.server.v1_14_R1.ControllerLook; @@ -196,15 +193,7 @@ public class PhantomController extends MobEntityController { @Override public void movementTick() { - try { - super.movementTick(); - } catch (NoSuchMethodError ex) { - try { - MOVEMENT_TICK.invoke(this); - } catch (Throwable ex2) { - ex2.printStackTrace(); - } - } + super.movementTick(); if (npc != null) { if (npc.isProtected()) { this.setOnFire(0); @@ -225,8 +214,6 @@ public class PhantomController extends MobEntityController { this.world.getWorldData().setDifficulty(EnumDifficulty.PEACEFUL); } } - - private static final Method MOVEMENT_TICK = NMS.getMethod(EntityPhantom.class, "k", false); } public static class PhantomNPC extends CraftPhantom implements NPCHolder { diff --git a/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/entity/ShulkerController.java b/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/entity/ShulkerController.java index e9c08c8fd..b376ee2c9 100644 --- a/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/entity/ShulkerController.java +++ b/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/entity/ShulkerController.java @@ -1,9 +1,5 @@ package net.citizensnpcs.nms.v1_14_R1.entity; -import net.minecraft.server.v1_14_R1.Vec3D; - -import java.lang.reflect.Method; - import org.bukkit.Bukkit; import org.bukkit.craftbukkit.v1_14_R1.CraftServer; import org.bukkit.craftbukkit.v1_14_R1.entity.CraftEntity; @@ -17,7 +13,6 @@ import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.nms.v1_14_R1.util.NMSImpl; import net.citizensnpcs.npc.CitizensNPC; import net.citizensnpcs.npc.ai.NPCHolder; -import net.citizensnpcs.util.NMS; import net.citizensnpcs.util.Util; import net.minecraft.server.v1_14_R1.BlockPosition; import net.minecraft.server.v1_14_R1.DamageSource; @@ -27,6 +22,7 @@ import net.minecraft.server.v1_14_R1.EntityTypes; import net.minecraft.server.v1_14_R1.IBlockData; import net.minecraft.server.v1_14_R1.NBTTagCompound; import net.minecraft.server.v1_14_R1.SoundEffect; +import net.minecraft.server.v1_14_R1.Vec3D; import net.minecraft.server.v1_14_R1.World; public class ShulkerController extends MobEntityController { @@ -61,15 +57,6 @@ public class ShulkerController extends MobEntityController { } } - @Override - public void e(Vec3D vec3d) { - if (npc == null || !npc.isFlyable()) { - super.e(vec3d); - } else { - NMSImpl.flyingMoveLogic(this, vec3d); - } - } - @Override public void b(float f, float f1) { if (npc == null || !npc.isFlyable()) { @@ -98,6 +85,15 @@ public class ShulkerController extends MobEntityController { return npc == null ? super.d(save) : false; } + @Override + public void e(Vec3D vec3d) { + if (npc == null || !npc.isFlyable()) { + super.e(vec3d); + } else { + NMSImpl.flyingMoveLogic(this, vec3d); + } + } + @Override public void enderTeleportTo(double d0, double d1, double d2) { if (npc == null) { @@ -160,6 +156,15 @@ public class ShulkerController extends MobEntityController { return NMSImpl.getSoundEffect(npc, super.getSoundHurt(damagesource), NPC.HURT_SOUND_METADATA); } + @Override + public boolean isClimbing() { + if (npc == null || !npc.isFlyable()) { + return super.isClimbing(); + } else { + return false; + } + } + @Override public boolean isLeashed() { if (npc == null) @@ -176,15 +181,7 @@ public class ShulkerController extends MobEntityController { @Override public void movementTick() { if (npc == null) { - try { - super.movementTick(); - } catch (NoSuchMethodError ex) { - try { - MOVEMENT_TICK.invoke(this); - } catch (Throwable ex2) { - ex2.printStackTrace(); - } - } + super.movementTick(); } } @@ -193,15 +190,6 @@ public class ShulkerController extends MobEntityController { return new EntityAIBodyControl(this); } - @Override - public void updateSize() { - if (npc == null) { - super.updateSize(); - } else { - NMSImpl.setSize(this, justCreated); - } - } - @Override public void tick() { if (npc != null) { @@ -212,15 +200,13 @@ public class ShulkerController extends MobEntityController { } @Override - public boolean isClimbing() { - if (npc == null || !npc.isFlyable()) { - return super.isClimbing(); + public void updateSize() { + if (npc == null) { + super.updateSize(); } else { - return false; + NMSImpl.setSize(this, justCreated); } } - - private static final Method MOVEMENT_TICK = NMS.getMethod(EntityShulker.class, "k", false); } public static class ShulkerNPC extends CraftShulker implements NPCHolder { diff --git a/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/entity/SnowmanController.java b/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/entity/SnowmanController.java index 486cd11e4..a1ac23ca6 100644 --- a/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/entity/SnowmanController.java +++ b/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/entity/SnowmanController.java @@ -1,9 +1,5 @@ package net.citizensnpcs.nms.v1_14_R1.entity; -import net.minecraft.server.v1_14_R1.Vec3D; - -import java.lang.reflect.Method; - import org.bukkit.Bukkit; import org.bukkit.craftbukkit.v1_14_R1.CraftServer; import org.bukkit.craftbukkit.v1_14_R1.entity.CraftEntity; @@ -17,7 +13,6 @@ import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.nms.v1_14_R1.util.NMSImpl; import net.citizensnpcs.npc.CitizensNPC; import net.citizensnpcs.npc.ai.NPCHolder; -import net.citizensnpcs.util.NMS; import net.citizensnpcs.util.Util; import net.minecraft.server.v1_14_R1.BlockPosition; import net.minecraft.server.v1_14_R1.DamageSource; @@ -26,6 +21,7 @@ import net.minecraft.server.v1_14_R1.EntityTypes; import net.minecraft.server.v1_14_R1.IBlockData; import net.minecraft.server.v1_14_R1.NBTTagCompound; import net.minecraft.server.v1_14_R1.SoundEffect; +import net.minecraft.server.v1_14_R1.Vec3D; import net.minecraft.server.v1_14_R1.World; public class SnowmanController extends MobEntityController { @@ -60,15 +56,6 @@ public class SnowmanController extends MobEntityController { } } - @Override - public void e(Vec3D vec3d) { - if (npc == null || !npc.isFlyable()) { - super.e(vec3d); - } else { - NMSImpl.flyingMoveLogic(this, vec3d); - } - } - @Override public void b(float f, float f1) { if (npc == null || !npc.isFlyable()) { @@ -97,6 +84,15 @@ public class SnowmanController extends MobEntityController { return npc == null ? super.d(save) : false; } + @Override + public void e(Vec3D vec3d) { + if (npc == null || !npc.isFlyable()) { + super.e(vec3d); + } else { + NMSImpl.flyingMoveLogic(this, vec3d); + } + } + @Override public void enderTeleportTo(double d0, double d1, double d2) { if (npc == null) { @@ -159,6 +155,15 @@ public class SnowmanController extends MobEntityController { return NMSImpl.getSoundEffect(npc, super.getSoundHurt(damagesource), NPC.HURT_SOUND_METADATA); } + @Override + public boolean isClimbing() { + if (npc == null || !npc.isFlyable()) { + return super.isClimbing(); + } else { + return false; + } + } + @Override public boolean isLeashed() { if (npc == null) @@ -186,15 +191,7 @@ public class SnowmanController extends MobEntityController { if (npc != null) { this.world.getGameRules().set("mobGriefing", "false", this.world.getMinecraftServer()); } - try { - super.movementTick(); - } catch (NoSuchMethodError ex) { - try { - MOVEMENT_TICK.invoke(this); - } catch (Throwable ex2) { - ex2.printStackTrace(); - } - } + super.movementTick(); if (npc != null) { this.world.getGameRules().set("mobGriefing", Boolean.toString(allowsGriefing), this.world.getMinecraftServer()); @@ -209,17 +206,6 @@ public class SnowmanController extends MobEntityController { NMSImpl.setSize(this, justCreated); } } - - @Override - public boolean isClimbing() { - if (npc == null || !npc.isFlyable()) { - return super.isClimbing(); - } else { - return false; - } - } - - private static final Method MOVEMENT_TICK = NMS.getMethod(EntitySnowman.class, "k", false); } public static class SnowmanNPC extends CraftSnowman implements NPCHolder { diff --git a/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/util/EmptyAdvancementDataPlayer.java b/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/util/EmptyAdvancementDataPlayer.java index 054529abb..f2e43a55a 100644 --- a/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/util/EmptyAdvancementDataPlayer.java +++ b/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/util/EmptyAdvancementDataPlayer.java @@ -1,7 +1,7 @@ package net.citizensnpcs.nms.v1_14_R1.util; import java.io.File; -import java.lang.reflect.Field; +import java.lang.invoke.MethodHandle; import java.util.Set; import net.citizensnpcs.util.NMS; @@ -14,8 +14,8 @@ import net.minecraft.server.v1_14_R1.MinecraftServer; public class EmptyAdvancementDataPlayer extends AdvancementDataPlayer { public EmptyAdvancementDataPlayer(MinecraftServer minecraftserver, File file, EntityPlayer entityplayer) { super(minecraftserver, file, entityplayer); - this.b(); - } + this.b(); + } @Override public void a(Advancement advancement) { @@ -57,17 +57,15 @@ public class EmptyAdvancementDataPlayer extends AdvancementDataPlayer { data.a(); data.data.clear(); try { - ((Set) G.get(data)).clear(); - ((Set) H.get(data)).clear(); - ((Set) I.get(data)).clear(); - } catch (IllegalArgumentException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { + ((Set) G.invoke(data)).clear(); + ((Set) H.invoke(data)).clear(); + ((Set) I.invoke(data)).clear(); + } catch (Throwable e) { e.printStackTrace(); } } - private static final Field G = NMS.getField(AdvancementDataPlayer.class, "g"); - private static final Field H = NMS.getField(AdvancementDataPlayer.class, "h"); - private static final Field I = NMS.getField(AdvancementDataPlayer.class, "i"); + private static final MethodHandle G = NMS.getGetter(AdvancementDataPlayer.class, "g"); + private static final MethodHandle H = NMS.getGetter(AdvancementDataPlayer.class, "h"); + private static final MethodHandle I = NMS.getGetter(AdvancementDataPlayer.class, "i"); } diff --git a/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/util/NMSBoundingBox.java b/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/util/NMSBoundingBox.java index 8ecb17631..13237a769 100644 --- a/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/util/NMSBoundingBox.java +++ b/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/util/NMSBoundingBox.java @@ -1,9 +1,6 @@ package net.citizensnpcs.nms.v1_14_R1.util; -import java.lang.reflect.Field; - import net.citizensnpcs.util.BoundingBox; -import net.citizensnpcs.util.NMS; import net.minecraft.server.v1_14_R1.AxisAlignedBB; public class NMSBoundingBox { @@ -12,32 +9,12 @@ public class NMSBoundingBox { public static BoundingBox wrap(AxisAlignedBB bb) { double minX = 0, minY = 0, minZ = 0, maxX = 0, maxY = 0, maxZ = 0; - try { - minX = bb.minX; - minY = bb.minY; - minZ = bb.minZ; - maxX = bb.maxX; - maxY = bb.maxY; - maxZ = bb.maxZ; - } catch (NoSuchFieldError ex) { - try { - minX = a.getDouble(bb); - minY = b.getDouble(bb); - minZ = c.getDouble(bb); - maxX = d.getDouble(bb); - maxY = e.getDouble(bb); - maxZ = f.getDouble(bb); - } catch (Exception ex2) { - ex.printStackTrace(); - } - } + minX = bb.minX; + minY = bb.minY; + minZ = bb.minZ; + maxX = bb.maxX; + maxY = bb.maxY; + maxZ = bb.maxZ; return new BoundingBox(minX, minY, minZ, maxX, maxY, maxZ); } - - private static final Field a = NMS.getFinalField(AxisAlignedBB.class, "a", false); - private static final Field b = NMS.getFinalField(AxisAlignedBB.class, "b", false); - private static final Field c = NMS.getFinalField(AxisAlignedBB.class, "c", false); - private static final Field d = NMS.getFinalField(AxisAlignedBB.class, "d", false); - private static final Field e = NMS.getFinalField(AxisAlignedBB.class, "e", false); - private static final Field f = NMS.getFinalField(AxisAlignedBB.class, "f", false); } diff --git a/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/util/NMSImpl.java b/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/util/NMSImpl.java index 872d857db..bea91b2f4 100644 --- a/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/util/NMSImpl.java +++ b/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/util/NMSImpl.java @@ -1,8 +1,7 @@ package net.citizensnpcs.nms.v1_14_R1.util; +import java.lang.invoke.MethodHandle; import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; import java.net.SocketAddress; import java.net.URL; import java.util.ArrayList; @@ -248,7 +247,6 @@ import net.minecraft.server.v1_14_R1.PlayerChunkMap.EntityTracker; import net.minecraft.server.v1_14_R1.RegistryBlocks; import net.minecraft.server.v1_14_R1.ReportedException; import net.minecraft.server.v1_14_R1.SoundEffect; -import net.minecraft.server.v1_14_R1.SoundEffects; import net.minecraft.server.v1_14_R1.Vec3D; import net.minecraft.server.v1_14_R1.WorldServer; @@ -315,7 +313,7 @@ public class NMSImpl implements NMSBridge { } @Override - public GameProfile fillProfileProperties(GameProfile profile, boolean requireSecure) throws Exception { + public GameProfile fillProfileProperties(GameProfile profile, boolean requireSecure) throws Throwable { if (Bukkit.isPrimaryThread()) throw new IllegalStateException("NMS.fillProfileProperties cannot be invoked from the main thread."); @@ -407,10 +405,8 @@ public class NMSImpl implements NMSBridge { @Override public GameProfile getProfile(SkullMeta meta) { if (SKULL_PROFILE_FIELD == null) { - try { - SKULL_PROFILE_FIELD = meta.getClass().getDeclaredField("profile"); - SKULL_PROFILE_FIELD.setAccessible(true); - } catch (Exception e) { + SKULL_PROFILE_FIELD = NMS.getField(meta.getClass(), "profile", false); + if (SKULL_PROFILE_FIELD == null) { return null; } } @@ -526,20 +522,21 @@ public class NMSImpl implements NMSBridge { Entity handle = getHandle(entity); EntitySize size = null; try { - size = (EntitySize) SIZE_FIELD.get(handle); + size = (EntitySize) SIZE_FIELD_GETTER.invoke(handle); if (handle instanceof EntityHorse) { - SIZE_FIELD.set(handle, new EntitySize(Math.min(0.99F, size.width), size.height, false)); + SIZE_FIELD_SETTER.invoke(handle, + new EntitySize(Math.min(0.99F, size.width), size.height, false)); } - } catch (Exception e) { + } catch (Throwable e) { e.printStackTrace(); } if (!function.apply(navigation)) { reason = CancelReason.STUCK; } try { - SIZE_FIELD.set(handle, size); - } catch (Exception e) { + SIZE_FIELD_SETTER.invoke(handle, size); + } catch (Throwable e) { e.printStackTrace(); // minecraft requires that an entity fit onto both blocks if width >= 1f, but we'd prefer to // make it just fit on 1 so hack around it a bit. @@ -976,10 +973,8 @@ public class NMSImpl implements NMSBridge { @Override public void setProfile(SkullMeta meta, GameProfile profile) { if (SKULL_PROFILE_FIELD == null) { - try { - SKULL_PROFILE_FIELD = meta.getClass().getDeclaredField("profile"); - SKULL_PROFILE_FIELD.setAccessible(true); - } catch (Exception e) { + SKULL_PROFILE_FIELD = NMS.getField(meta.getClass(), "profile", false); + if (SKULL_PROFILE_FIELD == null) { return; } } @@ -1036,10 +1031,8 @@ public class NMSImpl implements NMSBridge { if (JUMP_FIELD == null || !(entity instanceof LivingEntity)) return false; try { - return JUMP_FIELD.getBoolean(NMSImpl.getHandle(entity)); - } catch (IllegalArgumentException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { + return (boolean) JUMP_FIELD.invoke(NMSImpl.getHandle(entity)); + } catch (Throwable e) { e.printStackTrace(); } return false; @@ -1049,13 +1042,10 @@ public class NMSImpl implements NMSBridge { public void shutdown() { if (ENTITY_REGISTRY == null) return; - Field field = NMS.getFinalField(EntityTypes.class, "REGISTRY", false); - if (field == null) { - field = NMS.getFinalField(IRegistry.class, "ENTITY_TYPE"); - } + MethodHandle field = NMS.getFinalSetter(IRegistry.class, "ENTITY_TYPE"); try { - field.set(null, ENTITY_REGISTRY.getWrapped()); - } catch (Exception e) { + field.invoke(null, ENTITY_REGISTRY.getWrapped()); + } catch (Throwable e) { } } @@ -1122,9 +1112,11 @@ public class NMSImpl implements NMSBridge { EntityInsentient handle = (EntityInsentient) en; WorldServer worldHandle = ((CraftWorld) world).getHandle(); try { - NAVIGATION_WORLD_FIELD.set(handle.getNavigation(), worldHandle); + NAVIGATION_WORLD_FIELD.invoke(handle.getNavigation(), worldHandle); } catch (Exception e) { Messaging.logTr(Messages.ERROR_UPDATING_NAVIGATION_WORLD, e.getMessage()); + } catch (Throwable e) { + e.printStackTrace(); } } @@ -1144,11 +1136,9 @@ public class NMSImpl implements NMSBridge { EntityInsentient handle = (EntityInsentient) en; NavigationAbstract navigation = handle.getNavigation(); try { - AttributeInstance inst = (AttributeInstance) PATHFINDING_RANGE.get(navigation); + AttributeInstance inst = (AttributeInstance) PATHFINDING_RANGE.invoke(navigation); inst.setValue(pathfindingRange); - } catch (IllegalArgumentException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { + } catch (Throwable e) { e.printStackTrace(); } } @@ -1227,8 +1217,8 @@ public class NMSImpl implements NMSBridge { public static void checkAndUpdateHeight(EntityLiving living, DataWatcherObject datawatcherobject) { EntitySize size; try { - size = (EntitySize) SIZE_FIELD.get(living); - } catch (Exception e) { + size = (EntitySize) SIZE_FIELD_GETTER.invoke(living); + } catch (Throwable e) { e.printStackTrace(); living.a(datawatcherobject); return; @@ -1247,10 +1237,12 @@ public class NMSImpl implements NMSBridge { return; for (PathfinderGoalSelector selector : goalSelectors) { try { - Collection list = (Collection) GOAL_FIELD.get(selector); + Collection list = (Collection) GOAL_FIELD.invoke(selector); list.clear(); } catch (Exception e) { Messaging.logTr(Messages.ERROR_CLEARING_GOALS, e.getLocalizedMessage()); + } catch (Throwable e) { + Messaging.logTr(Messages.ERROR_CLEARING_GOALS, e.getLocalizedMessage()); } } } @@ -1324,10 +1316,11 @@ public class NMSImpl implements NMSBridge { double d6 = d4 - d5; float f4 = (float) (d6 * 10.0D - 3.0D); if (f4 > 0.0F) { - - entity.a(/* TODO ?implement properly entity.getSoundFall((int) f4)*/f4 > 4 - ? SoundEffects.ENTITY_GENERIC_BIG_FALL - : SoundEffects.ENTITY_GENERIC_SMALL_FALL, 1.0F, 1.0F); + try { + entity.a((SoundEffect) ENTITY_GET_SOUND_FALL.invoke(entity, (int) f4), 1.0F, 1.0F); + } catch (Throwable e) { + e.printStackTrace(); + } entity.damageEntity(DamageSource.FLY_INTO_WALL, f4); } } @@ -1341,21 +1334,19 @@ public class NMSImpl implements NMSBridge { entity.locZ); float f5 = entity.world.getType(blockposition).getBlock().m(); f1 = entity.onGround ? f5 * 0.91F : 0.91F; - entity.a(/* TODO ?implement properly entity.r(f5)*/ entity.onGround - ? entity.da() * (0.21600002F / (f5 * f5 * f5)) - : entity.aO, vec3d); try { + entity.a((float) ENTITY_R.invoke(entity, f5), vec3d); entity.setMot((Vec3D) ISCLIMBING_METHOD.invoke(entity, entity.getMot())); - } catch (Exception e) { + } catch (Throwable e) { e.printStackTrace(); } entity.move(EnumMoveType.SELF, entity.getMot()); vec3d2 = entity.getMot(); try { - if ((entity.positionChanged || JUMP_FIELD.getBoolean(entity)) && entity.isClimbing()) { + if ((entity.positionChanged || (boolean) JUMP_FIELD.invoke(entity)) && entity.isClimbing()) { vec3d2 = new Vec3D(vec3d2.x, 0.2D, vec3d2.z); } - } catch (Exception e) { + } catch (Throwable e) { e.printStackTrace(); } @@ -1457,17 +1448,19 @@ public class NMSImpl implements NMSBridge { if (entity.getType() == EntityType.WITHER) { bserver = ((EntityWither) NMSImpl.getHandle(entity)).bossBattle; } else if (entity.getType() == EntityType.ENDER_DRAGON) { - bserver = ((EnderDragonBattle) ENDERDRAGON_BATTLE_FIELD.get(NMSImpl.getHandle(entity))).bossBattle; + bserver = ((EnderDragonBattle) ENDERDRAGON_BATTLE_FIELD.invoke(NMSImpl.getHandle(entity))).bossBattle; } - } catch (Exception e) { + } catch (Throwable e) { + e.printStackTrace(); } if (bserver == null) { return null; } BossBar ret = Bukkit.createBossBar("", BarColor.BLUE, BarStyle.SEGMENTED_10); try { - CRAFT_BOSSBAR_HANDLE_FIELD.set(ret, bserver); - } catch (Exception e) { + CRAFT_BOSSBAR_HANDLE_FIELD.invoke(ret, bserver); + } catch (Throwable e) { + e.printStackTrace(); } return ret; } @@ -1500,10 +1493,8 @@ public class NMSImpl implements NMSBridge { if (RABBIT_FIELD == null) return null; try { - return (DataWatcherObject) RABBIT_FIELD.get(null); - } catch (IllegalArgumentException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { + return (DataWatcherObject) RABBIT_FIELD.invoke(null); + } catch (Throwable e) { e.printStackTrace(); } return null; @@ -1579,10 +1570,8 @@ public class NMSImpl implements NMSBridge { public static void setAdvancement(Player entity, AdvancementDataPlayer instance) { try { - ADVANCEMENT_PLAYER_FIELD.set(getHandle(entity), instance); - } catch (IllegalArgumentException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { + ADVANCEMENT_PLAYER_FIELD.invoke(getHandle(entity), instance); + } catch (Throwable e) { e.printStackTrace(); } } @@ -1590,9 +1579,9 @@ public class NMSImpl implements NMSBridge { public static void setNotInSchool(EntityFish entity) { try { if (ENTITY_FISH_NUM_IN_SCHOOL != null) { - ENTITY_FISH_NUM_IN_SCHOOL.set(entity, 2); + ENTITY_FISH_NUM_IN_SCHOOL.invoke(entity, 2); } - } catch (Exception ex) { + } catch (Throwable ex) { ex.printStackTrace(); } } @@ -1603,12 +1592,12 @@ public class NMSImpl implements NMSBridge { public static void setSize(Entity entity, boolean justCreated) { try { - EntitySize entitysize = (EntitySize) SIZE_FIELD.get(entity); + EntitySize entitysize = (EntitySize) SIZE_FIELD_GETTER.invoke(entity); EntityPose entitypose = entity.Z(); EntitySize entitysize1 = entity.a(entitypose); - SIZE_FIELD.set(entity, entitysize1); - HEAD_HEIGHT.set(entity, HEAD_HEIGHT_METHOD.invoke(entity, entitypose, entitysize1)); - if (entitysize1.width < entitysize.width && false /* CITIZENS ADDITION ?reason */) { + SIZE_FIELD_SETTER.invoke(entity, entitysize1); + HEAD_HEIGHT.invoke(entity, HEAD_HEIGHT_METHOD.invoke(entity, entitypose, entitysize1)); + if (entitysize1.width < entitysize.width && false /* TODO: PREVIOUS CITIZENS ADDITION ?reason */) { double d0 = entitysize1.width / 2.0D; entity.a(new AxisAlignedBB(entity.locX - d0, entity.locY, entity.locZ - d0, entity.locX + d0, entity.locY + entitysize1.height, entity.locZ + d0)); @@ -1622,11 +1611,7 @@ public class NMSImpl implements NMSBridge { entity.move(EnumMoveType.SELF, new Vec3D(f, 0.0D, f)); } } - } catch (IllegalArgumentException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } catch (InvocationTargetException e) { + } catch (Throwable e) { e.printStackTrace(); } } @@ -1652,35 +1637,41 @@ public class NMSImpl implements NMSBridge { navigation.c(); } - private static Field ADVANCEMENT_PLAYER_FIELD = NMS.getFinalField(EntityPlayer.class, "advancementDataPlayer"); + private static final MethodHandle ADVANCEMENT_PLAYER_FIELD = NMS.getFinalSetter(EntityPlayer.class, + "advancementDataPlayer"); private static final Set BAD_CONTROLLER_LOOK = EnumSet.of(EntityType.POLAR_BEAR, EntityType.SILVERFISH, EntityType.SHULKER, EntityType.ENDERMITE, EntityType.ENDER_DRAGON, EntityType.BAT, EntityType.SLIME, EntityType.MAGMA_CUBE, EntityType.HORSE, EntityType.GHAST); - private static final Method BLOCK_POSITION_B_D = NMS.getMethod(BlockPosition.PooledBlockPosition.class, "c", false, - double.class, double.class, double.class); - private static Map, EntityTypes> CITIZENS_ENTITY_TYPES = Maps.newHashMap(); - private static final Field CRAFT_BOSSBAR_HANDLE_FIELD = NMS.getField(CraftBossBar.class, "handle"); + private static final MethodHandle BLOCK_POSITION_B_D = NMS.getMethodHandle(BlockPosition.PooledBlockPosition.class, + "c", false, double.class, double.class, double.class); + private static final Map, EntityTypes> CITIZENS_ENTITY_TYPES = Maps.newHashMap(); + private static final MethodHandle CRAFT_BOSSBAR_HANDLE_FIELD = NMS.getSetter(CraftBossBar.class, "handle"); private static final float DEFAULT_SPEED = 1F; - private static final Field ENDERDRAGON_BATTLE_FIELD = NMS.getField(EntityEnderDragon.class, "bP"); - private static Field ENTITY_FISH_NUM_IN_SCHOOL = NMS.getField(EntityFishSchool.class, "c", false); + private static final MethodHandle ENDERDRAGON_BATTLE_FIELD = NMS.getGetter(EntityEnderDragon.class, "bP"); + private static final MethodHandle ENTITY_FISH_NUM_IN_SCHOOL = NMS.getSetter(EntityFishSchool.class, "c", false); + private static final MethodHandle ENTITY_GET_SOUND_FALL = NMS.getMethodHandle(EntityLiving.class, "getSoundFall", + true, int.class); + private static final MethodHandle ENTITY_R = NMS.getMethodHandle(EntityLiving.class, "r", true, float.class); private static CustomEntityRegistry ENTITY_REGISTRY; private static final Location FROM_LOCATION = new Location(null, 0, 0, 0); - public static Field GOAL_FIELD = NMS.getField(PathfinderGoalSelector.class, "d"); - private static final Field HEAD_HEIGHT = NMS.getField(Entity.class, "headHeight"); - private static final Method HEAD_HEIGHT_METHOD = NMS.getMethod(Entity.class, "getHeadHeight", true, + private static final MethodHandle GOAL_FIELD = NMS.getGetter(PathfinderGoalSelector.class, "d"); + private static final MethodHandle HEAD_HEIGHT = NMS.getSetter(Entity.class, "headHeight"); + private static final MethodHandle HEAD_HEIGHT_METHOD = NMS.getMethodHandle(Entity.class, "getHeadHeight", true, EntityPose.class, EntitySize.class); - private static Method ISCLIMBING_METHOD = NMS.getMethod(EntityLiving.class, "f", true, Vec3D.class); - private static final Field JUMP_FIELD = NMS.getField(EntityLiving.class, "jumping"); - private static Method MAKE_REQUEST; - private static Field NAVIGATION_WORLD_FIELD = NMS.getField(NavigationAbstract.class, "b"); + private static final MethodHandle ISCLIMBING_METHOD = NMS.getMethodHandle(EntityLiving.class, "f", true, + Vec3D.class); + private static final MethodHandle JUMP_FIELD = NMS.getGetter(EntityLiving.class, "jumping"); + private static final MethodHandle MAKE_REQUEST = NMS.getMethodHandle(YggdrasilAuthenticationService.class, + "makeRequest", true, URL.class, Object.class, Class.class); + private static final MethodHandle NAVIGATION_WORLD_FIELD = NMS.getSetter(NavigationAbstract.class, "b"); public static final Location PACKET_CACHE_LOCATION = new Location(null, 0, 0, 0); - private static Field PATHFINDING_RANGE = NMS.getField(NavigationAbstract.class, "p"); - private static final Field RABBIT_FIELD = NMS.getField(EntityRabbit.class, "bz"); + private static final MethodHandle PATHFINDING_RANGE = NMS.getGetter(NavigationAbstract.class, "p"); + private static final MethodHandle RABBIT_FIELD = NMS.getGetter(EntityRabbit.class, "bz"); private static final Random RANDOM = Util.getFastRandom(); - private static final Field SIZE_FIELD = NMS.getField(Entity.class, "size"); + private static final MethodHandle SIZE_FIELD_GETTER = NMS.getGetter(Entity.class, "size"); + private static final MethodHandle SIZE_FIELD_SETTER = NMS.getSetter(Entity.class, "size"); private static Field SKULL_PROFILE_FIELD; static { - try { Field field = NMS.getFinalField(IRegistry.class, "ENTITY_TYPE"); ENTITY_REGISTRY = new CustomEntityRegistry((RegistryBlocks>) field.get(null)); @@ -1688,13 +1679,5 @@ public class NMSImpl implements NMSBridge { } catch (Exception e) { Messaging.logTr(Messages.ERROR_GETTING_ID_MAPPING, e.getMessage()); } - - try { - MAKE_REQUEST = YggdrasilAuthenticationService.class.getDeclaredMethod("makeRequest", URL.class, - Object.class, Class.class); - MAKE_REQUEST.setAccessible(true); - } catch (Exception ex) { - ex.printStackTrace(); - } } } diff --git a/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/util/PlayerNavigation.java b/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/util/PlayerNavigation.java index 7c1e2329d..653a17c60 100644 --- a/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/util/PlayerNavigation.java +++ b/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/util/PlayerNavigation.java @@ -1,10 +1,8 @@ package net.citizensnpcs.nms.v1_14_R1.util; -import java.lang.reflect.Method; import java.util.Iterator; import net.citizensnpcs.nms.v1_14_R1.entity.EntityHumanNPC; -import net.citizensnpcs.util.NMS; import net.minecraft.server.v1_14_R1.AttributeInstance; import net.minecraft.server.v1_14_R1.Block; import net.minecraft.server.v1_14_R1.BlockPosition; @@ -17,7 +15,6 @@ import net.minecraft.server.v1_14_R1.GenericAttributes; import net.minecraft.server.v1_14_R1.IBlockAccess; import net.minecraft.server.v1_14_R1.IBlockData; import net.minecraft.server.v1_14_R1.MathHelper; -import net.minecraft.server.v1_14_R1.MethodProfiler; import net.minecraft.server.v1_14_R1.NavigationAbstract; import net.minecraft.server.v1_14_R1.PathEntity; import net.minecraft.server.v1_14_R1.PathMode; @@ -581,11 +578,4 @@ public class PlayerNavigation extends NavigationAbstract { return new EntityInsentient(EntityTypes.VILLAGER, world) { }; } - - private static long getMonotonicMillis() { - return SystemUtils.getMonotonicMillis(); - } - - private static final Method PROFILER_ENTER = NMS.getMethod(MethodProfiler.class, "a", false, String.class); - private static final Method PROFILER_EXIT = NMS.getMethod(MethodProfiler.class, "e", false); } diff --git a/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/util/PlayerlistTracker.java b/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/util/PlayerlistTracker.java index dbf72c5a4..084b73341 100644 --- a/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/util/PlayerlistTracker.java +++ b/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/util/PlayerlistTracker.java @@ -1,8 +1,6 @@ package net.citizensnpcs.nms.v1_14_R1.util; -import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; +import java.lang.invoke.MethodHandle; import org.bukkit.entity.Player; @@ -38,10 +36,8 @@ public class PlayerlistTracker extends PlayerChunkMap.EntityTracker { private int getA(PlayerChunkMap map2) { try { - return A.getInt(map2); - } catch (IllegalArgumentException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { + return (int) A.invoke(map2); + } catch (Throwable e) { e.printStackTrace(); } return 0; @@ -49,12 +45,8 @@ public class PlayerlistTracker extends PlayerChunkMap.EntityTracker { private int getb(ChunkCoordIntPair chunkcoordintpair, EntityPlayer entityplayer, boolean b) { try { - return (int) B.invoke(map, chunkcoordintpair, entityplayer, b); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } catch (IllegalArgumentException e) { - e.printStackTrace(); - } catch (InvocationTargetException e) { + return (int) B.invoke(chunkcoordintpair, entityplayer, b); + } catch (Throwable e) { e.printStackTrace(); } return 0; @@ -63,11 +55,7 @@ public class PlayerlistTracker extends PlayerChunkMap.EntityTracker { private PlayerChunk getVisibleChunk(long pair) { try { return (PlayerChunk) GET_VISIBLE_CHUNK.invoke(map, pair); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } catch (IllegalArgumentException e) { - e.printStackTrace(); - } catch (InvocationTargetException e) { + } catch (Throwable e) { e.printStackTrace(); } return null; @@ -107,10 +95,8 @@ public class PlayerlistTracker extends PlayerChunkMap.EntityTracker { private static int getD(EntityTracker entry) { try { - return D.getInt(TRACKER_ENTRY.get(entry)); - } catch (IllegalArgumentException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { + return (int) D.invoke(TRACKER_ENTRY.invoke(entry)); + } catch (Throwable e) { e.printStackTrace(); } return 0; @@ -118,10 +104,8 @@ public class PlayerlistTracker extends PlayerChunkMap.EntityTracker { private static boolean getE(EntityTracker entry) { try { - return E.getBoolean(TRACKER_ENTRY.get(entry)); - } catch (IllegalArgumentException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { + return (boolean) E.invoke(TRACKER_ENTRY.invoke(entry)); + } catch (Throwable e) { e.printStackTrace(); } return false; @@ -129,10 +113,8 @@ public class PlayerlistTracker extends PlayerChunkMap.EntityTracker { private static int getI(EntityTracker entry) { try { - return (Integer) I.get(entry); - } catch (IllegalArgumentException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { + return (Integer) I.invoke(entry); + } catch (Throwable e) { e.printStackTrace(); } return 0; @@ -140,10 +122,8 @@ public class PlayerlistTracker extends PlayerChunkMap.EntityTracker { private static Entity getTracker(EntityTracker entry) { try { - return (Entity) TRACKER.get(entry); - } catch (IllegalArgumentException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { + return (Entity) TRACKER.invoke(entry); + } catch (Throwable e) { e.printStackTrace(); } return null; @@ -151,23 +131,21 @@ public class PlayerlistTracker extends PlayerChunkMap.EntityTracker { private static EntityTrackerEntry getTrackerEntry(EntityTracker entry) { try { - return (EntityTrackerEntry) TRACKER_ENTRY.get(entry); - } catch (IllegalArgumentException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { + return (EntityTrackerEntry) TRACKER_ENTRY.invoke(entry); + } catch (Throwable e) { e.printStackTrace(); } return null; } - private static Field A = NMS.getField(PlayerChunkMap.class, "A"); - private static Method B = NMS.getMethod(PlayerChunkMap.class, "b", true, ChunkCoordIntPair.class, + private static final MethodHandle A = NMS.getGetter(PlayerChunkMap.class, "A"); + private static final MethodHandle B = NMS.getMethodHandle(PlayerChunkMap.class, "b", true, ChunkCoordIntPair.class, EntityPlayer.class, boolean.class); - private static Field D = NMS.getField(EntityTrackerEntry.class, "d"); - private static Field E = NMS.getField(EntityTrackerEntry.class, "e"); - private static final Method GET_VISIBLE_CHUNK = NMS.getMethod(PlayerChunkMap.class, "getVisibleChunk", true, - long.class); - private static Field I = NMS.getField(EntityTracker.class, "trackingDistance"); - private static Field TRACKER = NMS.getField(EntityTracker.class, "tracker"); - private static Field TRACKER_ENTRY = NMS.getField(EntityTracker.class, "trackerEntry"); + private static final MethodHandle D = NMS.getGetter(EntityTrackerEntry.class, "d"); + private static final MethodHandle E = NMS.getGetter(EntityTrackerEntry.class, "e"); + private static final MethodHandle GET_VISIBLE_CHUNK = NMS.getMethodHandle(PlayerChunkMap.class, "getVisibleChunk", + true, long.class); + private static final MethodHandle I = NMS.getGetter(EntityTracker.class, "trackingDistance"); + private static final MethodHandle TRACKER = NMS.getGetter(EntityTracker.class, "tracker"); + private static final MethodHandle TRACKER_ENTRY = NMS.getGetter(EntityTracker.class, "trackerEntry"); }