#809: Throw a more clear error for BlockIterators with zero direction, add Vector#isZero()

By: Parker Hawke <hawkeboyz2@hotmail.com>
This commit is contained in:
Bukkit/Spigot 2023-02-11 09:44:56 +11:00
parent e17edb7785
commit d6d7c1a64f
3 changed files with 25 additions and 0 deletions

View file

@ -1,6 +1,7 @@
package org.bukkit.util;
import static org.bukkit.util.NumberConversions.*;
import com.google.common.base.Preconditions;
import java.util.Iterator;
import java.util.NoSuchElementException;
import org.bukkit.Location;
@ -53,6 +54,11 @@ public class BlockIterator implements Iterator<Block> {
*
*/
public BlockIterator(@NotNull World world, @NotNull Vector start, @NotNull Vector direction, double yOffset, int maxDistance) {
Preconditions.checkArgument(world != null, "world must not be null");
Preconditions.checkArgument(start != null, "start must not be null");
Preconditions.checkArgument(direction != null, "direction must not be null");
Preconditions.checkArgument(!direction.isZero(), "direction must have at least one non-zero component");
this.world = world;
this.maxDistance = maxDistance;

View file

@ -365,6 +365,15 @@ public class Vector implements Cloneable, ConfigurationSerializable {
return this;
}
/**
* Check whether or not each component of this vector is equal to 0.
*
* @return true if equal to zero, false if at least one component is non-zero
*/
public boolean isZero() {
return x == 0 && y == 0 && z == 0;
}
/**
* Converts each component of value <code>-0.0</code> to <code>0.0</code>.
*

View file

@ -122,4 +122,14 @@ public class VectorTest {
assertTrue(Double.isFinite(a.angle(b)));
}
@Test
public void testIsZero() {
assertTrue(new Vector().isZero());
assertTrue(new Vector(0, 0, 0).isZero());
Vector vector = new Vector(1, 2, 3);
vector.zero();
assertTrue(vector.isZero());
}
}