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,265 @@
1
+ export { Packet, PacketInfo } from './Packet.js';
2
+ export { PacketRegistry, defaultRegistry } from './PacketRegistry.js';
3
+
4
+ export { HandshakePacket } from './handshake/HandshakePacket.js';
5
+
6
+ export { StatusRequestPacket } from './status/StatusRequestPacket.js';
7
+ export { StatusResponsePacket } from './status/StatusResponsePacket.js';
8
+ export { PingPacket } from './status/PingPacket.js';
9
+ export { PongPacket } from './status/PongPacket.js';
10
+
11
+ export { LoginStartPacket } from './login/LoginStartPacket.js';
12
+ export { EncryptionRequestPacket } from './login/EncryptionRequestPacket.js';
13
+ export { EncryptionResponsePacket } from './login/EncryptionResponsePacket.js';
14
+ export { LoginSuccessPacket } from './login/LoginSuccessPacket.js';
15
+ export { SetCompressionPacket } from './login/SetCompressionPacket.js';
16
+ export { DisconnectPacket, LoginDisconnectPacket, PlayDisconnectPacket } from './login/DisconnectPacket.js';
17
+
18
+ export { KeepAliveClientboundPacket, KeepAliveServerboundPacket } from './play/KeepAlivePacket.js';
19
+
20
+ export {
21
+ ChatMessageServerboundPacket,
22
+ SystemChatMessagePacket,
23
+ PlayerChatMessagePacket
24
+ } from './play/ChatPacket.js';
25
+
26
+ export {
27
+ SetPlayerPositionPacket,
28
+ SetPlayerPositionAndRotationPacket,
29
+ SetPlayerRotationPacket,
30
+ SetPlayerOnGroundPacket,
31
+ SynchronizePlayerPositionPacket,
32
+ ConfirmTeleportationPacket
33
+ } from './play/PlayerPositionPacket.js';
34
+
35
+ export { ChunkDataPacket, UnloadChunkPacket } from './play/ChunkPacket.js';
36
+
37
+ export {
38
+ SpawnEntityPacket,
39
+ RemoveEntitiesPacket,
40
+ UpdateEntityPositionPacket,
41
+ SetEntityMetadataPacket
42
+ } from './play/EntityPacket.js';
43
+
44
+ import { ConnectionState } from '../ConnectionState.js';
45
+ import { PacketDirection } from '../PacketDirection.js';
46
+
47
+ import { HandshakePacket } from './handshake/HandshakePacket.js';
48
+ import { StatusRequestPacket } from './status/StatusRequestPacket.js';
49
+ import { StatusResponsePacket } from './status/StatusResponsePacket.js';
50
+ import { PingPacket } from './status/PingPacket.js';
51
+ import { PongPacket } from './status/PongPacket.js';
52
+ import { LoginStartPacket } from './login/LoginStartPacket.js';
53
+ import { EncryptionRequestPacket } from './login/EncryptionRequestPacket.js';
54
+ import { EncryptionResponsePacket } from './login/EncryptionResponsePacket.js';
55
+ import { LoginSuccessPacket } from './login/LoginSuccessPacket.js';
56
+ import { SetCompressionPacket } from './login/SetCompressionPacket.js';
57
+ import { LoginDisconnectPacket } from './login/DisconnectPacket.js';
58
+ import { KeepAliveClientboundPacket, KeepAliveServerboundPacket } from './play/KeepAlivePacket.js';
59
+ import { ChatMessageServerboundPacket, SystemChatMessagePacket, PlayerChatMessagePacket } from './play/ChatPacket.js';
60
+ import { SetPlayerPositionPacket, SetPlayerPositionAndRotationPacket, SetPlayerRotationPacket, SetPlayerOnGroundPacket, SynchronizePlayerPositionPacket, ConfirmTeleportationPacket } from './play/PlayerPositionPacket.js';
61
+ import { ChunkDataPacket, UnloadChunkPacket } from './play/ChunkPacket.js';
62
+ import { SpawnEntityPacket, RemoveEntitiesPacket, UpdateEntityPositionPacket, SetEntityMetadataPacket } from './play/EntityPacket.js';
63
+
64
+ import { defaultRegistry as registry } from './PacketRegistry.js';
65
+
66
+ export function registerDefaultPackets() {
67
+
68
+ registry.register({
69
+ packetClass: HandshakePacket,
70
+ state: ConnectionState.HANDSHAKING,
71
+ direction: PacketDirection.SERVERBOUND,
72
+ packetId: 0x00
73
+ });
74
+
75
+ registry.register({
76
+ packetClass: StatusRequestPacket,
77
+ state: ConnectionState.STATUS,
78
+ direction: PacketDirection.SERVERBOUND,
79
+ packetId: 0x00
80
+ });
81
+
82
+ registry.register({
83
+ packetClass: PingPacket,
84
+ state: ConnectionState.STATUS,
85
+ direction: PacketDirection.SERVERBOUND,
86
+ packetId: 0x01
87
+ });
88
+
89
+ registry.register({
90
+ packetClass: StatusResponsePacket,
91
+ state: ConnectionState.STATUS,
92
+ direction: PacketDirection.CLIENTBOUND,
93
+ packetId: 0x00
94
+ });
95
+
96
+ registry.register({
97
+ packetClass: PongPacket,
98
+ state: ConnectionState.STATUS,
99
+ direction: PacketDirection.CLIENTBOUND,
100
+ packetId: 0x01
101
+ });
102
+
103
+ registry.register({
104
+ packetClass: LoginStartPacket,
105
+ state: ConnectionState.LOGIN,
106
+ direction: PacketDirection.SERVERBOUND,
107
+ packetId: 0x00
108
+ });
109
+
110
+ registry.register({
111
+ packetClass: EncryptionResponsePacket,
112
+ state: ConnectionState.LOGIN,
113
+ direction: PacketDirection.SERVERBOUND,
114
+ packetId: 0x01
115
+ });
116
+
117
+ registry.register({
118
+ packetClass: LoginDisconnectPacket,
119
+ state: ConnectionState.LOGIN,
120
+ direction: PacketDirection.CLIENTBOUND,
121
+ packetId: 0x00
122
+ });
123
+
124
+ registry.register({
125
+ packetClass: EncryptionRequestPacket,
126
+ state: ConnectionState.LOGIN,
127
+ direction: PacketDirection.CLIENTBOUND,
128
+ packetId: 0x01
129
+ });
130
+
131
+ registry.register({
132
+ packetClass: LoginSuccessPacket,
133
+ state: ConnectionState.LOGIN,
134
+ direction: PacketDirection.CLIENTBOUND,
135
+ packetId: 0x02
136
+ });
137
+
138
+ registry.register({
139
+ packetClass: SetCompressionPacket,
140
+ state: ConnectionState.LOGIN,
141
+ direction: PacketDirection.CLIENTBOUND,
142
+ packetId: 0x03
143
+ });
144
+
145
+ registry.register({
146
+ packetClass: ConfirmTeleportationPacket,
147
+ state: ConnectionState.PLAY,
148
+ direction: PacketDirection.SERVERBOUND,
149
+ packetId: 0x00
150
+ });
151
+
152
+ registry.register({
153
+ packetClass: ChatMessageServerboundPacket,
154
+ state: ConnectionState.PLAY,
155
+ direction: PacketDirection.SERVERBOUND,
156
+ packetId: 0x06
157
+ });
158
+
159
+ registry.register({
160
+ packetClass: KeepAliveServerboundPacket,
161
+ state: ConnectionState.PLAY,
162
+ direction: PacketDirection.SERVERBOUND,
163
+ packetId: 0x18
164
+ });
165
+
166
+ registry.register({
167
+ packetClass: SetPlayerPositionPacket,
168
+ state: ConnectionState.PLAY,
169
+ direction: PacketDirection.SERVERBOUND,
170
+ packetId: 0x1A
171
+ });
172
+
173
+ registry.register({
174
+ packetClass: SetPlayerPositionAndRotationPacket,
175
+ state: ConnectionState.PLAY,
176
+ direction: PacketDirection.SERVERBOUND,
177
+ packetId: 0x1B
178
+ });
179
+
180
+ registry.register({
181
+ packetClass: SetPlayerRotationPacket,
182
+ state: ConnectionState.PLAY,
183
+ direction: PacketDirection.SERVERBOUND,
184
+ packetId: 0x1C
185
+ });
186
+
187
+ registry.register({
188
+ packetClass: SetPlayerOnGroundPacket,
189
+ state: ConnectionState.PLAY,
190
+ direction: PacketDirection.SERVERBOUND,
191
+ packetId: 0x1D
192
+ });
193
+
194
+ registry.register({
195
+ packetClass: SpawnEntityPacket,
196
+ state: ConnectionState.PLAY,
197
+ direction: PacketDirection.CLIENTBOUND,
198
+ packetId: 0x01
199
+ });
200
+
201
+ registry.register({
202
+ packetClass: UnloadChunkPacket,
203
+ state: ConnectionState.PLAY,
204
+ direction: PacketDirection.CLIENTBOUND,
205
+ packetId: 0x21
206
+ });
207
+
208
+ registry.register({
209
+ packetClass: KeepAliveClientboundPacket,
210
+ state: ConnectionState.PLAY,
211
+ direction: PacketDirection.CLIENTBOUND,
212
+ packetId: 0x26
213
+ });
214
+
215
+ registry.register({
216
+ packetClass: ChunkDataPacket,
217
+ state: ConnectionState.PLAY,
218
+ direction: PacketDirection.CLIENTBOUND,
219
+ packetId: 0x27
220
+ });
221
+
222
+ registry.register({
223
+ packetClass: UpdateEntityPositionPacket,
224
+ state: ConnectionState.PLAY,
225
+ direction: PacketDirection.CLIENTBOUND,
226
+ packetId: 0x2E
227
+ });
228
+
229
+ registry.register({
230
+ packetClass: PlayerChatMessagePacket,
231
+ state: ConnectionState.PLAY,
232
+ direction: PacketDirection.CLIENTBOUND,
233
+ packetId: 0x39
234
+ });
235
+
236
+ registry.register({
237
+ packetClass: SynchronizePlayerPositionPacket,
238
+ state: ConnectionState.PLAY,
239
+ direction: PacketDirection.CLIENTBOUND,
240
+ packetId: 0x40
241
+ });
242
+
243
+ registry.register({
244
+ packetClass: RemoveEntitiesPacket,
245
+ state: ConnectionState.PLAY,
246
+ direction: PacketDirection.CLIENTBOUND,
247
+ packetId: 0x42
248
+ });
249
+
250
+ registry.register({
251
+ packetClass: SetEntityMetadataPacket,
252
+ state: ConnectionState.PLAY,
253
+ direction: PacketDirection.CLIENTBOUND,
254
+ packetId: 0x58
255
+ });
256
+
257
+ registry.register({
258
+ packetClass: SystemChatMessagePacket,
259
+ state: ConnectionState.PLAY,
260
+ direction: PacketDirection.CLIENTBOUND,
261
+ packetId: 0x6C
262
+ });
263
+ }
264
+
265
+ registerDefaultPackets();
@@ -0,0 +1,71 @@
1
+ import { Packet } from '../Packet.js';
2
+ import { TextComponent } from '../../types/TextComponent.js';
3
+
4
+ export class DisconnectPacket extends Packet {
5
+ static get packetId() { return 0x00; }
6
+ static get packetName() { return 'Disconnect'; }
7
+
8
+ constructor() {
9
+ super();
10
+
11
+ this.reason = null;
12
+ }
13
+
14
+ read(buffer, context) {
15
+ const jsonStr = buffer.readString(262144);
16
+
17
+ try {
18
+ const json = JSON.parse(jsonStr);
19
+ this.reason = TextComponent.fromJSON(json);
20
+ } catch (e) {
21
+ this.reason = TextComponent.text(jsonStr);
22
+ }
23
+ }
24
+
25
+ write(buffer, context) {
26
+ let json;
27
+
28
+ if (this.reason instanceof TextComponent) {
29
+ json = JSON.stringify(this.reason.toJSON());
30
+ } else if (typeof this.reason === 'string') {
31
+ json = JSON.stringify({ text: this.reason });
32
+ } else {
33
+ json = JSON.stringify(this.reason);
34
+ }
35
+
36
+ buffer.writeString(json);
37
+ }
38
+
39
+ getReasonText() {
40
+ if (this.reason instanceof TextComponent) {
41
+ return this.reason.toPlainText();
42
+ }
43
+ if (typeof this.reason === 'string') {
44
+ return this.reason;
45
+ }
46
+ if (this.reason?.text) {
47
+ return this.reason.text;
48
+ }
49
+ return JSON.stringify(this.reason);
50
+ }
51
+
52
+ toJSON() {
53
+ return {
54
+ ...super.toJSON(),
55
+ reason: this.reason instanceof TextComponent ?
56
+ this.reason.toJSON() : this.reason
57
+ };
58
+ }
59
+ }
60
+
61
+ export class LoginDisconnectPacket extends DisconnectPacket {
62
+ static get packetId() { return 0x00; }
63
+ static get packetName() { return 'LoginDisconnect'; }
64
+ }
65
+
66
+ export class PlayDisconnectPacket extends DisconnectPacket {
67
+ static get packetId() { return 0x1D; }
68
+ static get packetName() { return 'PlayDisconnect'; }
69
+ }
70
+
71
+ export default DisconnectPacket;
@@ -0,0 +1,47 @@
1
+ import { Packet } from '../Packet.js';
2
+
3
+ export class EncryptionRequestPacket extends Packet {
4
+ static get packetId() { return 0x01; }
5
+ static get packetName() { return 'EncryptionRequest'; }
6
+
7
+ constructor() {
8
+ super();
9
+
10
+ this.serverId = '';
11
+
12
+ this.publicKey = Buffer.alloc(0);
13
+
14
+ this.verifyToken = Buffer.alloc(0);
15
+
16
+ this.shouldAuthenticate = true;
17
+ }
18
+
19
+ read(buffer, context) {
20
+ this.serverId = buffer.readString(20);
21
+ this.publicKey = buffer.readByteArray();
22
+ this.verifyToken = buffer.readByteArray();
23
+
24
+ if (buffer.isReadable(1)) {
25
+ this.shouldAuthenticate = buffer.readBoolean();
26
+ }
27
+ }
28
+
29
+ write(buffer, context) {
30
+ buffer.writeString(this.serverId);
31
+ buffer.writeByteArray(this.publicKey);
32
+ buffer.writeByteArray(this.verifyToken);
33
+ buffer.writeBoolean(this.shouldAuthenticate);
34
+ }
35
+
36
+ toJSON() {
37
+ return {
38
+ ...super.toJSON(),
39
+ serverId: this.serverId,
40
+ publicKeyLength: this.publicKey.length,
41
+ verifyTokenLength: this.verifyToken.length,
42
+ shouldAuthenticate: this.shouldAuthenticate
43
+ };
44
+ }
45
+ }
46
+
47
+ export default EncryptionRequestPacket;
@@ -0,0 +1,34 @@
1
+ import { Packet } from '../Packet.js';
2
+
3
+ export class EncryptionResponsePacket extends Packet {
4
+ static get packetId() { return 0x01; }
5
+ static get packetName() { return 'EncryptionResponse'; }
6
+
7
+ constructor() {
8
+ super();
9
+
10
+ this.sharedSecret = Buffer.alloc(0);
11
+
12
+ this.verifyToken = Buffer.alloc(0);
13
+ }
14
+
15
+ read(buffer, context) {
16
+ this.sharedSecret = buffer.readByteArray();
17
+ this.verifyToken = buffer.readByteArray();
18
+ }
19
+
20
+ write(buffer, context) {
21
+ buffer.writeByteArray(this.sharedSecret);
22
+ buffer.writeByteArray(this.verifyToken);
23
+ }
24
+
25
+ toJSON() {
26
+ return {
27
+ ...super.toJSON(),
28
+ sharedSecretLength: this.sharedSecret.length,
29
+ verifyTokenLength: this.verifyToken.length
30
+ };
31
+ }
32
+ }
33
+
34
+ export default EncryptionResponsePacket;
@@ -0,0 +1,35 @@
1
+ import { Packet } from '../Packet.js';
2
+ import { UUID } from '../../types/UUID.js';
3
+
4
+ export class LoginStartPacket extends Packet {
5
+ static get packetId() { return 0x00; }
6
+ static get packetName() { return 'LoginStart'; }
7
+
8
+ constructor() {
9
+ super();
10
+
11
+ this.username = '';
12
+
13
+ this.uuid = null;
14
+ }
15
+
16
+ read(buffer, context) {
17
+ this.username = buffer.readString(16);
18
+ this.uuid = buffer.readUUID();
19
+ }
20
+
21
+ write(buffer, context) {
22
+ buffer.writeString(this.username);
23
+ buffer.writeUUID(this.uuid || UUID.randomUUID());
24
+ }
25
+
26
+ toJSON() {
27
+ return {
28
+ ...super.toJSON(),
29
+ username: this.username,
30
+ uuid: this.uuid?.toString()
31
+ };
32
+ }
33
+ }
34
+
35
+ export default LoginStartPacket;
@@ -0,0 +1,61 @@
1
+ import { Packet } from '../Packet.js';
2
+ import { UUID } from '../../types/UUID.js';
3
+
4
+ export class LoginSuccessPacket extends Packet {
5
+ static get packetId() { return 0x02; }
6
+ static get packetName() { return 'LoginSuccess'; }
7
+
8
+ constructor() {
9
+ super();
10
+
11
+ this.uuid = null;
12
+
13
+ this.username = '';
14
+
15
+ this.properties = [];
16
+
17
+ this.strictErrorHandling = true;
18
+ }
19
+
20
+ read(buffer, context) {
21
+ this.uuid = buffer.readUUID();
22
+ this.username = buffer.readString(16);
23
+
24
+ this.properties = buffer.readArray(() => {
25
+ const name = buffer.readString(32767);
26
+ const value = buffer.readString(32767);
27
+ const signature = buffer.readOptional(() => buffer.readString(32767));
28
+
29
+ return { name, value, signature };
30
+ });
31
+
32
+ if (buffer.isReadable(1)) {
33
+ this.strictErrorHandling = buffer.readBoolean();
34
+ }
35
+ }
36
+
37
+ write(buffer, context) {
38
+ buffer.writeUUID(this.uuid);
39
+ buffer.writeString(this.username);
40
+
41
+ buffer.writeArray(this.properties, (prop) => {
42
+ buffer.writeString(prop.name);
43
+ buffer.writeString(prop.value);
44
+ buffer.writeOptional(prop.signature, (sig) => buffer.writeString(sig));
45
+ });
46
+
47
+ buffer.writeBoolean(this.strictErrorHandling);
48
+ }
49
+
50
+ toJSON() {
51
+ return {
52
+ ...super.toJSON(),
53
+ uuid: this.uuid?.toString(),
54
+ username: this.username,
55
+ properties: this.properties,
56
+ strictErrorHandling: this.strictErrorHandling
57
+ };
58
+ }
59
+ }
60
+
61
+ export default LoginSuccessPacket;
@@ -0,0 +1,29 @@
1
+ import { Packet } from '../Packet.js';
2
+
3
+ export class SetCompressionPacket extends Packet {
4
+ static get packetId() { return 0x03; }
5
+ static get packetName() { return 'SetCompression'; }
6
+
7
+ constructor() {
8
+ super();
9
+
10
+ this.threshold = -1;
11
+ }
12
+
13
+ read(buffer, context) {
14
+ this.threshold = buffer.readVarInt();
15
+ }
16
+
17
+ write(buffer, context) {
18
+ buffer.writeVarInt(this.threshold);
19
+ }
20
+
21
+ toJSON() {
22
+ return {
23
+ ...super.toJSON(),
24
+ threshold: this.threshold
25
+ };
26
+ }
27
+ }
28
+
29
+ export default SetCompressionPacket;