import type { Coordinates, Range, Rotation, STRUCTURE_MIRROR, STRUCTURE_ROTATION } from 'sandstone/arguments' import type { SandstoneCore } from 'sandstone/core/sandstoneCore' import { isMacroArgument, type MacroArgument } from '../core/Macro' import { VectorClass } from './Coordinates' // PARSERS export function coordinatesParser( coordinates: T, ): T extends Coordinates ? VectorClass<[string, string, string]> : T { if (Array.isArray(coordinates)) { if (coordinates.length === 3 || coordinates.length === 2) { return new VectorClass(coordinates.map((coord) => { if (typeof coord === 'string') { return coord } if (typeof coord === 'number') { return `${coord}` } if ('toMacro' in coord) { /* @ts-ignore */ return coord.toMacro() } throw new Error(`[coordinatesParser] Unsupported vector component ${coord}`) })) as any } throw new Error(`[coordinatesParser] Unsupported vector size ${coordinates.length}`) } else { return coordinates as any } } export function rotationParser(rotation: T): T extends Rotation ? VectorClass<[string, string]> : T { if (Array.isArray(rotation)) { if (rotation.length === 2) { return new VectorClass(rotation.map((coord) => { if (typeof coord === 'string') { return coord } if (typeof coord === 'number') { return `${coord}` } if ('toMacro' in coord) { /* @ts-ignore */ return coord.toMacro() } throw new Error(`[rotationParser] Unsupported vector component ${coord}`) })) as any } throw new Error(`[rotationParser] Unsupported vector size ${rotation.length}`) } else { return rotation as any } } // Sanitize score values. null => '', Infinity => '', any number => itself export const sanitizeValue = ( core: SandstoneCore, value: MacroArgument | number | string | null | undefined, ): string => { if (value === undefined || value === null) { return '' } if (typeof value === 'string') { return value } if (isMacroArgument(core, value)) { return (value as MacroArgument).toMacro() } if (Number.isFinite(value)) { return value.toString() } // Value is Infinity or -Infinity return '' } // Returns the string representation of a range. [0, null] => '0..', [-Infinity, 5] => '..5', 8 => '8' export const rangeParser = (core: SandstoneCore, range: Range | MacroArgument): string => { if (Array.isArray(range)) { return `${sanitizeValue(core, range[0])}..${sanitizeValue(core, range[1])}` } return range.toString() } export type StructureRotation = STRUCTURE_ROTATION | number | `${90 | 180 | 270}` | `-${90 | 180 | 270}` export const structureRotationParser = (rotation?: StructureRotation | MacroArgument) => { if (!rotation) { return 'none' } if (typeof rotation === 'object') { return rotation } const numToLiteral = (angle: number): STRUCTURE_ROTATION => { /** biome-ignore format: excessive formatting */ switch (angle) { case 0: return 'none' case 90: return 'clockwise_90' case 180: return '180' case 270: return 'counterclockwise_90' case -90: return 'counterclockwise_90' case -180: return '180' case -270: return 'clockwise_90' default: { if (!Number.isInteger(angle / 90)) { throw new Error('Structure rotation must be in increments of 90!') } // reduce the angle angle %= 360 // force it to be the positive remainder, so that 0 <= angle < 360 angle = (angle + 360) % 360 // force into the minimum absolute value residue class, so that -180 < angle <= 180 if (angle > 180) angle -= 360 return numToLiteral(angle) } } } if (typeof rotation === 'number') { return numToLiteral(rotation) } if (rotation === 'clockwise_90' || rotation === 'counterclockwise_90') { return rotation } return numToLiteral(Number(rotation)) } export type StructureMirror = STRUCTURE_MIRROR | '^x' | '^z' | 'x' | 'z' | boolean export const structureMirrorParser = (mirror?: StructureMirror | MacroArgument): STRUCTURE_MIRROR => { if (typeof mirror === 'object') { return mirror.toMacro() as STRUCTURE_MIRROR } if (typeof mirror === 'string') { const lastCharacter = mirror.slice(0, -1) if (lastCharacter === 'x') { return 'left_right' } if (lastCharacter === 'z') { return 'front_back' } return mirror as STRUCTURE_MIRROR } if (mirror) { return 'left_right' } return 'none' } /** * Parses a target into a string representation. * If the target is a string, it is returned as is. * If the target has a `_toSelector` method, it is called to get the string representation. * If the target has a `toMacro` method, it is called to get the string representation. * Otherwise, the `toString` method is called to get the string representation. * * If the target is undefined, an error is thrown. * @param target The target to parse. * @returns The string representation of the target. */ export function targetParser(target: any): string { if (target === undefined) { throw new Error('Target cannot be undefined.') } if (typeof target === 'string') { return target } if (target._toSelector) { return target._toSelector() } if (target.toMacro) { return target } return target.toString() }