Skip to content

Commit 9d08687

Browse files
committed
Add Minecraft 1.21.x protocol versions
1 parent ea2ecb2 commit 9d08687

4 files changed

Lines changed: 87 additions & 69 deletions

File tree

android/app/src/main/java/com/enderchat/modules/connection/ConnectionModule.kt

Lines changed: 12 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -187,54 +187,7 @@ class ConnectionModule(reactContext: ReactApplicationContext)
187187
return@launch
188188
}
189189

190-
// Login state packet IDs
191-
val loginSuccessId = 0x02
192-
val loginAcknowledgedId = 0x03
193-
val setCompressionId = 0x03
194-
// Configuration state packet IDs
195-
val configurationKeepAliveClientBoundId =
196-
if (protocolVersion >= PROTOCOL_VERSION_1205) 0x04
197-
else 0x03
198-
val configurationKeepAliveServerBoundId =
199-
if (protocolVersion >= PROTOCOL_VERSION_1205) 0x04
200-
else 0x03
201-
val finishConfigurationClientBoundId =
202-
if (protocolVersion >= PROTOCOL_VERSION_1205) 0x03
203-
else 0x02
204-
val finishConfigurationServerBoundId =
205-
if (protocolVersion >= PROTOCOL_VERSION_1205) 0x03
206-
else 0x02
207-
// Play state packet IDs
208-
val startConfigurationClientBoundId =
209-
if (protocolVersion >= PROTOCOL_VERSION_1205) 0x69
210-
else if (protocolVersion >= PROTOCOL_VERSION_1203) 0x67
211-
else if (protocolVersion >= PROTOCOL_VERSION_1202) 0x65
212-
else -1
213-
val acknowledgeConfigurationServerBoundId =
214-
if (protocolVersion >= PROTOCOL_VERSION_1205) 0x0c
215-
else if (protocolVersion >= PROTOCOL_VERSION_1202) 0x0b
216-
else -1
217-
val playKeepAliveClientBoundId =
218-
if (protocolVersion >= PROTOCOL_VERSION_1205) 0x26
219-
else if (protocolVersion >= PROTOCOL_VERSION_1202) 0x24
220-
else if (protocolVersion >= PROTOCOL_VERSION_1194) 0x23
221-
else if (protocolVersion >= PROTOCOL_VERSION_1193) 0x1f
222-
else if (protocolVersion >= PROTOCOL_VERSION_1191) 0x20
223-
else if (protocolVersion >= PROTOCOL_VERSION_119) 0x1e
224-
else if (protocolVersion >= PROTOCOL_VERSION_117) 0x21
225-
else if (protocolVersion >= PROTOCOL_VERSION_1164) 0x1f
226-
else -1
227-
val playKeepAliveServerBoundId =
228-
if (protocolVersion >= PROTOCOL_VERSION_1205) 0x18
229-
else if (protocolVersion >= PROTOCOL_VERSION_1203) 0x15
230-
else if (protocolVersion >= PROTOCOL_VERSION_1202) 0x14
231-
else if (protocolVersion >= PROTOCOL_VERSION_1194) 0x12
232-
else if (protocolVersion >= PROTOCOL_VERSION_1193) 0x11
233-
else if (protocolVersion >= PROTOCOL_VERSION_1191) 0x12
234-
else if (protocolVersion >= PROTOCOL_VERSION_119) 0x11
235-
else if (protocolVersion >= PROTOCOL_VERSION_117) 0x0f
236-
else if (protocolVersion >= PROTOCOL_VERSION_1164) 0x10
237-
else -1
190+
val ids = PacketIds(protocolVersion)
238191

239192
// Re-use the current thread, start reading from the socket.
240193
val buffer = ByteArrayOutputStream()
@@ -274,28 +227,28 @@ class ConnectionModule(reactContext: ReactApplicationContext)
274227
// and Start/Finish Configuration state changes.
275228
// FIXME: I feel this is incorrect logic and writePacket should also have a write lock?
276229
// No write lock since writePacket isn't called during login/config sequence (usually).
277-
if (packet.id.value == playKeepAliveClientBoundId && state == ConnectionState.PLAY) {
278-
directlyWritePacket(playKeepAliveServerBoundId, packet.data)
279-
} else if (packet.id.value == configurationKeepAliveClientBoundId &&
230+
if (packet.id.value == ids.playKeepAliveClientBound && state == ConnectionState.PLAY) {
231+
directlyWritePacket(ids.playKeepAliveServerBound, packet.data)
232+
} else if (packet.id.value == ids.configurationKeepAliveClientBound &&
280233
state == ConnectionState.CONFIGURATION) {
281-
directlyWritePacket(configurationKeepAliveServerBoundId, packet.data)
282-
} else if (packet.id.value == setCompressionId && state == ConnectionState.LOGIN) {
234+
directlyWritePacket(ids.configurationKeepAliveServerBound, packet.data)
235+
} else if (packet.id.value == ids.setCompression && state == ConnectionState.LOGIN) {
283236
val threshold = VarInt.read(packet.data)?.value ?: 0
284237
compressionThreshold = threshold
285238
compressionEnabled = threshold >= 0
286-
} else if (packet.id.value == loginSuccessId && state == ConnectionState.LOGIN) {
239+
} else if (packet.id.value == ids.loginSuccess && state == ConnectionState.LOGIN) {
287240
state = if (protocolVersion >= PROTOCOL_VERSION_1202) {
288-
directlyWritePacket(loginAcknowledgedId, ByteArray(0))
241+
directlyWritePacket(ids.loginAcknowledged, ByteArray(0))
289242
ConnectionState.CONFIGURATION
290243
} else ConnectionState.PLAY
291-
} else if (packet.id.value == finishConfigurationClientBoundId &&
244+
} else if (packet.id.value == ids.finishConfigurationClientBound &&
292245
state == ConnectionState.CONFIGURATION) {
293246
state = ConnectionState.PLAY
294-
directlyWritePacket(finishConfigurationServerBoundId, ByteArray(0))
295-
} else if (packet.id.value == startConfigurationClientBoundId &&
247+
directlyWritePacket(ids.finishConfigurationServerBound, ByteArray(0))
248+
} else if (packet.id.value == ids.startConfigurationClientBound &&
296249
state == ConnectionState.PLAY) {
297250
state = ConnectionState.CONFIGURATION
298-
directlyWritePacket(acknowledgeConfigurationServerBoundId, ByteArray(0))
251+
directlyWritePacket(ids.acknowledgeConfigurationServerBound, ByteArray(0))
299252
}
300253

301254
// Forward the packet to JavaScript.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.enderchat.modules.connection
2+
3+
const val PROTOCOL_VERSION_1164 = 754
4+
const val PROTOCOL_VERSION_117 = 755
5+
const val PROTOCOL_VERSION_119 = 759
6+
const val PROTOCOL_VERSION_1191 = 760
7+
const val PROTOCOL_VERSION_1193 = 761
8+
const val PROTOCOL_VERSION_1194 = 762
9+
const val PROTOCOL_VERSION_1202 = 764
10+
const val PROTOCOL_VERSION_1203 = 765
11+
const val PROTOCOL_VERSION_1205 = 766
12+
13+
class PacketIds(protocolVersion: Int) {
14+
// Login state packet IDs
15+
val loginSuccess = 0x02
16+
val loginAcknowledged = 0x03
17+
val setCompression = 0x03
18+
19+
// Configuration state packet IDs
20+
val configurationKeepAliveClientBound =
21+
if (protocolVersion >= PROTOCOL_VERSION_1205) 0x04
22+
else 0x03
23+
24+
val configurationKeepAliveServerBound =
25+
if (protocolVersion >= PROTOCOL_VERSION_1205) 0x04
26+
else 0x03
27+
28+
val finishConfigurationClientBound =
29+
if (protocolVersion >= PROTOCOL_VERSION_1205) 0x03
30+
else 0x02
31+
32+
val finishConfigurationServerBound =
33+
if (protocolVersion >= PROTOCOL_VERSION_1205) 0x03
34+
else 0x02
35+
36+
// Play state packet IDs
37+
val startConfigurationClientBound =
38+
if (protocolVersion >= PROTOCOL_VERSION_1205) 0x69
39+
else if (protocolVersion >= PROTOCOL_VERSION_1203) 0x67
40+
else if (protocolVersion >= PROTOCOL_VERSION_1202) 0x65
41+
else -1
42+
43+
val acknowledgeConfigurationServerBound =
44+
if (protocolVersion >= PROTOCOL_VERSION_1205) 0x0c
45+
else if (protocolVersion >= PROTOCOL_VERSION_1202) 0x0b
46+
else -1
47+
48+
val playKeepAliveClientBound =
49+
if (protocolVersion >= PROTOCOL_VERSION_1205) 0x26
50+
else if (protocolVersion >= PROTOCOL_VERSION_1202) 0x24
51+
else if (protocolVersion >= PROTOCOL_VERSION_1194) 0x23
52+
else if (protocolVersion >= PROTOCOL_VERSION_1193) 0x1f
53+
else if (protocolVersion >= PROTOCOL_VERSION_1191) 0x20
54+
else if (protocolVersion >= PROTOCOL_VERSION_119) 0x1e
55+
else if (protocolVersion >= PROTOCOL_VERSION_117) 0x21
56+
else if (protocolVersion >= PROTOCOL_VERSION_1164) 0x1f
57+
else -1
58+
59+
val playKeepAliveServerBound =
60+
if (protocolVersion >= PROTOCOL_VERSION_1205) 0x18
61+
else if (protocolVersion >= PROTOCOL_VERSION_1203) 0x15
62+
else if (protocolVersion >= PROTOCOL_VERSION_1202) 0x14
63+
else if (protocolVersion >= PROTOCOL_VERSION_1194) 0x12
64+
else if (protocolVersion >= PROTOCOL_VERSION_1193) 0x11
65+
else if (protocolVersion >= PROTOCOL_VERSION_1191) 0x12
66+
else if (protocolVersion >= PROTOCOL_VERSION_119) 0x11
67+
else if (protocolVersion >= PROTOCOL_VERSION_117) 0x0f
68+
else if (protocolVersion >= PROTOCOL_VERSION_1164) 0x10
69+
else -1
70+
}

android/app/src/main/java/com/enderchat/modules/connection/Utils.kt

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,6 @@ enum class ConnectionState {
1010
PLAY
1111
}
1212

13-
const val PROTOCOL_VERSION_1164 = 754
14-
const val PROTOCOL_VERSION_117 = 755
15-
const val PROTOCOL_VERSION_119 = 759
16-
const val PROTOCOL_VERSION_1191 = 760
17-
const val PROTOCOL_VERSION_1193 = 761
18-
const val PROTOCOL_VERSION_1194 = 762
19-
const val PROTOCOL_VERSION_1202 = 764
20-
const val PROTOCOL_VERSION_1203 = 765
21-
const val PROTOCOL_VERSION_1205 = 766
22-
2313
fun compressData(bytes: ByteArray): ByteArray {
2414
ByteArrayOutputStream(bytes.size).use {
2515
val deflater = Deflater().apply {

src/minecraft/utils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ export const protocolMap = {
2424
'1.20.4': 765,
2525
'1.20.5': 766,
2626
'1.20.6': 766,
27+
'1.21': 767,
28+
'1.21.1': 767,
29+
'1.21.2': 768,
30+
'1.21.3': 768,
31+
'1.21.4': 769,
2732
latest: 766,
2833
auto: -1,
2934
}

0 commit comments

Comments
 (0)