Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use of undefined in plugins/breath.js #3534

Closed
1 task done
wAIfu-DEV opened this issue Dec 27, 2024 · 3 comments
Closed
1 task done

Use of undefined in plugins/breath.js #3534

wAIfu-DEV opened this issue Dec 27, 2024 · 3 comments
Labels
possible bug Stage1 just created by someone new to the project, we don't know yet if it deserves an implementation / a f

Comments

@wAIfu-DEV
Copy link
Contributor

  • The FAQ doesn't contain a resolution to my issue

Versions

  • mineflayer: 4.23.0
  • server: vanilla LAN 1.16
  • node: 20.9.0 (x64)

Detailed description of a problem

Error arises at spawn of bot, forcing a shutdown.

Here is the error message:

C:\Users\<user>\Desktop\mineflayerTest\node_modules\mineflayer\lib\plugins\breath.js:16
        bot.oxygenLevel = Math.round(packet.metadata[1].value / 15)
                                                        ^

TypeError: Cannot read properties of undefined (reading 'value')
    at Client.<anonymous> (C:\Users\<user>\Desktop\mineflayerTest\node_modules\mineflayer\lib\plugins\breath.js:16:57)
    at Client.emit (node:events:526:35)
    at emitPacket (C:\Users\<user>\Desktop\mineflayerTest\node_modules\minecraft-protocol\src\client.js:83:12)
    at FullPacketParser.<anonymous> (C:\Users\<user>\Desktop\mineflayerTest\node_modules\minecraft-protocol\src\client.js:112:9)
    at FullPacketParser.emit (node:events:514:28)
    at addChunk (C:\Users\<user>\Desktop\mineflayerTest\node_modules\readable-stream\lib\internal\streams\readable.js:321:12)
    at readableAddChunk (C:\Users\<user>\Desktop\mineflayerTest\node_modules\readable-stream\lib\internal\streams\readable.js:298:9)
    at Readable.push (C:\Users\<user>\Desktop\mineflayerTest\node_modules\readable-stream\lib\internal\streams\readable.js:244:10)
    at FullPacketParser._transform (C:\Users\<user>\Desktop\mineflayerTest\node_modules\protodef\src\serializer.js:89:10)
    at Transform._write (C:\Users\<user>\Desktop\mineflayerTest\node_modules\readable-stream\lib\internal\streams\transform.js:153:8)

The concerned piece of code is as follow:

function inject (bot) {
  if (bot.supportFeature('mcDataHasEntityMetadata')) {
    // this is handled inside entities.js. We don't yet have entity metadataKeys for all versions but once we do
    // we can delete the numerical checks here and in entities.js https://github.com/extremeheat/mineflayer/blob/eb9982aa04973b0086aac68a2847005d77f01a3d/lib/plugins/entities.js#L469
    return
  }
  bot._client.on('entity_metadata', (packet) => {
    if (!bot.entity) return
    if (bot.entity.id !== packet.entityId) return
    for (const metadata of packet.metadata) {
      if (metadata.key === 1) {
        bot.oxygenLevel = Math.round(packet.metadata[1].value / 15) // <- this causes the error, packet.metadata[1] may be undefined, use metadata instead
        bot.emit('breath')
      }
    }
  })
}

Using the following code, here is the log of metadata and packet:

metadata: { key: 1, type: 1, value: 252 }
packet: { entityId: <id>, metadata: [ { key: 1, type: 1, value: 252 } ] }

As you can observe, packet.metdata[1] does not exist.

Fix

for (const metadata of packet.metadata) {
      if (metadata.key === 1) {
        bot.oxygenLevel = Math.round(metadata.value / 15) // <- this fixes it
        bot.emit('breath')
      }
    }

Expected behavior

Not to crash at spawn :^)

@wAIfu-DEV wAIfu-DEV added possible bug Stage1 just created by someone new to the project, we don't know yet if it deserves an implementation / a f labels Dec 27, 2024
@wAIfu-DEV
Copy link
Contributor Author

also sorry for not providing the fix as a pull request, I have 0 idea how those work

@rom1504
Copy link
Member

rom1504 commented Dec 28, 2024 via email

wAIfu-DEV added a commit to wAIfu-DEV/mineflayer that referenced this issue Dec 28, 2024
rom1504 pushed a commit that referenced this issue Jan 1, 2025
@wAIfu-DEV
Copy link
Contributor Author

fix was merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
possible bug Stage1 just created by someone new to the project, we don't know yet if it deserves an implementation / a f
Projects
None yet
Development

No branches or pull requests

2 participants