The goal of this lesson is to create a Minecraft bot that connects to a server and follows a player around.
Create a new directory and initialize a Node.js project:
mkdir bot-follow-a-player
cd bot-follow-a-player
npm init -y
Install the dependencies we'll need:
npm install mineflayer mineflayer-pathfinder yargs
npm install -D typescript @types/node @types/yargs tsx
Create src/index.ts. We'll start with imports and CLI arguments so users can configure the bot without editing code:
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import mineflayer from "mineflayer";
import mineflayerPathfinder from "mineflayer-pathfinder";
const { pathfinder, Movements, goals } = mineflayerPathfinder;
const { GoalNear } = goals;
const argv = yargs(hideBin(process.argv))
.option("player", {
alias: "p",
description: "Name of the player to follow",
type: "string",
demandOption: true,
})
.option("interval", {
alias: "i",
description: "Interval in ms to check for the player",
type: "number",
default: 500,
})
.option("range", {
alias: "r",
description: "Range in blocks to check for the player",
type: "number",
default: 3,
})
.option("bot", {
alias: "b",
description: "Name for the bot",
type: "string",
default: "bot",
})
.help()
.parseSync();
const playerName = argv.player;
const interval = argv.interval;
const range = argv.range;
const botName = argv.bot;
The key parameter is --player — this is the Minecraft username the bot will follow. The others have sensible defaults: the bot checks every 500ms and tries to stay within 3 blocks.
Now we create a bot that connects to a local Minecraft server:
const bot = mineflayer.createBot({ host: "localhost", username: botName });
That's it — one line to create a bot and connect. By default it connects to localhost:25565. The username is what other players will see in-game.
The imports we set up earlier give us three things from pathfinder:
Once the bot spawns into the world, we load the pathfinder plugin and set up a loop that continuously tracks the target player:
bot.on("spawn", () => {
bot.loadPlugin(pathfinder);
bot.pathfinder.setMovements(new Movements(bot));
bot.chat(`Hey, ${playerName}, I am here!`);
setInterval(() => {
bot.nearestEntity((e) => {
if (e.type !== "player" && e.username !== playerName) {
return false;
}
const { x, y, z } = e.position;
bot.pathfinder.setGoal(new GoalNear(x, y, z, range));
return true;
});
}, interval);
});
Here's what's happening:
bot.on('spawn') — this fires once the bot has fully joined the server and is in the world.bot.loadPlugin(pathfinder) — activates the pathfinder plugin so the bot can navigate.new Movements(bot) — tells pathfinder what the bot is capable of (it reads the world data to know which blocks are walkable, which are dangerous, etc.).setInterval — every interval ms (default 500), we look for the player.bot.nearestEntity() — scans nearby entities and calls our filter function. When we find the target player, we grab their position and set it as the pathfinder goal.GoalNear(x, y, z, range) — "navigate to within range blocks of this position." The bot will walk, jump, and swim to get there.The bot continuously updates the goal because the player is moving — so every 500ms it recalculates where to go.
Make sure your Minecraft server is running locally, then:
cd examples/bot-follow-a-player
npm install
npx tsx src/index.ts -- --player <your-minecraft-username>
Join your local server and watch the bot follow you around!