mineflayer-navigate-promise
v0.2.0
Published
thin wrapper around the mineflayer-navigate plugin, to work with promises instead of events
Downloads
6
Maintainers
Readme
mineflayer-navigate-promise
A thin wrapper around the mineflayer-navigate plugin which enables your mineflayer bot to navigate. For a description on the mineflayer-navigate plugin see https://github.com/PrismarineJS/mineflayer-navigate. The injected methods of this plugin are completely preserved and can be used as indicated. This module adds additional methods as described below.
Motivation: I build this in order to use the navigation of my bot in a more convenient way. Especially when it comes to perform some tasks in a sequential way it turns out to be useful to support promises (e.g. walk to some place and interact with the chest there).
See also the mineflayer project from PrismarineJS at https://github.com/PrismarineJS/mineflayer
Documentation
In the following sections the functions are described and short usage examples are provided. See the examples/test.js
file for a more detailed example and also see the examples in the mineflayer-navigate project.
bot.navigate.promise.to(destination, options)
Finds a path to the destination and walks there. It calls the bot.navigate.to function from the mineflayer-navigate module.
destination
- the block you want to gooptions
- see the mineflayer-navigate documentation for it
This returns a promise which will be in a pending state as long as the navigation (find path and walk along) process takes place. Once fulfilled the bot has reached its destination. If the promise gets rejected you will find one of the following reasons in the returned error object:
- "aborted": Returned whenever a pending navigation process is interfered by some other calls to the navigation module. This applies also if you call bot.navigate.promise.to again while one promise is still pending.
- "cannotFind": Returned when a path could not be found.
- "obstructed": Returned when the bot finds itself in an strange state (e.g. on top of a fence and is unable to move further)
Note that whenever the promise is rejected the position of the bot may already have changed.
Promises enable you to use those nice language features like the sequential looking programs you get under the use of await:
try {
await bot.navigate.promise.to(target.position);
bot.chat(`destination reached :)`);
} catch (err) {
bot.chat(`error (message=${err.message})`);
}
Or you could use it more traditional with the .then() method.
bot.navigate.promise.findPath(destination, [options])
destination
- the block you want to gooptions
- consult the mineflayer-navigate documentation for it
Finds a path to the destination
and returns a promise for it. Once fulfilled it contains the path (consisting of a sequence of points, including the starting point and destination). A rejected promise will contain one of the following messages in the error object:
- "noPath": A path could not be found.
- "timeout": The process timed out before obtaining a path.
- "tooFar": The destination is out of reach.
Example see below.
bot.navigate.promise.walk(path)
Walks the bot along the given path
and resolves once it has arrived. If errors occur the promise will be rejected with an error object containing the reason for it ("obstructed" or "interrupted"). Note that whenever the promise is rejected the position of the bot may already have changed.
Here is an example call:
try {
const path = await bot.navigate.promise.findPath(target.position);
await bot.navigate.promise.walk(path);
bot.chat('promise resolved, i am here :)');
} catch (err) {
bot.chat(`problems :( (message=${err.message})`);
}
Notes
License: MIT