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

174 lines
4.0 KiB
TypeScript

import { useBridge } from "./core";
type TagType = number | boolean | string | CompoundTag | ListTag;
/**
* @internal
*/
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 if (obj[1]) {
return obj[1] as Exclude<TagType, boolean>;
} else {
return null;
}
}
/**
* 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) {
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) {
return new CompoundTag(obj);
} else {
return null;
}
}
}
/**
* 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) {
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) {
return new ListTag(obj);
} else {
return null;
}
}
}