From 9f071ab402a8cdab804626d58bfc19855e8a8e05 Mon Sep 17 00:00:00 2001 From: Jonas Konrad Date: Sat, 27 Dec 2014 16:48:21 +0100 Subject: Only allow finite x,y,z and yaw,pitch values for Vector and Location Many API methods rely on these to be finite (especially not NaN). --- src/main/java/org/bukkit/Location.java | 83 ++++++++++------- src/main/java/org/bukkit/util/BlockVector.java | 20 +--- src/main/java/org/bukkit/util/Vector.java | 122 +++++++++++-------------- 3 files changed, 110 insertions(+), 115 deletions(-) (limited to 'src') diff --git a/src/main/java/org/bukkit/Location.java b/src/main/java/org/bukkit/Location.java index 2a6b3942..21f98cb5 100644 --- a/src/main/java/org/bukkit/Location.java +++ b/src/main/java/org/bukkit/Location.java @@ -42,12 +42,12 @@ public class Location implements Cloneable, ConfigurationSerializable { * @param pitch The absolute rotation on the y-plane, in degrees */ public Location(final World world, final double x, final double y, final double z, final float yaw, final float pitch) { - this.world = world; - this.x = x; - this.y = y; - this.z = z; - this.pitch = pitch; - this.yaw = yaw; + setWorld(world); + setX(x); + setY(y); + setZ(z); + setPitch(pitch); + setYaw(yaw); } /** @@ -92,6 +92,7 @@ public class Location implements Cloneable, ConfigurationSerializable { * @param x X-coordinate */ public void setX(double x) { + checkFinite(x, "x must be finite"); this.x = x; } @@ -120,6 +121,7 @@ public class Location implements Cloneable, ConfigurationSerializable { * @param y y-coordinate */ public void setY(double y) { + checkFinite(y, "y must be finite"); this.y = y; } @@ -148,6 +150,7 @@ public class Location implements Cloneable, ConfigurationSerializable { * @param z z-coordinate */ public void setZ(double z) { + checkFinite(z, "z must be finite"); this.z = z; } @@ -185,6 +188,7 @@ public class Location implements Cloneable, ConfigurationSerializable { * @param yaw new rotation's yaw */ public void setYaw(float yaw) { + checkFiniteFloat(yaw, "yaw must be finite"); this.yaw = yaw; } @@ -219,6 +223,7 @@ public class Location implements Cloneable, ConfigurationSerializable { * @param pitch new incline's pitch */ public void setPitch(float pitch) { + checkFiniteFloat(pitch, "pitch must be finite"); this.pitch = pitch; } @@ -282,17 +287,17 @@ public class Location implements Cloneable, ConfigurationSerializable { final double z = vector.getZ(); if (x == 0 && z == 0) { - pitch = vector.getY() > 0 ? -90 : 90; + setPitch(vector.getY() > 0 ? -90 : 90); return this; } double theta = Math.atan2(-x, z); - yaw = (float) Math.toDegrees((theta + _2PI) % _2PI); + setYaw((float) Math.toDegrees((theta + _2PI) % _2PI)); double x2 = NumberConversions.square(x); double z2 = NumberConversions.square(z); double xz = Math.sqrt(x2 + z2); - pitch = (float) Math.toDegrees(Math.atan(-vector.getY() / xz)); + setPitch((float) Math.toDegrees(Math.atan(-vector.getY() / xz))); return this; } @@ -310,9 +315,9 @@ public class Location implements Cloneable, ConfigurationSerializable { throw new IllegalArgumentException("Cannot add Locations of differing worlds"); } - x += vec.x; - y += vec.y; - z += vec.z; + setX(getX() + vec.getX()); + setY(getY() + vec.getY()); + setZ(getZ() + vec.getZ()); return this; } @@ -324,9 +329,9 @@ public class Location implements Cloneable, ConfigurationSerializable { * @return the same location */ public Location add(Vector vec) { - this.x += vec.getX(); - this.y += vec.getY(); - this.z += vec.getZ(); + setX(getX() + vec.getX()); + setY(getY() + vec.getY()); + setZ(getZ() + vec.getZ()); return this; } @@ -340,9 +345,9 @@ public class Location implements Cloneable, ConfigurationSerializable { * @return the same location */ public Location add(double x, double y, double z) { - this.x += x; - this.y += y; - this.z += z; + setX(getX() + x); + setY(getY() + y); + setZ(getZ() + z); return this; } @@ -359,9 +364,9 @@ public class Location implements Cloneable, ConfigurationSerializable { throw new IllegalArgumentException("Cannot add Locations of differing worlds"); } - x -= vec.x; - y -= vec.y; - z -= vec.z; + setX(getX() - vec.getX()); + setY(getY() - vec.getY()); + setZ(getZ() - vec.getZ()); return this; } @@ -373,9 +378,9 @@ public class Location implements Cloneable, ConfigurationSerializable { * @return the same location */ public Location subtract(Vector vec) { - this.x -= vec.getX(); - this.y -= vec.getY(); - this.z -= vec.getZ(); + setX(getX() - vec.getX()); + setY(getY() - vec.getY()); + setZ(getZ() - vec.getZ()); return this; } @@ -390,9 +395,9 @@ public class Location implements Cloneable, ConfigurationSerializable { * @return the same location */ public Location subtract(double x, double y, double z) { - this.x -= x; - this.y -= y; - this.z -= z; + setX(getX() - x); + setY(getY() - y); + setZ(getZ() - z); return this; } @@ -467,9 +472,9 @@ public class Location implements Cloneable, ConfigurationSerializable { * @return the same location */ public Location multiply(double m) { - x *= m; - y *= m; - z *= m; + setX(getX() * m); + setY(getY() * m); + setZ(getZ() * m); return this; } @@ -480,9 +485,9 @@ public class Location implements Cloneable, ConfigurationSerializable { * @return the same location */ public Location zero() { - x = 0; - y = 0; - z = 0; + setX(0D); + setY(0D); + setZ(0D); return this; } @@ -596,4 +601,16 @@ public class Location implements Cloneable, ConfigurationSerializable { return new Location(world, NumberConversions.toDouble(args.get("x")), NumberConversions.toDouble(args.get("y")), NumberConversions.toDouble(args.get("z")), NumberConversions.toFloat(args.get("yaw")), NumberConversions.toFloat(args.get("pitch"))); } + + private static void checkFinite(double d, String message) { + if (Double.isNaN(d) || Double.isInfinite(d)) { + throw new IllegalArgumentException(message); + } + } + + private static void checkFiniteFloat(float d, String message) { + if (Float.isNaN(d) || Float.isInfinite(d)) { + throw new IllegalArgumentException(message); + } + } } diff --git a/src/main/java/org/bukkit/util/BlockVector.java b/src/main/java/org/bukkit/util/BlockVector.java index bdf8f6d5..9f5bef22 100644 --- a/src/main/java/org/bukkit/util/BlockVector.java +++ b/src/main/java/org/bukkit/util/BlockVector.java @@ -16,9 +16,7 @@ public class BlockVector extends Vector { * Construct the vector with all components as 0. */ public BlockVector() { - this.x = 0; - this.y = 0; - this.z = 0; + super(); } /** @@ -27,9 +25,7 @@ public class BlockVector extends Vector { * @param vec The other vector. */ public BlockVector(Vector vec) { - this.x = vec.getX(); - this.y = vec.getY(); - this.z = vec.getZ(); + this(vec.getX(), vec.getY(), vec.getZ()); } /** @@ -40,9 +36,7 @@ public class BlockVector extends Vector { * @param z Z component */ public BlockVector(int x, int y, int z) { - this.x = x; - this.y = y; - this.z = z; + super(x, y, z); } /** @@ -53,9 +47,7 @@ public class BlockVector extends Vector { * @param z Z component */ public BlockVector(double x, double y, double z) { - this.x = x; - this.y = y; - this.z = z; + super(x, y, z); } /** @@ -66,9 +58,7 @@ public class BlockVector extends Vector { * @param z Z component */ public BlockVector(float x, float y, float z) { - this.x = x; - this.y = y; - this.z = z; + super(x, y, z); } /** diff --git a/src/main/java/org/bukkit/util/Vector.java b/src/main/java/org/bukkit/util/Vector.java index 61116ea3..7cd60481 100644 --- a/src/main/java/org/bukkit/util/Vector.java +++ b/src/main/java/org/bukkit/util/Vector.java @@ -33,9 +33,7 @@ public class Vector implements Cloneable, ConfigurationSerializable { * Construct the vector with all components as 0. */ public Vector() { - this.x = 0; - this.y = 0; - this.z = 0; + this(0, 0, 0); } /** @@ -46,9 +44,7 @@ public class Vector implements Cloneable, ConfigurationSerializable { * @param z Z component */ public Vector(int x, int y, int z) { - this.x = x; - this.y = y; - this.z = z; + this((double) x, (double) y, (double) z); } /** @@ -59,9 +55,10 @@ public class Vector implements Cloneable, ConfigurationSerializable { * @param z Z component */ public Vector(double x, double y, double z) { - this.x = x; - this.y = y; - this.z = z; + // use setters for range checks + setX(x); + setY(y); + setZ(z); } /** @@ -72,9 +69,7 @@ public class Vector implements Cloneable, ConfigurationSerializable { * @param z Z component */ public Vector(float x, float y, float z) { - this.x = x; - this.y = y; - this.z = z; + this((double) x, (double) y, (double) z); } /** @@ -84,9 +79,9 @@ public class Vector implements Cloneable, ConfigurationSerializable { * @return the same vector */ public Vector add(Vector vec) { - x += vec.x; - y += vec.y; - z += vec.z; + setX(getX() + vec.getX()); + setY(getY() + vec.getY()); + setZ(getZ() + vec.getZ()); return this; } @@ -97,9 +92,9 @@ public class Vector implements Cloneable, ConfigurationSerializable { * @return the same vector */ public Vector subtract(Vector vec) { - x -= vec.x; - y -= vec.y; - z -= vec.z; + setX(getX() - vec.getX()); + setY(getY() - vec.getY()); + setZ(getZ() - vec.getZ()); return this; } @@ -110,9 +105,9 @@ public class Vector implements Cloneable, ConfigurationSerializable { * @return the same vector */ public Vector multiply(Vector vec) { - x *= vec.x; - y *= vec.y; - z *= vec.z; + setX(getX() * vec.getX()); + setY(getY() * vec.getY()); + setZ(getZ() * vec.getZ()); return this; } @@ -123,9 +118,9 @@ public class Vector implements Cloneable, ConfigurationSerializable { * @return the same vector */ public Vector divide(Vector vec) { - x /= vec.x; - y /= vec.y; - z /= vec.z; + setX(getX() / vec.getX()); + setY(getY() / vec.getY()); + setZ(getZ() / vec.getZ()); return this; } @@ -136,9 +131,9 @@ public class Vector implements Cloneable, ConfigurationSerializable { * @return the same vector */ public Vector copy(Vector vec) { - x = vec.x; - y = vec.y; - z = vec.z; + setX(vec.getX()); + setY(vec.getY()); + setZ(vec.getZ()); return this; } @@ -207,9 +202,9 @@ public class Vector implements Cloneable, ConfigurationSerializable { * @return this same vector (now a midpoint) */ public Vector midpoint(Vector other) { - x = (x + other.x) / 2; - y = (y + other.y) / 2; - z = (z + other.z) / 2; + setX((getX() + other.getX()) / 2); + setY((getY() + other.getY()) / 2); + setZ((getZ() + other.getZ()) / 2); return this; } @@ -234,10 +229,7 @@ public class Vector implements Cloneable, ConfigurationSerializable { * @return the same vector */ public Vector multiply(int m) { - x *= m; - y *= m; - z *= m; - return this; + return multiply((double) m); } /** @@ -248,9 +240,9 @@ public class Vector implements Cloneable, ConfigurationSerializable { * @return the same vector */ public Vector multiply(double m) { - x *= m; - y *= m; - z *= m; + setX(getX() * m); + setY(getY() * m); + setZ(getZ() * m); return this; } @@ -262,10 +254,7 @@ public class Vector implements Cloneable, ConfigurationSerializable { * @return the same vector */ public Vector multiply(float m) { - x *= m; - y *= m; - z *= m; - return this; + return multiply((double) m); } /** @@ -292,13 +281,13 @@ public class Vector implements Cloneable, ConfigurationSerializable { * @return the same vector */ public Vector crossProduct(Vector o) { - double newX = y * o.z - o.y * z; - double newY = z * o.x - o.z * x; - double newZ = x * o.y - o.x * y; + double newX = getY() * o.getZ() - o.getY() * getZ(); + double newY = getZ() * o.getX() - o.getZ() * getX(); + double newZ = getX() * o.getY() - o.getX() * getY(); - x = newX; - y = newY; - z = newZ; + setX(newX); + setY(newY); + setZ(newZ); return this; } @@ -310,11 +299,7 @@ public class Vector implements Cloneable, ConfigurationSerializable { public Vector normalize() { double length = length(); - x /= length; - y /= length; - z /= length; - - return this; + return multiply(1 / length); } /** @@ -323,9 +308,9 @@ public class Vector implements Cloneable, ConfigurationSerializable { * @return the same vector */ public Vector zero() { - x = 0; - y = 0; - z = 0; + setX(0D); + setY(0D); + setZ(0D); return this; } @@ -418,8 +403,7 @@ public class Vector implements Cloneable, ConfigurationSerializable { * @return This vector. */ public Vector setX(int x) { - this.x = x; - return this; + return setX((double) x); } /** @@ -429,6 +413,7 @@ public class Vector implements Cloneable, ConfigurationSerializable { * @return This vector. */ public Vector setX(double x) { + checkFinite(x, "x must be finite"); this.x = x; return this; } @@ -440,8 +425,7 @@ public class Vector implements Cloneable, ConfigurationSerializable { * @return This vector. */ public Vector setX(float x) { - this.x = x; - return this; + return setX((double) x); } /** @@ -451,8 +435,7 @@ public class Vector implements Cloneable, ConfigurationSerializable { * @return This vector. */ public Vector setY(int y) { - this.y = y; - return this; + return setY((double) y); } /** @@ -462,6 +445,7 @@ public class Vector implements Cloneable, ConfigurationSerializable { * @return This vector. */ public Vector setY(double y) { + checkFinite(y, "y must be finite"); this.y = y; return this; } @@ -473,8 +457,7 @@ public class Vector implements Cloneable, ConfigurationSerializable { * @return This vector. */ public Vector setY(float y) { - this.y = y; - return this; + return setY((double) y); } /** @@ -484,8 +467,7 @@ public class Vector implements Cloneable, ConfigurationSerializable { * @return This vector. */ public Vector setZ(int z) { - this.z = z; - return this; + return setZ((double) z); } /** @@ -495,6 +477,7 @@ public class Vector implements Cloneable, ConfigurationSerializable { * @return This vector. */ public Vector setZ(double z) { + checkFinite(z, "z must be finite"); this.z = z; return this; } @@ -506,8 +489,7 @@ public class Vector implements Cloneable, ConfigurationSerializable { * @return This vector. */ public Vector setZ(float z) { - this.z = z; - return this; + return setZ((double) z); } /** @@ -664,4 +646,10 @@ public class Vector implements Cloneable, ConfigurationSerializable { return new Vector(x, y, z); } + + private static void checkFinite(double d, String message) { + if (Double.isNaN(d) || Double.isInfinite(d)) { + throw new IllegalArgumentException(message); + } + } } -- cgit v1.2.3