From ba5f710c8351bd355f29f378d5f0d62e15e538bb Mon Sep 17 00:00:00 2001 From: asofold Date: Tue, 22 Nov 2016 21:04:59 +0100 Subject: [PATCH] Fix pre-checks for getHeight, use fail() rather. --- .../cbreflect/reflect/ReflectHelper.java | 36 ++++++++++--------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/NCPCompatBukkit/src/main/java/fr/neatmonster/nocheatplus/compat/cbreflect/reflect/ReflectHelper.java b/NCPCompatBukkit/src/main/java/fr/neatmonster/nocheatplus/compat/cbreflect/reflect/ReflectHelper.java index e7329d28..58f61278 100644 --- a/NCPCompatBukkit/src/main/java/fr/neatmonster/nocheatplus/compat/cbreflect/reflect/ReflectHelper.java +++ b/NCPCompatBukkit/src/main/java/fr/neatmonster/nocheatplus/compat/cbreflect/reflect/ReflectHelper.java @@ -157,7 +157,8 @@ public class ReflectHelper { } /** - * Quick fail with exception. + * Quick fail with exception. Used both for setup and runtime. + * @throws ReflectFailureException Always. */ private void fail() { throw new ReflectFailureException(); @@ -357,14 +358,15 @@ public class ReflectHelper { public double getWidth(final Entity entity) { float width = -16f; - if (reflectEntity.nmsWidth != null) { - final Object handle = reflectEntity.getHandle(entity); - if (handle != null) { - width = ReflectionUtil.getFloat(reflectEntity.nmsWidth, handle, width); - } + if (reflectEntity.nmsWidth == null) { + fail(); + } + final Object handle = reflectEntity.getHandle(entity); + if (handle != null) { + width = ReflectionUtil.getFloat(reflectEntity.nmsWidth, handle, width); } if (width < 0f) { - throw new ReflectFailureException(); + fail(); } return (double) width; } @@ -372,15 +374,17 @@ public class ReflectHelper { public double getHeight(final Entity entity) { float floatHeight = -16f; final Object handle = reflectEntity.getHandle(entity); // TODO: Distinguish classes (living vs not)? - if (handle != null) { - if (reflectEntity.nmsLength != null) { - floatHeight = Math.max(ReflectionUtil.getFloat(reflectEntity.nmsLength, handle, floatHeight), floatHeight); - } - if (reflectEntity.nmsHeight != null) { - floatHeight = Math.max(ReflectionUtil.getFloat(reflectEntity.nmsHeight, handle, floatHeight), floatHeight); - } + double height; + if (handle == null) { + fail(); } - double height = (double) floatHeight; + if (reflectEntity.nmsLength != null) { + floatHeight = Math.max(ReflectionUtil.getFloat(reflectEntity.nmsLength, handle, floatHeight), floatHeight); + } + if (reflectEntity.nmsHeight != null) { + floatHeight = Math.max(ReflectionUtil.getFloat(reflectEntity.nmsHeight, handle, floatHeight), floatHeight); + } + height = (double) floatHeight; // TODO: Consider dropping the box for performance? if (reflectAxisAlignedBB != null && reflectEntity.nmsGetBoundingBox != null) { final Object box = ReflectionUtil.invokeMethodNoArgs(reflectEntity.nmsGetBoundingBox, handle); @@ -394,7 +398,7 @@ public class ReflectHelper { } } if (height < 0.0) { - throw new ReflectFailureException(); + fail(); } // On success only: Check eye height (MCAccessBukkit is better than just eye height.). if (entity instanceof LivingEntity) {