From 8b5f26f4b1ed30e5716b96bc4c9b9009589bee5c Mon Sep 17 00:00:00 2001 From: Evenprime Date: Sun, 25 Sep 2011 11:01:17 +0200 Subject: [PATCH] Find out what name the "isSprinting()" method has at runtime --- .../nocheat/checks/moving/FlyingCheck.java | 32 +++++++++++++++++- .../nocheat/checks/moving/RunningCheck.java | 33 ++++++++++++++++++- 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/src/cc/co/evenprime/bukkit/nocheat/checks/moving/FlyingCheck.java b/src/cc/co/evenprime/bukkit/nocheat/checks/moving/FlyingCheck.java index 3e237c89..8a33d822 100644 --- a/src/cc/co/evenprime/bukkit/nocheat/checks/moving/FlyingCheck.java +++ b/src/cc/co/evenprime/bukkit/nocheat/checks/moving/FlyingCheck.java @@ -1,8 +1,11 @@ package cc.co.evenprime.bukkit.nocheat.checks.moving; +import java.lang.reflect.Method; import java.util.HashMap; import java.util.Locale; +import net.minecraft.server.EntityPlayer; + import org.bukkit.GameMode; import org.bukkit.Location; import org.bukkit.craftbukkit.entity.CraftPlayer; @@ -27,6 +30,8 @@ public class FlyingCheck { private final ActionExecutor action; + private static Method isRunningMethod; + private static final double creativeSpeed = 0.60D; public FlyingCheck(NoCheat plugin) { @@ -55,7 +60,18 @@ public class FlyingCheck { result += Math.max(0.0D, horizontalDistance - data.horizFreedom - speedLimitHorizontal); - boolean sprinting = !(player instanceof CraftPlayer) || ((CraftPlayer)player).getHandle().at(); + if(isRunningMethod == null) { + isRunningMethod = getIsRunningMethod(); + } + + + boolean sprinting = true; + + try { + sprinting = !(player instanceof CraftPlayer) || isRunningMethod.invoke(((CraftPlayer) player).getHandle()).equals(true); + } catch(Exception e) { + e.printStackTrace(); + } data.bunnyhopdelay--; @@ -102,4 +118,18 @@ public class FlyingCheck { return newToLocation; } + + private Method getIsRunningMethod() { + try { + return EntityPlayer.class.getMethod("isSprinting"); + } catch(NoSuchMethodException e) { + try { + return EntityPlayer.class.getMethod("at"); + } catch(Exception e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + return null; + } + } + } } diff --git a/src/cc/co/evenprime/bukkit/nocheat/checks/moving/RunningCheck.java b/src/cc/co/evenprime/bukkit/nocheat/checks/moving/RunningCheck.java index a26e3dd6..14fdb0d6 100644 --- a/src/cc/co/evenprime/bukkit/nocheat/checks/moving/RunningCheck.java +++ b/src/cc/co/evenprime/bukkit/nocheat/checks/moving/RunningCheck.java @@ -1,8 +1,12 @@ package cc.co.evenprime.bukkit.nocheat.checks.moving; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.util.HashMap; import java.util.Locale; +import net.minecraft.server.EntityPlayer; + import org.bukkit.Location; import org.bukkit.craftbukkit.entity.CraftPlayer; import org.bukkit.entity.Player; @@ -27,6 +31,8 @@ import cc.co.evenprime.bukkit.nocheat.data.MovingData; public class RunningCheck { private final static double maxBonus = 1D; + + private static Method isRunningMethod = null; // How many move events can a player have in air before he is expected to // lose altitude (or eventually land somewhere) @@ -134,7 +140,18 @@ public class RunningCheck { // How much further did the player move than expected?? double distanceAboveLimit = 0.0D; - boolean sprinting = !(player instanceof CraftPlayer) || ((CraftPlayer) player).getHandle().at(); + if(isRunningMethod == null) { + isRunningMethod = getIsRunningMethod(); + } + + boolean sprinting = true; + try { + sprinting = !(player instanceof CraftPlayer) || isRunningMethod.invoke(((CraftPlayer) player).getHandle()).equals(true); + } catch(Exception e) { + e.printStackTrace(); + } + + System.out.println("IsSprinting "+sprinting); if(cc.moving.sneakingCheck && player.isSneaking() && !player.hasPermission(Permissions.MOVE_SNEAK)) { distanceAboveLimit = totalDistance - cc.moving.sneakingSpeedLimit - data.horizFreedom; @@ -197,4 +214,18 @@ public class RunningCheck { return distanceAboveLimit; } + + private Method getIsRunningMethod() { + try { + return EntityPlayer.class.getMethod("isSprinting"); + } catch(NoSuchMethodException e) { + try { + return EntityPlayer.class.getMethod("at"); + } catch(Exception e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + return null; + } + } + } }