util#callbackify TypeScript Examples
The following examples show how to use
util#callbackify.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Inventory.ts From mineflayer-collectblock with MIT License | 6 votes |
export async function emptyInventory (bot: Bot, chestLocations: Vec3[], itemFilter: ItemFilter, cb?: Callback): Promise<void> {
// @ts-expect-error
if (cb != null) return callbackify(emptyInventory)(bot, chestLocations, cb)
if (chestLocations.length === 0) {
throw error('NoChests', 'There are no defined chest locations!')
}
// Shallow clone so we can safely remove chests from the list that are full.
chestLocations = [...chestLocations]
while (true) {
const chest = getClosestChest(bot, chestLocations)
if (chest == null) {
throw error('NoChests', 'All chests are full.')
}
const hasRemaining = await tryEmptyInventory(bot, chest, itemFilter)
if (!hasRemaining) return
}
}
Example #2
Source File: Inventory.ts From mineflayer-collectblock with MIT License | 6 votes |
async function placeItems (bot: Bot, chestPos: Vec3, itemFilter: ItemFilter, cb?: (err: Error | undefined, hasRemaining: boolean) => void): Promise<boolean> {
// @ts-expect-error
if (cb != null) return callbackify(placeItems)(bot, chestPos, itemFilter, cb)
const chestBlock = bot.blockAt(chestPos)
if (chestBlock == null) {
throw error('UnloadedChunk', 'Chest is in an unloaded chunk!')
}
const chest = await bot.openChest(chestBlock)
for (const item of bot.inventory.items()) {
if (!itemFilter(item)) continue
// @ts-expect-error; A workaround for checking if the chest is already full
if (chest.firstEmptyContainerSlot() === null) {
// We have items that didn't fit.
return true
}
await chest.deposit(item.type, item.metadata, item.count)
}
return false
}
Example #3
Source File: CollectBlock.ts From mineflayer-collectblock with MIT License | 5 votes |
/**
* If target is a block:
* Causes the bot to break and collect the target block.
*
* If target is an item drop:
* Causes the bot to collect the item drop.
*
* If target is an array containing items or blocks, preforms the correct action for
* all targets in that array sorting dynamically by distance.
*
* @param target - The block(s) or item(s) to collect.
* @param options - The set of options to use when handling these targets
* @param cb - The callback that is called finished.
*/
async collect (target: Collectable | Collectable[], options: CollectOptions | Callback = {}, cb?: Callback): Promise<void> {
if (typeof options === 'function') {
cb = options
options = {}
}
// @ts-expect-error
if (cb != null) return callbackify(this.collect)(target, options, cb)
const optionsFull: CollectOptionsFull = {
append: options.append ?? false,
ignoreNoPath: options.ignoreNoPath ?? false,
chestLocations: options.chestLocations ?? this.chestLocations,
itemFilter: options.itemFilter ?? this.itemFilter,
targets: this.targets
}
if (this.bot.pathfinder == null) {
throw error('UnresolvedDependency', 'The mineflayer-collectblock plugin relies on the mineflayer-pathfinder plugin to run!')
}
if (this.bot.tool == null) {
throw error('UnresolvedDependency', 'The mineflayer-collectblock plugin relies on the mineflayer-tool plugin to run!')
}
if (this.movements != null) {
this.bot.pathfinder.setMovements(this.movements)
}
if (!optionsFull.append) await this.cancelTask()
if (Array.isArray(target)) {
this.targets.appendTargets(target)
} else {
this.targets.appendTarget(target)
}
try {
await collectAll(this.bot, optionsFull)
} catch (err) {
this.targets.clear()
// Ignore path stopped error for cancelTask to work properly (imo we shouldn't throw any pathing errors)
// @ts-expect-error
if (err.name !== 'PathStopped') throw err
} finally {
// @ts-expect-error
this.bot.emit('collectBlock_finished')
}
}
Example #4
Source File: Inventory.ts From mineflayer-collectblock with MIT License | 5 votes |
export async function emptyInventoryIfFull (bot: Bot, chestLocations: Vec3[], itemFilter: ItemFilter, cb?: Callback): Promise<void> {
// @ts-expect-error
if (cb != null) return callbackify(emptyInventoryIfFull)(bot, chestLocations, cb)
if (bot.inventory.emptySlotCount() > 0) return
return await emptyInventory(bot, chestLocations, itemFilter)
}
Example #5
Source File: Inventory.ts From mineflayer-collectblock with MIT License | 5 votes |
async function tryEmptyInventory (bot: Bot, chestLocation: Vec3, itemFilter: ItemFilter, cb?: (err: Error | undefined, hasRemaining: boolean) => void): Promise<boolean> {
// @ts-expect-error
if (cb != null) return callbackify(tryEmptyInventory)(bot, chestLocation, itemFilter, cb)
await gotoChest(bot, chestLocation)
return await placeItems(bot, chestLocation, itemFilter)
}
Example #6
Source File: Inventory.ts From mineflayer-collectblock with MIT License | 5 votes |
async function gotoChest (bot: Bot, location: Vec3, cb?: Callback): Promise<void> {
// @ts-expect-error
if (cb != null) return callbackify(gotoChest)(bot, location)
await bot.pathfinder.goto(new goals.GoalGetToBlock(location.x, location.y, location.z))
}