-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
67 lines (56 loc) · 1.94 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import {Tag} from "./src/tag/Tag";
import {BinaryCompression, Edition, SNBTCompression} from "./src/FileFormat";
import NBTError from "./src/NBTError";
import {gzip, inflate} from "pako";
import CompoundPayload from "./src/tag/payloads/CompoundPayload";
import * as JSONBig from 'json-bigint'
export default class NBT {
private rootTag: Tag;
constructor(rootTag: Tag) {
if (!rootTag.isRoot())
throw new NBTError('Must be a root tag');
this.rootTag = rootTag;
}
static parseJSON(json: object): NBT {
return new NBT(Tag.fromJSON({'': json}, true));
}
static parseSNBT(snbt: string): NBT {
return new NBT(new Tag('', CompoundPayload.fromSNBTValue(snbt), true));
}
static parseNBT(data: Uint8Array, edition: Edition, compression ?: BinaryCompression): NBT {
if (!compression) {
if (data[0] === 0x1f && data[1] === 0x8b)
compression = 'gzip';
else if (data[0] <= 0x0c)
compression = 'none';
else
throw new NBTError('Unknown compression type');
}
if (compression === 'gzip') {
data = inflate(data);
}
return new NBT(Tag.fromUint8Array(data, edition, true));
}
toJSON(): object {
return this.rootTag.toJSON();
}
toJSONString(): string {
return JSONBig.stringify(this.toJSON());
}
toSNBT(compress: SNBTCompression): string {
return this.rootTag.toSNBT(compress);
}
toNBT(compression: BinaryCompression = 'none', edition: Edition): Uint8Array {
if (compression === 'gzip')
return gzip(this.rootTag.toUint8Array(edition));
return this.rootTag.toUint8Array(edition);
}
getRootTag(): Tag {
return this.rootTag;
}
setRootTag(rootTag: Tag): void {
if (!rootTag.isRoot())
throw new NBTError('Must be a root tag');
this.rootTag = rootTag;
}
};