packet-events-js 1.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.
Files changed (40) hide show
  1. package/README.md +398 -0
  2. package/package.json +31 -0
  3. package/src/auth/AuthHandler.js +138 -0
  4. package/src/auth/MojangAPI.js +186 -0
  5. package/src/client/MinecraftClient.js +336 -0
  6. package/src/crypto/Encryption.js +125 -0
  7. package/src/events/EventEmitter.js +267 -0
  8. package/src/events/PacketEvent.js +78 -0
  9. package/src/index.js +18 -0
  10. package/src/manager/PacketManager.js +258 -0
  11. package/src/protocol/ConnectionState.js +37 -0
  12. package/src/protocol/PacketDirection.js +8 -0
  13. package/src/protocol/ProtocolVersion.js +141 -0
  14. package/src/protocol/packets/Packet.js +119 -0
  15. package/src/protocol/packets/PacketRegistry.js +145 -0
  16. package/src/protocol/packets/handshake/HandshakePacket.js +44 -0
  17. package/src/protocol/packets/index.js +265 -0
  18. package/src/protocol/packets/login/DisconnectPacket.js +71 -0
  19. package/src/protocol/packets/login/EncryptionRequestPacket.js +47 -0
  20. package/src/protocol/packets/login/EncryptionResponsePacket.js +34 -0
  21. package/src/protocol/packets/login/LoginStartPacket.js +35 -0
  22. package/src/protocol/packets/login/LoginSuccessPacket.js +61 -0
  23. package/src/protocol/packets/login/SetCompressionPacket.js +29 -0
  24. package/src/protocol/packets/play/ChatPacket.js +238 -0
  25. package/src/protocol/packets/play/ChunkPacket.js +122 -0
  26. package/src/protocol/packets/play/EntityPacket.js +302 -0
  27. package/src/protocol/packets/play/KeepAlivePacket.js +55 -0
  28. package/src/protocol/packets/play/PlayerPositionPacket.js +266 -0
  29. package/src/protocol/packets/status/PingPacket.js +29 -0
  30. package/src/protocol/packets/status/PongPacket.js +29 -0
  31. package/src/protocol/packets/status/StatusRequestPacket.js +20 -0
  32. package/src/protocol/packets/status/StatusResponsePacket.js +58 -0
  33. package/src/protocol/types/NBT.js +594 -0
  34. package/src/protocol/types/Position.js +125 -0
  35. package/src/protocol/types/TextComponent.js +355 -0
  36. package/src/protocol/types/UUID.js +105 -0
  37. package/src/protocol/types/VarInt.js +144 -0
  38. package/src/protocol/types/index.js +5 -0
  39. package/src/utils/Logger.js +207 -0
  40. package/src/utils/PacketBuffer.js +389 -0
@@ -0,0 +1,594 @@
1
+ import { encodeVarInt, decodeVarInt } from './VarInt.js';
2
+
3
+ export const NBTTagType = Object.freeze({
4
+ END: 0,
5
+ BYTE: 1,
6
+ SHORT: 2,
7
+ INT: 3,
8
+ LONG: 4,
9
+ FLOAT: 5,
10
+ DOUBLE: 6,
11
+ BYTE_ARRAY: 7,
12
+ STRING: 8,
13
+ LIST: 9,
14
+ COMPOUND: 10,
15
+ INT_ARRAY: 11,
16
+ LONG_ARRAY: 12
17
+ });
18
+
19
+ export class NBTTag {
20
+
21
+ constructor(type, name = '') {
22
+ this.type = type;
23
+ this.name = name;
24
+ }
25
+
26
+ getSize() {
27
+ throw new Error('Not implemented');
28
+ }
29
+ }
30
+
31
+ export class NBTCompound extends NBTTag {
32
+
33
+ constructor(name = '', children = new Map()) {
34
+ super(NBTTagType.COMPOUND, name);
35
+ this.children = children;
36
+ }
37
+
38
+ set(name, tag) {
39
+ tag.name = name;
40
+ this.children.set(name, tag);
41
+ return this;
42
+ }
43
+
44
+ get(name) {
45
+ return this.children.get(name);
46
+ }
47
+
48
+ has(name) {
49
+ return this.children.has(name);
50
+ }
51
+
52
+ delete(name) {
53
+ return this.children.delete(name);
54
+ }
55
+
56
+ setByte(name, value) {
57
+ return this.set(name, new NBTByte(name, value));
58
+ }
59
+
60
+ setShort(name, value) {
61
+ return this.set(name, new NBTShort(name, value));
62
+ }
63
+
64
+ setInt(name, value) {
65
+ return this.set(name, new NBTInt(name, value));
66
+ }
67
+
68
+ setLong(name, value) {
69
+ return this.set(name, new NBTLong(name, value));
70
+ }
71
+
72
+ setFloat(name, value) {
73
+ return this.set(name, new NBTFloat(name, value));
74
+ }
75
+
76
+ setDouble(name, value) {
77
+ return this.set(name, new NBTDouble(name, value));
78
+ }
79
+
80
+ setString(name, value) {
81
+ return this.set(name, new NBTString(name, value));
82
+ }
83
+
84
+ setByteArray(name, value) {
85
+ return this.set(name, new NBTByteArray(name, value));
86
+ }
87
+
88
+ setIntArray(name, value) {
89
+ return this.set(name, new NBTIntArray(name, value));
90
+ }
91
+
92
+ setLongArray(name, value) {
93
+ return this.set(name, new NBTLongArray(name, value));
94
+ }
95
+
96
+ setList(name, listType, values) {
97
+ return this.set(name, new NBTList(name, listType, values));
98
+ }
99
+
100
+ setCompound(name, compound) {
101
+ compound.name = name;
102
+ this.children.set(name, compound);
103
+ return this;
104
+ }
105
+
106
+ toJSON() {
107
+ const obj = {};
108
+ for (const [key, tag] of this.children) {
109
+ if (tag instanceof NBTCompound) {
110
+ obj[key] = tag.toJSON();
111
+ } else if (tag instanceof NBTList) {
112
+ obj[key] = tag.toJSON();
113
+ } else {
114
+ obj[key] = tag.value;
115
+ }
116
+ }
117
+ return obj;
118
+ }
119
+ }
120
+
121
+ export class NBTByte extends NBTTag {
122
+ constructor(name = '', value = 0) {
123
+ super(NBTTagType.BYTE, name);
124
+ this.value = value << 24 >> 24;
125
+ }
126
+ }
127
+
128
+ export class NBTShort extends NBTTag {
129
+ constructor(name = '', value = 0) {
130
+ super(NBTTagType.SHORT, name);
131
+ this.value = value << 16 >> 16;
132
+ }
133
+ }
134
+
135
+ export class NBTInt extends NBTTag {
136
+ constructor(name = '', value = 0) {
137
+ super(NBTTagType.INT, name);
138
+ this.value = value | 0;
139
+ }
140
+ }
141
+
142
+ export class NBTLong extends NBTTag {
143
+ constructor(name = '', value = 0n) {
144
+ super(NBTTagType.LONG, name);
145
+ this.value = BigInt(value);
146
+ }
147
+ }
148
+
149
+ export class NBTFloat extends NBTTag {
150
+ constructor(name = '', value = 0.0) {
151
+ super(NBTTagType.FLOAT, name);
152
+ this.value = value;
153
+ }
154
+ }
155
+
156
+ export class NBTDouble extends NBTTag {
157
+ constructor(name = '', value = 0.0) {
158
+ super(NBTTagType.DOUBLE, name);
159
+ this.value = value;
160
+ }
161
+ }
162
+
163
+ export class NBTString extends NBTTag {
164
+ constructor(name = '', value = '') {
165
+ super(NBTTagType.STRING, name);
166
+ this.value = value;
167
+ }
168
+ }
169
+
170
+ export class NBTByteArray extends NBTTag {
171
+ constructor(name = '', value = []) {
172
+ super(NBTTagType.BYTE_ARRAY, name);
173
+ this.value = Array.isArray(value) ? value : Array.from(value);
174
+ }
175
+ }
176
+
177
+ export class NBTIntArray extends NBTTag {
178
+ constructor(name = '', value = []) {
179
+ super(NBTTagType.INT_ARRAY, name);
180
+ this.value = value;
181
+ }
182
+ }
183
+
184
+ export class NBTLongArray extends NBTTag {
185
+ constructor(name = '', value = []) {
186
+ super(NBTTagType.LONG_ARRAY, name);
187
+ this.value = value.map(v => BigInt(v));
188
+ }
189
+ }
190
+
191
+ export class NBTList extends NBTTag {
192
+ constructor(name = '', listType = NBTTagType.END, values = []) {
193
+ super(NBTTagType.LIST, name);
194
+ this.listType = listType;
195
+ this.values = values;
196
+ }
197
+
198
+ push(value) {
199
+ this.values.push(value);
200
+ }
201
+
202
+ get(index) {
203
+ return this.values[index];
204
+ }
205
+
206
+ get length() {
207
+ return this.values.length;
208
+ }
209
+
210
+ toJSON() {
211
+ return this.values.map(tag => {
212
+ if (tag instanceof NBTCompound) return tag.toJSON();
213
+ if (tag instanceof NBTList) return tag.toJSON();
214
+ return tag.value;
215
+ });
216
+ }
217
+ }
218
+
219
+ export class NBTReader {
220
+ constructor(buffer, offset = 0) {
221
+ this.buffer = buffer;
222
+ this.offset = offset;
223
+ }
224
+
225
+ readByte() {
226
+ return this.buffer.readInt8(this.offset++);
227
+ }
228
+
229
+ readUByte() {
230
+ return this.buffer.readUInt8(this.offset++);
231
+ }
232
+
233
+ readShort() {
234
+ const value = this.buffer.readInt16BE(this.offset);
235
+ this.offset += 2;
236
+ return value;
237
+ }
238
+
239
+ readInt() {
240
+ const value = this.buffer.readInt32BE(this.offset);
241
+ this.offset += 4;
242
+ return value;
243
+ }
244
+
245
+ readLong() {
246
+ const value = this.buffer.readBigInt64BE(this.offset);
247
+ this.offset += 8;
248
+ return value;
249
+ }
250
+
251
+ readFloat() {
252
+ const value = this.buffer.readFloatBE(this.offset);
253
+ this.offset += 4;
254
+ return value;
255
+ }
256
+
257
+ readDouble() {
258
+ const value = this.buffer.readDoubleBE(this.offset);
259
+ this.offset += 8;
260
+ return value;
261
+ }
262
+
263
+ readString() {
264
+ const length = this.readShort();
265
+ const value = this.buffer.toString('utf8', this.offset, this.offset + length);
266
+ this.offset += length;
267
+ return value;
268
+ }
269
+
270
+ read(named = true) {
271
+ const type = this.readUByte();
272
+
273
+ if (type === NBTTagType.END) {
274
+ return null;
275
+ }
276
+
277
+ if (type !== NBTTagType.COMPOUND) {
278
+ throw new Error(`Expected compound tag, got ${type}`);
279
+ }
280
+
281
+ const name = named ? this.readString() : '';
282
+ return this.readCompound(name);
283
+ }
284
+
285
+ readTag(type, name = '') {
286
+ switch (type) {
287
+ case NBTTagType.END:
288
+ return null;
289
+ case NBTTagType.BYTE:
290
+ return new NBTByte(name, this.readByte());
291
+ case NBTTagType.SHORT:
292
+ return new NBTShort(name, this.readShort());
293
+ case NBTTagType.INT:
294
+ return new NBTInt(name, this.readInt());
295
+ case NBTTagType.LONG:
296
+ return new NBTLong(name, this.readLong());
297
+ case NBTTagType.FLOAT:
298
+ return new NBTFloat(name, this.readFloat());
299
+ case NBTTagType.DOUBLE:
300
+ return new NBTDouble(name, this.readDouble());
301
+ case NBTTagType.BYTE_ARRAY:
302
+ return this.readByteArray(name);
303
+ case NBTTagType.STRING:
304
+ return new NBTString(name, this.readString());
305
+ case NBTTagType.LIST:
306
+ return this.readList(name);
307
+ case NBTTagType.COMPOUND:
308
+ return this.readCompound(name);
309
+ case NBTTagType.INT_ARRAY:
310
+ return this.readIntArray(name);
311
+ case NBTTagType.LONG_ARRAY:
312
+ return this.readLongArray(name);
313
+ default:
314
+ throw new Error(`Unknown NBT tag type: ${type}`);
315
+ }
316
+ }
317
+
318
+ readCompound(name = '') {
319
+ const compound = new NBTCompound(name);
320
+
321
+ while (true) {
322
+ const type = this.readUByte();
323
+
324
+ if (type === NBTTagType.END) {
325
+ break;
326
+ }
327
+
328
+ const tagName = this.readString();
329
+ const tag = this.readTag(type, tagName);
330
+ compound.children.set(tagName, tag);
331
+ }
332
+
333
+ return compound;
334
+ }
335
+
336
+ readList(name = '') {
337
+ const listType = this.readUByte();
338
+ const length = this.readInt();
339
+ const values = [];
340
+
341
+ for (let i = 0; i < length; i++) {
342
+ values.push(this.readTag(listType, ''));
343
+ }
344
+
345
+ return new NBTList(name, listType, values);
346
+ }
347
+
348
+ readByteArray(name = '') {
349
+ const length = this.readInt();
350
+ const values = [];
351
+
352
+ for (let i = 0; i < length; i++) {
353
+ values.push(this.readByte());
354
+ }
355
+
356
+ return new NBTByteArray(name, values);
357
+ }
358
+
359
+ readIntArray(name = '') {
360
+ const length = this.readInt();
361
+ const values = [];
362
+
363
+ for (let i = 0; i < length; i++) {
364
+ values.push(this.readInt());
365
+ }
366
+
367
+ return new NBTIntArray(name, values);
368
+ }
369
+
370
+ readLongArray(name = '') {
371
+ const length = this.readInt();
372
+ const values = [];
373
+
374
+ for (let i = 0; i < length; i++) {
375
+ values.push(this.readLong());
376
+ }
377
+
378
+ return new NBTLongArray(name, values);
379
+ }
380
+ }
381
+
382
+ export class NBTWriter {
383
+ constructor() {
384
+ this.buffers = [];
385
+ }
386
+
387
+ writeByte(value) {
388
+ const buf = Buffer.alloc(1);
389
+ buf.writeInt8(value);
390
+ this.buffers.push(buf);
391
+ }
392
+
393
+ writeUByte(value) {
394
+ const buf = Buffer.alloc(1);
395
+ buf.writeUInt8(value);
396
+ this.buffers.push(buf);
397
+ }
398
+
399
+ writeShort(value) {
400
+ const buf = Buffer.alloc(2);
401
+ buf.writeInt16BE(value);
402
+ this.buffers.push(buf);
403
+ }
404
+
405
+ writeInt(value) {
406
+ const buf = Buffer.alloc(4);
407
+ buf.writeInt32BE(value);
408
+ this.buffers.push(buf);
409
+ }
410
+
411
+ writeLong(value) {
412
+ const buf = Buffer.alloc(8);
413
+ buf.writeBigInt64BE(BigInt(value));
414
+ this.buffers.push(buf);
415
+ }
416
+
417
+ writeFloat(value) {
418
+ const buf = Buffer.alloc(4);
419
+ buf.writeFloatBE(value);
420
+ this.buffers.push(buf);
421
+ }
422
+
423
+ writeDouble(value) {
424
+ const buf = Buffer.alloc(8);
425
+ buf.writeDoubleBE(value);
426
+ this.buffers.push(buf);
427
+ }
428
+
429
+ writeString(value) {
430
+ const strBuf = Buffer.from(value, 'utf8');
431
+ this.writeShort(strBuf.length);
432
+ this.buffers.push(strBuf);
433
+ }
434
+
435
+ write(compound, named = true) {
436
+ this.writeUByte(NBTTagType.COMPOUND);
437
+
438
+ if (named) {
439
+ this.writeString(compound.name);
440
+ }
441
+
442
+ this.writeCompound(compound);
443
+
444
+ return Buffer.concat(this.buffers);
445
+ }
446
+
447
+ writeTag(tag) {
448
+ switch (tag.type) {
449
+ case NBTTagType.BYTE:
450
+ this.writeByte(tag.value);
451
+ break;
452
+ case NBTTagType.SHORT:
453
+ this.writeShort(tag.value);
454
+ break;
455
+ case NBTTagType.INT:
456
+ this.writeInt(tag.value);
457
+ break;
458
+ case NBTTagType.LONG:
459
+ this.writeLong(tag.value);
460
+ break;
461
+ case NBTTagType.FLOAT:
462
+ this.writeFloat(tag.value);
463
+ break;
464
+ case NBTTagType.DOUBLE:
465
+ this.writeDouble(tag.value);
466
+ break;
467
+ case NBTTagType.BYTE_ARRAY:
468
+ this.writeByteArray(tag);
469
+ break;
470
+ case NBTTagType.STRING:
471
+ this.writeString(tag.value);
472
+ break;
473
+ case NBTTagType.LIST:
474
+ this.writeList(tag);
475
+ break;
476
+ case NBTTagType.COMPOUND:
477
+ this.writeCompound(tag);
478
+ break;
479
+ case NBTTagType.INT_ARRAY:
480
+ this.writeIntArray(tag);
481
+ break;
482
+ case NBTTagType.LONG_ARRAY:
483
+ this.writeLongArray(tag);
484
+ break;
485
+ }
486
+ }
487
+
488
+ writeCompound(compound) {
489
+ for (const [name, tag] of compound.children) {
490
+ this.writeUByte(tag.type);
491
+ this.writeString(name);
492
+ this.writeTag(tag);
493
+ }
494
+
495
+ this.writeUByte(NBTTagType.END);
496
+ }
497
+
498
+ writeList(list) {
499
+ this.writeUByte(list.listType);
500
+ this.writeInt(list.values.length);
501
+
502
+ for (const tag of list.values) {
503
+ this.writeTagOfType(list.listType, tag);
504
+ }
505
+ }
506
+
507
+ writeTagOfType(type, tag) {
508
+ const value = tag.value !== undefined ? tag.value : tag;
509
+
510
+ switch (type) {
511
+ case NBTTagType.BYTE:
512
+ this.writeByte(value);
513
+ break;
514
+ case NBTTagType.SHORT:
515
+ this.writeShort(value);
516
+ break;
517
+ case NBTTagType.INT:
518
+ this.writeInt(value);
519
+ break;
520
+ case NBTTagType.LONG:
521
+ this.writeLong(value);
522
+ break;
523
+ case NBTTagType.FLOAT:
524
+ this.writeFloat(value);
525
+ break;
526
+ case NBTTagType.DOUBLE:
527
+ this.writeDouble(value);
528
+ break;
529
+ case NBTTagType.BYTE_ARRAY:
530
+ this.writeByteArray(tag);
531
+ break;
532
+ case NBTTagType.STRING:
533
+ this.writeString(value);
534
+ break;
535
+ case NBTTagType.LIST:
536
+ this.writeList(tag);
537
+ break;
538
+ case NBTTagType.COMPOUND:
539
+ this.writeCompound(tag);
540
+ break;
541
+ case NBTTagType.INT_ARRAY:
542
+ this.writeIntArray(tag);
543
+ break;
544
+ case NBTTagType.LONG_ARRAY:
545
+ this.writeLongArray(tag);
546
+ break;
547
+ }
548
+ }
549
+
550
+ writeByteArray(tag) {
551
+ this.writeInt(tag.value.length);
552
+ for (const byte of tag.value) {
553
+
554
+ this.writeUByte(byte & 0xFF);
555
+ }
556
+ }
557
+
558
+ writeIntArray(tag) {
559
+ this.writeInt(tag.value.length);
560
+ for (const int of tag.value) {
561
+ this.writeInt(int);
562
+ }
563
+ }
564
+
565
+ writeLongArray(tag) {
566
+ this.writeInt(tag.value.length);
567
+ for (const long of tag.value) {
568
+ this.writeLong(long);
569
+ }
570
+ }
571
+
572
+ getBuffer() {
573
+ return Buffer.concat(this.buffers);
574
+ }
575
+ }
576
+
577
+ export default {
578
+ NBTTagType,
579
+ NBTTag,
580
+ NBTCompound,
581
+ NBTByte,
582
+ NBTShort,
583
+ NBTInt,
584
+ NBTLong,
585
+ NBTFloat,
586
+ NBTDouble,
587
+ NBTString,
588
+ NBTByteArray,
589
+ NBTIntArray,
590
+ NBTLongArray,
591
+ NBTList,
592
+ NBTReader,
593
+ NBTWriter
594
+ };
@@ -0,0 +1,125 @@
1
+ export class Position {
2
+
3
+ constructor(x, y, z) {
4
+ this.x = x | 0;
5
+ this.y = y | 0;
6
+ this.z = z | 0;
7
+ }
8
+
9
+ encode() {
10
+ let x = BigInt(this.x);
11
+ let y = BigInt(this.y);
12
+ let z = BigInt(this.z);
13
+
14
+ if (x < 0n) x = x + (1n << 26n);
15
+ if (y < 0n) y = y + (1n << 12n);
16
+ if (z < 0n) z = z + (1n << 26n);
17
+
18
+ return ((x & 0x3FFFFFFn) << 38n) | ((z & 0x3FFFFFFn) << 12n) | (y & 0xFFFn);
19
+ }
20
+
21
+ static decode(value) {
22
+ value = BigInt(value);
23
+
24
+ let x = Number((value >> 38n) & 0x3FFFFFFn);
25
+ let y = Number(value & 0xFFFn);
26
+ let z = Number((value >> 12n) & 0x3FFFFFFn);
27
+
28
+ if (x >= (1 << 25)) x -= (1 << 26);
29
+ if (y >= (1 << 11)) y -= (1 << 12);
30
+ if (z >= (1 << 25)) z -= (1 << 26);
31
+
32
+ return new Position(x, y, z);
33
+ }
34
+
35
+ static fromBuffer(buffer, offset = 0) {
36
+ const value = buffer.readBigUInt64BE(offset);
37
+ return Position.decode(value);
38
+ }
39
+
40
+ toBuffer(buffer, offset = 0) {
41
+ buffer.writeBigUInt64BE(this.encode(), offset);
42
+ }
43
+
44
+ toBytes() {
45
+ const buffer = Buffer.alloc(8);
46
+ this.toBuffer(buffer);
47
+ return buffer;
48
+ }
49
+
50
+ toArray() {
51
+ return [this.x, this.y, this.z];
52
+ }
53
+
54
+ static fromArray(arr) {
55
+ return new Position(arr[0], arr[1], arr[2]);
56
+ }
57
+
58
+ add(other) {
59
+ return new Position(
60
+ this.x + other.x,
61
+ this.y + other.y,
62
+ this.z + other.z
63
+ );
64
+ }
65
+
66
+ subtract(other) {
67
+ return new Position(
68
+ this.x - other.x,
69
+ this.y - other.y,
70
+ this.z - other.z
71
+ );
72
+ }
73
+
74
+ distanceTo(other) {
75
+ const dx = this.x - other.x;
76
+ const dy = this.y - other.y;
77
+ const dz = this.z - other.z;
78
+ return Math.sqrt(dx * dx + dy * dy + dz * dz);
79
+ }
80
+
81
+ distanceSquaredTo(other) {
82
+ const dx = this.x - other.x;
83
+ const dy = this.y - other.y;
84
+ const dz = this.z - other.z;
85
+ return dx * dx + dy * dy + dz * dz;
86
+ }
87
+
88
+ equals(other) {
89
+ if (!(other instanceof Position)) return false;
90
+ return this.x === other.x && this.y === other.y && this.z === other.z;
91
+ }
92
+
93
+ toString() {
94
+ return `Position(${this.x}, ${this.y}, ${this.z})`;
95
+ }
96
+
97
+ clone() {
98
+ return new Position(this.x, this.y, this.z);
99
+ }
100
+
101
+ getChunkCoords() {
102
+ return {
103
+ x: Math.floor(this.x / 16),
104
+ z: Math.floor(this.z / 16)
105
+ };
106
+ }
107
+
108
+ getChunkX() {
109
+ return Math.floor(this.x / 16);
110
+ }
111
+
112
+ getChunkZ() {
113
+ return Math.floor(this.z / 16);
114
+ }
115
+
116
+ getRelativeToChunk() {
117
+ return new Position(
118
+ ((this.x % 16) + 16) % 16,
119
+ this.y,
120
+ ((this.z % 16) + 16) % 16
121
+ );
122
+ }
123
+ }
124
+
125
+ export default Position;