Skip to content

Commit

Permalink
Don't send now deprecated tick sync packets on 1.21 and newer (#504)
Browse files Browse the repository at this point in the history
* Tick Sync is now deprecated

* Add new function here

* Add check

* Remove useless require by me

* Fix
  • Loading branch information
kpxyy authored Jun 16, 2024
1 parent 1fee54f commit 84c5231
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
4 changes: 4 additions & 0 deletions src/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
46 changes: 26 additions & 20 deletions src/createClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

client.on('tick_sync', async packet => {
client.emit('heartbeat', packet.response_time)
client.tick = packet.response_time
if (client.versionLessThanOrEqualTo('1.20.80')) {
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)

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 {
Expand Down

0 comments on commit 84c5231

Please sign in to comment.