I'm programming a Minecraft bot to follow me, I used this code in lot of projects before 8 months and it worked. But now I have a problem that NodeJS is saying he cannot read 'position'. Code :
const mineflayer = require("mineflayer")
const { pathfinder, goals, Movements } = require('mineflayer-pathfinder')
const GoalFollow = goals.GoalFollow
const bot = mineflayer.createBot({
host: "localhost",
port: 57470,
version: "1.16.5",
username: "BotWG"
})
bot.loadPlugin(pathfinder)
function followPlayer() {
bot.chat("I've spawned")
const playerFilter = entity => entity.type === "player"
const player = bot.nearestEntity(playerFilter)
if(!player) return
const playerTF = bot.players[player]
const mcData = require("minecraft-data")(bot.version)
const mcMovements = new Movements(bot, mcData)
bot.pathfinder.setMovements(mcMovements)
const goal = new GoalFollow(playerTF, 1)
bot.pathfinder.setGoal(goal, true)
}
bot.on("spawn", followPlayer)
I've done this code before 8 months ago and it worked. I've installed all the needed packages and when I active the bot he'll join to my world and write "I've joined" then he'll leave and show me this error:
C:\Users\ahmed\Desktop\wg bot\node_modules\mineflayer-pathfinder\lib\goals.js:329 this.x = Math.floor(entity.position.x) ^ TypeError: Cannot read properties of undefined (reading 'position') at new GoalFollow (C:\Users\ahmed\Desktop\wg bot\node_modules\mineflayer-pathfinder\lib\goals.js:329:32) at EventEmitter.followPlayer (C:\Users\ahmed\Desktop\wg bot\index.js:24:18) at EventEmitter.emit (node:events:525:35) at Client. (C:\Users\ahmed\Desktop\wg bot\node_modules\mineflayer\lib\plugins\health.js:13:11) at Object.onceWrapper (node:events:628:26) at Client.emit (node:events:525:35) at emitPacket (C:\Users\ahmed\Desktop\wg bot\node_modules\minecraft-protocol\src\client.js:83:12) at FullPacketParser. (C:\Users\ahmed\Desktop\wg bot\node_modules\minecraft-protocol\src\client.js:106:9) at FullPacketParser.emit (node:events:513:28) at addChunk (C:\Users\ahmed\Desktop\wg bot\node_modules\protodef\node_modules\readable-stream\lib_stream_readable.js:279:12) Node.js v18.16.0