Find out what name the "isSprinting()" method has at runtime

This commit is contained in:
Evenprime 2011-09-25 11:01:17 +02:00
parent dad338db65
commit 8b5f26f4b1
2 changed files with 63 additions and 2 deletions

View File

@ -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;
}
}
}
}

View File

@ -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;
}
}
}
}