PrismarineJS / mineflayer

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

Unexpected token t in JSON at position 1 #2583

Open Koteinik opened 2 years ago

Koteinik commented 2 years ago

Versions

Detailed description of a problem

I am making an AFK bot, just for fun. After running about some hours, it throws errors.

What did you try yet?

Try-cathes, and repl.it while loop (not working).

Your current code


/*
const mineflayer = require('mineflayer');
const antiafk = require("mineflayer-antiafk");

const config = require('./settings.json');

function createBot() {
  const bot = mineflayer.createBot({
    username: config['bot-account']['username'],
    password: config['bot-account']['password'],
    auth: config['bot-account']['type'],
    host: config.server.ip,
    port: config.server.port,
    version: config.server.version,
  });
  const mcData = require('minecraft-data')(bot.version);
  bot.settings.colorsEnabled = false;
  bot.loadPlugin(antiafk);

  bot.once('spawn', () => {
    console.log('\x1b[33m[BotLog] Bot joined to the server', '\x1b[0m');

    if (config.utils['auto-auth'].enabled) {
      console.log('[INFO] Started auto-auth module');

      var password = config.utils['auto-auth'].password;
      setTimeout(() => {
        bot.chat(`/register ${password} ${password}`);
        bot.chat(`/login ${password}`);
        bot.chat(`/afk`);
      }, 5000);

      console.log(`[Auth] Authentification commands executed.`);
    }

    const pos = config.position;

    if (config.utils['anti-afk'].enabled) {
      bot.setControlState('jump', true);
      if (config.utils['anti-afk'].sneak) {
        bot.setControlState('sneak', true);
      }
    }
  });

  bot.on('chat', (username, message) => {
    console.log('[INFO] Message recieved from (' + username + "): " + message);
    //    if (message === "join") {
    //      bot.chat("/join-game");
    //    }
  });

  bot.on('death', () => {
    console.log(
      `\x1b[33m[BotLog] Bot has been died and was respawned ${bot.entity.position}`,
      '\x1b[0m'
    );
  });

  if (config.utils['auto-reconnect']) {
    bot.on('end', () => {
      setTimeout(() => {
        createBot();
      }, config.utils['auto-recconect-delay']);
    });
  }

  bot.on('kicked', (reason) =>
    console.log(
      '\x1b[33m',
      `[BotLog] Bot was kicked from the server. Reason: \n${reason}`,
      '\x1b[0m'
    )
  );

  bot.on('error', (err) =>{
    console.log(`\x1b[31m[ERROR] ${err.message}`, '\x1b[0m');
  });

  bot.on('spawn', () => {
    bot.afk.setOptions({ fishing: false }); //disables fishing
    bot.afk.setOptions({ chatting: false });
    bot.afk.start();
  });
}

const sleep = ms => new Promise(r => setTimeout(r, ms));

createBot();
*/

Expected behavior

I also installed mineflayer-antiafk, and the bot runs good until exceptions.

Additional context

I have this exception:

/home/user/node_modules/minecraft-protocol/src/transforms/framing.js:67
          } else { throw e }
                   ^

SyntaxError: Unexpected token t in JSON at position 1
    at JSON.parse (<anonymous>)
    at Client.<anonymous> (/home/user/node_modules/minecraft-protocol/src/client/versionChecking.js:4:25)
    at Client.emit (node:events:402:35)
    at Client.emit (node:domain:475:12)
    at FullPacketParser.<anonymous> (/home/user/node_modules/minecraft-protocol/src/client.js:91:12)
    at FullPacketParser.emit (node:events:390:28)
    at FullPacketParser.emit (node:domain:475:12)
    at addChunk (/home/user/node_modules/readable-stream/lib/_stream_readable.js:298:12)
    at readableAddChunk (/home/user/node_modules/readable-stream/lib/_stream_readable.js:280:11)
repl process died unexpectedly: exit status 1

Also, sometimes I recieve exception that block what bot tries to dig is null

Koteinik commented 2 years ago

Also, I have update my node.js version to latest of 16.x

IceTank commented 2 years ago

This is a bug in node-minecraft-protocol. You are being kicked by the server. node-minecraft-protocol tries to figure out why by parsing the reason as a json string but fails and throws this error.

IceTank commented 2 years ago

Made a bug report with node-minecraft-protocol https://github.com/PrismarineJS/node-minecraft-protocol/issues/989

You can try and fix node-minecraft-protocol yourself in your node_modules folder to see why the server is kicking you.

Koteinik commented 2 years ago

Maybe I can place try/catch on json parse, and if fails, just return?