This repository has been archived on 2023-11-26. You can view files and clone it, but cannot push or open issues or pull requests.
ScriptCraft/src/main/ts/src/minecraft/core.ts

298 lines
6.0 KiB
TypeScript

/**
* Action Result
*/
export enum ActionResult {
/**
* Indicates an action is not performed but allows other actions to perform.
*/
PASS = 'PASS',
/**
* Indicates an action is performed and the actor's hand should swing to indicate the performance.
*/
SUCCESS = 'SUCCESS',
/**
* Indicates that an action is not performed and prevents other actions from performing.
*/
FAIL = 'FAIL',
/**
* Indicates an action is performed but no animation should accompany the performance.
*/
CONSUME = 'CONSUME'
}
/**
* Hand
*/
export enum Hand {
/**
* Main Hand
*/
MAIN_HAND = 'MAIN_HAND',
/**
* Off Hand
*/
OFF_HAND = 'OFF_HAND'
}
/**
* Direction
*/
export enum Direction {
/**
* Down
*/
DOWN = 'DOWN',
/**
* Up
*/
UP = 'UP',
/**
* North
*/
NORTH = 'NORTH',
/**
* South
*/
SOUTH = 'SOUTH',
/**
* West
*/
WEST = 'WEST',
/**
* East
*/
EAST = 'EAST'
}
/**
* Utility Class for {@link Direction}
*/
export class DirectionUtil {
/**
* Get Direction's Offset
* @param direction Direction
*/
static getOffset(direction: Direction): Pos {
switch (direction) {
case Direction.UP: {
return new Pos(0, 1, 0);
}
case Direction.DOWN: {
return new Pos(0, -1, 0);
}
case Direction.NORTH: {
return new Pos(0, 0, -1);
}
case Direction.SOUTH: {
return new Pos(0, 0, 1);
}
case Direction.WEST: {
return new Pos(-1, 0, 0);
}
case Direction.EAST: {
return new Pos(1, 0, 0);
}
}
}
}
/**
* Namespaced Identifier
*
* Formatted as "<namespace>:<path>"
*/
export class Identifier {
readonly #namespace: string;
readonly #path: string;
/**
* Create Identifier
* @param str Existing Identifier
* @param namespace Namespace
* @param path Path
*/
constructor(str: string);
constructor(namespace: string, path: string);
constructor(data: string, path?: string) {
if (path) {
this.#namespace = data;
this.#path = path;
} else {
const id = data.split(':');
this.#namespace = id[1] ? id[0] : 'minecraft';
this.#path = id[1] ? id[1] : id[0];
}
}
/**
* Get Namespace
*/
getNamespace(): string {
return this.#namespace;
}
/**
* Get Path
*/
getPath(): string {
return this.#path;
}
/**
* Convert To String
* @returns String
*/
toString(): string {
return this.#namespace + ':' + this.#path;
}
/**
* Check Equality
* @param id Other Value
* @returns TRUE If Equal, Otherwise FALSE
*/
equals(id: Identifier): boolean {
return id.getNamespace() === this.getNamespace() && id.getPath() === this.getPath();
}
}
/**
* Arbitrary Position
*/
export class Pos {
readonly #x: number;
readonly #y: number;
readonly #z: number;
/**
* Create Position
* @param x X Coordinate
* @param y Y Coordinate
* @param z Z Coordinate
*/
constructor(x: number, y: number, z: number) {
this.#x = x;
this.#y = y;
this.#z = z;
}
/**
* Get X Coordinate
* @returns X Coordinate
*/
getX(): number {
return this.#x;
}
/**
* Get Y Coordinate
* @returns Y Coordinate
*/
getY(): number {
return this.#y;
}
/**
* Get Z Coordinate
* @returns Z Coordinate
*/
getZ(): number {
return this.#z;
}
/**
* Add Position
* @param pos Other Position
* @returns Position Sum
*/
add(pos: Pos): Pos {
return new Pos(this.getX() + pos.getX(), this.getY() + pos.getY(), this.getZ() + pos.getZ());
}
/**
* Subtract Position
* @param pos Other Position
* @returns Position Difference
*/
subtract(pos: Pos): Pos {
const newBlockPos = new Pos(-pos.getX(), -pos.getY(), -pos.getZ());
return this.add(newBlockPos);
}
/**
* Multiply Position
* @param pos Other Position
* @returns Position Product
*/
multiply(pos: Pos): Pos {
return new Pos(this.getX() * pos.getX(), this.getY() * pos.getY(), this.getZ() * pos.getZ());
}
/**
* Divide Position
* @param pos Position Quotient
*/
divide(pos: Pos): Pos {
return new Pos(this.getX() / pos.getX(), this.getY() / pos.getY(), this.getZ() / pos.getZ());
}
/**
* Offset Position with a {@link Direction}
* @param direction Offset Position
*/
offset(direction: Direction): Pos {
return this.add(DirectionUtil.getOffset(direction));
}
/**
* Round Position
* @returns Rounded Position
*/
round(): Pos {
return new Pos(Math.round(this.getX()), Math.round(this.getY()), Math.round(this.getZ()));
}
/**
* Convert To String
* @returns String
*/
toString(): string {
return 'Pos{' + this.getX() + ', ' + this.getY() + ', ' + this.getZ() + '}';
}
/**
* Check Equality
* @param id Other Value
* @returns TRUE If Equal, Otherwise FALSE
*/
equals(pos: Pos): boolean {
return pos.getX() === this.getX() && pos.getY() === this.getY() && pos.getZ() === this.getZ();
}
}
/**
* Simple Registry
*/
export interface SimpleRegistry<T> {
/**
* Register Object
* @param id ID
* @param obj Object
*/
register(id: Identifier, obj: T): void;
/**
* Get Object From ID
* @param id ID
* @returns Object
*/
get(id: Identifier): T;
/**
* Get ID From Object
* @param obj Object
* @returns ID
*/
getID(obj: T): Identifier;
}