diff --git a/src/connection.js b/src/connection.js index 2fcbd62..ca862ee 100644 --- a/src/connection.js +++ b/src/connection.js @@ -39,6 +39,10 @@ class Connection extends EventEmitter { return this.options.protocolVersion >= (typeof version === 'string' ? Versions[version] : version) } + versionLessThanOrEqualTo (version) { + return this.options.protocolVersion <= (typeof version === 'string' ? Versions[version] : version) + } + startEncryption (iv) { this.encryptionEnabled = true this.inLog?.('Started encryption', this.sharedSecret, iv) diff --git a/src/createClient.js b/src/createClient.js index 154a8ae..9d14134 100644 --- a/src/createClient.js +++ b/src/createClient.js @@ -56,35 +56,41 @@ function connect (client) { }) client.queue('client_cache_status', { enabled: false }) - client.queue('tick_sync', { request_time: BigInt(Date.now()), response_time: 0n }) + + if (client.versionLessThanOrEqualTo('1.20.80')) client.queue('tick_sync', { request_time: BigInt(Date.now()), response_time: 0n }) + sleep(500).then(() => client.queue('request_chunk_radius', { chunk_radius: client.viewDistance || 10 })) }) - // Send tick sync packets every 10 ticks - const keepAliveInterval = 10 - const keepAliveIntervalBig = BigInt(keepAliveInterval) - let keepalive - client.tick = 0n - client.once('spawn', () => { - keepalive = setInterval(() => { - // Client fills out the request_time and the server does response_time in its reply. - client.queue('tick_sync', { request_time: client.tick, response_time: 0n }) - client.tick += keepAliveIntervalBig - }, 50 * keepAliveInterval) + if (client.versionLessThanOrEqualTo('1.20.80')) { + const keepAliveInterval = 10 + const keepAliveIntervalBig = BigInt(keepAliveInterval) - client.on('tick_sync', async packet => { - client.emit('heartbeat', packet.response_time) - client.tick = packet.response_time + let keepalive + client.tick = 0n + + client.once('spawn', () => { + keepalive = setInterval(() => { + // Client fills out the request_time and the server does response_time in its reply. + client.queue('tick_sync', { request_time: client.tick, response_time: 0n }) + client.tick += keepAliveIntervalBig + }, 50 * keepAliveInterval) + + client.on('tick_sync', async packet => { + client.emit('heartbeat', packet.response_time) + client.tick = packet.response_time + }) }) - }) - client.once('close', () => { - clearInterval(keepalive) - }) + client.once('close', () => { + clearInterval(keepalive) + }) + } } async function ping ({ host, port }) { const con = new RakClient({ host, port }) + try { return advertisement.fromServerName(await con.ping()) } finally {