@bedrock-oss/bedrock-boost
Version:
A utility package with helper functions for developing add-ons with Script API in Minecraft Bedrock Edition
1 lines • 382 kB
Source Map (JSON)
{"version":3,"sources":["../src/math/Vec3.ts","../src/math/MutVec3.ts","../src/Logging.ts","../src/ChatColor.ts","../src/ColorJSON.ts","../src/math/Vec2.ts","../src/math/MutVec2.ts","../src/Timings.ts","../src/Cache.ts","../src/polyfill/Polyfill.ts","../src/polyfill/PlayerPolyfill.ts","../src/utils/JobUtils.ts","../src/utils/BlockUtils.ts","../src/scheduling/PulseScheduler.ts","../src/scheduling/TaskPulseScheduler.ts","../src/scheduling/UniquePulseScheduler.ts","../src/scheduling/EntityPulseScheduler.ts","../src/scheduling/PlayerPulseScheduler.ts","../src/utils/CommandUtils.ts","../src/VariableSender.ts","../src/vanilla/VanillaBlockTags.ts","../src/vanilla/VanillaItemTags.ts","../src/vanilla/TimeOfDay.ts","../src/utils/DirectionUtils.ts","../src/ColorUtils.ts","../src/utils/EntitySaver.ts","../src/utils/ItemUtils.ts","../src/utils/EntityUtils.ts","../src/TypeAssertion.ts"],"sourcesContent":["import {\r\n Vector3,\r\n Direction,\r\n Vector2,\r\n StructureRotation,\r\n} from '@minecraft/server';\r\nimport MutVec3 from './MutVec3';\r\nimport { Logger } from '../Logging';\r\n\r\ntype VectorLike = Vector3 | MutVec3 | Vec3 | Direction | number[] | number;\r\n\r\nexport default class Vec3 implements Vector3 {\r\n private static readonly log = Logger.getLogger(\r\n 'vec3',\r\n 'vec3',\r\n 'bedrock-boost'\r\n );\r\n /**\r\n * Zero vector\r\n */\r\n public static readonly Zero = new Vec3(0, 0, 0);\r\n /**\r\n * Down vector, negative towards Y\r\n */\r\n public static readonly Down = new Vec3(Direction.Down);\r\n /**\r\n * Up vector, positive towards Y\r\n */\r\n public static readonly Up = new Vec3(Direction.Up);\r\n /**\r\n * North vector, negative towards Z\r\n */\r\n public static readonly North = new Vec3(Direction.North);\r\n /**\r\n * South vector, positive towards Z\r\n */\r\n public static readonly South = new Vec3(Direction.South);\r\n /**\r\n * East vector, positive towards X\r\n */\r\n public static readonly East = new Vec3(Direction.East);\r\n /**\r\n * West vector, negative towards X\r\n */\r\n public static readonly West = new Vec3(Direction.West);\r\n\r\n readonly x: number;\r\n readonly y: number;\r\n readonly z: number;\r\n constructor(x: number, y: number, z: number);\r\n constructor(x: Vec3);\r\n constructor(x: Vector3);\r\n constructor(x: Direction);\r\n constructor(x: number[]);\r\n constructor(x: VectorLike, y?: number, z?: number) {\r\n if (x === Direction.Down) {\r\n this.x = 0;\r\n this.y = -1;\r\n this.z = 0;\r\n } else if (x === Direction.Up) {\r\n this.x = 0;\r\n this.y = 1;\r\n this.z = 0;\r\n } else if (x === Direction.North) {\r\n this.x = 0;\r\n this.y = 0;\r\n this.z = -1;\r\n } else if (x === Direction.South) {\r\n this.x = 0;\r\n this.y = 0;\r\n this.z = 1;\r\n } else if (x === Direction.East) {\r\n this.x = 1;\r\n this.y = 0;\r\n this.z = 0;\r\n } else if (x === Direction.West) {\r\n this.x = -1;\r\n this.y = 0;\r\n this.z = 0;\r\n } else if (typeof x === 'number') {\r\n this.x = x;\r\n this.y = y!;\r\n this.z = z!;\r\n } else if (Array.isArray(x)) {\r\n this.x = x[0];\r\n this.y = x[1];\r\n this.z = x[2];\r\n } else if (x instanceof Vec3) {\r\n this.x = x.x;\r\n this.y = x.y;\r\n this.z = x.z;\r\n } else {\r\n if (\r\n !x ||\r\n (!x.x && x.x !== 0) ||\r\n (!x.y && x.y !== 0) ||\r\n (!x.z && x.z !== 0)\r\n ) {\r\n Vec3.log.error(new Error('Invalid vector'), x);\r\n throw new Error('Invalid vector');\r\n }\r\n this.x = x.x;\r\n this.y = x.y;\r\n this.z = x.z;\r\n }\r\n }\r\n /**\r\n * Creates a new vector from the given values.\r\n */\r\n static from(x: number, y: number, z: number): Vec3;\r\n static from(x: Vec3): Vec3;\r\n static from(x: Vector3): Vec3;\r\n static from(x: Direction): Vec3;\r\n static from(x: number[]): Vec3;\r\n static from(x: VectorLike, y?: number, z?: number): Vec3 {\r\n if (x instanceof Vec3) return x;\r\n if (typeof x === 'number' && y !== undefined && z !== undefined) {\r\n return new Vec3(x, y, z);\r\n }\r\n if (Array.isArray(x)) {\r\n return new Vec3(x);\r\n }\r\n if (x === Direction.Down) return Vec3.Down;\r\n if (x === Direction.Up) return Vec3.Up;\r\n if (x === Direction.North) return Vec3.North;\r\n if (x === Direction.South) return Vec3.South;\r\n if (x === Direction.East) return Vec3.East;\r\n if (x === Direction.West) return Vec3.West;\r\n if (\r\n !x ||\r\n (!(x as any).x && (x as any).x !== 0) ||\r\n (!(x as any).y && (x as any).y !== 0) ||\r\n (!(x as any).z && (x as any).z !== 0)\r\n ) {\r\n Vec3.log.error(new Error('Invalid arguments'), x, y, z);\r\n throw new Error('Invalid arguments');\r\n }\r\n return new Vec3(\r\n (x as any).x as number,\r\n (x as any).y as number,\r\n (x as any).z as number\r\n );\r\n }\r\n private static _from(x: VectorLike, y?: number, z?: number): Vec3 {\r\n if (typeof x === 'number' && y === undefined && z === undefined) {\r\n return new Vec3(x, x, x);\r\n }\r\n if (x instanceof Vec3) return x;\r\n if (typeof x === 'number' && y !== undefined && z !== undefined) {\r\n return new Vec3(x, y, z);\r\n }\r\n if (Array.isArray(x)) {\r\n return new Vec3(x);\r\n }\r\n if (x === Direction.Down) return Vec3.Down;\r\n if (x === Direction.Up) return Vec3.Up;\r\n if (x === Direction.North) return Vec3.North;\r\n if (x === Direction.South) return Vec3.South;\r\n if (x === Direction.East) return Vec3.East;\r\n if (x === Direction.West) return Vec3.West;\r\n if (\r\n !x ||\r\n (!(x as any).x && (x as any).x !== 0) ||\r\n (!(x as any).y && (x as any).y !== 0) ||\r\n (!(x as any).z && (x as any).z !== 0)\r\n ) {\r\n Vec3.log.error(new Error('Invalid arguments'), x, y, z);\r\n throw new Error('Invalid arguments');\r\n }\r\n return new Vec3(\r\n (x as any).x as number,\r\n (x as any).y as number,\r\n (x as any).z as number\r\n );\r\n }\r\n /**\r\n * Creates a copy of the current vector.\r\n *\r\n * @returns A new vector with the same values as the current vector.\r\n */\r\n copy(): Vec3 {\r\n return new Vec3(this.x, this.y, this.z);\r\n }\r\n\r\n /**\r\n * Converts this immutable vector to a new mutable vector.\r\n */\r\n toMutable(): MutVec3 {\r\n return new MutVec3(this.x, this.y, this.z);\r\n }\r\n\r\n /**\r\n * Creates a new direction vector from yaw and pitch values.\r\n *\r\n * @param rotation - The yaw and pitch values in degrees.\r\n * @returns A new vector representing the direction.\r\n */\r\n static fromRotation(rotation: Vector2): Vec3;\r\n /**\r\n * Creates a new direction vector from yaw and pitch values.\r\n *\r\n * @param yaw - The yaw value in degrees.\r\n * @param pitch - The pitch value in degrees.\r\n * @returns A new vector representing the direction.\r\n */\r\n static fromRotation(yaw: number, pitch: number): Vec3;\r\n static fromRotation(yawOrRotation: number | Vector2, pitch?: number): Vec3 {\r\n let yaw: number;\r\n if (typeof yawOrRotation === 'number') {\r\n yaw = yawOrRotation as number;\r\n pitch = pitch!;\r\n } else {\r\n yaw = yawOrRotation.y;\r\n pitch = yawOrRotation.x;\r\n }\r\n // Convert degrees to radians\r\n const psi = yaw * (Math.PI / 180);\r\n const theta = pitch * (Math.PI / 180);\r\n\r\n const x = -Math.cos(theta) * Math.sin(psi);\r\n const y = -Math.sin(theta);\r\n const z = Math.cos(theta) * Math.cos(psi);\r\n return new Vec3(x, y, z);\r\n }\r\n\r\n /**\r\n * Converts the normal vector to yaw and pitch values.\r\n *\r\n * @returns A Vector2 containing the yaw and pitch values.\r\n */\r\n toRotation(): Vector2 {\r\n if (this.isZero()) {\r\n Vec3.log.error(\r\n new Error('Cannot convert zero-length vector to direction')\r\n );\r\n throw new Error('Cannot convert zero-length vector to direction');\r\n }\r\n const direction = this.normalize();\r\n const yaw = -Math.atan2(direction.x, direction.z) * (180 / Math.PI);\r\n const pitch = Math.asin(-direction.y) * (180 / Math.PI);\r\n return {\r\n x: pitch,\r\n y: yaw,\r\n };\r\n }\r\n\r\n /**\r\n * Adds three numbers to the current vector.\r\n *\r\n * @param x - The x component to be added.\r\n * @param y - The y component to be added.\r\n * @param z - The z component to be added.\r\n * @returns The updated vector after addition.\r\n */\r\n add(x: number, y: number, z: number): Vec3;\r\n\r\n /**\r\n * Adds another Vec3 to the current vector.\r\n *\r\n * @param x - The Vec3 to be added.\r\n * @returns The updated vector after addition.\r\n */\r\n add(x: Vec3): Vec3;\r\n\r\n /**\r\n * Adds another Vector3 to the current vector.\r\n *\r\n * @param x - The Vector3 to be added.\r\n * @returns The updated vector after addition.\r\n */\r\n add(x: Vector3): Vec3;\r\n\r\n /**\r\n * Adds a Direction to the current vector.\r\n *\r\n * @param x - The Direction to be added.\r\n * @returns The updated vector after addition.\r\n */\r\n add(x: Direction): Vec3;\r\n\r\n /**\r\n * Adds a Scaler to the current vector.\r\n *\r\n * @param x - The Scaler to be added.\r\n * @returns The updated vector after addition.\r\n */\r\n add(x: number): Vec3;\r\n\r\n /**\r\n * Adds an array of numbers to the current vector.\r\n *\r\n * @param x - The array of numbers to be added.\r\n * @returns The updated vector after addition.\r\n */\r\n add(x: number[]): Vec3;\r\n\r\n add(x: VectorLike, y?: number, z?: number): Vec3 {\r\n const v: Vec3 = Vec3._from(x, y, z);\r\n return Vec3.from(v.x + this.x, v.y + this.y, v.z + this.z);\r\n }\r\n\r\n /**\r\n * Returns the normalized vector pointing from this vector to the input.\r\n *\r\n * @param x - The x component of the target vector.\r\n * @param y - The y component target vector.\r\n * @param z - The z component target vector.\r\n * @returns The direction to the passed in Vector. Equivalent to (B-A).normalized()\r\n */\r\n directionTo(x: number, y: number, z: number): Vec3;\r\n\r\n /**\r\n * Returns the normalized vector pointing from this vector to the input.\r\n *\r\n * @param x - The Vec3 to be added.\r\n * @returns The direction to the passed in Vector. Equivalent to (B-A).normalized()\r\n */\r\n directionTo(x: Vec3): Vec3;\r\n\r\n /**\r\n * Returns the normalized vector pointing from this vector to the input.\r\n *\r\n * @param x - The Vector3 to be added.\r\n * @returns The direction to the passed in Vector. Equivalent to (B-A).normalized()\r\n */\r\n directionTo(x: Vector3): Vec3;\r\n\r\n /**\r\n * Returns the normalized vector pointing from this vector to the input.\r\n *\r\n * @param x - The Direction to be added.\r\n * @returns The direction to the passed in Vector. Equivalent to (B-A).normalized()\r\n */\r\n directionTo(x: Direction): Vec3;\r\n\r\n /**\r\n * Returns the normalized vector pointing from this vector to the input.\r\n *\r\n * @param x - The array of numbers to be added.\r\n * @returns The direction to the passed in Vector. Equivalent to (B-A).normalized()\r\n */\r\n directionTo(x: number[]): Vec3;\r\n\r\n directionTo(x: VectorLike, y?: number, z?: number) {\r\n const v: Vec3 = Vec3._from(x, y, z);\r\n return v.subtract(this).normalize();\r\n }\r\n\r\n /**\r\n * Subtracts three numbers from the current vector.\r\n *\r\n * @param x - The x component to be subtracted.\r\n * @param y - The y component to be subtracted.\r\n * @param z - The z component to be subtracted.\r\n * @returns The updated vector after subtraction.\r\n */\r\n subtract(x: number, y: number, z: number): Vec3;\r\n\r\n /**\r\n * Subtracts another Vec3 from the current vector.\r\n *\r\n * @param x - The Vec3 to be subtracted.\r\n * @returns The updated vector after subtraction.\r\n */\r\n subtract(x: Vec3): Vec3;\r\n\r\n /**\r\n * Subtracts a Scaler from the current vector.\r\n *\r\n * @param x - The Scaler to be subtracted.\r\n * @returns The updated vector after subtraction.\r\n */\r\n subtract(x: number): Vec3;\r\n\r\n /**\r\n * Subtracts another Vector3 from the current vector.\r\n *\r\n * @param x - The Vector3 to be subtracted.\r\n * @returns The updated vector after subtraction.\r\n */\r\n subtract(x: Vector3): Vec3;\r\n\r\n /**\r\n * Subtracts a Direction from the current vector.\r\n *\r\n * @param x - The Direction to be subtracted.\r\n * @returns The updated vector after subtraction.\r\n */\r\n subtract(x: Direction): Vec3;\r\n\r\n /**\r\n * Subtracts an array of numbers from the current vector.\r\n *\r\n * @param x - The array of numbers to be subtracted.\r\n * @returns The updated vector after subtraction.\r\n */\r\n subtract(x: number[]): Vec3;\r\n\r\n /**\r\n * Subtracts a Scaler from the current vector.\r\n *\r\n * @param x - The number to be subtracted.\r\n * @returns The updated vector after subtraction.\r\n */\r\n subtract(x: number): Vec3;\r\n\r\n subtract(x: VectorLike, y?: number, z?: number): Vec3 {\r\n const v: Vec3 = Vec3._from(x, y, z);\r\n return Vec3.from(this.x - v.x, this.y - v.y, this.z - v.z);\r\n }\r\n\r\n /**\r\n * Multiplies the current vector by three numbers.\r\n *\r\n * @param x - The multiplier for the x component.\r\n * @param y - The multiplier for the y component.\r\n * @param z - The multiplier for the z component.\r\n * @returns The updated vector after multiplication.\r\n */\r\n multiply(x: number, y: number, z: number): Vec3;\r\n\r\n /**\r\n * Multiplies the current vector by another Vec3.\r\n *\r\n * @param x - The Vec3 multiplier.\r\n * @returns The updated vector after multiplication.\r\n */\r\n multiply(x: Vec3): Vec3;\r\n\r\n /**\r\n * Multiplies the current vector by another Vector3.\r\n *\r\n * @param x - The Vector3 multiplier.\r\n * @returns The updated vector after multiplication.\r\n */\r\n multiply(x: Vector3): Vec3;\r\n\r\n /**\r\n * Multiplies the current vector by a Direction.\r\n *\r\n * @param x - The Direction multiplier.\r\n * @returns The updated vector after multiplication.\r\n */\r\n multiply(x: Direction): Vec3;\r\n\r\n /**\r\n * Multiplies the current vector by an array of numbers.\r\n *\r\n * @param x - The array multiplier.\r\n * @returns The updated vector after multiplication.\r\n */\r\n multiply(x: number[]): Vec3;\r\n\r\n /**\r\n * Multiplies the current vector by a scalar.\r\n *\r\n * @param x - The scalar multiplier.\r\n * @returns The updated vector after multiplication.\r\n */\r\n multiply(x: number): Vec3;\r\n\r\n multiply(x: VectorLike, y?: number, z?: number): Vec3 {\r\n if (typeof x === 'number' && y === undefined && z === undefined) {\r\n return Vec3.from(this.x * x, this.y * x, this.z * x);\r\n }\r\n const v: Vec3 = Vec3._from(x, y, z);\r\n return Vec3.from(v.x * this.x, v.y * this.y, v.z * this.z);\r\n }\r\n\r\n /**\r\n * Scales the current vector by a scalar.\r\n *\r\n * @param scalar - The scalar to scale the vector by.\r\n * @returns The updated vector after scaling.\r\n */\r\n scale(scalar: number): Vec3 {\r\n return Vec3.from(this.x * scalar, this.y * scalar, this.z * scalar);\r\n }\r\n\r\n /**\r\n * Divides the current vector by three numbers.\r\n *\r\n * @param x - The divisor for the x component.\r\n * @param y - The divisor for the y component.\r\n * @param z - The divisor for the z component.\r\n * @returns The updated vector after division.\r\n */\r\n divide(x: number, y: number, z: number): Vec3;\r\n\r\n /**\r\n * Divides the current vector by another Vec3.\r\n *\r\n * @param x - The Vec3 divisor.\r\n * @returns The updated vector after division.\r\n */\r\n divide(x: Vec3): Vec3;\r\n\r\n /**\r\n * Divides the current vector by another Vector3.\r\n *\r\n * @param x - The Vector3 divisor.\r\n * @returns The updated vector after division.\r\n */\r\n divide(x: Vector3): Vec3;\r\n\r\n /**\r\n * Divides the current vector by a Direction.\r\n *\r\n * @param x - The Direction divisor.\r\n * @returns The updated vector after division.\r\n */\r\n divide(x: Direction): Vec3;\r\n\r\n /**\r\n * Divides the current vector by an array of numbers.\r\n *\r\n * @param x - The array divisor.\r\n * @returns The updated vector after division.\r\n */\r\n divide(x: number[]): Vec3;\r\n\r\n /**\r\n * Divides the current vector by a scalar.\r\n *\r\n * @param x - The scalar divisor.\r\n * @returns The updated vector after division.\r\n */\r\n divide(x: number): Vec3;\r\n\r\n divide(x: VectorLike, y?: number, z?: number): Vec3 {\r\n if (typeof x === 'number' && y === undefined && z === undefined) {\r\n if (x === 0) throw new Error('Cannot divide by zero');\r\n return Vec3.from(this.x / x, this.y / x, this.z / x);\r\n }\r\n const v: Vec3 = Vec3._from(x, y, z);\r\n if (v.x === 0 || v.y === 0 || v.z === 0)\r\n throw new Error('Cannot divide by zero');\r\n return Vec3.from(this.x / v.x, this.y / v.y, this.z / v.z);\r\n }\r\n\r\n /**\r\n * Normalizes the vector to have a length (magnitude) of 1.\r\n * Normalized vectors are often used as a direction vectors.\r\n *\r\n * @returns The normalized vector.\r\n */\r\n normalize(): Vec3 {\r\n if (this.isZero()) {\r\n Vec3.log.error(new Error('Cannot normalize zero-length vector'));\r\n throw new Error('Cannot normalize zero-length vector');\r\n }\r\n const len = this.length();\r\n return Vec3.from(this.x / len, this.y / len, this.z / len);\r\n }\r\n\r\n /**\r\n * Computes the length (magnitude) of the vector.\r\n *\r\n * @returns The length of the vector.\r\n */\r\n length(): number {\r\n return Math.hypot(this.x, this.y, this.z);\r\n }\r\n /**\r\n * Computes the squared length of the vector.\r\n * This is faster than computing the actual length and can be useful for comparison purposes.\r\n *\r\n * @returns The squared length of the vector.\r\n */\r\n lengthSquared(): number {\r\n return this.x * this.x + this.y * this.y + this.z * this.z;\r\n }\r\n /**\r\n * Computes the cross product of the current vector with three numbers.\r\n *\r\n * A cross product is a vector that is perpendicular to both vectors.\r\n *\r\n * @param x - The x component of the other vector.\r\n * @param y - The y component of the other vector.\r\n * @param z - The z component of the other vector.\r\n * @returns A new vector representing the cross product.\r\n */\r\n cross(x: number, y: number, z: number): Vec3;\r\n\r\n /**\r\n * Computes the cross product of the current vector with another Vec3.\r\n *\r\n * A cross product is a vector that is perpendicular to both vectors.\r\n *\r\n * @param x - The Vec3 to be crossed.\r\n * @returns A new vector representing the cross product.\r\n */\r\n cross(x: Vec3): Vec3;\r\n\r\n /**\r\n * Computes the cross product of the current vector with another Vector3.\r\n *\r\n * A cross product is a vector that is perpendicular to both vectors.\r\n *\r\n * @param x - The Vector3 to be crossed.\r\n * @returns A new vector representing the cross product.\r\n */\r\n cross(x: Vector3): Vec3;\r\n\r\n /**\r\n * Computes the cross product of the current vector with a Direction.\r\n *\r\n * A cross product is a vector that is perpendicular to both vectors.\r\n *\r\n * @param x - The Direction to be crossed.\r\n * @returns A new vector representing the cross product.\r\n */\r\n cross(x: Direction): Vec3;\r\n\r\n /**\r\n * Computes the cross product of the current vector with an array of numbers.\r\n *\r\n * A cross product is a vector that is perpendicular to both vectors.\r\n *\r\n * @param x - The array of numbers representing a vector.\r\n * @returns A new vector representing the cross product.\r\n */\r\n cross(x: number[]): Vec3;\r\n\r\n cross(x: VectorLike, y?: number, z?: number): Vec3 {\r\n const v: Vec3 = Vec3._from(x, y, z);\r\n return Vec3.from(\r\n this.y * v.z - this.z * v.y,\r\n this.z * v.x - this.x * v.z,\r\n this.x * v.y - this.y * v.x\r\n );\r\n }\r\n\r\n /**\r\n * Computes the distance between the current vector and a vector represented by three numbers.\r\n *\r\n * @param x - The x component of the other vector.\r\n * @param y - The y component of the other vector.\r\n * @param z - The z component of the other vector.\r\n * @returns The distance between the two vectors.\r\n */\r\n distance(x: number, y: number, z: number): number;\r\n\r\n /**\r\n * Computes the distance between the current vector and another Vec3.\r\n *\r\n * @param x - The Vec3 to measure the distance to.\r\n * @returns The distance between the two vectors.\r\n */\r\n distance(x: Vec3): number;\r\n\r\n /**\r\n * Computes the distance between the current vector and another Vector3.\r\n *\r\n * @param x - The Vector3 to measure the distance to.\r\n * @returns The distance between the two vectors.\r\n */\r\n distance(x: Vector3): number;\r\n\r\n /**\r\n * Computes the distance between the current vector and a Direction.\r\n *\r\n * @param x - The Direction to measure the distance to.\r\n * @returns The distance between the two vectors.\r\n */\r\n distance(x: Direction): number;\r\n\r\n /**\r\n * Computes the distance between the current vector and a vector represented by an array of numbers.\r\n *\r\n * @param x - The array of numbers representing the other vector.\r\n * @returns The distance between the two vectors.\r\n */\r\n distance(x: number[]): number;\r\n\r\n distance(x: VectorLike, y?: number, z?: number): number {\r\n const v: Vec3 = Vec3._from(x, y, z);\r\n return this.subtract(v).length();\r\n }\r\n\r\n /**\r\n * Computes the squared distance between the current vector and a vector represented by three numbers.\r\n * This is faster than computing the actual distance and can be useful for comparison purposes.\r\n *\r\n * @param x - The x component of the other vector.\r\n * @param y - The y component of the other vector.\r\n * @param z - The z component of the other vector.\r\n * @returns The squared distance between the two vectors.\r\n */\r\n distanceSquared(x: number, y: number, z: number): number;\r\n\r\n /**\r\n * Computes the squared distance between the current vector and another Vec3.\r\n * This is faster than computing the actual distance and can be useful for comparison purposes.\r\n *\r\n * @param x - The Vec3 to measure the squared distance to.\r\n * @returns The squared distance between the two vectors.\r\n */\r\n distanceSquared(x: Vec3): number;\r\n\r\n /**\r\n * Computes the squared distance between the current vector and another Vector3.\r\n * This is faster than computing the actual distance and can be useful for comparison purposes.\r\n *\r\n * @param x - The Vector3 to measure the squared distance to.\r\n * @returns The squared distance between the two vectors.\r\n */\r\n distanceSquared(x: Vector3): number;\r\n\r\n /**\r\n * Computes the squared distance between the current vector and a Direction.\r\n * This is faster than computing the actual distance and can be useful for comparison purposes.\r\n *\r\n * @param x - The Direction to measure the squared distance to.\r\n * @returns The squared distance between the two vectors.\r\n */\r\n distanceSquared(x: Direction): number;\r\n\r\n /**\r\n * Computes the squared distance between the current vector and a vector represented by an array of numbers.\r\n * This is faster than computing the actual distance and can be useful for comparison purposes.\r\n *\r\n * @param x - The array of numbers representing the other vector.\r\n * @returns The squared distance between the two vectors.\r\n */\r\n distanceSquared(x: number[]): number;\r\n\r\n distanceSquared(x: VectorLike, y?: number, z?: number): number {\r\n const v: Vec3 = Vec3._from(x, y, z);\r\n return this.subtract(v).lengthSquared();\r\n }\r\n\r\n /**\r\n * Computes the linear interpolation between the current vector and another vector, when t is in the range [0, 1].\r\n * Computes the extrapolation when t is outside this range.\r\n *\r\n * @param v - The other vector.\r\n * @param t - The interpolation factor.\r\n * @returns A new vector after performing the lerp operation.\r\n */\r\n lerp(v: Vector3, t: number): Vec3 {\r\n if (!v || !t) return Vec3.from(this);\r\n if (t === 1) return Vec3.from(v);\r\n if (t === 0) return Vec3.from(this);\r\n return Vec3.from(\r\n this.x + (v.x - this.x) * t,\r\n this.y + (v.y - this.y) * t,\r\n this.z + (v.z - this.z) * t\r\n );\r\n }\r\n /**\r\n * Computes the spherical linear interpolation between the current vector and another vector, when t is in the range [0, 1].\r\n * Computes the extrapolation when t is outside this range.\r\n *\r\n * @param v - The other vector.\r\n * @param t - The interpolation factor.\r\n * @returns A new vector after performing the slerp operation.\r\n */\r\n slerp(v: Vector3, t: number): Vec3 {\r\n if (!v || !t) return Vec3.from(this);\r\n if (t === 1) return Vec3.from(v);\r\n if (t === 0) return Vec3.from(this);\r\n const dot = this.dot(v);\r\n const theta = Math.acos(dot) * t;\r\n const relative = Vec3.from(v).subtract(this.multiply(dot)).normalize();\r\n return this.multiply(Math.cos(theta)).add(\r\n relative.multiply(Math.sin(theta))\r\n );\r\n }\r\n /**\r\n * Computes the dot product of the current vector with a vector specified by three numbers.\r\n *\r\n * @param x - The x component of the other vector.\r\n * @param y - The y component of the other vector.\r\n * @param z - The z component of the other vector.\r\n * @returns The dot product of the two vectors.\r\n */\r\n dot(x: number, y: number, z: number): number;\r\n\r\n /**\r\n * Computes the dot product of the current vector with another Vec3.\r\n *\r\n * @param x - The Vec3 to compute the dot product with.\r\n * @returns The dot product of the two vectors.\r\n */\r\n dot(x: Vec3): number;\r\n\r\n /**\r\n * Computes the dot product of the current vector with another Vector3.\r\n *\r\n * @param x - The Vector3 to compute the dot product with.\r\n * @returns The dot product of the two vectors.\r\n */\r\n dot(x: Vector3): number;\r\n\r\n /**\r\n * Computes the dot product of the current vector with a Direction.\r\n *\r\n * @param x - The Direction to compute the dot product with.\r\n * @returns The dot product of the two vectors.\r\n */\r\n dot(x: Direction): number;\r\n\r\n /**\r\n * Computes the dot product of the current vector with a vector represented by an array of numbers.\r\n *\r\n * @param x - The array of numbers representing the other vector.\r\n * @returns The dot product of the two vectors.\r\n */\r\n dot(x: number[]): number;\r\n\r\n dot(x: VectorLike, y?: number, z?: number): number {\r\n const v: Vec3 = Vec3._from(x, y, z);\r\n return this.x * v.x + this.y * v.y + this.z * v.z;\r\n }\r\n\r\n /**\r\n * Computes the angle (in radians) between the current vector and a vector specified by three numbers.\r\n *\r\n * @param x - The x component of the other vector.\r\n * @param y - The y component of the other vector.\r\n * @param z - The z component of the other vector.\r\n * @returns The angle in radians between the two vectors.\r\n */\r\n angleBetween(x: number, y: number, z: number): number;\r\n\r\n /**\r\n * Computes the angle (in radians) between the current vector and another Vec3.\r\n *\r\n * @param x - The Vec3 to compute the angle with.\r\n * @returns The angle in radians between the two vectors.\r\n */\r\n angleBetween(x: Vec3): number;\r\n\r\n /**\r\n * Computes the angle (in radians) between the current vector and another Vector3.\r\n *\r\n * @param x - The Vector3 to compute the angle with.\r\n * @returns The angle in radians between the two vectors.\r\n */\r\n angleBetween(x: Vector3): number;\r\n\r\n /**\r\n * Computes the angle (in radians) between the current vector and a Direction.\r\n *\r\n * @param x - The Direction to compute the angle with.\r\n * @returns The angle in radians between the two vectors.\r\n */\r\n angleBetween(x: Direction): number;\r\n\r\n /**\r\n * Computes the angle (in radians) between the current vector and a vector represented by an array of numbers.\r\n *\r\n * @param x - The array of numbers representing the other vector.\r\n * @returns The angle in radians between the two vectors.\r\n */\r\n angleBetween(x: number[]): number;\r\n\r\n angleBetween(x: VectorLike, y?: number, z?: number): number {\r\n const v: Vec3 = Vec3._from(x, y, z);\r\n const dotProduct = this.dot(v);\r\n const lenSq1 = this.lengthSquared();\r\n if (lenSq1 === 0) {\r\n return 0;\r\n }\r\n const lenSq2 = v.lengthSquared();\r\n if (lenSq2 === 0) {\r\n return 0;\r\n }\r\n const denom = Math.sqrt(lenSq1 * lenSq2);\r\n // Clamp for numerical stability\r\n const cosAngle = Math.min(1, Math.max(-1, dotProduct / denom));\r\n return Math.acos(cosAngle);\r\n }\r\n\r\n /**\r\n * Computes the projection of the current vector onto a vector specified by three numbers.\r\n * This method finds how much of the current vector lies in the direction of the given vector.\r\n *\r\n * @param x - The x component of the vector to project onto.\r\n * @param y - The y component of the vector to project onto.\r\n * @param z - The z component of the vector to project onto.\r\n * @returns A new vector representing the projection of the current vector.\r\n */\r\n projectOnto(x: number, y: number, z: number): Vec3;\r\n\r\n /**\r\n * Computes the projection of the current vector onto another Vec3.\r\n * This method finds how much of the current vector lies in the direction of the given vector.\r\n *\r\n * @param x - The Vec3 to project onto.\r\n * @returns A new vector representing the projection of the current vector.\r\n */\r\n projectOnto(x: Vec3): Vec3;\r\n\r\n /**\r\n * Computes the projection of the current vector onto another Vector3.\r\n * This method finds how much of the current vector lies in the direction of the given vector.\r\n *\r\n * @param x - The Vector3 to project onto.\r\n * @returns A new vector representing the projection of the current vector.\r\n */\r\n projectOnto(x: Vector3): Vec3;\r\n\r\n /**\r\n * Computes the projection of the current vector onto a Direction.\r\n * This method finds how much of the current vector lies in the direction of the given vector.\r\n *\r\n * @param x - The Direction to project onto.\r\n * @returns A new vector representing the projection of the current vector.\r\n */\r\n projectOnto(x: Direction): Vec3;\r\n\r\n /**\r\n * Computes the projection of the current vector onto a vector represented by an array of numbers.\r\n * This method finds how much of the current vector lies in the direction of the given vector.\r\n *\r\n * @param x - The array of numbers representing the vector to project onto.\r\n * @returns A new vector representing the projection of the current vector.\r\n */\r\n projectOnto(x: number[]): Vec3;\r\n\r\n projectOnto(x: VectorLike, y?: number, z?: number): Vec3 {\r\n const v: Vec3 = Vec3._from(x, y, z);\r\n // If the vector is zero-length, then the projection is the zero vector.\r\n if (v.isZero()) {\r\n return Vec3.Zero;\r\n }\r\n const denom = v.dot(v);\r\n if (denom === 0) {\r\n return Vec3.Zero;\r\n }\r\n const scale = this.dot(v) / denom;\r\n return Vec3.from(v.x * scale, v.y * scale, v.z * scale);\r\n }\r\n\r\n /**\r\n * Computes the reflection of the current vector against a normal vector specified by three numbers.\r\n * Useful for simulating light reflections or bouncing objects.\r\n *\r\n * @param x - The x component of the normal vector.\r\n * @param y - The y component of the normal vector.\r\n * @param z - The z component of the normal vector.\r\n * @returns A new vector representing the reflection of the current vector.\r\n */\r\n reflect(x: number, y: number, z: number): Vec3;\r\n\r\n /**\r\n * Computes the reflection of the current vector against another Vec3 normal vector.\r\n * Useful for simulating light reflections or bouncing objects.\r\n *\r\n * @param x - The Vec3 representing the normal vector.\r\n * @returns A new vector representing the reflection of the current vector.\r\n */\r\n reflect(x: Vec3): Vec3;\r\n\r\n /**\r\n * Computes the reflection of the current vector against another Vector3 normal vector.\r\n * Useful for simulating light reflections or bouncing objects.\r\n *\r\n * @param x - The Vector3 representing the normal vector.\r\n * @returns A new vector representing the reflection of the current vector.\r\n */\r\n reflect(x: Vector3): Vec3;\r\n\r\n /**\r\n * Computes the reflection of the current vector against a Direction normal vector.\r\n * Useful for simulating light reflections or bouncing objects.\r\n *\r\n * @param x - The Direction representing the normal vector.\r\n * @returns A new vector representing the reflection of the current vector.\r\n */\r\n reflect(x: Direction): Vec3;\r\n\r\n /**\r\n * Computes the reflection of the current vector against a normal vector represented by an array of numbers.\r\n * Useful for simulating light reflections or bouncing objects.\r\n *\r\n * @param x - The array of numbers representing the normal vector.\r\n * @returns A new vector representing the reflection of the current vector.\r\n */\r\n reflect(x: number[]): Vec3;\r\n\r\n reflect(x: VectorLike, y?: number, z?: number): Vec3 {\r\n const normal: Vec3 = Vec3._from(x, y, z);\r\n const proj = this.projectOnto(normal);\r\n return this.subtract(proj.multiply(2));\r\n }\r\n\r\n /**\r\n * Rotates the current normalized vector by a given angle around a given axis.\r\n *\r\n * @param axis - The axis of rotation.\r\n * @param angle - The angle of rotation in degrees.\r\n * @returns The rotated vector.\r\n */\r\n rotate(axis: Vector3, angle: number): Vec3 {\r\n // Convert angle from degrees to radians and compute half angle\r\n const halfAngle = (angle * Math.PI) / 180 / 2;\r\n\r\n // Quaternion representing the rotation\r\n const w = Math.cos(halfAngle);\r\n const x = axis.x * Math.sin(halfAngle);\r\n const y = axis.y * Math.sin(halfAngle);\r\n const z = axis.z * Math.sin(halfAngle);\r\n // eslint-disable-next-line @typescript-eslint/no-this-alias\r\n const v = this;\r\n\r\n // Rotate vector (v) using quaternion\r\n // Simplified direct computation reflecting quaternion rotation and its conjugate effect\r\n const qv_x =\r\n w * w * v.x +\r\n 2 * y * w * v.z -\r\n 2 * z * w * v.y +\r\n x * x * v.x +\r\n 2 * y * x * v.y +\r\n 2 * z * x * v.z -\r\n z * z * v.x -\r\n y * y * v.x;\r\n const qv_y =\r\n 2 * x * y * v.x +\r\n y * y * v.y +\r\n 2 * z * y * v.z +\r\n 2 * w * z * v.x -\r\n z * z * v.y +\r\n w * w * v.y -\r\n 2 * x * w * v.z -\r\n x * x * v.y;\r\n const qv_z =\r\n 2 * x * z * v.x +\r\n 2 * y * z * v.y +\r\n z * z * v.z -\r\n 2 * w * y * v.x -\r\n y * y * v.z +\r\n 2 * w * x * v.y -\r\n x * x * v.z +\r\n w * w * v.z;\r\n\r\n return new Vec3(qv_x, qv_y, qv_z);\r\n }\r\n /**\r\n * Updates the X, Y, and Z components of the vector.\r\n *\r\n * @param x - The function to use to update the X value.\r\n * @param y - The function to use to update the Y value.\r\n * @param z - The function to use to update the Z value.\r\n * @returns The updated vector with the new values.\r\n */\r\n update(\r\n x: ((x: number) => number) | undefined,\r\n y: ((y: number) => number) | undefined,\r\n z: ((z: number) => number) | undefined\r\n ): Vec3 {\r\n if (!x) {\r\n x = (value: number) => value;\r\n }\r\n if (!y) {\r\n y = (value: number) => value;\r\n }\r\n if (!z) {\r\n z = (value: number) => value;\r\n }\r\n return new Vec3(x(this.x), y(this.y), z(this.z));\r\n }\r\n /**\r\n * Sets the X component of the vector.\r\n *\r\n * @param value - The new X value.\r\n * @returns The updated vector with the new X value.\r\n */\r\n setX(value: number): Vec3;\r\n setX(value: (x: number) => number): Vec3;\r\n setX(value: number | ((x: number) => number)): Vec3 {\r\n if (typeof value === 'number') {\r\n return new Vec3(value, this.y, this.z);\r\n }\r\n return new Vec3(value(this.x), this.y, this.z);\r\n }\r\n /**\r\n * Sets the Y component of the vector.\r\n *\r\n * @param value - The new Y value.\r\n * @returns The updated vector with the new Y value.\r\n */\r\n setY(value: number): Vec3;\r\n setY(value: (y: number) => number): Vec3;\r\n setY(value: number | ((y: number) => number)): Vec3 {\r\n if (typeof value === 'number') {\r\n return new Vec3(this.x, value, this.z);\r\n }\r\n return new Vec3(this.x, value(this.y), this.z);\r\n }\r\n /**\r\n * Sets the Z component of the vector.\r\n *\r\n * @param value - The new Z value.\r\n * @returns The updated vector with the new Z value.\r\n */\r\n setZ(value: number): Vec3;\r\n setZ(value: (z: number) => number): Vec3;\r\n setZ(value: number | ((z: number) => number)): Vec3 {\r\n if (typeof value === 'number') {\r\n return new Vec3(this.x, this.y, value);\r\n }\r\n return new Vec3(this.x, this.y, value(this.z));\r\n }\r\n /**\r\n * Calculates the shortest distance between a point (represented by this Vector3 instance) and a line segment.\r\n *\r\n * This method finds the perpendicular projection of the point onto the line defined by the segment. If this\r\n * projection lies outside the line segment, then the method calculates the distance from the point to the\r\n * nearest segment endpoint.\r\n *\r\n * @param start - The starting point of the line segment.\r\n * @param end - The ending point of the line segment.\r\n * @returns The shortest distance between the point and the line segment.\r\n */\r\n distanceToLineSegment(start: Vector3, end: Vector3): number {\r\n const lineDirection = Vec3.from(end).subtract(start);\r\n // If the line is zero-length, then the distance is the distance to the start point.\r\n if (lineDirection.lengthSquared() === 0) {\r\n return this.subtract(start).length();\r\n }\r\n const t = Math.max(\r\n 0,\r\n Math.min(\r\n 1,\r\n this.subtract(start).dot(lineDirection) /\r\n lineDirection.dot(lineDirection)\r\n )\r\n );\r\n const projection = Vec3.from(start).add(lineDirection.multiply(t));\r\n return this.subtract(projection).length();\r\n }\r\n /**\r\n * Floors the X, Y, and Z components of the vector.\r\n * @returns A new vector with the floored components.\r\n */\r\n floor(): Vec3 {\r\n return this.update(Math.floor, Math.floor, Math.floor);\r\n }\r\n /**\r\n * Floors the X component of the vector.\r\n * @returns A new vector with the floored X component.\r\n */\r\n floorX(): Vec3 {\r\n return this.setX(Math.floor);\r\n }\r\n /**\r\n * Floors the Y component of the vector.\r\n * @returns A new vector with the floored Y component.\r\n */\r\n floorY(): Vec3 {\r\n return this.setY(Math.floor);\r\n }\r\n /**\r\n * Floors the Z component of the vector.\r\n * @returns A new vector with the floored Z component.\r\n */\r\n floorZ(): Vec3 {\r\n return this.setZ(Math.floor);\r\n }\r\n /**\r\n * Ceils the X, Y, and Z components of the vector.\r\n * @returns A new vector with the ceiled components.\r\n */\r\n ceil(): Vec3 {\r\n return new Vec3(\r\n Math.ceil(this.x),\r\n Math.ceil(this.y),\r\n Math.ceil(this.z)\r\n );\r\n }\r\n /**\r\n * Ceils the X component of the vector.\r\n * @returns A new vector with the ceiled X component.\r\n */\r\n ceilX(): Vec3 {\r\n return this.setX(Math.ceil);\r\n }\r\n /**\r\n * Ceils the Y component of the vector.\r\n * @returns A new vector with the ceiled Y component.\r\n */\r\n ceilY(): Vec3 {\r\n return this.setY(Math.ceil);\r\n }\r\n /**\r\n * Ceils the Z component of the vector.\r\n * @returns A new vector with the ceiled Z component.\r\n */\r\n ceilZ(): Vec3 {\r\n return this.setZ(Math.ceil);\r\n }\r\n /**\r\n * Rounds the X, Y, and Z components of the vector.\r\n * @returns A new vector with the rounded components.\r\n */\r\n round(): Vec3 {\r\n return this.update(Math.round, Math.round, Math.round);\r\n }\r\n /**\r\n * Rounds the X component of the vector.\r\n * @returns A new vector with the rounded X component.\r\n */\r\n roundX(): Vec3 {\r\n return this.setX(Math.round);\r\n }\r\n /**\r\n * Rounds the Y component of the vector.\r\n * @returns A new vector with the rounded Y component.\r\n */\r\n roundY(): Vec3 {\r\n return this.setY(Math.round);\r\n }\r\n /**\r\n * Rounds the Z component of the vector.\r\n * @returns A new vector with the rounded Z component.\r\n */\r\n roundZ(): Vec3 {\r\n return this.setZ(Math.round);\r\n }\r\n /**\r\n * Returns a new vector offset from the current vector up by 1 block.\r\n * @returns A new vector offset from the current vector up by 1 block.\r\n */\r\n up(): Vec3 {\r\n return this.add(Vec3.Up);\r\n }\r\n /**\r\n * Returns a new vector offset from the current vector down by 1 block.\r\n * @returns A new vector offset from the current vector down by 1 block.\r\n */\r\n down(): Vec3 {\r\n return this.add(Vec3.Down);\r\n }\r\n /**\r\n * Returns a new vector offset from the current vector north by 1 block.\r\n * @returns A new vector offset from the current vector north by 1 block.\r\n */\r\n north(): Vec3 {\r\n return this.add(Vec3.North);\r\n }\r\n /**\r\n * Returns a new vector offset from the current vector south by 1 block.\r\n * @returns A new vector offset from the current vector south by 1 block.\r\n */\r\n south(): Vec3 {\r\n return this.add(Vec3.South);\r\n }\r\n /**\r\n * Returns a new vector offset from the current vector east by 1 block.\r\n * @returns A new vector offset from the current vector east by 1 block.\r\n */\r\n east(): Vec3 {\r\n return this.add(Vec3.East);\r\n }\r\n /**\r\n * Returns a new vector offset from the current vector west by 1 block.\r\n * @returns A new vector offset from the current vector west by 1 block.\r\n */\r\n west(): Vec3 {\r\n return this.add(Vec3.West);\r\n }\r\n /**\r\n * Checks if the current vector is equal to the zero vector.\r\n * @returns true if the vector is equal to the zero vector, else returns false.\r\n */\r\n isZero(): boolean {\r\n return this.x === 0 && this.y === 0 && this.z === 0;\r\n }\r\n /**\r\n * Converts the vector to an array containing the X, Y, and Z components of the vector.\r\n * @returns An array containing the X, Y, and Z components of the vector.\r\n */\r\n toArray(): number[] {\r\n return [this.x, this.y, this.z];\r\n }\r\n /**\r\n * Converts the vector to a direction.\r\n * If the vector is not a unit vector, then it will be normalized and rounded to the nearest direction.\r\n */\r\n toDirection(): Direction {\r\n if (this.isZero()) {\r\n Vec3.log.error(\r\n new Error('Cannot convert zero-length vector to direction')\r\n );\r\n throw new Error('Cannot convert zero-length vector to direction');\r\n }\r\n const normalized = this.normalize();\r\n const maxValue = Math.max(\r\n Math.abs(normalized.x),\r\n Math.abs(normalized.y),\r\n Math.abs(normalized.z)\r\n );\r\n if (maxValue === normalized.x) return Direction.East;\r\n if (maxValue === -normalized.x) return Direction.West;\r\n if (maxValue === normalized.y) return Direction.Up;\r\n if (maxValue === -normalized.y) return Direction.Down;\r\n if (maxValue === normalized.z) return Direction.South;\r\n if (maxValue === -normalized.z) return Direction.North;\r\n // This should never happen\r\n Vec3.log.error(new Error('Cannot convert vector to direction'), this);\r\n throw new Error('Cannot convert vector to direction');\r\n }\r\n /**\r\n * Converts the vector to a structure rotation.\r\n * If the vector is not a unit vector, then it will be normalized and rounded to the nearest 90 degrees rotation.\r\n */\r\n toStructureRotation(): StructureRotation {\r\n const rotation = this.toRotation();\r\n let aligned = Math.round(rotation.y / 90) * 90;\r\n if (aligned < 0) {\r\n aligned += 360;\r\n }\r\n if (aligned >= 360) {\r\n aligned -= 360;\r\n }\r\n if (aligned === 0) return StructureRotation.None;\r\n if (aligned === 90) return StructureRotation.Rotate90;\r\n if (aligned === 180) return StructureRotation.Rotate180;\r\n if (aligned === 270) return StructureRotation.Rotate270;\r\n // This should never happen\r\n Vec3.log.error(\r\n new Error('Cannot convert vector to structure rotation'),\r\n this\r\n );\r\n throw new Error('Cannot convert vector to structure rotation');\r\n }\r\n /**\r\n * Returns a new vector with the X, Y, and Z components rounded to the nearest block location.\r\n */\r\n toBlockLocation(): Vec3 {\r\n // At this point I'm not sure if it wouldn't be better to use Math.floor instead\r\n return Vec3.from(\r\n (this.x << 0) - (this.x < 0 && this.x !== this.x << 0 ? 1 : 0),\r\n (this.y << 0) - (this.y < 0 && this.y !== this.y << 0 ? 1 : 0),\r\n (this.z <<