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

Unexpected error #3244

Closed gabrielmar closed 9 months ago

gabrielmar commented 10 months ago

Versions:

Problem:

Unexpected error crashes the application

Errors:

PartialReadError: Read error for undefined : Missing characters in string, found size is 24 expected size was 111
    at new ExtendableError (/node_modules/protodef/src/utils.js:63:13)
    at new PartialReadError (/node_modules/protodef/src/utils.js:70:5)
    at ProtoDef.readPString (/node_modules/protodef/src/datatypes/utils.js:109:11)
    at ProtoDef.read (/node_modules/protodef/src/protodef.js:36:25)
    at ProtoDef.read (/node_modules/protodef/src/protodef.js:36:25)
    at ProtoDef.read (/node_modules/protodef/src/protodef.js:114:29)
    at e.message (/node_modules/protodef/src/protodef.js:153:49)
    at tryCatch (/node_modules/protodef/src/utils.js:50:16)
    at ProtoDef.parsePacketBuffer (/node_modules/protodef/src/protodef.js:153:29)
    at Client.onCustomPayload (/node_modules/minecraft-protocol/src/client/pluginChannels.js:56:31) {
  partialReadError: true
}
RangeError [ERR_OUT_OF_RANGE]: The value of "sourceStart" is out of range. It must be >= 0 && <= 256. Received 8192
    at _copy (node:buffer:226:13)
    at Buffer.copy (node:buffer:816:12)
    at Chunk.load (/node_modules/prismarine-chunk/src/pc/1.8/chunk.js:205:31)
    at addColumn (/node_modules/mineflayer/lib/plugins/blocks.js:57:14)
    at Client.<anonymous> (/node_modules/mineflayer/lib/plugins/blocks.js:272:5)
    at Client.emit (node:events:514:28)
    at emitPacket (/node_modules/minecraft-protocol/src/client.js:83:12)
    at FullPacketParser.<anonymous> (/node_modules/minecraft-protocol/src/client.js:107:9)
    at FullPacketParser.emit (node:events:514:28)
    at addChunk (/node_modules/protodef/node_modules/readable-stream/lib/_stream_readable.js:279:12) {
  code: 'ERR_OUT_OF_RANGE'
}

Code

import mineflayer from 'mineflayer';

const bot = mineflayer.createBot({
  host: 'IP ADDRESS',
  username: Array.from({ length: 2 }, () => (Math.random() * 10).toString(36))
    .join('')
    .replace(/\W/g, '')
    .slice(0, 16),
  auth: 'offline',
  version: '1.8.9',
});

bot.on('entityUpdate', (entity) => {
  if (entity.type === 'object' && entity.name === 'ArmorStand') {
    const armor = entity.metadata
      .filter((meta) => typeof meta === 'string' && String(meta).includes(textToSearch))
      .map((meta) => String(meta).replace(/\D/g, ''))
      .join('');
    if (armor) {
      resolve(parseInt(armor));
      bot.end();
    }
  }
});

bot.on('kicked', () => {
  resolve(-1);
});

bot.on('end', () => {
  resolve(-1);
});

bot.on('error', () => {
  resolve(-1);
  bot.end();
});
extremeheat commented 10 months ago

There's no reason that should be crashing the bot. If the bot quits the server, that's because you explicitly instruct it to do so in this code on error. Serialization errors can be ignored, it could very well just be the server sending invalid data. The second error could also be an issue with prismarine-chunk's handling of 1.8 chunks if it's not the servers' fault, but that's not clear based on the data.

gabrielmar commented 9 months ago

I did a try catch to avoid crashing my code, I don't know why this error occurred.