2025年8月11日月曜日

Mineflayer で指定の座標にボットを移動する方法

Mineflayer で指定の座標にボットを移動する方法

概要

前回ボットの開発に必要なベースとなるクラスを作成しました
今回は実際に機能を追加していきます
ボットへは基本的にチャット経由で命令するのが一番簡単なのでチャットで特定のコマンドを実行するとボットが指定した座標に移動する機能を追加してみます

環境

  • macOS 15.6
  • Minecraft 1.21.4
  • nodejs 22.15.1
  • mineflayer 4.31.0

コーディング方針

  • チャットメッセージをパースしてどのコマンド化判定するクラスを作成 (lib/command/parse.js)
  • 実際にボットに命令する処理を管理するファイル (lib/command/commands.js)
  • メインファイル (index.js)

という感じで今のうちに管理しやすいようにファイルに分割しておきます

ライブラリインストール

追加で pathfinder というプラグインをインストールします

  • npm install mineflayer-pathfinder

lib/command/parser.js

チャットの内容を受取りスペースで分割します
最初の引数とそれ以降の引数を取得できるようにします

class CommandParser {
  constructor(message) {
    this.message = message.trim();
    this.parts = this.message.split(/\s+/);
  }

  isMoveCommand() {
    return this.parts[0].toLowerCase() === "move";
  }

  getArgs() {
    return this.parts.slice(1);
  }
}

module.exports = CommandParser;

lib/command/commands.js

実際に bot に命令する関数を管理します
pathfinder を使って実際にボットを動かす処理は bot.pathfinder.setGoal(new goals.GoalBlock(x, y, z)); になります

const { goals } = require("mineflayer-pathfinder");

function moveCmd(bot, username, args) {
  if (args.length < 3) {
    bot.chat(`Usage: move <x> <y> <z>`);
    return;
  }

  const [x, y, z] = args.map(Number);
  if ([x, y, z].some(isNaN)) {
    bot.chat("座標は数値で指定してください");
    return;
  }

  bot.chat(`${username} の指示で (${x}, ${y}, ${z}) に移動します`);
  bot.pathfinder.setGoal(new goals.GoalBlock(x, y, z));
}

module.exports = { moveCmd };

index.js

メイン部分です
分割したファイルを読み込んで使う感じになります

const mineflayer = require("mineflayer");
const { pathfinder, Movements } = require("mineflayer-pathfinder");
const CommandParser = require("./lib/command/parser");
const { moveCmd } = require("./lib/command/commands");

class MinecraftBot {
  constructor(host, port, username) {
    this.bot = mineflayer.createBot({
      host,
      port,
      username,
      version: false,
    });

    this.bot.loadPlugin(pathfinder);
    this.registerEvents();
  }

  registerEvents() {
    this.bot.once("spawn", () => this.onSpawn());
    this.bot.on("chat", (username, message) => this.onChat(username, message));
    this.bot.on("kicked", (reason) =>
      console.error("キックされました:", reason),
    );
    this.bot.on("error", (err) => console.error("エラー:", err));
    this.bot.on("end", () => console.log("Bot が切断されました"));
  }

  onSpawn() {
    console.log("Bot がログインしました!");
    this.bot.chat("こんにちは!Botクラスです!");

    const defaultMovements = new Movements(this.bot, this.bot.registry);
    this.bot.pathfinder.setMovements(defaultMovements);
  }

  onChat(username, message) {
    if (username === this.bot.username) return;
    console.log(`[${username}]: ${message}`);

    const parser = new CommandParser(message);

    if (parser.isMoveCommand()) {
      moveCmd(this.bot, username, parser.getArgs());
    }
  }

  sendChat(text) {
    this.bot.chat(text);
  }
}

const myBot = new MinecraftBot("localhost", 25565, "bot");

動作確認

チャットを開いて「move -631 10 534」のように入力してみましょう
スラッシュは不要なので注意してください

最後に

Mineflayer で実際にボットに命令する第一弾としてボットを指定の座標に移動する機能を追加してみました
ワープではなく実際に歩いて移動してくるので Z 座標が高い場合で登ることが不可能な場合には Z 軸まではたどり着けません

参考サイト

0 件のコメント:

コメントを投稿