Search code examples
node.jsminecraftmineflayer

Cant access mineflayer pathfinder inside an object


Here is all the code in nodejs

const mineflayer = require('mineflayer')
const { pathfinder, Movements, goals } = require('mineflayer-pathfinder');
const GoalFollow = goals.GoalFollow


function getRandomInt(min, max) {
  return Math.floor(Math.random() * (max - min)) + min;
}

class bot{
  constructor(name, ip, port){
    this.name = name;
    this.ip = ip;
    this.port = port


    this.minebot = mineflayer.createBot({
      host: this.ip,
      username: this.name,
      port: this.port
    })

    this.minebot.on('spawn', () => {
      for(let i = 0; i < 1; i++){
        this.minebot.chat(this.name);
        this.followPlayer('Squisheyyy')
      }
    })

    this.minebot.on('kicked', () => {
      console.log('Connection closed, retrying');
      new bot(this.name, this.ip, this.port);
      delete this;
    });

    this.minebot.on('error', () => {
      console.log('Connection closed, retrying');
      new bot(this.name, this.ip, this.port);
      delete this;
    });

  }

  followPlayer(name){
    const playerCI = this.minebot.players[name];

    if(!playerCI){
      bot.chat('I dont see him');
      return;
    }

    const mcData = require('minecraft-data')(this.minebot.version);
    const movements = new Movements(this.minebot, mcData);
    this.minebot.pathfinder.setMovements(movements);

    const goal = new GoalFollow(playerCI.entity, 1);
    this.minebot.pathfinder.setGoal(goal, true);
  }
}

new bot('bot', 'ip', PORT)

Here is the line with this issue this.minebot.pathfinder.setMovements(movements); Error: TypeError: Cannot read properties of undefined (reading 'setMovements')

It says pathfinder is undefined, also the IDE tells me that 'pathfinder' is declared at the top of the code but its value is never read, I have watched some videos and this should work, but it just doesn't, probably because I am inside an object.

I am using the latest version of mineflayer at the moment


Solution

  • pathfinder is a mineflayer plugin, which means it has to be loaded. I forgot to write

    this.mineflayer.loadPlugin(pathfinder)