Welcome to the Mineflayer crash course! This guide will help you create your own Minecraft bots using the Mineflayer library.
Prerequisites:
- Node.js v18 or newer
- NPM (included with Node.js)
Installation and execution:
npm install
node your_code.js
Build the Docker image:
docker build -t my-bot .
Run the Docker container:
docker run --network="host" my-bot
Mineflayer is a powerful JavaScript library for creating Minecraft bots that interact with the game world. It leverages an event-driven architecture, enabling your bot to respond to in-game events. When you create a bot, it connects to a Minecraft server, listens for events like player movements, chat messages, and block updates, and lets you programmatically respond to these events.
How Mineflayer works:
- Connects to a Minecraft server
- Receives data about the world and entities
- Maintains an internal representation of the game state
- Provides an API to interact with the world
A common challenge is converting between human-readable names and internal Minecraft IDs:
// Initialize Minecraft data with your bot's version
const minecraftData = require('minecraft-data')(bot.version)
// Convert block name to ID
function getBlockId(blockName) {
const block = minecraftData.blocksByName[blockName]
if (!block) {
console.log(`Block "${blockName}" not found!`)
return null
}
return block.id
}
// Convert item name to ID
function getItemId(itemName) {
const item = minecraftData.itemsByName[itemName]
if (!item) {
console.log(`Item "${itemName}" not found!`)
return null
}
return item.id
}
// Example usage
const diamondBlockId = getBlockId('diamond_block')
const diamondItemId = getItemId('diamond')
Important: Blocks and items have different IDs - don't confuse them!
Always use Vec3
for positions to keep your code clean and consistent:
const Vec3 = require('vec3').Vec3
const position = new Vec3(0, 64, 0)
// Offset a position
const blockAbove = position.offset(0, 1, 0)
// Calculate distance
const distance = position.distanceTo(bot.entity.position)
// Vector operations
const direction = position.minus(bot.entity.position).normalize()
Most Mineflayer actions return promises, so ensure proper error handling is in place:
async function safeDigBlock(block) {
try {
await bot.tool.equipForBlock(block)
await bot.dig(block)
return true
} catch (error) {
console.log(`Failed to dig block: ${error.message}`)
return false
}
}
Efficiently parse user commands:
bot.on('chat', (username, message) => {
if (username === bot.username) return
const args = message.split(' ')
const command = args[0]
switch (command) {
case 'come':
// Handle "come" command
followPlayer(username)
break
case 'find':
// Handle "find" command with args[1] as the block name
if (args.length > 1) findBlocks(args[1])
break
// Add more commands...
}
})
Below I describe all the example functions that I have stolen from another course.
Navigates to specific coordinates using pathfinding.
- When
y
is set to-1
, the bot only navigates in the X and Z dimensions. - Returns a
Promise
that resolves when destination is reached.
Makes the bot follow a specific player, maintaining the given distance.
- Uses dynamic pathfinding that adjusts as the player moves.
- Perfect for creating companion bots.
Searches for blocks of a specific type within a radius.
- Returns
Vec3
positions of found blocks. - Useful for resource gathering and navigation.
Navigates to and mines a specific block, attempting to collect dropped items.
- Automatically selects the appropriate tool.
- Handles navigation, block breaking, and item collection.
Locates and collects multiple blocks of the specified type.
- Combines finding, navigating, and mining in one function.
- Great for gathering resources efficiently.
Lists all items in the bot's inventory.
Counts how many of a specific item the bot has.
- Useful for crafting decisions and resource management.
Crafts items automatically, finding recipes and using crafting tables if needed.
- Handles recipe lookup and table navigation.
- Works with both inventory crafting and crafting tables.
Places a block near a specific player.
- Finds a suitable location near the player.
- Navigates and places the block safely.
Inspects a furnace, reporting its contents and status.
- Shows input, fuel, output, and progress.
Automates furnace operation by adding input and fuel, then waiting for output.
- Complete smelting automation.
- Monitors progress and collects the output.
Finds and attacks the nearest entity of the specified type.
- Equips the best available weapon.
- Navigates to within attacking range.
These functions demonstrate core Mineflayer capabilities and can be used as building blocks for more complex behaviors. Use them as inspiration for your own bots!