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/scriptcraft/src/main/ts/src/minecraft/tag.ts

220 lines
5.0 KiB
TypeScript

import { useBridge } from 'scriptcraft-core';
type TagType = number | boolean | string | CompoundTag | ListTag;
function getTagValue(obj: [boolean, BridgeValueType]): Exclude<TagType, boolean> {
if (typeof obj[1] === 'object') {
if (obj[0]) {
return new ListTag(obj[1] as JavaObject);
} else {
return new CompoundTag(obj[1] as JavaObject);
}
} else {
return obj[1] as Exclude<TagType, boolean>;
}
}
function valueToString(obj: Exclude<TagType, boolean>): string {
if (typeof obj === 'string') {
return obj.__quote();
} else {
return String(obj);
}
}
/**
* Number Types
*/
export enum NumberType {
/**
* Integer
*/
INT = 'INT',
/**
* Byte
*/
BYTE = 'BYTE',
/**
* Double (JS Number)
*/
DOUBLE = 'DOUBLE',
/**
* Long
*/
LONG = 'LONG',
/**
* Float
*/
FLOAT = 'FLOAT',
/**
* Short
*/
SHORT = 'SHORT'
}
/**
* Compound Tag
*/
export class CompoundTag {
/**
* @internal
*/
readonly javaObject: JavaObject;
/**
* @internal
*/
constructor(javaObject: JavaObject) {
this.javaObject = javaObject;
}
/**
* Get Value In Tag
*
* Note: This will never return a boolean, because booleans are stored as numbers in NBT.
* @param key Key
* @returns Value
*/
get(key: string): Exclude<TagType, boolean> {
const obj = useBridge('CompoundTag.get', this.javaObject, key) as [boolean, BridgeValueType];
return getTagValue(obj);
}
/**
* Set Value In Tag
* @param key Key
* @param value Value
* @param numberType Number Type
*/
set(key: string, value: number, numberType: NumberType): void;
set(key: string, value: Exclude<TagType, number>): void;
set(key: string, value: TagType, numberType?: NumberType): void {
useBridge('CompoundTag.set', this.javaObject, key, value instanceof CompoundTag || value instanceof ListTag ? value.javaObject : value, numberType !== null ? numberType : NumberType.DOUBLE);
}
/**
* Get All Keys
* @returns List Of Keys
*/
keys(): string[] {
return useBridge('CompoundTag.keys', this.javaObject) as string[];
}
/**
* Create Empty Tag
* @returns Empty Tag
*/
static create(): CompoundTag {
const obj = useBridge('CompoundTag.create') as JavaObject;
if (obj !== null) {
return new CompoundTag(obj);
} else {
return null;
}
}
/**
* Convert To String
* @returns String
*/
toString(): string {
const keys = this.keys();
let out = '{';
let first = true;
for (const key of keys) {
if (!first) {
out = out + ', ';
} else {
first = false;
}
out = out + key + ': ' + valueToString(this.get(key));
}
return out + '}';
}
}
/**
* List Tag
*/
export class ListTag {
/**
* @internal
*/
readonly javaObject: JavaObject;
/**
* @internal
*/
constructor(javaObject: JavaObject) {
this.javaObject = javaObject;
}
/**
* Get Value In Tag
*
* Note: This will never return a boolean, because booleans are stored as numbers in NBT.
* @param key Key
* @returns Value
*/
get(key: number): Exclude<TagType, boolean> {
const obj = useBridge('ListTag.get', this.javaObject, key) as [boolean, BridgeValueType];
return getTagValue(obj);
}
/**
* Set Value In Tag
* @param key Key
* @param value Value
* @param numberType Number Type
*/
set(key: number, value: number, numberType: NumberType): void;
set(key: number, value: Exclude<TagType, number>): void;
set(key: number, value: TagType, numberType?: NumberType): void {
useBridge('ListTag.set', this.javaObject, key, value instanceof CompoundTag || value instanceof ListTag ? value.javaObject : value, numberType !== null ? numberType : NumberType.DOUBLE);
}
/**
* Get Size Of Tag
* @returns Size
*/
size(): number {
return useBridge('ListTag.size', this.javaObject) as number;
}
/**
* Create Empty Tag
* @returns Empty Tag
*/
static create(): ListTag {
const obj = useBridge('ListTag.create') as JavaObject;
if (obj !== null) {
return new ListTag(obj);
} else {
return null;
}
}
/**
* Convert To String
* @returns String
*/
toString(): string {
const size = this.size();
let out = '[';
let first = true;
for (let i = 0; i < size; i++) {
if (!first) {
out = out + ', ';
} else {
first = false;
}
out = out + valueToString(this.get(i));
}
return out + ']';
}
}