Skip to content

Commit b32f3b4

Browse files
committed
Support 1.20.2 config state in native and JS conns
1 parent 452fa2d commit b32f3b4

4 files changed

Lines changed: 91 additions & 11 deletions

File tree

src/minecraft/connection/javascript.ts

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
ConnectionState
2323
} from '.'
2424
import { getLoginPacket, handleEncryptionRequest } from './shared'
25-
import { readVarInt, writeVarInt, resolveHostname } from '../utils'
25+
import { readVarInt, writeVarInt, resolveHostname, protocolMap } from '../utils'
2626
import packetIds from '../packets/ids'
2727

2828
export declare interface JavaScriptServerConnection {
@@ -153,17 +153,52 @@ const initiateJavaScriptConnection = async (
153153
packet.id === 0x02 &&
154154
conn.state === ConnectionState.LOGIN /* Login Success */
155155
) {
156+
if (version >= protocolMap['1.20.2']) {
157+
conn
158+
.writePacket(0x03 /* Login Acknowledged */, Buffer.from([]))
159+
.catch(err => conn.emit('error', err))
160+
conn.state = ConnectionState.CONFIGURATION
161+
} else conn.state = ConnectionState.PLAY
162+
} else if (
163+
packet.id ===
164+
packetIds.CLIENTBOUND_FINISH_CONFIGURATION(version) &&
165+
conn.state === ConnectionState.CONFIGURATION
166+
) {
167+
conn
168+
.writePacket(0x02 /* Finish Configuration */, Buffer.from([]))
169+
.catch(err => conn.emit('error', err))
156170
conn.state = ConnectionState.PLAY
157171
} else if (
158-
packet.id === packetIds.CLIENTBOUND_KEEP_ALIVE(version)
172+
packet.id ===
173+
packetIds.CLIENTBOUND_START_CONFIGURATION(version) &&
174+
conn.state === ConnectionState.PLAY
175+
) {
176+
const ackPacketId =
177+
packetIds.SERVERBOUND_CONFIGURATION_ACKNOWLEDGED(version)
178+
conn
179+
.writePacket(ackPacketId ?? 0, Buffer.from([]))
180+
.catch(err => conn.emit('error', err))
181+
conn.state = ConnectionState.CONFIGURATION
182+
} else if (
183+
(packet.id === packetIds.CLIENTBOUND_KEEP_ALIVE_PLAY(version) &&
184+
conn.state === ConnectionState.PLAY) ||
185+
(packet.id ===
186+
packetIds.CLIENTBOUND_KEEP_ALIVE_CONFIGURATION(version) &&
187+
conn.state === ConnectionState.CONFIGURATION)
159188
) {
160-
const id = packetIds.SERVERBOUND_KEEP_ALIVE(version)
189+
const id =
190+
conn.state === ConnectionState.PLAY
191+
? packetIds.SERVERBOUND_KEEP_ALIVE_PLAY(version)
192+
: packetIds.SERVERBOUND_KEEP_ALIVE_CONFIGURATION(version)
161193
conn
162194
.writePacket(id ?? 0, packet.data)
163195
.catch(err => conn.emit('error', err))
164196
} else if (
165-
// Disconnect (login) or Disconnect (play)
197+
// Disconnect (login), Disconnect (configuration) or Disconnect (play)
166198
(packet.id === 0x00 && conn.state === ConnectionState.LOGIN) ||
199+
(packet.id ===
200+
packetIds.CLIENTBOUND_DISCONNECT_CONFIGURATION(version) &&
201+
conn.state === ConnectionState.CONFIGURATION) ||
167202
(packet.id === packetIds.CLIENTBOUND_DISCONNECT_PLAY(version) &&
168203
conn.state === ConnectionState.PLAY)
169204
) {

src/minecraft/connection/native.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
} from '.'
1212
import { concatPacketData, type Packet } from '../packet'
1313
import { getLoginPacket, handleEncryptionRequest } from './shared'
14-
import { readVarInt, writeVarInt, resolveHostname } from '../utils'
14+
import { readVarInt, writeVarInt, resolveHostname, protocolMap } from '../utils'
1515
import packetIds from '../packets/ids'
1616

1717
const { ConnectionModule } = NativeModules
@@ -92,13 +92,29 @@ export class NativeServerConnection
9292
// Set Compression and Keep Alive are handled in native for now.
9393
// When modifying this code, apply the same changes to the JavaScript back-end.
9494
if (
95-
packet.id === 0x02 &&
96-
this.state === ConnectionState.LOGIN /* Login Success */
95+
packet.id === 0x02 /* Login Success */ &&
96+
this.state === ConnectionState.LOGIN
9797
) {
98-
this.state = ConnectionState.PLAY
98+
this.state =
99+
version >= protocolMap['1.20.2']
100+
? ConnectionState.CONFIGURATION // Ack sent by native code
101+
: ConnectionState.PLAY
99102
} else if (
100-
// Disconnect (login) or Disconnect (play)
103+
packet.id === packetIds.CLIENTBOUND_FINISH_CONFIGURATION(version) &&
104+
this.state === ConnectionState.CONFIGURATION
105+
) {
106+
this.state = ConnectionState.PLAY // Ack sent by native code
107+
} else if (
108+
packet.id === packetIds.CLIENTBOUND_START_CONFIGURATION(version) &&
109+
this.state === ConnectionState.PLAY
110+
) {
111+
this.state = ConnectionState.CONFIGURATION // Ack sent by native code
112+
} else if (
113+
// Disconnect (login), Disconnect (configuration) or Disconnect (play)
101114
(packet.id === 0x00 && this.state === ConnectionState.LOGIN) ||
115+
(packet.id ===
116+
packetIds.CLIENTBOUND_DISCONNECT_CONFIGURATION(version) &&
117+
this.state === ConnectionState.CONFIGURATION) ||
102118
(packet.id === packetIds.CLIENTBOUND_DISCONNECT_PLAY(version) &&
103119
this.state === ConnectionState.PLAY)
104120
) {

src/minecraft/packets/ids.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ const generateIdFunction =
99
}
1010

1111
const packetIds = {
12+
// ===================
1213
// Clientbound (login)
14+
// ===================
1315
CLIENTBOUND_SET_COMPRESSION: generateIdFunction([
1416
[protocolMap['1.16.4'], 0x03]
1517
]),
@@ -26,10 +28,30 @@ const packetIds = {
2628
[protocolMap['1.16.4'], 0x00]
2729
]),
2830

31+
// ===========================
32+
// Clientbound (configuration)
33+
// ===========================
34+
CLIENTBOUND_DISCONNECT_CONFIGURATION: generateIdFunction([
35+
[protocolMap['1.20.2'], 0x01]
36+
]),
37+
CLIENTBOUND_FINISH_CONFIGURATION: generateIdFunction([
38+
[protocolMap['1.20.2'], 0x02]
39+
]),
40+
CLIENTBOUND_KEEP_ALIVE_CONFIGURATION: generateIdFunction([
41+
[protocolMap['1.20.2'], 0x03]
42+
]),
43+
44+
// ===========================
45+
// Serverbound (configuration)
46+
// ===========================
47+
SERVERBOUND_KEEP_ALIVE_CONFIGURATION: generateIdFunction([
48+
[protocolMap['1.20.2'], 0x03]
49+
]),
50+
2951
// ==================
3052
// Clientbound (play)
3153
// ==================
32-
CLIENTBOUND_KEEP_ALIVE: generateIdFunction([
54+
CLIENTBOUND_KEEP_ALIVE_PLAY: generateIdFunction([
3355
[protocolMap['1.19.4'], 0x23],
3456
[protocolMap['1.19.3'], 0x1f],
3557
[protocolMap['1.19.1'], 0x20],
@@ -110,11 +132,14 @@ const packetIds = {
110132
[protocolMap['1.19'], 0x2d],
111133
[protocolMap['1.17'], 0x30]
112134
]),
135+
CLIENTBOUND_START_CONFIGURATION: generateIdFunction([
136+
[protocolMap['1.20.2'], 0x65]
137+
]),
113138

114139
// ==================
115140
// Serverbound (play)
116141
// ==================
117-
SERVERBOUND_KEEP_ALIVE: generateIdFunction([
142+
SERVERBOUND_KEEP_ALIVE_PLAY: generateIdFunction([
118143
[protocolMap['1.19.4'], 0x12],
119144
[protocolMap['1.19.3'], 0x11],
120145
[protocolMap['1.19.1'], 0x12],
@@ -163,6 +188,9 @@ const packetIds = {
163188
[protocolMap['1.19.1'], 0x20],
164189
[protocolMap['1.19'], 0x1f],
165190
[protocolMap['1.17'], 0x1d]
191+
]),
192+
SERVERBOUND_CONFIGURATION_ACKNOWLEDGED: generateIdFunction([
193+
[protocolMap['1.20.2'], 0x0b]
166194
])
167195
}
168196

src/minecraft/utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export const protocolMap = {
1717
'1.19.4': 762,
1818
'1.20': 763,
1919
'1.20.1': 763,
20+
'1.20.2': 764,
2021
latest: 763,
2122
auto: -1
2223
}

0 commit comments

Comments
 (0)