Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds checks to not normalize zero vectors #6642

Merged
merged 8 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 ((deltaLength < 0 && vector.lengthSquared() < deltaLength * deltaLength) || VectorMath.isZero(vector)) {
Phill310 marked this conversation as resolved.
Show resolved Hide resolved
vector.zero();
} else {
double newLength = deltaLength + vector.length();
Expand All @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/ch/njol/util/VectorMath.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* <br>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);
}

}