Cannot follow user (Mineflayer - Node.js)

66 Views Asked by At

I am trying to follow a player using the mineflayer library in Node.js but it turns up with a error within the source code of the library itself.

I tried the following code:

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

const bot = mineflayer.createBot({
  host: 'PRIVATE',
  username: 'TRASHCAN'
})

bot.loadPlugin(pathfinder)

function followPlayer() {
  const player = p => p.type === "player";
  const mcData = require('minecraft-data')(bot.version);
  const movements = new Movements(mcData);
  
  bot.pathfinder.setMovements(movements);
  const goal = new GoalFollow(player.entity, 3)
  bot.pathfinder.setGoal(goal, true);

}
const prefix = ",s"
const password = 'REMOVED'
const login = `/login ${password}`

const { mineflayer: mineflayerViewer } = require('prismarine-viewer')
bot.once('spawn', () => {
  mineflayerViewer(bot, { port: 3007, firstPerson: true }) // port is the minecraft server port, if first person is false, you get a bird's-eye view
  bot.chat(login)
  bot.on('whisper', (username, message) => {
    if (message.startsWith(prefix) && username === 'Hitrocol') {
        bot.chat(message.split(',TRASHCAN_')[1]);
    }
    if (message.content === ',TRASHCAN_LOGIN' && username === 'Hitrocol') {
        bot.chat(login)
    }
  })
})

function lookAtNearestPlayer() {
    const playerFilter = (entity) => entity.type === 'player'
    const playerEntity = bot.nearestEntity(playerFilter);

    if (!playerEntity) return;
    const pos= playerEntity.position;
    bot.lookAt(pos);
}

bot.on('physicsTick', lookAtNearestPlayer);
bot.on('physicsTick', followPlayer);

I expected it to follow the player but instead it resulted with a error which looked like it was an error from the source code of mineflayer pathfinder (both mineflayer and mineflayer pathfinder are latest):

/Users/aarav/node_modules/mineflayer-pathfinder/lib/movements.js:42
    this.blocksCantBreak.add(registry.blocksByName.chest.id)
                                      ^

TypeError: Cannot read properties of undefined (reading 'blocksByName')
    at new Movements (/Users/aarav/node_modules/mineflayer-pathfinder/lib/movements.js:42:39)
    at EventEmitter.followPlayer (/Users/aarav/Downloads/codingshit/untitled folder/index.js:15:21)
    at EventEmitter.emit (node:events:529:35)
    at tickPhysics (/Users/aarav/node_modules/mineflayer/lib/plugins/physics.js:81:11)
    at Timeout.doPhysics [as _onTimeout] (/Users/aarav/node_modules/mineflayer/lib/plugins/physics.js:70:7)
    at listOnTimeout (node:internal/timers:569:17)
    at process.processTimers (node:internal/timers:512:7)

Node.js v18.18.0
0

There are 0 best solutions below