Migrate vectors to a dedicated API interface

This commit is contained in:
zontreck 2024-04-07 15:53:04 -07:00
parent 0f01475ec3
commit 81286767f4
18 changed files with 1124 additions and 469 deletions

View file

@ -0,0 +1,126 @@
package dev.zontreck.libzontreck.api;
import dev.zontreck.libzontreck.vectors.Vector2d;
import dev.zontreck.libzontreck.vectors.Vector2i;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.phys.Vec2;
public interface Vector2
{
/**
* Converts the current Vector2 representation into a minecraft Vec2
* @return Minecraft equivalent Vec2
*/
Vec2 asMinecraftVector();
/**
* Parses a string in serialized format.
* @param vector2 Expects it in the same format returned by Vector2#toString
* @return New Vector2, or a null Vector2 initialized with zeros if invalid data
*/
static Vector2 parseString(String vector2){
throw new UnsupportedOperationException("This method is not implemented by this implementation");
}
/**
* Copies the values to a new and detached instance
* @return New Vector2
*/
Vector2 Clone();
/**
* Saves the X and Y positions to a NBT tag
* @return NBT compound tag
*/
CompoundTag serialize();
/**
* Loads a Vector2 from a NBT tag
* @param tag The NBT tag to load
*/
static Vector2 deserialize(CompoundTag tag)
{
throw new UnsupportedOperationException("This method is not implemented by this implementation");
}
/**
* Compares the two vector2 instances
* @param other The position to check
* @return True if same position
*/
boolean Same(Vector2 other);
/**
* True if the current position is inside the two points
* @param point1 Lowest point
* @param point2 Hightest Point
* @return True if inside
*/
boolean Inside(Vector2 point1, Vector2 point2);
/**
* Converts, if necessary, to Vector2d
* @return A vector2d instance
*/
Vector2d asVector2d();
/**
* Converts, if necessary, to Vector2i
* @return A vector2i instance
*/
Vector2i asVector2i();
/**
* Checks if the current vector is greater than the provided one
* @param other The other vector to check
* @return True if greater
*/
boolean greater(Vector2 other);
/**
* Checks if the current vector is less than the provided one
* @param other The vector to check
* @return True if less than other
*/
boolean less(Vector2 other);
/**
* Alias for Vector2#same
* @param other Vector to check
* @return True if same position
*/
boolean equal(Vector2 other);
/**
* Adds the two vectors together
* @param other Vector to add
* @return New instance after adding the other vector
*/
Vector2 add(Vector2 other);
/**
* Subtracts the other vector from this one
* @param other Vector to subtract
* @return New instance after subtracting
*/
Vector2 subtract(Vector2 other);
/**
* Calculates the distance between the two vectors
* @param other
* @return The distance
*/
double distance(Vector2 other);
/**
* Increments the Y axis by 1
* @return New instance
*/
Vector2 moveUp();
/**
* Decrements the Y axis by 1
* @return New instance
*/
Vector2 moveDown();
}

View file

@ -0,0 +1,143 @@
package dev.zontreck.libzontreck.api;
import dev.zontreck.libzontreck.vectors.Vector2d;
import dev.zontreck.libzontreck.vectors.Vector2i;
import dev.zontreck.libzontreck.vectors.Vector3d;
import dev.zontreck.libzontreck.vectors.Vector3i;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Vec3i;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.phys.Vec2;
import net.minecraft.world.phys.Vec3;
public interface Vector3
{
/**
* Converts the current Vector3 representation into a minecraft Vec3
* @return Minecraft equivalent Vec3
*/
Vec3 asMinecraftVector();
/**
* Converts to a vec3i position
* @return Equivalent vec3i
*/
Vec3i asVec3i();
/**
* Converts to a block position
* @return Equivalent block position
*/
BlockPos asBlockPos();
/**
* Parses a string in serialized format.
* @param vector3 Expects it in the same format returned by Vector3#toString
* @return New Vector3, or a null Vector3 initialized with zeros if invalid data
*/
static Vector3 parseString(String vector3){
throw new UnsupportedOperationException("This method is not implemented by this implementation");
}
/**
* Copies the values to a new and detached instance
* @return New Vector3
*/
Vector3 Clone();
/**
* Saves the X, Y, and Z positions to a NBT tag
* @return NBT compound tag
*/
CompoundTag serialize();
/**
* Loads a Vector3 from a NBT tag
* @param tag The NBT tag to load
*/
static Vector3 deserialize(CompoundTag tag)
{
throw new UnsupportedOperationException("This method is not implemented by this implementation");
}
/**
* Compares the two vector3 instances
* @param other The position to check
* @return True if same position
*/
boolean Same(Vector3 other);
/**
* True if the current position is inside the two points
* @param point1 Lowest point
* @param point2 Hightest Point
* @return True if inside
*/
boolean Inside(Vector3 point1, Vector3 point2);
/**
* Converts, if necessary, to Vector3d
* @return A vector2d instance
*/
Vector3d asVector3d();
/**
* Converts, if necessary, to Vector3i
* @return A vector3i instance
*/
Vector3i asVector3i();
/**
* Checks if the current vector is greater than the provided one
* @param other The other vector to check
* @return True if greater
*/
boolean greater(Vector3 other);
/**
* Checks if the current vector is less than the provided one
* @param other The vector to check
* @return True if less than other
*/
boolean less(Vector3 other);
/**
* Alias for Vector3#same
* @param other Vector to check
* @return True if same position
*/
boolean equal(Vector3 other);
/**
* Adds the two vectors together
* @param other Vector to add
* @return New instance after adding the other vector
*/
Vector3 add(Vector3 other);
/**
* Subtracts the other vector from this one
* @param other Vector to subtract
* @return New instance after subtracting
*/
Vector3 subtract(Vector3 other);
/**
* Calculates the distance between the two vectors
* @param other
* @return The distance
*/
double distance(Vector3 other);
/**
* Increments the Y axis by 1
* @return New instance
*/
Vector3 moveUp();
/**
* Decrements the Y axis by 1
* @return New instance
*/
Vector3 moveDown();
}