PrismarineJS/mineflayer-pathfinder
Pathfinder goes to last death position of target instead to new positions #128
SinanAkkoyun posted onGitHub
Hi, these are relevant code snippets: Basically the normal following works great, but (default behaviour) when the target disconnects the bot just stays where it was. So, I tried this by setting a new goal after the target joins again. After 3 seconds (delay) the bot runs for half a sec towards the target BUT then stops, stays in place and says: "I can get there in 1 moves. Computation took 2115.72 ms" with increasing ms up to around 5000.
What is going on? Also, the bot just stays there after the target died, even if it respawns right next to him. Is there some callback to catch for the entity going missing instead of manually tracking who joined or died?
My code:
//Follow command, works everytime after being called:
goal_memory = [new goals.GoalFollow(target, 2), (cmd[2] ? (isYes(cmd[2]) ? true : false) : true)]
bot.pathfinder.setGoal(goal_memory[0], goal_memory[1])
//Event, doesnt work at all in very strage way.
//Note that the getPlayer function is somewhat a bot.players[username]
//The log of the entity returns the valid entity as json
//goal_memory[1] stores if its dynamic (which it is everytime in this scenario)
bot.on("playerJoined", (player) => {
if(goal_memory && goal_memory[0] instanceof goals.GoalFollow && goal_memory[0].entity.username.trim() === player.username.trim()) {
setTimeout(() => { //Non blocking while(!p.entity)) would be better
if(getPlayer(player.username) && getPlayer(player.username).entity) {
log(JSON.stringify(getPlayer(player.username).entity, null, 2))
//log(getPlayer(player.username).entity.username)
//bot.pathfinder.setGoal(null)
bot.pathfinder.setGoal(null)
bot.pathfinder.setGoal(new goals.GoalFollow(getPlayer(player.username).entity), goal_memory[1])
//let walk_pos = bot.spawnPoint
//bot.pathfinder.setGoal(new goals.GoalXZ(walk_pos.x, walk_pos.z, 0))
log("[Pathfinder] Set goal to last memory.")
} else {
log("[Memory] Error, could not get entity of " + player.username + ".")
}
}, 3000);
}
})