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

134 lines
2.8 KiB
TypeScript

import { useBridge } from "./core";
type TagType = number | boolean | string | CompoundTag | ListTag;
/**
* @internal
*/
function getTagValue(obj: [boolean, BridgeValueType]): TagType {
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];
} else {
return null;
}
}
/**
* Compound Tag
*/
export class CompoundTag {
/**
* @internal
*/
readonly javaObject: JavaObject;
/**
* @internal
*/
constructor(javaObject: JavaObject) {
this.javaObject = javaObject;
}
/**
* Get Value In Tag
* @param key Key
* @returns Value
*/
get(key: string): TagType {
const obj = useBridge('CompoundTag.get', this.javaObject, key) as [boolean, BridgeValueType];
return getTagValue(obj);
}
/**
* Set Value In Tag
* @param key Key
* @param value Value
*/
set(key: string, value: TagType) {
useBridge('CompoundTag.set', this.javaObject, key, (value instanceof CompoundTag || value instanceof ListTag) ? value.javaObject : value);
}
/**
* 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
* @param key Key
* @returns Value
*/
get(key: number): TagType {
const obj = useBridge('ListTag.get', this.javaObject, key) as [boolean, BridgeValueType];
return getTagValue(obj);
}
/**
* Set Value In Tag
* @param key Key
* @param value Value
*/
set(key: number, value: TagType) {
useBridge('ListTag.set', this.javaObject, key, (value instanceof CompoundTag || value instanceof ListTag) ? value.javaObject : value);
}
/**
* 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;
}
}
}