@serenityjs/nbt 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,2 @@
1
+ # Nbt
2
+
@@ -0,0 +1,304 @@
1
+ import { BinaryStream, VarInt, Uint16 } from '@serenityjs/binarystream';
2
+
3
+ declare abstract class NBTTag<T = unknown> {
4
+ /**
5
+ * The type of the tag.
6
+ */
7
+ static readonly type: Tag;
8
+ /**
9
+ * The name of the tag.
10
+ */
11
+ readonly name: string;
12
+ /**
13
+ * The value of the tag.
14
+ */
15
+ readonly value: T;
16
+ /**
17
+ * Creates a new NBTTag.
18
+ *
19
+ * @param name The name of the tag.
20
+ * @param value The value of the tag.
21
+ * @returns A new NBTTag.
22
+ */
23
+ constructor(name: string, value: T);
24
+ /**
25
+ * Returns the value of the tag.
26
+ *
27
+ * @returns The value of the tag.
28
+ */
29
+ valueOf(_snbt?: boolean): string | unknown;
30
+ /**
31
+ * Reads a tag from the stream.
32
+ *
33
+ * @param _stream The stream to read from.
34
+ * @param _type Whether or not to read the type of the tag.
35
+ * @returns The tag that was read.
36
+ */
37
+ static read(_stream: BinaryStream, _varint: unknown, _type: unknown): typeof this.prototype;
38
+ /**
39
+ * Writes a tag to the stream.
40
+ *
41
+ * @param _stream The stream to write to.
42
+ */
43
+ static write(_stream: BinaryStream, _tag: NBTTag<unknown>, _varint: unknown): void;
44
+ protected static readString(stream: BinaryStream, varint?: boolean): string;
45
+ protected static writeString(value: string, stream: BinaryStream, varint?: boolean): void;
46
+ }
47
+
48
+ /**
49
+ * A tag that contains a byte value.
50
+ */
51
+ declare class ByteTag<T extends number = number> extends NBTTag<T> {
52
+ static readonly type = Tag.Byte;
53
+ valueOf(snbt?: boolean): number | string;
54
+ /**
55
+ * Reads a byte tag from the stream.
56
+ */
57
+ static read<T extends number>(stream: BinaryStream, varint?: boolean, type?: boolean): ByteTag<T>;
58
+ /**
59
+ * Writes a byte tag to the stream.
60
+ */
61
+ static write<T extends number>(stream: BinaryStream, tag: ByteTag<T>, varint?: boolean): void;
62
+ }
63
+
64
+ /**
65
+ * A tag that contains a short value.
66
+ */
67
+ declare class ShortTag<T extends number = number> extends NBTTag<T> {
68
+ static readonly type = Tag.Short;
69
+ valueOf(snbt?: boolean): number | string;
70
+ /**
71
+ * Reads a short tag from the stream.
72
+ */
73
+ static read<T extends number = number>(stream: BinaryStream, varint?: boolean, type?: boolean): ShortTag<T>;
74
+ /**
75
+ * Writes a short tag to the stream.
76
+ */
77
+ static write<T extends number = number>(stream: BinaryStream, tag: ShortTag<T>, varint?: boolean): void;
78
+ }
79
+
80
+ /**
81
+ * A tag that contains a int value.
82
+ */
83
+ declare class IntTag<T extends number = number> extends NBTTag<T> {
84
+ static readonly type = Tag.Int;
85
+ valueOf(snbt?: boolean): number | string;
86
+ /**
87
+ * Reads a int tag from the stream.
88
+ */
89
+ static read<T extends number = number>(stream: BinaryStream, varint?: boolean, type?: boolean): IntTag<T>;
90
+ /**
91
+ * Writes a int tag to the stream.
92
+ */
93
+ static write<T extends number = number>(stream: BinaryStream, tag: IntTag<T>, varint?: boolean): void;
94
+ }
95
+
96
+ /**
97
+ * A tag that contains a long value.
98
+ */
99
+ declare class LongTag<T extends bigint = bigint> extends NBTTag<T> {
100
+ static readonly type = Tag.Long;
101
+ valueOf(snbt?: boolean): bigint | string;
102
+ /**
103
+ * Reads a long tag from the stream.
104
+ */
105
+ static read<T extends bigint = bigint>(stream: BinaryStream, varint?: boolean, type?: boolean): LongTag<T>;
106
+ /**
107
+ * Writes a long tag to the stream.
108
+ */
109
+ static write<T extends bigint = bigint>(stream: BinaryStream, tag: LongTag<T>, varint?: boolean): void;
110
+ }
111
+
112
+ /**
113
+ * A tag that contains a float value.
114
+ */
115
+ declare class FloatTag<T extends number = number> extends NBTTag<T> {
116
+ static readonly type = Tag.Float;
117
+ valueOf(snbt?: boolean): number | string;
118
+ /**
119
+ * Reads a float tag from the stream.
120
+ */
121
+ static read<T extends number = number>(stream: BinaryStream, varint?: boolean, type?: boolean): FloatTag<T>;
122
+ /**
123
+ * Writes a float tag to the stream.
124
+ */
125
+ static write<T extends number = number>(stream: BinaryStream, tag: FloatTag<T>, varint?: boolean): void;
126
+ }
127
+
128
+ /**
129
+ * A tag that contains a double value.
130
+ */
131
+ declare class DoubleTag<T extends number = number> extends NBTTag<T> {
132
+ static readonly type = Tag.Double;
133
+ valueOf(snbt?: boolean): number | string;
134
+ /**
135
+ * Reads a double tag from the stream.
136
+ */
137
+ static read<T extends number = number>(stream: BinaryStream, varint?: boolean, type?: boolean): DoubleTag<T>;
138
+ /**
139
+ * Writes a double tag to the stream.
140
+ */
141
+ static write<T extends number = number>(stream: BinaryStream, tag: DoubleTag<T>, varint?: boolean): void;
142
+ }
143
+
144
+ /**
145
+ * A tag that contains a byte list value.
146
+ */
147
+ declare class ByteListTag extends NBTTag<Array<number>> {
148
+ static readonly type = Tag.ByteList;
149
+ /**
150
+ * Reads a byte list tag from the stream.
151
+ */
152
+ static read(stream: BinaryStream, varint?: boolean, type?: boolean): ByteListTag;
153
+ /**
154
+ * Writes a byte list tag to the stream.
155
+ */
156
+ static write(stream: BinaryStream, tag: ByteListTag, varint?: boolean): void;
157
+ }
158
+
159
+ /**
160
+ * A tag that contains a string value.
161
+ */
162
+ declare class StringTag<T extends string = string> extends NBTTag<T> {
163
+ static readonly type = Tag.String;
164
+ /**
165
+ * Reads a string tag from the stream.
166
+ */
167
+ static read<T extends string = string>(stream: BinaryStream, varint?: boolean, type?: boolean): StringTag<T>;
168
+ /**
169
+ * Writes a string tag to the stream.
170
+ */
171
+ static write<T extends string = string>(stream: BinaryStream, tag: StringTag<T>, varint?: boolean): void;
172
+ }
173
+
174
+ /**
175
+ * A tag that contains a compound list value.
176
+ */
177
+ declare class CompoundTag<T = Record<string, NBTTag<unknown>>> extends NBTTag<T> {
178
+ static readonly type = Tag.Compound;
179
+ /**
180
+ * Creates a new compound tag.
181
+ *
182
+ * @param name The name of the tag.
183
+ * @param value The value of the tag.
184
+ * @returns A new compound tag.
185
+ */
186
+ addTag(...tags: Array<NBTTag<T>>): this;
187
+ /**
188
+ * Removes a tag from the compound tag.
189
+ *
190
+ * @param name The name of the tag to remove.
191
+ */
192
+ removeTag(name: string): this;
193
+ /**
194
+ * Returns a tag from the compound tag.
195
+ *
196
+ * @param name The name of the tag to get.
197
+ * @returns The tag that was found.
198
+ */
199
+ getTag(name: string): NBTTag<T> | undefined;
200
+ /**
201
+ * Returns a tag from the compound tag.
202
+ *
203
+ * @param name The name of the tag to get.
204
+ * @returns The tag that was found.
205
+ */
206
+ hasTag(name: string): boolean;
207
+ /**
208
+ * Returns all the tags in the compound tag.
209
+ *
210
+ * @returns All the tags in the compound tag.
211
+ */
212
+ getTags(): Array<NBTTag<T>>;
213
+ valueOf<K = unknown>(snbt?: boolean): K | string;
214
+ /**
215
+ * Reads a compound tag from the stream.
216
+ */
217
+ static read<T = Record<string, NBTTag<unknown>>>(stream: BinaryStream, varint?: boolean, type?: boolean): CompoundTag<T>;
218
+ /**
219
+ * Writes a compound tag to the stream.
220
+ */
221
+ static write<T = unknown>(stream: BinaryStream, tag: CompoundTag<T>, varint?: boolean): void;
222
+ }
223
+
224
+ /**
225
+ * A tag that contains a list value.
226
+ */
227
+ declare class ListTag<T = unknown> extends NBTTag<Array<T>> {
228
+ static readonly type = Tag.List;
229
+ readonly type: Tag;
230
+ constructor(name: string, value: Array<T>, type: Tag);
231
+ valueOf(snbt?: boolean): string | unknown;
232
+ /**
233
+ * Reads a list tag from the stream.
234
+ */
235
+ static read<T>(stream: BinaryStream, varint?: boolean, type?: boolean): ListTag<T>;
236
+ /**
237
+ * Writes a list tag to the stream.
238
+ */
239
+ static write<T = unknown>(stream: BinaryStream, tag: ListTag<T>, varint?: boolean): void;
240
+ }
241
+
242
+ declare const NBT_TAGS: (typeof ByteTag | typeof ShortTag | typeof IntTag | typeof LongTag | typeof FloatTag | typeof DoubleTag | typeof ByteListTag | typeof StringTag | typeof CompoundTag | typeof ListTag)[];
243
+
244
+ declare enum Tag {
245
+ End = 0,
246
+ Byte = 1,
247
+ Short = 2,
248
+ Int = 3,
249
+ Long = 4,
250
+ Float = 5,
251
+ Double = 6,
252
+ ByteList = 7,
253
+ String = 8,
254
+ List = 9,
255
+ Compound = 10,
256
+ IntList = 11,
257
+ LongList = 12
258
+ }
259
+
260
+ declare class NamedBinaryTag extends BinaryStream {
261
+ readonly varint: boolean;
262
+ protected readonly length: typeof VarInt | typeof Uint16;
263
+ constructor(buffer?: Buffer, varint?: boolean);
264
+ protected readLength(): number;
265
+ protected writeLength(value: number): void;
266
+ readString(): string;
267
+ writeString(value: string): void;
268
+ /**
269
+ * Reads a tag from the stream.
270
+ * @returns The tag that was read.
271
+ */
272
+ readByteTag(): ByteTag;
273
+ /**
274
+ * Writes a tag to the stream.
275
+ * @param tag The tag to write.
276
+ */
277
+ writeByteTag(tag: ByteTag): void;
278
+ /**
279
+ * Reads a tag from the stream.
280
+ * @returns The tag that was read.
281
+ */
282
+ readShortTag(): ShortTag;
283
+ /**
284
+ * Writes a tag to the stream.
285
+ * @param tag The tag to write.
286
+ */
287
+ writeShortTag(tag: ShortTag): void;
288
+ readIntTag(): IntTag;
289
+ writeIntTag(tag: IntTag): void;
290
+ readLongTag(): LongTag;
291
+ writeLongTag(tag: LongTag): void;
292
+ readFloatTag(): FloatTag;
293
+ writeFloatTag(tag: FloatTag): void;
294
+ readDoubleTag(): DoubleTag;
295
+ writeDoubleTag(tag: DoubleTag): void;
296
+ readStringTag(): StringTag;
297
+ writeStringTag(tag: StringTag): void;
298
+ readCompoundTag<T = unknown>(): CompoundTag<T>;
299
+ writeCompoundTag<T = unknown>(tag: CompoundTag<T>): void;
300
+ readListTag<T = unknown>(): ListTag;
301
+ writeListTag<T = unknown>(tag: ListTag<T>): void;
302
+ }
303
+
304
+ export { ByteListTag, ByteTag, CompoundTag, DoubleTag, FloatTag, IntTag, ListTag, LongTag, NBTTag, NBT_TAGS, NamedBinaryTag, ShortTag, StringTag, Tag };
package/dist/index.js ADDED
@@ -0,0 +1,766 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ ByteListTag: () => ByteListTag,
24
+ ByteTag: () => ByteTag,
25
+ CompoundTag: () => CompoundTag,
26
+ DoubleTag: () => DoubleTag,
27
+ FloatTag: () => FloatTag,
28
+ IntTag: () => IntTag,
29
+ ListTag: () => ListTag,
30
+ LongTag: () => LongTag,
31
+ NBTTag: () => NBTTag,
32
+ NBT_TAGS: () => NBT_TAGS,
33
+ NamedBinaryTag: () => NamedBinaryTag,
34
+ ShortTag: () => ShortTag,
35
+ StringTag: () => StringTag,
36
+ Tag: () => Tag
37
+ });
38
+ module.exports = __toCommonJS(src_exports);
39
+
40
+ // src/named-binary-tag.ts
41
+ var import_binarystream9 = require("@serenityjs/binarystream");
42
+
43
+ // src/tags/tag.ts
44
+ var import_binarystream = require("@serenityjs/binarystream");
45
+ var NBTTag = class {
46
+ /**
47
+ * The type of the tag.
48
+ */
49
+ static type;
50
+ /**
51
+ * The name of the tag.
52
+ */
53
+ name;
54
+ /**
55
+ * The value of the tag.
56
+ */
57
+ value;
58
+ /**
59
+ * Creates a new NBTTag.
60
+ *
61
+ * @param name The name of the tag.
62
+ * @param value The value of the tag.
63
+ * @returns A new NBTTag.
64
+ */
65
+ constructor(name, value) {
66
+ this.name = name;
67
+ this.value = value;
68
+ }
69
+ /**
70
+ * Returns the value of the tag.
71
+ *
72
+ * @returns The value of the tag.
73
+ */
74
+ valueOf(_snbt) {
75
+ return this.value;
76
+ }
77
+ /**
78
+ * Reads a tag from the stream.
79
+ *
80
+ * @param _stream The stream to read from.
81
+ * @param _type Whether or not to read the type of the tag.
82
+ * @returns The tag that was read.
83
+ */
84
+ static read(_stream, _varint, _type) {
85
+ throw new Error("NBTTag.read() is not implemented.");
86
+ }
87
+ /**
88
+ * Writes a tag to the stream.
89
+ *
90
+ * @param _stream The stream to write to.
91
+ */
92
+ static write(_stream, _tag, _varint) {
93
+ throw new Error("NBTTag.write() is not implemented.");
94
+ }
95
+ static readString(stream, varint = false) {
96
+ const length = varint ? stream.readVarInt() : stream.readShort(import_binarystream.Endianness.Little);
97
+ const buffer = stream.readBuffer(length);
98
+ return String.fromCodePoint(...buffer);
99
+ }
100
+ static writeString(value, stream, varint = false) {
101
+ const buffer = Buffer.from(value);
102
+ varint ? stream.writeVarInt(buffer.length) : stream.writeShort(buffer.length, import_binarystream.Endianness.Little);
103
+ stream.writeBuffer(buffer);
104
+ }
105
+ };
106
+
107
+ // src/tags/byte.ts
108
+ var ByteTag = class _ByteTag extends NBTTag {
109
+ static type = 1 /* Byte */;
110
+ valueOf(snbt) {
111
+ return snbt ? this.value + "b" : this.value;
112
+ }
113
+ /**
114
+ * Reads a byte tag from the stream.
115
+ */
116
+ static read(stream, varint = false, type = true) {
117
+ if (type) {
118
+ const type2 = stream.readByte();
119
+ if (type2 !== this.type) {
120
+ throw new Error(`Expected tag type to be ${this.type} but got ${type2}`);
121
+ }
122
+ }
123
+ const name = this.readString(stream, varint);
124
+ const value = stream.readByte();
125
+ return new _ByteTag(name, value);
126
+ }
127
+ /**
128
+ * Writes a byte tag to the stream.
129
+ */
130
+ static write(stream, tag, varint = false) {
131
+ stream.writeByte(this.type);
132
+ this.writeString(tag.name, stream, varint);
133
+ stream.writeByte(tag.value);
134
+ }
135
+ };
136
+
137
+ // src/tags/short.ts
138
+ var import_binarystream2 = require("@serenityjs/binarystream");
139
+ var ShortTag = class _ShortTag extends NBTTag {
140
+ static type = 2 /* Short */;
141
+ valueOf(snbt) {
142
+ return snbt ? this.value + "s" : this.value;
143
+ }
144
+ /**
145
+ * Reads a short tag from the stream.
146
+ */
147
+ static read(stream, varint = false, type = true) {
148
+ if (type) {
149
+ const type2 = stream.readByte();
150
+ if (type2 !== this.type) {
151
+ throw new Error(`Expected tag type to be ${this.type} but got ${type2}`);
152
+ }
153
+ }
154
+ const name = this.readString(stream, varint);
155
+ const value = stream.readShort(import_binarystream2.Endianness.Little);
156
+ return new _ShortTag(name, value);
157
+ }
158
+ /**
159
+ * Writes a short tag to the stream.
160
+ */
161
+ static write(stream, tag, varint = false) {
162
+ stream.writeByte(this.type);
163
+ this.writeString(tag.name, stream, varint);
164
+ stream.writeShort(tag.value, import_binarystream2.Endianness.Little);
165
+ }
166
+ };
167
+
168
+ // src/tags/int.ts
169
+ var import_binarystream3 = require("@serenityjs/binarystream");
170
+ var IntTag = class _IntTag extends NBTTag {
171
+ static type = 3 /* Int */;
172
+ valueOf(snbt) {
173
+ return snbt ? this.value + "i" : this.value;
174
+ }
175
+ /**
176
+ * Reads a int tag from the stream.
177
+ */
178
+ static read(stream, varint = false, type = true) {
179
+ if (type) {
180
+ const type2 = stream.readByte();
181
+ if (type2 !== this.type) {
182
+ throw new Error(`Expected tag type to be ${this.type} but got ${type2}`);
183
+ }
184
+ }
185
+ const name = this.readString(stream, varint);
186
+ const value = varint ? stream.readVarInt() : stream.readInt32(import_binarystream3.Endianness.Little);
187
+ return new _IntTag(name, value);
188
+ }
189
+ /**
190
+ * Writes a int tag to the stream.
191
+ */
192
+ static write(stream, tag, varint = false) {
193
+ stream.writeByte(this.type);
194
+ this.writeString(tag.name, stream, varint);
195
+ varint ? stream.writeVarInt(tag.value) : stream.writeInt32(tag.value, import_binarystream3.Endianness.Little);
196
+ }
197
+ };
198
+
199
+ // src/tags/long.ts
200
+ var import_binarystream4 = require("@serenityjs/binarystream");
201
+ var LongTag = class _LongTag extends NBTTag {
202
+ static type = 4 /* Long */;
203
+ valueOf(snbt) {
204
+ return snbt ? this.value + "l" : this.value;
205
+ }
206
+ /**
207
+ * Reads a long tag from the stream.
208
+ */
209
+ static read(stream, varint = false, type = true) {
210
+ if (type) {
211
+ const type2 = stream.readByte();
212
+ if (type2 !== this.type) {
213
+ throw new Error(`Expected tag type to be ${this.type} but got ${type2}`);
214
+ }
215
+ }
216
+ const name = this.readString(stream, varint);
217
+ const value = varint ? stream.readVarLong() : stream.readLong(import_binarystream4.Endianness.Little);
218
+ return new _LongTag(name, value);
219
+ }
220
+ /**
221
+ * Writes a long tag to the stream.
222
+ */
223
+ static write(stream, tag, varint = false) {
224
+ stream.writeByte(this.type);
225
+ this.writeString(tag.name, stream, varint);
226
+ varint ? stream.writeVarLong(tag.value) : stream.writeLong(tag.value, import_binarystream4.Endianness.Little);
227
+ }
228
+ };
229
+
230
+ // src/tags/float.ts
231
+ var import_binarystream5 = require("@serenityjs/binarystream");
232
+ var FloatTag = class _FloatTag extends NBTTag {
233
+ static type = 5 /* Float */;
234
+ valueOf(snbt) {
235
+ return snbt ? this.value + "f" : this.value;
236
+ }
237
+ /**
238
+ * Reads a float tag from the stream.
239
+ */
240
+ static read(stream, varint = false, type = true) {
241
+ if (type) {
242
+ const type2 = stream.readByte();
243
+ if (type2 !== this.type) {
244
+ throw new Error(`Expected tag type to be ${this.type} but got ${type2}`);
245
+ }
246
+ }
247
+ const name = this.readString(stream, varint);
248
+ const value = stream.readFloat32(import_binarystream5.Endianness.Little);
249
+ return new _FloatTag(name, value);
250
+ }
251
+ /**
252
+ * Writes a float tag to the stream.
253
+ */
254
+ static write(stream, tag, varint = false) {
255
+ stream.writeByte(this.type);
256
+ this.writeString(tag.name, stream, varint);
257
+ stream.writeFloat32(tag.value, import_binarystream5.Endianness.Little);
258
+ }
259
+ };
260
+
261
+ // src/tags/double.ts
262
+ var import_binarystream6 = require("@serenityjs/binarystream");
263
+ var DoubleTag = class _DoubleTag extends NBTTag {
264
+ static type = 6 /* Double */;
265
+ valueOf(snbt) {
266
+ return snbt ? this.value + "d" : this.value;
267
+ }
268
+ /**
269
+ * Reads a double tag from the stream.
270
+ */
271
+ static read(stream, varint = false, type = true) {
272
+ if (type) {
273
+ const type2 = stream.readByte();
274
+ if (type2 !== this.type) {
275
+ throw new Error(`Expected tag type to be ${this.type} but got ${type2}`);
276
+ }
277
+ }
278
+ const name = this.readString(stream, varint);
279
+ const value = stream.readFloat64(import_binarystream6.Endianness.Little);
280
+ return new _DoubleTag(name, value);
281
+ }
282
+ /**
283
+ * Writes a double tag to the stream.
284
+ */
285
+ static write(stream, tag, varint = false) {
286
+ stream.writeByte(this.type);
287
+ this.writeString(tag.name, stream, varint);
288
+ stream.writeFloat64(tag.value, import_binarystream6.Endianness.Little);
289
+ }
290
+ };
291
+
292
+ // src/tags/byte-list.ts
293
+ var import_binarystream7 = require("@serenityjs/binarystream");
294
+ var ByteListTag = class _ByteListTag extends NBTTag {
295
+ static type = 7 /* ByteList */;
296
+ /**
297
+ * Reads a byte list tag from the stream.
298
+ */
299
+ static read(stream, varint = false, type = true) {
300
+ if (type) {
301
+ const type2 = stream.readByte();
302
+ if (type2 !== this.type) {
303
+ throw new Error(`Expected tag type to be ${this.type} but got ${type2}`);
304
+ }
305
+ }
306
+ const name = this.readString(stream, varint);
307
+ const length = varint ? stream.readVarInt() : stream.readInt32(import_binarystream7.Endianness.Little);
308
+ const value = stream.read(length);
309
+ return new _ByteListTag(name, value);
310
+ }
311
+ /**
312
+ * Writes a byte list tag to the stream.
313
+ */
314
+ static write(stream, tag, varint = false) {
315
+ stream.writeByte(this.type);
316
+ this.writeString(tag.name, stream, varint);
317
+ varint ? stream.writeVarInt(tag.value.length) : stream.writeInt32(tag.value.length, import_binarystream7.Endianness.Little);
318
+ stream.write(tag.value);
319
+ }
320
+ };
321
+
322
+ // src/tags/string.ts
323
+ var StringTag = class _StringTag extends NBTTag {
324
+ static type = 8 /* String */;
325
+ /**
326
+ * Reads a string tag from the stream.
327
+ */
328
+ static read(stream, varint = false, type = true) {
329
+ if (type) {
330
+ const type2 = stream.readByte();
331
+ if (type2 !== this.type) {
332
+ throw new Error(`Expected tag type to be ${this.type} but got ${type2}`);
333
+ }
334
+ }
335
+ const name = this.readString(stream, varint);
336
+ const value = this.readString(stream, varint);
337
+ return new _StringTag(name, value);
338
+ }
339
+ /**
340
+ * Writes a string tag to the stream.
341
+ */
342
+ static write(stream, tag, varint = false) {
343
+ stream.writeByte(this.type);
344
+ this.writeString(tag.name, stream, varint);
345
+ this.writeString(tag.value, stream, varint);
346
+ }
347
+ };
348
+
349
+ // src/tags/compound.ts
350
+ var CompoundTag = class _CompoundTag extends NBTTag {
351
+ static type = 10 /* Compound */;
352
+ /**
353
+ * Creates a new compound tag.
354
+ *
355
+ * @param name The name of the tag.
356
+ * @param value The value of the tag.
357
+ * @returns A new compound tag.
358
+ */
359
+ addTag(...tags) {
360
+ for (const tag of tags) {
361
+ this.value[tag.name] = tag;
362
+ }
363
+ return this;
364
+ }
365
+ /**
366
+ * Removes a tag from the compound tag.
367
+ *
368
+ * @param name The name of the tag to remove.
369
+ */
370
+ removeTag(name) {
371
+ delete this.value[name];
372
+ return this;
373
+ }
374
+ /**
375
+ * Returns a tag from the compound tag.
376
+ *
377
+ * @param name The name of the tag to get.
378
+ * @returns The tag that was found.
379
+ */
380
+ getTag(name) {
381
+ return this.value[name];
382
+ }
383
+ /**
384
+ * Returns a tag from the compound tag.
385
+ *
386
+ * @param name The name of the tag to get.
387
+ * @returns The tag that was found.
388
+ */
389
+ hasTag(name) {
390
+ return name in this.value;
391
+ }
392
+ /**
393
+ * Returns all the tags in the compound tag.
394
+ *
395
+ * @returns All the tags in the compound tag.
396
+ */
397
+ getTags() {
398
+ return Object.values(this.value);
399
+ }
400
+ valueOf(snbt) {
401
+ if (snbt) {
402
+ const value = {};
403
+ for (const key in this.value) {
404
+ value[key] = this.value[key] instanceof _CompoundTag ? JSON.parse(
405
+ this.value[key].valueOf(
406
+ true
407
+ )
408
+ ) : this.value[key].valueOf(true);
409
+ }
410
+ return JSON.stringify(value);
411
+ } else {
412
+ const value = {};
413
+ for (const key in this.value) {
414
+ value[key] = this.value[key].valueOf();
415
+ }
416
+ return value;
417
+ }
418
+ }
419
+ /**
420
+ * Reads a compound tag from the stream.
421
+ */
422
+ static read(stream, varint = false, type = true) {
423
+ if (type) {
424
+ const type2 = stream.readByte();
425
+ if (type2 !== this.type) {
426
+ throw new Error(`Expected tag type to be ${this.type} but got ${type2}`);
427
+ }
428
+ }
429
+ const name = this.readString(stream, varint);
430
+ const value = {};
431
+ do {
432
+ const type2 = stream.readByte();
433
+ if (type2 === 0 /* End */)
434
+ break;
435
+ const reader = NBT_TAGS.find((tag) => tag.type === type2);
436
+ if (!reader) {
437
+ throw new Error(`Unknown tag type: ${type2} at ${stream.offset}`);
438
+ }
439
+ const read = reader.read(stream, varint, false);
440
+ value[read.name] = read;
441
+ } while (!stream.cursorAtEnd());
442
+ return new _CompoundTag(name, value);
443
+ }
444
+ /**
445
+ * Writes a compound tag to the stream.
446
+ */
447
+ static write(stream, tag, varint = false) {
448
+ stream.writeByte(this.type);
449
+ this.writeString(tag.name, stream, varint);
450
+ for (const key in tag.value) {
451
+ const type = tag.value[key];
452
+ const writter = NBT_TAGS.find(
453
+ (tag2) => type instanceof tag2
454
+ );
455
+ if (!writter) {
456
+ throw new Error(`Unknown tag type: ${type}`);
457
+ }
458
+ writter.write(stream, type, varint);
459
+ }
460
+ stream.writeByte(0 /* End */);
461
+ }
462
+ };
463
+
464
+ // src/tags/list.ts
465
+ var import_binarystream8 = require("@serenityjs/binarystream");
466
+ var ListTag = class _ListTag extends NBTTag {
467
+ static type = 9 /* List */;
468
+ type;
469
+ constructor(name, value, type) {
470
+ super(name, value);
471
+ this.type = type;
472
+ }
473
+ valueOf(snbt) {
474
+ if (snbt) {
475
+ const values = [];
476
+ for (const key in this.value) {
477
+ values.push(this.value[key].valueOf() + "");
478
+ }
479
+ return JSON.stringify(values);
480
+ } else {
481
+ const values = [];
482
+ for (const key in this.value) {
483
+ values.push(this.value[key].valueOf());
484
+ }
485
+ return values;
486
+ }
487
+ }
488
+ /**
489
+ * Reads a list tag from the stream.
490
+ */
491
+ static read(stream, varint = false, type = true) {
492
+ if (type) {
493
+ const type2 = stream.readByte();
494
+ if (type2 !== this.type) {
495
+ throw new Error(`Expected tag type to be ${this.type} but got ${type2}`);
496
+ }
497
+ }
498
+ const name = this.readString(stream, varint);
499
+ const tag = stream.readByte();
500
+ const length = varint ? stream.readVarInt() : stream.readInt32(import_binarystream8.Endianness.Little);
501
+ const values = [];
502
+ for (let index = 0; index < length; index++) {
503
+ switch (tag) {
504
+ default: {
505
+ throw new Error(
506
+ `Tag type '${Tag[tag]}' has not been implemented for ListTag. ${stream.offset}`
507
+ );
508
+ }
509
+ case 1 /* Byte */: {
510
+ values.push(stream.readByte());
511
+ break;
512
+ }
513
+ case 2 /* Short */: {
514
+ values.push(stream.readInt16(import_binarystream8.Endianness.Little));
515
+ break;
516
+ }
517
+ case 3 /* Int */: {
518
+ values.push(
519
+ varint ? stream.readVarInt() : stream.readInt32(import_binarystream8.Endianness.Little)
520
+ );
521
+ break;
522
+ }
523
+ case 4 /* Long */: {
524
+ values.push(
525
+ varint ? stream.readVarLong() : stream.readLong(import_binarystream8.Endianness.Little)
526
+ );
527
+ break;
528
+ }
529
+ case 5 /* Float */: {
530
+ values.push(stream.readFloat32(import_binarystream8.Endianness.Little));
531
+ break;
532
+ }
533
+ case 6 /* Double */: {
534
+ values.push(stream.readFloat64(import_binarystream8.Endianness.Little));
535
+ break;
536
+ }
537
+ case 8 /* String */: {
538
+ values.push(this.readString(stream, varint));
539
+ break;
540
+ }
541
+ case 10 /* Compound */: {
542
+ const value = {};
543
+ do {
544
+ const type2 = stream.readByte();
545
+ if (type2 === 0 /* End */)
546
+ break;
547
+ const reader = NBT_TAGS.find(
548
+ (tag2) => tag2.type === type2
549
+ );
550
+ if (!reader) {
551
+ throw new Error(`Unknown tag type: ${type2} at ${stream.offset}`);
552
+ }
553
+ const read = reader.read(stream, varint, false);
554
+ value[read.name] = read;
555
+ } while (!stream.cursorAtEnd());
556
+ values.push(new CompoundTag("", value));
557
+ }
558
+ }
559
+ }
560
+ return new _ListTag(name, values, tag);
561
+ }
562
+ /**
563
+ * Writes a list tag to the stream.
564
+ */
565
+ static write(stream, tag, varint = false) {
566
+ stream.writeByte(this.type);
567
+ this.writeString(tag.name, stream, varint);
568
+ stream.writeByte(tag.type);
569
+ varint ? stream.writeVarInt(tag.value.length) : stream.writeInt32(tag.value.length, import_binarystream8.Endianness.Little);
570
+ for (const value of tag.value) {
571
+ switch (tag.type) {
572
+ default: {
573
+ throw new Error(
574
+ `Tag type '${Tag[tag.type]}' has not been implemented for ListTag.`
575
+ );
576
+ }
577
+ case 1 /* Byte */: {
578
+ stream.writeByte(value);
579
+ break;
580
+ }
581
+ case 2 /* Short */: {
582
+ stream.writeInt16(value, import_binarystream8.Endianness.Little);
583
+ break;
584
+ }
585
+ case 3 /* Int */: {
586
+ varint ? stream.writeVarInt(value) : stream.writeInt32(value, import_binarystream8.Endianness.Little);
587
+ break;
588
+ }
589
+ case 4 /* Long */: {
590
+ varint ? stream.writeVarLong(value) : stream.writeLong(value, import_binarystream8.Endianness.Little);
591
+ break;
592
+ }
593
+ case 5 /* Float */: {
594
+ stream.writeFloat32(value, import_binarystream8.Endianness.Little);
595
+ break;
596
+ }
597
+ case 6 /* Double */: {
598
+ stream.writeFloat64(value, import_binarystream8.Endianness.Little);
599
+ break;
600
+ }
601
+ case 8 /* String */: {
602
+ this.writeString(value, stream, varint);
603
+ break;
604
+ }
605
+ case 10 /* Compound */: {
606
+ const compound = value;
607
+ for (const key in compound.value) {
608
+ const type = compound.value[key];
609
+ const writter = NBT_TAGS.find(
610
+ (tag2) => type instanceof tag2
611
+ );
612
+ if (!writter) {
613
+ throw new Error(`Unknown tag type: ${type}`);
614
+ }
615
+ writter.write(stream, type, varint);
616
+ }
617
+ break;
618
+ }
619
+ }
620
+ }
621
+ }
622
+ };
623
+
624
+ // src/tags/tags.ts
625
+ var NBT_TAGS = [
626
+ ByteTag,
627
+ ShortTag,
628
+ IntTag,
629
+ LongTag,
630
+ FloatTag,
631
+ DoubleTag,
632
+ ByteListTag,
633
+ StringTag,
634
+ ListTag,
635
+ CompoundTag
636
+ ];
637
+
638
+ // src/named-binary-tag.ts
639
+ var Tag = /* @__PURE__ */ ((Tag2) => {
640
+ Tag2[Tag2["End"] = 0] = "End";
641
+ Tag2[Tag2["Byte"] = 1] = "Byte";
642
+ Tag2[Tag2["Short"] = 2] = "Short";
643
+ Tag2[Tag2["Int"] = 3] = "Int";
644
+ Tag2[Tag2["Long"] = 4] = "Long";
645
+ Tag2[Tag2["Float"] = 5] = "Float";
646
+ Tag2[Tag2["Double"] = 6] = "Double";
647
+ Tag2[Tag2["ByteList"] = 7] = "ByteList";
648
+ Tag2[Tag2["String"] = 8] = "String";
649
+ Tag2[Tag2["List"] = 9] = "List";
650
+ Tag2[Tag2["Compound"] = 10] = "Compound";
651
+ Tag2[Tag2["IntList"] = 11] = "IntList";
652
+ Tag2[Tag2["LongList"] = 12] = "LongList";
653
+ return Tag2;
654
+ })(Tag || {});
655
+ var NamedBinaryTag = class extends import_binarystream9.BinaryStream {
656
+ varint;
657
+ length;
658
+ constructor(buffer, varint = false) {
659
+ super(buffer);
660
+ this.varint = varint;
661
+ this.length = varint ? import_binarystream9.VarInt : import_binarystream9.Uint16;
662
+ }
663
+ readLength() {
664
+ return this.length.read(this, import_binarystream9.Endianness.Little);
665
+ }
666
+ writeLength(value) {
667
+ this.length.write(this, value, import_binarystream9.Endianness.Little);
668
+ }
669
+ readString() {
670
+ const length = this.readLength();
671
+ const buffer = this.readBuffer(length);
672
+ return String.fromCodePoint(...buffer);
673
+ }
674
+ writeString(value) {
675
+ const buffer = Buffer.from(value);
676
+ this.writeLength(buffer.length);
677
+ this.writeBuffer(buffer);
678
+ }
679
+ /**
680
+ * Reads a tag from the stream.
681
+ * @returns The tag that was read.
682
+ */
683
+ readByteTag() {
684
+ return ByteTag.read(this);
685
+ }
686
+ /**
687
+ * Writes a tag to the stream.
688
+ * @param tag The tag to write.
689
+ */
690
+ writeByteTag(tag) {
691
+ ByteTag.write(this, tag);
692
+ }
693
+ /**
694
+ * Reads a tag from the stream.
695
+ * @returns The tag that was read.
696
+ */
697
+ readShortTag() {
698
+ return ShortTag.read(this);
699
+ }
700
+ /**
701
+ * Writes a tag to the stream.
702
+ * @param tag The tag to write.
703
+ */
704
+ writeShortTag(tag) {
705
+ ShortTag.write(this, tag);
706
+ }
707
+ readIntTag() {
708
+ return IntTag.read(this);
709
+ }
710
+ writeIntTag(tag) {
711
+ IntTag.write(this, tag);
712
+ }
713
+ readLongTag() {
714
+ return LongTag.read(this);
715
+ }
716
+ writeLongTag(tag) {
717
+ LongTag.write(this, tag);
718
+ }
719
+ readFloatTag() {
720
+ return FloatTag.read(this);
721
+ }
722
+ writeFloatTag(tag) {
723
+ FloatTag.write(this, tag);
724
+ }
725
+ readDoubleTag() {
726
+ return DoubleTag.read(this);
727
+ }
728
+ writeDoubleTag(tag) {
729
+ DoubleTag.write(this, tag);
730
+ }
731
+ readStringTag() {
732
+ return StringTag.read(this);
733
+ }
734
+ writeStringTag(tag) {
735
+ StringTag.write(this, tag);
736
+ }
737
+ readCompoundTag() {
738
+ return CompoundTag.read(this);
739
+ }
740
+ writeCompoundTag(tag) {
741
+ CompoundTag.write(this, tag);
742
+ }
743
+ readListTag() {
744
+ return ListTag.read(this);
745
+ }
746
+ writeListTag(tag) {
747
+ ListTag.write(this, tag);
748
+ }
749
+ };
750
+ // Annotate the CommonJS export names for ESM import in node:
751
+ 0 && (module.exports = {
752
+ ByteListTag,
753
+ ByteTag,
754
+ CompoundTag,
755
+ DoubleTag,
756
+ FloatTag,
757
+ IntTag,
758
+ ListTag,
759
+ LongTag,
760
+ NBTTag,
761
+ NBT_TAGS,
762
+ NamedBinaryTag,
763
+ ShortTag,
764
+ StringTag,
765
+ Tag
766
+ });
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@serenityjs/nbt",
3
+ "version": "0.0.0",
4
+ "main": "./dist/index.js",
5
+ "types": "./dist/index.d.ts",
6
+ "repository": "https://github.com/SerenityJS/serenity",
7
+ "files": [
8
+ "dist/**"
9
+ ],
10
+ "scripts": {
11
+ "build": "tsup",
12
+ "watch": "tsup --watch",
13
+ "lint": "eslint src/",
14
+ "typecheck": "tsc --noEmit",
15
+ "test": "jest"
16
+ },
17
+ "jest": {
18
+ "preset": "@serenityjs/jest-presets/jest/node"
19
+ },
20
+ "devDependencies": {
21
+ "@serenityjs/eslint-config": "*",
22
+ "@serenityjs/jest-presets": "*",
23
+ "@serenityjs/typescript-config": "*",
24
+ "@types/jest": "^29.5.12",
25
+ "@types/node": "^20.11.24",
26
+ "jest": "^29.7.0",
27
+ "tsup": "^8.0.2",
28
+ "typescript": "^5.4.2"
29
+ },
30
+ "dependencies": {
31
+ "@serenityjs/binarystream": "^2.5.2"
32
+ }
33
+ }