From 43228b1138dc90d8c246b2eecb5b04dc2fefba66 Mon Sep 17 00:00:00 2001 From: theEvilReaper Date: Wed, 22 Jul 2026 16:21:25 +0200 Subject: [PATCH 1/2] fix(vectors): swap atan2 arguments and replace SecureRandom usage --- .../net/theevilreaper/aves/util/Vectors.java | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/main/java/net/theevilreaper/aves/util/Vectors.java b/src/main/java/net/theevilreaper/aves/util/Vectors.java index 7faf6c4a..54e04f05 100644 --- a/src/main/java/net/theevilreaper/aves/util/Vectors.java +++ b/src/main/java/net/theevilreaper/aves/util/Vectors.java @@ -4,19 +4,20 @@ import net.minestom.server.coordinate.Vec; import org.jetbrains.annotations.Contract; -import java.security.SecureRandom; +import java.util.concurrent.ThreadLocalRandom; /** - * The class contains some useful method to work with vectors. + * Utility class providing mathematical vector operations and generation functions + * tailored for Minecraft server development using Minestom's {@link Vec} and {@link Pos}. + * All random vector computations leverage non-blocking, thread-safe {@link ThreadLocalRandom} instances + * for high-throughput performance across server tick workers. * * @author theEvilReaper - * @version 1.0.1 + * @version 1.1.0 * @since 1.0.0 */ public final class Vectors { - private static final SecureRandom random = new SecureRandom(); - private Vectors() { } @@ -25,8 +26,8 @@ private Vectors() { * * @return a new vector with random values */ - @Contract(pure = true) public static Vec getRandomVector() { + var random = ThreadLocalRandom.current(); double x = random.nextDouble() * 2.0D - 1.0D; double y = random.nextDouble() * 2.0D - 1.0D; double z = random.nextDouble() * 2.0D - 1.0D; @@ -51,9 +52,8 @@ public static Vec getBackVector(Pos location) { * * @return a new vector with random values */ - @Contract(pure = true) public static Vec getRandomCircleVector() { - double rnd = random.nextDouble() * 2.0D * Math.PI; + double rnd = ThreadLocalRandom.current().nextDouble() * 2.0D * Math.PI; double x = Math.cos(rnd); double z = Math.sin(rnd); return new Vec(x, 0.0D, z); @@ -63,10 +63,11 @@ public static Vec getRandomCircleVector() { * Calculates the angle between a 2D vector and the positive x-axis in a Cartesian coordinate system. * The angle is measured in radians and ranges from -π to π radians. * - * @param vector the vector for which to calculate the angle + * @param vector the vector for which to calculate the angle * @return the angle between the vector and the positive x-axis in radians */ + @Contract(pure = true) public static double angleToXAxis(Vec vector) { - return Math.atan2(vector.x(), vector.y()); + return Math.atan2(vector.y(), vector.x()); } } From 2be57549210e2361e6a12689e65ff8a5f0a71852 Mon Sep 17 00:00:00 2001 From: theEvilReaper Date: Wed, 22 Jul 2026 16:22:17 +0200 Subject: [PATCH 2/2] test(vectors): adjust angleToXAxis test case --- src/test/java/net/theevilreaper/aves/util/VectorsTest.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/test/java/net/theevilreaper/aves/util/VectorsTest.java b/src/test/java/net/theevilreaper/aves/util/VectorsTest.java index bc4acd37..51b3e2b2 100644 --- a/src/test/java/net/theevilreaper/aves/util/VectorsTest.java +++ b/src/test/java/net/theevilreaper/aves/util/VectorsTest.java @@ -24,7 +24,9 @@ void testGetRandomCircleVector() { } @Test - void angleToXAxis() { - assertNotSame(0.7853981633974483, Vectors.angleToXAxis(Vec.ZERO)); + void testAngleToXAxis() { + assertEquals(0.0, Vectors.angleToXAxis(new Vec(1, 0, 0)), 1e-6, "Vector along positive X axis should have angle 0"); + assertEquals(Math.PI / 2, Vectors.angleToXAxis(new Vec(0, 1, 0)), 1e-6, "Vector along positive Y axis should have angle PI/2"); + assertEquals(Math.PI / 4, Vectors.angleToXAxis(new Vec(1, 1, 0)), 1e-6, "Vector (1, 1) should have angle PI/4"); } }