Skip to content

Commit

Permalink
Initial 1.20.3 work, fix pinging certain servers
Browse files Browse the repository at this point in the history
  • Loading branch information
retrixe committed Dec 23, 2023
1 parent 7420409 commit 0846183
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ class ConnectionModule(reactContext: ReactApplicationContext)
val is1193 = protocolVersion >= PROTOCOL_VERSION_1193
val is1194 = protocolVersion >= PROTOCOL_VERSION_1194
val is1202 = protocolVersion >= PROTOCOL_VERSION_1202
val is1203 = protocolVersion >= PROTOCOL_VERSION_1203
// Login state packet IDs
val loginSuccessId = 0x02
val loginAcknowledgedId = 0x03
Expand All @@ -208,9 +209,10 @@ class ConnectionModule(reactContext: ReactApplicationContext)
val finishConfigurationServerBoundId = 0x02
// Play state packet IDs
val startConfigurationClientBoundId =
if (is1202) 0x65
if (is1203) 0x67
else if (is1202) 0x65
else -1
val configurationAcknowledgedServerBoundId =
val acknowledgeConfigurationServerBoundId =
if (is1202) 0x0b
else -1
val playKeepAliveClientBoundId =
Expand All @@ -223,7 +225,8 @@ class ConnectionModule(reactContext: ReactApplicationContext)
else if (is1164) 0x1f
else -1
val playKeepAliveServerBoundId =
if (is1202) 0x14
if (is1203) 0x15
else if (is1202) 0x14
else if (is1194) 0x12
else if (is1193) 0x11
else if (is1191) 0x12
Expand Down Expand Up @@ -291,7 +294,7 @@ class ConnectionModule(reactContext: ReactApplicationContext)
} else if (packet.id.value == startConfigurationClientBoundId &&
state == ConnectionState.PLAY) {
state = ConnectionState.CONFIGURATION
directlyWritePacket(configurationAcknowledgedServerBoundId, ByteArray(0))
directlyWritePacket(acknowledgeConfigurationServerBoundId, ByteArray(0))
}

// Forward the packet to JavaScript.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const val PROTOCOL_VERSION_1191 = 760
const val PROTOCOL_VERSION_1193 = 761
const val PROTOCOL_VERSION_1194 = 762
const val PROTOCOL_VERSION_1202 = 764
const val PROTOCOL_VERSION_1203 = 765

fun compressData(bytes: ByteArray): ByteArray {
ByteArrayOutputStream(bytes.size).use {
Expand Down
1 change: 1 addition & 0 deletions src/minecraft/chatToJsx.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export interface BaseChat {
insertion?: string // LOW-TODO: Support this.
clickEvent?: ClickEvent
hoverEvent?: HoverEvent
type?: 'text' | 'translatable' | 'score' | 'keybind' | 'nbt' // LOW-TODO: Support keybind and score
}

export interface PlainTextChat extends BaseChat {
Expand Down
2 changes: 1 addition & 1 deletion src/minecraft/connection/javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ const initiateJavaScriptConnection = async (
conn.state === ConnectionState.PLAY
) {
const ackPacketId =
packetIds.SERVERBOUND_CONFIGURATION_ACKNOWLEDGED(version)
packetIds.SERVERBOUND_ACKNOWLEDGE_CONFIGURATION(version)
conn
.writePacket(ackPacketId ?? 0, Buffer.from([]))
.catch(err => conn.emit('error', err))
Expand Down
8 changes: 7 additions & 1 deletion src/minecraft/packets/ids.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ const packetIds = {
[protocolMap['1.16.4'], 0x24]
]),
CLIENTBOUND_RESPAWN: generateIdFunction([
[protocolMap['1.20.3'], 0x45],
[protocolMap['1.20.2'], 0x43],
[protocolMap['1.19.4'], 0x41],
[protocolMap['1.19.3'], 0x3d],
Expand All @@ -95,6 +96,7 @@ const packetIds = {
]),
// AKA Set Health
CLIENTBOUND_UPDATE_HEALTH: generateIdFunction([
[protocolMap['1.20.3'], 0x5b],
[protocolMap['1.20.2'], 0x59],
[protocolMap['1.19.4'], 0x57],
[protocolMap['1.19.3'], 0x53],
Expand Down Expand Up @@ -134,6 +136,7 @@ const packetIds = {
[protocolMap['1.19'], 0x30]
]),
CLIENTBOUND_SYSTEM_CHAT_MESSAGE: generateIdFunction([
[protocolMap['1.20.3'], 0x69],
[protocolMap['1.20.2'], 0x67],
[protocolMap['1.19.4'], 0x64],
[protocolMap['1.19.3'], 0x60],
Expand All @@ -149,13 +152,15 @@ const packetIds = {
[protocolMap['1.17'], 0x30]
]),
CLIENTBOUND_START_CONFIGURATION: generateIdFunction([
[protocolMap['1.20.3'], 0x67],
[protocolMap['1.20.2'], 0x65]
]),

// ==================
// Serverbound (play)
// ==================
SERVERBOUND_KEEP_ALIVE_PLAY: generateIdFunction([
[protocolMap['1.20.3'], 0x15],
[protocolMap['1.20.2'], 0x14],
[protocolMap['1.19.4'], 0x12],
[protocolMap['1.19.3'], 0x11],
Expand Down Expand Up @@ -203,14 +208,15 @@ const packetIds = {
[protocolMap['1.16.4'], null]
]),
SERVERBOUND_PONG_PLAY: generateIdFunction([
[protocolMap['1.20.3'], 0x24],
[protocolMap['1.20.2'], 0x23],
[protocolMap['1.19.4'], 0x20],
[protocolMap['1.19.3'], 0x1f],
[protocolMap['1.19.1'], 0x20],
[protocolMap['1.19'], 0x1f],
[protocolMap['1.17'], 0x1d]
]),
SERVERBOUND_CONFIGURATION_ACKNOWLEDGED: generateIdFunction([
SERVERBOUND_ACKNOWLEDGE_CONFIGURATION: generateIdFunction([
[protocolMap['1.20.2'], 0x0b]
])
}
Expand Down
12 changes: 9 additions & 3 deletions src/minecraft/pingServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
padBufferToLength,
resolveHostname,
readVarInt,
writeVarInt
writeVarInt,
protocolMap
} from './utils'
import {
makeBasePacket,
Expand Down Expand Up @@ -116,7 +117,12 @@ export const modernPing = async (opts: {
// Create data to send in Handshake.
const portBuf = Buffer.alloc(2)
portBuf.writeUInt16BE(port)
const handshakeData = [writeVarInt(-1), host, portBuf, writeVarInt(1)]
const handshakeData = [
writeVarInt(protocolMap.latest), // It would be better to use -1, but some servers misbehave
host,
portBuf,
writeVarInt(1)
]

// Initialise Handshake with server.
socket.write(makeBasePacket(0x00, concatPacketData(handshakeData)), () =>
Expand Down Expand Up @@ -146,7 +152,7 @@ export const modernPing = async (opts: {
try {
const responsePacket = packets.find(p => p.id === 0x00)
if (!responsePacket) {
return reject(new TypeError('No response packet was sent!'))
return reject(new Error('No response packet was sent!'))
}
const [jsonLength, varIntLength] = readVarInt(responsePacket.data)
const json = responsePacket.data
Expand Down
2 changes: 2 additions & 0 deletions src/minecraft/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export const protocolMap = {
'1.20': 763,
'1.20.1': 763,
'1.20.2': 764,
'1.20.3': 765,
'1.20.4': 765,
latest: 764,
auto: -1
}
Expand Down

0 comments on commit 0846183

Please sign in to comment.