[BLEEDING] Double trouble potential: NaN, infinity.

+ random Formatting + random fix (same angle checked twice in TrigUtil).
This commit is contained in:
asofold 2016-12-27 15:10:08 +01:00
parent 17db0bdcfe
commit d69dc0b44d
13 changed files with 88 additions and 49 deletions

View File

@ -129,7 +129,7 @@ public class FastBreak extends Check {
final ItemStack stack = Bridge1_9.getItemInMainHand(player);
final boolean isValidTool = BlockProperties.isValidTool(blockType, stack);
final double haste = PotionUtil.getPotionEffectAmplifier(player, PotionEffectType.FAST_DIGGING);
String msg = (isInstaBreak.decideOptimistically() ? ("[Insta=" + isInstaBreak + "]") : "[Normal]") + "[" + blockType + "] "+ elapsedTime + "u / " + expectedBreakingTime +"r (" + (isValidTool?"tool":"no-tool") + ")" + (haste == Double.NEGATIVE_INFINITY ? "" : " haste=" + ((int) haste + 1));
String msg = (isInstaBreak.decideOptimistically() ? ("[Insta=" + isInstaBreak + "]") : "[Normal]") + "[" + blockType + "] "+ elapsedTime + "u / " + expectedBreakingTime +"r (" + (isValidTool?"tool":"no-tool") + ")" + (Double.isInfinite(haste) ? "" : " haste=" + ((int) haste + 1));
player.sendMessage(msg);
// net.minecraft.server.Item mcItem = net.minecraft.server.Item.byId[stack.getTypeId()];
// if (mcItem != null) {

View File

@ -400,7 +400,7 @@ public class MovingConfig extends ACheckConfig {
// TODO: INCONSISTENT.
return modelGameMode;
}
if (Bridge1_9.getLevitationAmplifier(player) != Double.NEGATIVE_INFINITY) {
if (!Double.isInfinite(Bridge1_9.getLevitationAmplifier(player))) {
return flyingModelLevitation;
}
if (fromLocation.isOnGroundOrResetCond()) {

View File

@ -2255,14 +2255,14 @@ public class MovingListener extends CheckListener implements TickListener, IRemo
builder.append("(svel=" + v.getX() + "," + v.getY() + "," + v.getZ() + ")");
}
}
if (speed != Double.NEGATIVE_INFINITY) {
if (!Double.isInfinite(speed)) {
builder.append("(e_speed=" + (speed + 1) + ")");
}
final double slow = PotionUtil.getPotionEffectAmplifier(player, PotionEffectType.SLOW);
if (slow != Double.NEGATIVE_INFINITY) {
if (Double.isInfinite(slow)) {
builder.append("(e_slow=" + (slow + 1) + ")");
}
if (jump != Double.NEGATIVE_INFINITY) {
if (Double.isInfinite(jump)) {
builder.append("(e_jump=" + (jump + 1) + ")");
}
if (strider != 0) {

View File

@ -252,7 +252,7 @@ public class LocUtil {
* @return
*/
public static final boolean needsYawCorrection(final float yaw) {
return yaw == Float.NaN || yaw < 0f || yaw >= 360f;
return Float.isNaN(yaw) || yaw < 0f || yaw >= 360f;
}
/**
@ -262,7 +262,7 @@ public class LocUtil {
* @return
*/
public static final boolean needsPitchCorrection(final float pitch) {
return pitch == Float.NaN || pitch < -90f || pitch > 90f;
return Float.isNaN(pitch) || pitch < -90f || pitch > 90f;
}
/**
@ -283,7 +283,7 @@ public class LocUtil {
* @return
*/
public static final float correctYaw(float yaw) {
if (yaw == Float.NaN) {
if (Float.isNaN(yaw)) {
return 0f;
}
if (yaw >= 360f) {
@ -314,7 +314,7 @@ public class LocUtil {
* @return
*/
public static final float correctPitch(float pitch) {
if (pitch == Float.NaN) {
if (Float.isNaN(pitch)) {
return 0f;
} else if (pitch < -90f) {
return -90f;

View File

@ -274,7 +274,7 @@ public class CreativeFly extends Check {
// TODO: Make this configurable ! [Speed effect should not affect flying if not on ground.]
if (model.applyModifiers) {
final double speedModifier = mcAccess.getHandle().getFasterMovementAmplifier(player);
if (speedModifier == Double.NEGATIVE_INFINITY) {
if (Double.isInfinite(speedModifier)) {
fSpeed = 1.0;
}
else {

View File

@ -865,7 +865,7 @@ public class SurvivalFly extends Check {
// TODO: Slowness potion.
// Count in speed potions.
final double speedAmplifier = mcAccess.getHandle().getFasterMovementAmplifier(player);
if (speedAmplifier != Double.NEGATIVE_INFINITY) {
if (!Double.isInfinite(speedAmplifier)) {
hAllowedDistance *= 1.0D + 0.2D * (speedAmplifier + 1);
}
}

View File

@ -73,7 +73,7 @@ public class MovingUtil {
&& (cc.ignoreCreative || gameMode != GameMode.CREATIVE) && !player.isFlying()
&& (cc.ignoreAllowFlight || !player.getAllowFlight())
&& !NCPExemptionManager.isExempted(player, CheckType.MOVING_SURVIVALFLY)
&& (Bridge1_9.getLevitationAmplifier(player) == Double.NEGATIVE_INFINITY || fromLocation.isInLiquid())
&& (Double.isInfinite(Bridge1_9.getLevitationAmplifier(player)) || fromLocation.isInLiquid())
&& (!Bridge1_9.isGlidingWithElytra(player) || fromLocation.isOnGroundOrResetCond())
&& !player.hasPermission(Permissions.MOVING_SURVIVALFLY);
}
@ -262,7 +262,7 @@ public class MovingUtil {
public static double getJumpAmplifier(final Player player, final MCAccess mcAccess) {
final double amplifier = mcAccess.getJumpAmplifier(player);
if (amplifier == Double.NEGATIVE_INFINITY) {
if (Double.isInfinite(amplifier)) {
return 0.0;
}
else {

View File

@ -117,7 +117,7 @@ public class CheckUtils {
}
/**
* Check for NaN, infinity.
* Check for NaN, infinity, some random maximum coordinate.
*
* @param doubles
* the doubles

View File

@ -28,7 +28,7 @@ import org.bukkit.potion.PotionEffectType;
*/
public class PotionUtil {
/**
/**
* Get amplifier for a potion effect.
*
* @param player
@ -38,16 +38,18 @@ public class PotionUtil {
* @return Double.NEGATIVE_INFINITY if not present, otherwise the maximal
* amplifier.
*/
public static final double getPotionEffectAmplifier(final Player player, final PotionEffectType type) {
if (!player.hasPotionEffect(type)) return Double.NEGATIVE_INFINITY; // TODO: Might not win anything.
final Collection<PotionEffect> effects = player.getActivePotionEffects();
double max = Double.NEGATIVE_INFINITY;
for (final PotionEffect effect : effects){
if (effect.getType().equals(type)){
max = Math.max(max, effect.getAmplifier());
}
}
return max;
}
public static final double getPotionEffectAmplifier(final Player player, final PotionEffectType type) {
if (!player.hasPotionEffect(type)) {
return Double.NEGATIVE_INFINITY; // TODO: Might not win anything.
}
final Collection<PotionEffect> effects = player.getActivePotionEffects();
double max = Double.NEGATIVE_INFINITY;
for (final PotionEffect effect : effects){
if (effect.getType().equals(type)){
max = Math.max(max, effect.getAmplifier());
}
}
return max;
}
}

View File

@ -95,7 +95,7 @@ public class CollideRayVsAABB implements ICollideRayVsAABB {
final double tMaxY = CollisionUtil.getMaxTimeIncludeEdges(startY, dirY, minY, maxY, tMinY);
final double tMaxZ = CollisionUtil.getMaxTimeIncludeEdges(startZ, dirZ, minZ, maxZ, tMinZ);
//System.out.println("TIMING: " + tMinX + " " + tMinY + " " + tMinZ + " " + tMaxX + " " + tMaxY + " " + tMaxZ);
if (tMaxX != Double.NaN && tMaxY != Double.NaN && tMaxZ != Double.NaN) {
if (!Double.isNaN(tMaxX) && !Double.isNaN(tMaxY) && !Double.isNaN(tMaxZ)) {
// (Excludes any tMin value to be Double.MAX_VALUE.)
// Determine if there is overlapping intervals.
final double tMin = Math.max(tMinX, Math.max(tMinY, tMinZ));
@ -137,7 +137,7 @@ public class CollideRayVsAABB implements ICollideRayVsAABB {
// Find the closest point using set time values.
for (int i = 0; i < timeValues.length; i++) {
final double time = timeValues[i];
if (time == Double.NaN || time == Double.POSITIVE_INFINITY) {
if (Double.isNaN(time) || Double.isInfinite(time)) {
// Note that Double.POSITIVE_INFINITY could mean that we are either colliding forever, or never.
continue;
}

View File

@ -371,7 +371,7 @@ public class CollisionUtil {
*/
public static double getMaxTimeIncludeEdges(final double pos, final double dir,
final double minPos, final double maxPos, final double minTime) {
if (minTime == Double.POSITIVE_INFINITY) {
if (Double.isInfinite(minTime)) {
return Double.NaN;
}
else if (dir == 0.0) {

View File

@ -312,7 +312,9 @@ public class TrigUtil {
*/
public static float angle(final double sourceX, final double sourceY, final double sourceZ, final double dirX, final double dirY, final double dirZ, final double targetX, final double targetY, final double targetZ) {
double dirLength = Math.sqrt(dirX * dirX + dirY * dirY + dirZ * dirZ);
if (dirLength == 0.0) dirLength = 1.0; // ...
if (dirLength == 0.0) {
dirLength = 1.0; // ...
}
final double dX = targetX - sourceX;
final double dY = targetY - sourceY;
@ -338,15 +340,29 @@ public class TrigUtil {
*/
public static final double angle(final double x, final double z){
final double a;
if (x > 0.0) a = Math.atan(z / x);
else if (x < 0.0) a = Math.atan(z / x) + Math.PI;
else{
if (z < 0.0) a=3.0 * Math.PI / 2.0;
else if (z > 0.0) a = Math.PI / 2.0;
else return Double.NaN;
if (x > 0.0) {
a = Math.atan(z / x);
}
else if (x < 0.0) {
a = Math.atan(z / x) + Math.PI;
}
else{
if (z < 0.0) {
a = 3.0 * Math.PI / 2.0;
}
else if (z > 0.0) {
a = Math.PI / 2.0;
}
else {
return Double.NaN;
}
}
if (a < 0.0) {
return a + 2.0 * Math.PI;
}
else {
return a;
}
if (a < 0.0) return a + 2.0 * Math.PI;
else return a;
}
/**
@ -360,11 +376,20 @@ public class TrigUtil {
* @return Difference of angle from -pi to pi
*/
public static final double angleDiff(final double a1, final double a2){
if (Double.isNaN(a1) || Double.isNaN(a1)) return Double.NaN;
if (Double.isNaN(a1) || Double.isNaN(a2)) {
return Double.NaN;
}
final double diff = a2 - a1;
if (diff < -Math.PI) return diff + 2.0 * Math.PI;
else if (diff > Math.PI) return diff - 2.0 * Math.PI;
else return diff;
// TODO: What with resulting special values here?
if (diff < -Math.PI) {
return diff + 2.0 * Math.PI;
}
else if (diff > Math.PI) {
return diff - 2.0 * Math.PI;
}
else {
return diff;
}
}
/**
@ -379,13 +404,25 @@ public class TrigUtil {
* [-180, 180].
*/
public static final float yawDiff(float fromYaw, float toYaw){
if (fromYaw <= -360f) fromYaw = -((-fromYaw) % 360f);
else if (fromYaw >= 360f) fromYaw = fromYaw % 360f;
if (toYaw <= -360f) toYaw = -((-toYaw) % 360f);
else if (toYaw >= 360f) toYaw = toYaw % 360f;
if (fromYaw <= -360f) {
fromYaw = -((-fromYaw) % 360f);
}
else if (fromYaw >= 360f) {
fromYaw = fromYaw % 360f;
}
if (toYaw <= -360f) {
toYaw = -((-toYaw) % 360f);
}
else if (toYaw >= 360f) {
toYaw = toYaw % 360f;
}
float yawDiff = toYaw - fromYaw;
if (yawDiff < -180f) yawDiff += 360f;
else if (yawDiff > 180f) yawDiff -= 360f;
if (yawDiff < -180f) {
yawDiff += 360f;
}
else if (yawDiff > 180f) {
yawDiff -= 360f;
}
return yawDiff;
}

View File

@ -1405,7 +1405,7 @@ public class BlockProperties {
// Haste (faster digging).
final double haste = PotionUtil.getPotionEffectAmplifier(player, PotionEffectType.FAST_DIGGING);
// TODO: haste: int / double !?
return getBreakingDuration(blockId, itemInHand, onGround, inWater, helmet != null && helmet.containsEnchantment(Enchantment.WATER_WORKER), haste == Double.NEGATIVE_INFINITY ? 0 : 1 + (int) haste);
return getBreakingDuration(blockId, itemInHand, onGround, inWater, helmet != null && helmet.containsEnchantment(Enchantment.WATER_WORKER), Double.isInfinite(haste) ? 0 : 1 + (int) haste);
}
/**