#844: Add methods to convert Vector to and from JOML vectors

By: Parker Hawke <hawkeboyz2@hotmail.com>
This commit is contained in:
Bukkit/Spigot 2023-04-29 10:54:41 +10:00
parent dd112b4907
commit 71089b4713

View File

@ -10,6 +10,10 @@ import org.bukkit.World;
import org.bukkit.configuration.serialization.ConfigurationSerializable; import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.bukkit.configuration.serialization.SerializableAs; import org.bukkit.configuration.serialization.SerializableAs;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.joml.RoundingMode;
import org.joml.Vector3d;
import org.joml.Vector3f;
import org.joml.Vector3i;
/** /**
* Represents a mutable vector. Because the components of Vectors are mutable, * Represents a mutable vector. Because the components of Vectors are mutable,
@ -814,6 +818,48 @@ public class Vector implements Cloneable, ConfigurationSerializable {
return new BlockVector(x, y, z); return new BlockVector(x, y, z);
} }
/**
* Get this vector as a JOML {@link Vector3f}.
*
* @return the JOML vector
*/
@NotNull
public Vector3f toVector3f() {
return new Vector3f((float) x, (float) y, (float) z);
}
/**
* Get this vector as a JOML {@link Vector3d}.
*
* @return the JOML vector
*/
@NotNull
public Vector3d toVector3d() {
return new Vector3d(x, y, z);
}
/**
* Get this vector as a JOML {@link Vector3i}.
*
* @param roundingMode the {@link RoundingMode} to use for this vector's components
* @return the JOML vector
*/
@NotNull
public Vector3i toVector3i(int roundingMode) {
return new Vector3i(x, y, z, roundingMode);
}
/**
* Get this vector as a JOML {@link Vector3i} with its components floored.
*
* @return the JOML vector
* @see #toVector3i(int)
*/
@NotNull
public Vector3i toVector3i() {
return toVector3i(RoundingMode.FLOOR);
}
/** /**
* Check if each component of this Vector is finite. * Check if each component of this Vector is finite.
* *
@ -869,6 +915,39 @@ public class Vector implements Cloneable, ConfigurationSerializable {
return new Vector(random.nextDouble(), random.nextDouble(), random.nextDouble()); return new Vector(random.nextDouble(), random.nextDouble(), random.nextDouble());
} }
/**
* Gets a vector with components that match the provided JOML {@link Vector3f}.
*
* @param vector the vector to match
* @return the new vector
*/
@NotNull
public static Vector fromJOML(@NotNull Vector3f vector) {
return new Vector(vector.x(), vector.y(), vector.z());
}
/**
* Gets a vector with components that match the provided JOML {@link Vector3d}.
*
* @param vector the vector to match
* @return the new vector
*/
@NotNull
public static Vector fromJOML(@NotNull Vector3d vector) {
return new Vector(vector.x(), vector.y(), vector.z());
}
/**
* Gets a vector with components that match the provided JOML {@link Vector3i}.
*
* @param vector the vector to match
* @return the new vector
*/
@NotNull
public static Vector fromJOML(@NotNull Vector3i vector) {
return new Vector(vector.x(), vector.y(), vector.z());
}
@Override @Override
@NotNull @NotNull
public Map<String, Object> serialize() { public Map<String, Object> serialize() {