From ece931202b21513d5c0ad88423609904fcc8bfe0 Mon Sep 17 00:00:00 2001 From: Phill310 Date: Fri, 31 May 2024 04:33:58 -0700 Subject: [PATCH] Adds checks to not normalize zero vectors (#6642) * Adds checks to not normalize zero vectors * Make custom isZero method to support pre 1.19.3 versions * Check isZero before lengthSquared * Add a regression test * Fix assert statements --------- Co-authored-by: Moderocky --- .../ch/njol/skript/expressions/ExprVectorLength.java | 5 +++-- .../ch/njol/skript/expressions/ExprVectorNormalize.java | 3 ++- src/main/java/ch/njol/util/VectorMath.java | 9 +++++++++ src/test/skript/tests/regressions/6194-vectors + push.sk | 9 +++++++++ 4 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 src/test/skript/tests/regressions/6194-vectors + push.sk diff --git a/src/main/java/ch/njol/skript/expressions/ExprVectorLength.java b/src/main/java/ch/njol/skript/expressions/ExprVectorLength.java index 62f2a695e85..3b1ff35b6d9 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprVectorLength.java +++ b/src/main/java/ch/njol/skript/expressions/ExprVectorLength.java @@ -18,6 +18,7 @@ */ package ch.njol.skript.expressions; +import ch.njol.util.VectorMath; import org.bukkit.event.Event; import org.bukkit.util.Vector; import org.eclipse.jdt.annotation.Nullable; @@ -70,7 +71,7 @@ public void change(Event event, @Nullable Object[] delta, ChangeMode mode) { //$FALL-THROUGH$ case ADD: for (Vector vector : vectors) { - if (deltaLength < 0 && vector.lengthSquared() < deltaLength * deltaLength) { + if (VectorMath.isZero(vector) || (deltaLength < 0 && vector.lengthSquared() < deltaLength * deltaLength)) { vector.zero(); } else { double newLength = deltaLength + vector.length(); @@ -82,7 +83,7 @@ public void change(Event event, @Nullable Object[] delta, ChangeMode mode) { break; case SET: for (Vector vector : vectors) { - if (deltaLength < 0) { + if (deltaLength < 0 || VectorMath.isZero(vector)) { vector.zero(); } else { if (!vector.isNormalized()) diff --git a/src/main/java/ch/njol/skript/expressions/ExprVectorNormalize.java b/src/main/java/ch/njol/skript/expressions/ExprVectorNormalize.java index db136199bbb..94dca461a5a 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprVectorNormalize.java +++ b/src/main/java/ch/njol/skript/expressions/ExprVectorNormalize.java @@ -18,6 +18,7 @@ */ package ch.njol.skript.expressions; +import ch.njol.util.VectorMath; import org.bukkit.event.Event; import org.bukkit.util.Vector; import org.eclipse.jdt.annotation.Nullable; @@ -63,7 +64,7 @@ protected Vector[] get(Event event) { if (vector == null) return null; vector = vector.clone(); - if (!vector.isNormalized()) + if (!VectorMath.isZero(vector) && !vector.isNormalized()) vector.normalize(); return CollectionUtils.array(vector); } diff --git a/src/main/java/ch/njol/util/VectorMath.java b/src/main/java/ch/njol/util/VectorMath.java index fca9785dc6a..5db05ecc41b 100644 --- a/src/main/java/ch/njol/util/VectorMath.java +++ b/src/main/java/ch/njol/util/VectorMath.java @@ -198,4 +198,13 @@ public static void copyVector(Vector vector1, Vector vector2) { vector1.setX(vector2.getX()).setY(vector2.getY()).setZ(vector2.getZ()); } + /** + * Check whether or not each component of this vector is equal to 0. + *
Replaces {@code Vector#isZero()} since that method was added in spigot 1.19.3 + * @return true if equal to zero, false if at least one component is non-zero + */ + public static boolean isZero(Vector vector) { + return (vector.getX() == 0 && vector.getY() == 0 && vector.getZ() == 0); + } + } diff --git a/src/test/skript/tests/regressions/6194-vectors + push.sk b/src/test/skript/tests/regressions/6194-vectors + push.sk new file mode 100644 index 00000000000..258b091fba6 --- /dev/null +++ b/src/test/skript/tests/regressions/6194-vectors + push.sk @@ -0,0 +1,9 @@ +test "normalize zero vector": + set {_vector} to vector(0,0,0) + add 1 to vector length of {_vector} + assert {_vector} is vector(0,0,0) with "add to zero vector's length failed" + + set vector length of {_vector} to 1 + assert {_vector} is vector(0,0,0) with "set zero vector's length failed" + + assert normalize {_vector} is vector(0,0,0) with "normalize zero vector failed" \ No newline at end of file