### Install mineflayer-collectblock Source: https://github.com/prismarinejs/mineflayer-collectblock/blob/master/README.md Install the mineflayer-collectblock plugin using npm. This command adds the package as a dependency to your project. ```bash npm install --save mineflayer-collectblock ``` -------------------------------- ### Collect Grass Block with mineflayer-collectblock Source: https://github.com/prismarinejs/mineflayer-collectblock/blob/master/README.md This example demonstrates how to load the mineflayer-collectblock plugin and use its `collect` function to gather nearby grass blocks. It includes error handling and a recursive call to collect subsequent blocks. Ensure `minecraft-data` is loaded before calling `collectGrass`. ```javascript // Create your bot const mineflayer = require("mineflayer") const bot = mineflayer.createBot({ host: 'localhost', username: 'Player', }) let mcData // Load collect block bot.loadPlugin(require('mineflayer-collectblock').plugin) async function collectGrass() { // Find a nearby grass block const grass = bot.findBlock({ matching: mcData.blocksByName.grass_block.id, maxDistance: 64 }) if (grass) { // If we found one, collect it. try { await bot.collectBlock.collect(grass) collectGrass() // Collect another grass block } catch (err) { console.log(err) // Handle errors, if any } } } // On spawn, start collecting all nearby grass bot.once('spawn', () => { mcData = require('minecraft-data')(bot.version) collectGrass() }) ``` -------------------------------- ### bot.collectBlock.findFromVein Source: https://context7.com/prismarinejs/mineflayer-collectblock/llms.txt Uses a flood-fill algorithm to find all touching blocks of the same type starting from a seed block. This is useful for collecting entire ore veins or trees. ```APIDOC ## `bot.collectBlock.findFromVein(block, [maxBlocks], [maxDistance], [floodRadius])` — Ore vein detection Uses a flood-fill (BFS) algorithm starting at `block` to find all touching blocks of the same type. Ideal for collecting an entire ore vein or a whole tree by passing only one known block. Returns `Block[]`. | Parameter | Type | Default | Description | |---|---|---|---| | `block` | `Block` | required | The seed block to start from | | `maxBlocks` | `number` | `100` | Maximum number of blocks to return | | `maxDistance` | `number` | `16` | Manhattan distance limit from the seed block | | `floodRadius` | `number` | `1` | Adjacency radius — `1` means face/edge/corner neighbours | ```js const mineflayer = require('mineflayer') const { plugin: collectBlockPlugin } = require('mineflayer-collectblock') const bot = mineflayer.createBot({ host: 'localhost', port: 25565, username: 'oreMiner' }) bot.loadPlugin(collectBlockPlugin) let mcData bot.once('spawn', async () => { mcData = require('minecraft-data')(bot.version) // Find one iron ore block, then expand to the whole vein const seedBlock = bot.findBlock({ matching: mcData.blocksByName.iron_ore.id, maxDistance: 64 }) if (!seedBlock) { console.log('No iron ore nearby.') return } // Collect the full vein (up to 100 blocks, within 16 blocks of the seed) const vein = bot.collectBlock.findFromVein(seedBlock) console.log(`Found ${vein.length} iron ore blocks in the vein.`) try { await bot.collectBlock.collect(vein) console.log('Entire vein collected.') } catch (err) { console.error('Vein collection error:', err.message) } // Wider radius example — collect a whole oak tree (logs touch diagonally) const logSeed = bot.findBlock({ matching: mcData.blocksByName.oak_log.id, maxDistance: 32 }) if (logSeed) { const treeLogs = bot.collectBlock.findFromVein(logSeed, 50, 20, 1) await bot.collectBlock.collect(treeLogs) console.log(`Collected ${treeLogs.length} oak logs.`) } }) ``` ``` -------------------------------- ### Customize Pathfinder Movements Source: https://context7.com/prismarinejs/mineflayer-collectblock/llms.txt Configure `bot.collectBlock.movements` with a `mineflayer-pathfinder` Movements object to control bot navigation during collection. Set to `null` to retain existing pathfinder settings. This example disables parkour and scaffolding. ```javascript const mineflayer = require('mineflayer') const { plugin: collectBlockPlugin } = require('mineflayer-collectblock') const { Movements } = require('mineflayer-pathfinder') const bot = mineflayer.createBot({ host: 'localhost', port: 25565, username: 'safeBot' }) bot.loadPlugin(collectBlockPlugin) bot.once('spawn', async () => { const mcData = require('minecraft-data')(bot.version) // Customise movements — disable parkour and scaffolding const movements = new Movements(bot) movements.canDig = false // Don't dig extra blocks while pathfinding movements.allow1by1towers = false bot.collectBlock.movements = movements // Set to null to preserve whatever pathfinder movements are already configured // bot.collectBlock.movements = null const target = bot.findBlock({ matching: mcData.blocksByName.oak_log.id, maxDistance: 32 }) if (target) { await bot.collectBlock.collect(target) } }) ``` -------------------------------- ### Find and Collect Ore Veins with mineflayer-collectblock Source: https://context7.com/prismarinejs/mineflayer-collectblock/llms.txt Use `findFromVein` to detect all connected blocks of the same type, starting from a single seed block. This is useful for collecting entire ore veins or trees. The `collect` function then gathers the identified blocks. ```javascript const mineflayer = require('mineflayer') const { plugin: collectBlockPlugin } = require('mineflayer-collectblock') const bot = mineflayer.createBot({ host: 'localhost', port: 25565, username: 'oreMiner' }) bot.loadPlugin(collectBlockPlugin) let mcData bot.once('spawn', async () => { mcData = require('minecraft-data')(bot.version) // Find one iron ore block, then expand to the whole vein const seedBlock = bot.findBlock({ matching: mcData.blocksByName.iron_ore.id, maxDistance: 64 }) if (!seedBlock) { console.log('No iron ore nearby.') return } // Collect the full vein (up to 100 blocks, within 16 blocks of the seed) const vein = bot.collectBlock.findFromVein(seedBlock) console.log(`Found ${vein.length} iron ore blocks in the vein.`) try { await bot.collectBlock.collect(vein) console.log('Entire vein collected.') } catch (err) { console.error('Vein collection error:', err.message) } // Wider radius example — collect a whole oak tree (logs touch diagonally) const logSeed = bot.findBlock({ matching: mcData.blocksByName.oak_log.id, maxDistance: 32 }) if (logSeed) { const treeLogs = bot.collectBlock.findFromVein(logSeed, 50, 20, 1) await bot.collectBlock.collect(treeLogs) console.log(`Collected ${treeLogs.length} oak logs.`) } }) ``` -------------------------------- ### Register mineflayer-collectblock Plugin Source: https://context7.com/prismarinejs/mineflayer-collectblock/llms.txt Register the plugin with your Mineflayer bot. This makes `bot.collectBlock` available and automatically loads peer dependencies. ```javascript const mineflayer = require('mineflayer') const { plugin: collectBlockPlugin } = require('mineflayer-collectblock') const bot = mineflayer.createBot({ host: 'localhost', port: 25565, username: 'harvester' }) // Register the plugin — bot.collectBlock is now available bot.loadPlugin(collectBlockPlugin) bot.once('spawn', () => { console.log('collectBlock ready:', typeof bot.collectBlock) // 'object' }) ``` -------------------------------- ### Configure Global and Per-Call Item Filters Source: https://context7.com/prismarinejs/mineflayer-collectblock/llms.txt Override the global `bot.collectBlock.itemFilter` to define which items are deposited into chests. Use the `itemFilter` option in `collect()` for per-call specific filtering. Items for which the filter returns `false` are not deposited. ```javascript const mineflayer = require('mineflayer') const { plugin: collectBlockPlugin } = require('mineflayer-collectblock') const { Vec3 } = require('vec3') const bot = mineflayer.createBot({ host: 'localhost', port: 25565, username: 'selectiveBot' }) bot.loadPlugin(collectBlockPlugin) bot.once('spawn', async () => { const mcData = require('minecraft-data')(bot.version) bot.collectBlock.chestLocations = [new Vec3(0, 64, 0)] // Global override: only deposit cobblestone and dirt; keep everything else bot.collectBlock.itemFilter = (item) => { return item.name === 'cobblestone' || item.name === 'dirt' } // Per-call override: deposit everything except diamonds const targets = bot.findBlocks({ matching: mcData.blocksByName.stone.id, maxDistance: 32, count: 50 }).map(p => bot.blockAt(p)) await bot.collectBlock.collect(targets, { itemFilter: (item) => item.name !== 'diamond' }) }) ``` -------------------------------- ### bot.collectBlock.collect(target, [options], [cb]) Source: https://context7.com/prismarinejs/mineflayer-collectblock/llms.txt The primary API method for collecting blocks or item drops. It handles pathfinding, tool selection, digging, and item collection. It can accept a single target, an array of targets, or be used with a callback. ```APIDOC ## `bot.collectBlock.collect(target, [options], [cb])` — Collect blocks or item drops The primary API method. Accepts a single `Block` or `Entity` (item drop), or an array of either, as `target`. The bot pathfinds to each target in nearest-first order, equips the best available tool, digs the block, waits for item drops to appear, then walks over them. Returns a `Promise`; also accepts an optional Node-style callback as the last argument. ### Parameters - **target** (Block | Entity | Block[] | Entity[]) - Required - The block(s) or item entity/entities to collect. - **options** (CollectOptions) - Optional - Configuration options for the collection task. - **cb** (function) - Optional - A Node-style callback function that is called upon completion or error. ### CollectOptions reference | Option | Type | Default | Description | |---|---|---|---| | `append` | `boolean` | `false` | Append targets to the current task instead of replacing it | | `ignoreNoPath` | `boolean` | `false` | Skip blocks that have no reachable path instead of throwing | | `chestLocations` | `Vec3[]` | `bot.collectBlock.chestLocations` | Override chest locations for this call | | `itemFilter` | `(item) => boolean` | `bot.collectBlock.itemFilter` | Override the item filter used when depositing to chests | ### Request Example ```js // Collect a single block const logBlock = bot.findBlock({ matching: mcData.blocksByName.oak_log.id, maxDistance: 32 }) if (logBlock) { await bot.collectBlock.collect(logBlock) console.log('Collected one oak log.') } // Collect multiple blocks const stoneBlocks = bot.findBlocks({ matching: mcData.blocksByName.stone.id, maxDistance: 32, count: 10 }).map(pos => bot.blockAt(pos)) try { await bot.collectBlock.collect(stoneBlocks) console.log('Collected all stone blocks.') } catch (err) { console.error(err.message) } // Append additional targets bot.collectBlock.collect(moreStone, { append: true }) .then(() => console.log('Appended targets collected.')) // Ignore pathfinding errors const deepBlock = bot.findBlock({ matching: mcData.blocksByName.diamond_ore.id, maxDistance: 64 }) if (deepBlock) { await bot.collectBlock.collect(deepBlock, { ignoreNoPath: true }) } // Callback-style usage bot.collectBlock.collect(logBlock, (err) => { if (err) console.error(err) else console.log('Done via callback.') }) ``` ### Response Returns a `Promise` that resolves when all targets are collected or rejects if an error occurs (unless `ignoreNoPath` is true for unreachable blocks). ``` -------------------------------- ### Plugin Registration Source: https://context7.com/prismarinejs/mineflayer-collectblock/llms.txt Register the mineflayer-collectblock plugin with your Mineflayer bot. This makes the `bot.collectBlock` API available. ```APIDOC ## Plugin Registration — `plugin` The exported `plugin` function is loaded into a Mineflayer bot with `bot.loadPlugin()`. ```js const mineflayer = require('mineflayer') const { plugin: collectBlockPlugin } = require('mineflayer-collectblock') const bot = mineflayer.createBot({ host: 'localhost', port: 25565, username: 'harvester' }) // Register the plugin — bot.collectBlock is now available bot.loadPlugin(collectBlockPlugin) bot.once('spawn', () => { console.log('collectBlock ready:', typeof bot.collectBlock) // 'object' }) ``` ``` -------------------------------- ### Collect a Single Block Source: https://context7.com/prismarinejs/mineflayer-collectblock/llms.txt Collect a single specified block. The bot will find the nearest matching block, equip the best tool, dig it, and pick up the drop. ```javascript const mineflayer = require('mineflayer') const { plugin: collectBlockPlugin } = require('mineflayer-collectblock') const { Vec3 } = require('vec3') const bot = mineflayer.createBot({ host: 'localhost', port: 25565, username: 'miner' }) bot.loadPlugin(collectBlockPlugin) let mcData bot.once('spawn', async () => { mcData = require('minecraft-data')(bot.version) // --- Collect a single block --- const logBlock = bot.findBlock({ matching: mcData.blocksByName.oak_log.id, maxDistance: 32 }) if (logBlock) { try { await bot.collectBlock.collect(logBlock) console.log('Collected one oak log.') } catch (err) { console.error('Failed to collect block:', err.message) } } // --- Collect multiple blocks at once --- const stonePositions = bot.findBlocks({ matching: mcData.blocksByName.stone.id, maxDistance: 32, count: 10 }) const stoneBlocks = stonePositions.map(pos => bot.blockAt(pos)) try { await bot.collectBlock.collect(stoneBlocks) console.log('Collected all stone blocks.') } catch (err) { console.error(err.message) } // --- Append additional targets while a task is already running --- const moreStone = bot.findBlocks({ matching: mcData.blocksByName.stone.id, maxDistance: 64, count: 5 }).map(pos => bot.blockAt(pos)) bot.collectBlock.collect(moreStone, { append: true }) .then(() => console.log('Appended targets collected.')) // --- Ignore pathfinding errors (try best position) --- const deepBlock = bot.findBlock({ matching: mcData.blocksByName.diamond_ore.id, maxDistance: 64 }) if (deepBlock) { await bot.collectBlock.collect(deepBlock, { ignoreNoPath: true }) } // --- Callback-style usage --- if (logBlock) { bot.collectBlock.collect(logBlock, (err) => { if (err) console.error(err) else console.log('Done via callback.') }) } }) ``` -------------------------------- ### bot.collectblock.collect Source: https://github.com/prismarinejs/mineflayer-collectblock/blob/master/docs/api.md Causes the bot to collect the given block, item drop, or list of those. If the target is a block, the bot will move to the block, mine it, and pick up the item drop. If the target is an item drop, the bot will move to the item drop and pick it up. If the target is a list of collectables, the bot will move from target to target in order of closest to furthest and collect each target in turn. ```APIDOC ## Function: bot.collectblock.collect ### Description Causes the bot to collect the given block, item drop, or list of those. If the target is a block, the bot will move to the block, mine it, and pick up the item drop. If the target is an item drop, the bot will move to the item drop and pick it up. If the target is a list of collectables, the bot will move from target to target in order of closest to furthest and collect each target in turn. ### Usage `bot.collectblock.collect(target: Collectable | Collectable[], options?: CollectOptions, cb: (err?: Error) => void): void` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Options * `append` (boolean) - Optional - If true, the target(s) will be appended to the existing target list instead of starting a new task. Defaults to false. * `ignoreNoPath` (boolean) - Optional - If true, errors will not be thrown when a path to the target block cannot be found. The bot will attempt to choose the best available position it can find, instead. Errors are still thrown if the bot cannot interact with the block from it's final location. Defaults to false. * `chestLocations` (Vec3[]) - Optional - Gets the list of chest locations to use when storing items after the bot's inventory becomes full. If undefined, it defaults to the chest location list on the bot.collectBlock plugin. * `itemFilter` (ItemFilter) - Optional - When transferring items to a chest, this filter is used to determine what items are allowed to be moved, and what items aren't allowed to be moved. Defaults to the item filter specified on the bot.collectBlock plugin. ### Request Example None provided in source. ### Response #### Success Response (void) This function does not return a value directly, but it takes a callback function that is executed upon completion or error. #### Response Example None provided in source. ``` -------------------------------- ### Automatic Inventory Management with Chests in mineflayer-collectblock Source: https://context7.com/prismarinejs/mineflayer-collectblock/llms.txt Configure `bot.collectBlock.chestLocations` with `Vec3` positions of chests to enable automatic inventory management. When the inventory is full during a collection task, the bot will deposit items into the nearest registered chest. ```javascript const mineflayer = require('mineflayer') const { plugin: collectBlockPlugin } = require('mineflayer-collectblock') const { Vec3 } = require('vec3') const bot = mineflayer.createBot({ host: 'localhost', port: 25565, username: 'storageBot' }) bot.loadPlugin(collectBlockPlugin) let mcData bot.once('spawn', async () => { mcData = require('minecraft-data')(bot.version) // Discover all chests within 16 blocks and register them bot.collectBlock.chestLocations = bot.findBlocks({ matching: mcData.blocksByName.chest.id, maxDistance: 16, count: 999999 }) console.log(`Registered ${bot.collectBlock.chestLocations.length} chests.`) // Or register specific known positions bot.collectBlock.chestLocations = [ new Vec3(10, 64, 20), new Vec3(12, 64, 20) ] // Now collect — inventory auto-empties into the closest registered chest when full const targets = bot.findBlocks({ matching: mcData.blocksByName.cobblestone.id, maxDistance: 64, count: 200 }).map(p => bot.blockAt(p)) try { await bot.collectBlock.collect(targets) console.log('Done. Inventory was managed automatically.') } catch (err) { if (err.name === 'NoChests') { console.error('All chests are full or none are accessible.') } else { console.error(err.message) } } }) ``` -------------------------------- ### bot.collectblock.movements Source: https://github.com/prismarinejs/mineflayer-collectblock/blob/master/docs/api.md The movements object used by the pathfinder plugin to define the movement configuration. This object is passed to the pathfinder plugin when any API from this plugin is called in order to control how pathfinding should work when collecting the given blocks or item. If set to null, the pathfinder plugin movements is not updated. Defaults to a new movements object instance. ```APIDOC ## Property: bot.collectblock.movements ### Description The movements object used by the pathfinder plugin to define the movement configuration. This object is passed to the pathfinder plugin when any API from this plugin is called in order to control how pathfinding should work when collecting the given blocks or item. If set to null, the pathfinder plugin movements is not updated. Defaults to a new movements object instance. ### Type Movements ``` -------------------------------- ### bot.collectBlock.chestLocations Source: https://context7.com/prismarinejs/mineflayer-collectblock/llms.txt An array of Vec3 positions of chests that the bot can use to deposit items when its inventory is full during a collection task. The bot will automatically navigate to the nearest registered chest, deposit items, and resume collection. ```APIDOC ## `bot.collectBlock.chestLocations` — Automatic inventory management An array of `Vec3` positions of chests the bot is allowed to deposit items into when its inventory becomes full during collection. The bot walks to the nearest chest, deposits filtered items, then resumes collecting. If all chests are full, a `NoChests` error is thrown. ```js const mineflayer = require('mineflayer') const { plugin: collectBlockPlugin } = require('mineflayer-collectblock') const { Vec3 } = require('vec3') const bot = mineflayer.createBot({ host: 'localhost', port: 25565, username: 'storageBot' }) bot.loadPlugin(collectBlockPlugin) let mcData bot.once('spawn', async () => { mcData = require('minecraft-data')(bot.version) // Discover all chests within 16 blocks and register them bot.collectBlock.chestLocations = bot.findBlocks({ matching: mcData.blocksByName.chest.id, maxDistance: 16, count: 999999 }) console.log(`Registered ${bot.collectBlock.chestLocations.length} chests.`) // Or register specific known positions bot.collectBlock.chestLocations = [ new Vec3(10, 64, 20), new Vec3(12, 64, 20) ] // Now collect — inventory auto-empties into the closest registered chest when full const targets = bot.findBlocks({ matching: mcData.blocksByName.cobblestone.id, maxDistance: 64, count: 200 }).map(p => bot.blockAt(p)) try { await bot.collectBlock.collect(targets) console.log('Done. Inventory was managed automatically.') } catch (err) { if (err.name === 'NoChests') { console.error('All chests are full or none are accessible.') } else { console.error(err.message) } } }) ``` ``` -------------------------------- ### bot.collectBlock.cancelTask Source: https://context7.com/prismarinejs/mineflayer-collectblock/llms.txt Stops the active pathfinder movement and clears the pending target queue. It resolves (or calls a callback) once the task has fully stopped and is safe to call even when no task is running. ```APIDOC ## `bot.collectBlock.cancelTask([cb])` — Cancel an in-progress collection Stops the active pathfinder movement and clears the pending target queue. Resolves (or calls `cb`) once the task has fully stopped. Safe to call even when no task is running. ```js const mineflayer = require('mineflayer') const { plugin: collectBlockPlugin } = require('mineflayer-collectblock') const bot = mineflayer.createBot({ host: 'localhost', port: 25565, username: 'worker' }) bot.loadPlugin(collectBlockPlugin) let mcData bot.once('spawn', async () => { mcData = require('minecraft-data')(bot.version) const blocks = bot.findBlocks({ matching: mcData.blocksByName.dirt.id, maxDistance: 64, count: 100 }).map(p => bot.blockAt(p)) // Start a long-running collection task (don't await — cancel it shortly after) bot.collectBlock.collect(blocks).catch(() => {}) // Cancel after 3 seconds setTimeout(async () => { await bot.collectBlock.cancelTask() console.log('Task cancelled successfully.') }, 3000) // Callback style bot.collectBlock.collect(blocks).catch(() => {}) setTimeout(() => { bot.collectBlock.cancelTask(() => { console.log('Task cancelled via callback.') }) }, 1000) }) ``` ``` -------------------------------- ### Cancel Collection Task with mineflayer-collectblock Source: https://context7.com/prismarinejs/mineflayer-collectblock/llms.txt Use `cancelTask` to stop any ongoing collection or pathfinding initiated by the plugin. It can be called with or without a callback function. This is safe to use even if no task is currently active. ```javascript const mineflayer = require('mineflayer') const { plugin: collectBlockPlugin } = require('mineflayer-collectblock') const bot = mineflayer.createBot({ host: 'localhost', port: 25565, username: 'worker' }) bot.loadPlugin(collectBlockPlugin) let mcData bot.once('spawn', async () => { mcData = require('minecraft-data')(bot.version) const blocks = bot.findBlocks({ matching: mcData.blocksByName.dirt.id, maxDistance: 64, count: 100 }).map(p => bot.blockAt(p)) // Start a long-running collection task (don't await — cancel it shortly after) bot.collectBlock.collect(blocks).catch(() => {}) // Cancel after 3 seconds setTimeout(async () => { await bot.collectBlock.cancelTask() console.log('Task cancelled successfully.') }, 3000) // Callback style bot.collectBlock.collect(blocks).catch(() => {}) setTimeout(() => { bot.collectBlock.cancelTask(() => { console.log('Task cancelled via callback.') }) }, 1000) }) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.