Ensure onReady in client is called once

Fix https://github.com/PrismarineJS/node-minecraft-protocol/issues/1286

Server sending start_config packets will incorrectly cause onReady to call multiple times
This commit is contained in:
extremeheat 2024-02-06 18:59:16 -05:00 committed by GitHub
parent 092e10c53d
commit 9350bddd9b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -37,15 +37,15 @@ module.exports = function (client, options) {
if (mcData.supportFeature('hasConfigurationState')) { if (mcData.supportFeature('hasConfigurationState')) {
client.write('login_acknowledged', {}) client.write('login_acknowledged', {})
enterConfigState() enterConfigState(onReady)
// Server can tell client to re-enter config state // Server can tell client to re-enter config state
client.on('start_configuration', enterConfigState) client.on('start_configuration', () => enterConfigState())
} else { } else {
client.state = states.PLAY client.state = states.PLAY
onReady() onReady()
} }
function enterConfigState () { function enterConfigState (finishCb) {
if (client.state === states.CONFIGURATION) return if (client.state === states.CONFIGURATION) return
// If we are returning to the configuration state from the play state, we ahve to acknowledge it. // If we are returning to the configuration state from the play state, we ahve to acknowledge it.
if (client.state === states.PLAY) { if (client.state === states.PLAY) {
@ -57,7 +57,7 @@ module.exports = function (client, options) {
client.once('finish_configuration', () => { client.once('finish_configuration', () => {
client.write('finish_configuration', {}) client.write('finish_configuration', {})
client.state = states.PLAY client.state = states.PLAY
onReady() finishCb?.()
}) })
} }