无法关注用户(Mineflayer - Node.js)

问题描述 投票:0回答:1

我试图跟踪一个使用 Node.js 中的 mineflayer 库的玩家,但结果发现该库本身的源代码中存在错误。

我尝试了以下代码:

const mineflayer = require('mineflayer');
const { pathfinder, Movements, goals } = require('mineflayer-pathfinder')
const GoalFollow = goals.GoalFollow

const bot = mineflayer.createBot({
  host: 'PRIVATE',
  username: 'TRASHCAN'
})

bot.loadPlugin(pathfinder)

function followPlayer() {
  const player = p => p.type === "player";
  const mcData = require('minecraft-data')(bot.version);
  const movements = new Movements(mcData);
  
  bot.pathfinder.setMovements(movements);
  const goal = new GoalFollow(player.entity, 3)
  bot.pathfinder.setGoal(goal, true);

}
const prefix = ",s"
const password = 'REMOVED'
const login = `/login ${password}`

const { mineflayer: mineflayerViewer } = require('prismarine-viewer')
bot.once('spawn', () => {
  mineflayerViewer(bot, { port: 3007, firstPerson: true }) // port is the minecraft server port, if first person is false, you get a bird's-eye view
  bot.chat(login)
  bot.on('whisper', (username, message) => {
    if (message.startsWith(prefix) && username === 'Hitrocol') {
        bot.chat(message.split(',TRASHCAN_')[1]);
    }
    if (message.content === ',TRASHCAN_LOGIN' && username === 'Hitrocol') {
        bot.chat(login)
    }
  })
})

function lookAtNearestPlayer() {
    const playerFilter = (entity) => entity.type === 'player'
    const playerEntity = bot.nearestEntity(playerFilter);

    if (!playerEntity) return;
    const pos= playerEntity.position;
    bot.lookAt(pos);
}

bot.on('physicsTick', lookAtNearestPlayer);
bot.on('physicsTick', followPlayer);

我希望它会跟随玩家,但结果却出现了一个错误,看起来像是来自mineflayer探路者源代码的错误(mineflayer和mineflayer探路者都是最新的):

/Users/aarav/node_modules/mineflayer-pathfinder/lib/movements.js:42
    this.blocksCantBreak.add(registry.blocksByName.chest.id)
                                      ^

TypeError: Cannot read properties of undefined (reading 'blocksByName')
    at new Movements (/Users/aarav/node_modules/mineflayer-pathfinder/lib/movements.js:42:39)
    at EventEmitter.followPlayer (/Users/aarav/Downloads/codingshit/untitled folder/index.js:15:21)
    at EventEmitter.emit (node:events:529:35)
    at tickPhysics (/Users/aarav/node_modules/mineflayer/lib/plugins/physics.js:81:11)
    at Timeout.doPhysics [as _onTimeout] (/Users/aarav/node_modules/mineflayer/lib/plugins/physics.js:70:7)
    at listOnTimeout (node:internal/timers:569:17)
    at process.processTimers (node:internal/timers:512:7)

Node.js v18.18.0
javascript node.js minecraft mineflayer
1个回答
0
投票

我知道我迟到了。

显然,您必须等待机器人生成才能设置动作起作用,因为在此之前,

bot.registry
尚未初始化。

bot.once(`spawn`, () => {
    //setting up some example movement values
    const movements = new Pathfinder.Movements(bot)
    movements.allowFreeMotion = true;
    movements.allowSprinting = false;
    
    bot.pathfinder.setMovements(movements)
})
© www.soinside.com 2019 - 2024. All rights reserved.