PrismarineJS / mineflayer

Create Minecraft bots with a powerful, stable, and high level JavaScript API.
https://prismarinejs.github.io/mineflayer/
MIT License
4.96k stars 904 forks source link

TypeError in blocks.js #1052

Closed x4e closed 4 years ago

x4e commented 4 years ago

Versions

Detailed description of a problem

Upon joining the server the bot instantly crashes with the message:

D:\Computing\BinCommander\build\js\node_modules\mineflayer\lib\plugins\blocks.js:50
        if (!('text' in json)) return ''
                     ^
TypeError: Cannot use 'in' operator to search for 'text' in null
    at prepareJson (D:\Computing\BinCommander\build\js\node_modules\mineflayer\lib\plugins\blocks.js:50:22)
    at addBlockEntity (D:\Computing\BinCommander\build\js\node_modules\mineflayer\lib\plugins\blocks.js:57:43)
    at Client.<anonymous> (D:\Computing\BinCommander\build\js\node_modules\mineflayer\lib\plugins\blocks.js:351:5)
    at Client.emit (events.js:210:5)
    at FullPacketParser.<anonymous> (D:\Computing\BinCommander\build\js\node_modules\minecraft-protocol\src\client.js:87:12)
    at FullPacketParser.emit (events.js:210:5)
    at addChunk (D:\Computing\BinCommander\build\js\node_modules\protodef\node_modules\readable-stream\lib\_stream_readable.js:298:12)
    at readableAddChunk (D:\Computing\BinCommander\build\js\node_modules\protodef\node_modules\readable-stream\lib\_stream_readable.js:280:11)
    at FullPacketParser.Readable.push (D:\Computing\BinCommander\build\js\node_modules\protodef\node_modules\readable-stream\lib\_stream_readable.js:241:10)
    at FullPacketParser.Transform.push (D:\Computing\BinCommander\build\js\node_modules\protodef\node_modules\readable-stream\lib\_stream_transform.js:139:32)
    at FullPacketParser._transform (D:\Computing\BinCommander\build\js\node_modules\protodef\src\serializer.js:81:10)
    at FullPacketParser.Transform._read (D:\Computing\BinCommander\build\js\node_modules\protodef\node_modules\readable-stream\lib\_stream_transform.js:177:10)
    at FullPacketParser.Transform._write (D:\Computing\BinCommander\build\js\node_modules\protodef\node_modules\readable-stream\lib\_stream_transform.js:164:83)
    at doWrite (D:\Computing\BinCommander\build\js\node_modules\protodef\node_modules\readable-stream\lib\_stream_writable.js:409:139)
    at writeOrBuffer (D:\Computing\BinCommander\build\js\node_modules\protodef\node_modules\readable-stream\lib\_stream_writable.js:398:5)
    at FullPacketParser.Writable.write (D:\Computing\BinCommander\build\js\node_modules\protodef\node_modules\readable-stream\lib\_stream_writable.js:307:11)
    at Decompressor.ondata (D:\Computing\BinCommander\build\js\node_modules\minecraft-protocol\node_modules\readable-stream\lib\_stream_readable.js:681:20)
    at Decompressor.emit (events.js:210:5)
    at addChunk (D:\Computing\BinCommander\build\js\node_modules\minecraft-protocol\node_modules\readable-stream\lib\_stream_readable.js:298:12)
    at readableAddChunk (D:\Computing\BinCommander\build\js\node_modules\minecraft-protocol\node_modules\readable-stream\lib\_stream_readable.js:280:11)
    at Decompressor.Readable.push (D:\Computing\BinCommander\build\js\node_modules\minecraft-protocol\node_modules\readable-stream\lib\_stream_readable.js:241:10)
    at Decompressor.Transform.push (D:\Computing\BinCommander\build\js\node_modules\minecraft-protocol\node_modules\readable-stream\lib\_stream_transform.js:139:32)
    at Decompressor._transform (D:\Computing\BinCommander\build\js\node_modules\minecraft-protocol\src\transforms\compression.js:52:12)
    at Decompressor.Transform._read (D:\Computing\BinCommander\build\js\node_modules\minecraft-protocol\node_modules\readable-stream\lib\_stream_transform.js:177:10)
    at Decompressor.Transform._write (D:\Computing\BinCommander\build\js\node_modules\minecraft-protocol\node_modules\readable-stream\lib\_stream_transform.js:164:83)
    at doWrite (D:\Computing\BinCommander\build\js\node_modules\minecraft-protocol\node_modules\readable-stream\lib\_stream_writable.js:409:139)
    at clearBuffer (D:\Computing\BinCommander\build\js\node_modules\minecraft-protocol\node_modules\readable-stream\lib\_stream_writable.js:526:7)
    at onwrite (D:\Computing\BinCommander\build\js\node_modules\minecraft-protocol\node_modules\readable-stream\lib\_stream_writable.js:455:7)
    at WritableState.onwrite (D:\Computing\BinCommander\build\js\node_modules\minecraft-protocol\node_modules\readable-stream\lib\_stream_writable.js:160:5)
    at Decompressor.afterTransform (D:\Computing\BinCommander\build\js\node_modules\minecraft-protocol\node_modules\readable-stream\lib\_stream_transform.js:89:3)
    at Unzip.cb (D:\Computing\BinCommander\build\js\node_modules\minecraft-protocol\src\transforms\compression.js:70:16)
    at Unzip.zlibBufferOnEnd (zlib.js:139:10)
    at Unzip.emit (events.js:215:7)
    at endReadableNT (_stream_readable.js:1184:12)
    at processTicksAndRejections (internal/process/task_queues.js:80:21)

Current code

    // Handle signs
    if (blockEntity.id === 'minecraft:sign' || blockEntity.id === 'Sign') {
      const prepareJson = (i) => {
        const data = blockEntity[`Text${i}`]

        if (data === '') return ''

        const json = JSON.parse(data)
        if (!('text' in json)) return ''

        json.text = json.text.replace(/^"|"$/g, '')
        return json
      }

Expected behavior

Sign packets with invalid json should not cause a crash and be handled properly

Additional context

The server is 2b2tpvp.net if you want to test.

wvffle commented 4 years ago

This can be easily fixed by adding || data === null in https://github.com/PrismarineJS/mineflayer/blob/master/lib/plugins/blocks.js#L49