Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Banish callbacks to the shadow realm #248

Merged
merged 1 commit into from
Mar 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ bot.once('spawn', () => {
if (!target) return bot.chat("I don't see you !")

// Await pathfinder to complete the goal, then move to bot.chat and print "I've arrived !"
bot.pathfinder.goto(new GoalNear(target.position.x, target.position.y, target.position.z, 1), announceArrived)
bot.pathfinder.goto(new GoalNear(target.position.x, target.position.y, target.position.z, 1)).then(announceArrived)
}
})

Expand Down
9 changes: 0 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,6 @@ function inject (bot) {
return gotoUtil(bot, goal)
}

bot.pathfinder.goto = callbackify(bot.pathfinder.goto, 1)

bot.pathfinder.stop = () => {
stopPathing = true
}
Expand Down Expand Up @@ -538,13 +536,6 @@ function inject (bot) {
}
}

function callbackify (f) {
return function (...args) {
const cb = args[f.length]
return f(...args).then(r => { if (cb) { cb(null, r) } return r }, err => { if (cb) { cb(err) } else throw err })
}
}

module.exports = {
pathfinder: inject,
Movements: require('./lib/movements'),
Expand Down
6 changes: 2 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,9 @@ Also, **for now**, there is only the `pathfinder` module, `movements` and `goals

# Functions:

### bot.pathfinder.goto(goal, callback?)
Returns a Promise. Resolves when the goal is reached. Rejects on error.
### bot.pathfinder.goto(goal)
Returns a Promise with the path result. Resolves when the goal is reached. Rejects on error.
* `goal` - Goal instance
* `callback` `<function>` - (Optional) The callback is passed two arguments `(err, result)`, where error is `null` on success.


### bot.pathfinder.bestHarvestTool(block)
Returns the best harvest tool in the inventory for the specified block
Expand Down