mineflayer-pathfinder
mineflayer-pathfinder copied to clipboard
Pathfinder timeouts when it does a double goal
We have this code:
bot.on('chat', function(username, message) {
const pos = bot.entity.position;
if (username === bot.username) return
const target = bot.players[username] ? bot.players[username].entity : null
if (message === 'come') {
if (!target) {
bot.chat('I don\'t see you !')
return
}
const p = target.position
bot.pathfinder.setMovements(defaultMove)
// This line works
bot.pathfinder.setGoal(new GoalNear(p.x, p.y, p.z, 1))
bot.once('goal_reached', async() => {
console.log('Arrived, returning')
// Prints "Arrived, returning" correctly when it has arrived
bot.pathfinder.setGoal(new GoalNear(pos.x, pos.y, pos.z, 1))
// Doesn't path and pathfinder sends a status of timeout
});
}
})
async function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms))
}
Slightly modified code from example. But it has a problem, I need to add an await sleep(0), after console.log, because if I don't add it, it will timeout, this doesn't happen when I actually add an await sleep(0), like this:
bot.on('chat', function(username, message) {
const pos = bot.entity.position;
if (username === bot.username) return
const target = bot.players[username] ? bot.players[username].entity : null
if (message === 'come') {
if (!target) {
bot.chat('I don\'t see you !')
return
}
const p = target.position
bot.pathfinder.setMovements(defaultMove)
bot.pathfinder.setGoal(new GoalNear(p.x, p.y, p.z, 1))
bot.once('goal_reached', async() => {
console.log('Arrived, returning')
await sleep(0)
// Arrives, logs "Arrived, returning"
bot.pathfinder.setGoal(new GoalNear(pos.x, pos.y, pos.z, 1))
// And pathfind works
});
}
})
async function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms))
}