如何让Mineflayer机器人跟随玩家时冲刺?解决相关问题
Mineflayer机器人问题修复与冲刺功能实现
问题描述
我正在开发一款Mineflayer机器人,设定为跟随玩家并发起攻击,但目前存在两个问题:
- 当与玩家距离过远时,机器人会停止跟随;
- 机器人在搭桥过程中会出现掉落情况。
同时需要实现机器人跟随玩家时保持冲刺状态。
原始代码
const { versions } = require('minecraft-data') const MinecraftData = require('minecraft-data') const mineflayer = require('mineflayer') const { pathfinder, Movements, goals } = require('mineflayer-pathfinder') const GoalFollow = goals.GoalFollow const pvp = require('mineflayer-pvp').plugin const armorM = require('mineflayer-armor-manager') const bot = mineflayer.createBot({ host: 'EventWG.aternos.me', username: 'bot' }) //Follow the player function followPlayer() { bot.on('chat', (namee, msgg)=>{ const player = bot.players[namee] if (msgg == "attack") { bot.chat("ok " + namee) bot.chat("/effect give @p minecraft:resistance 111111 255") if (!player || !player.entity){ bot.chat("I can't see you " + namee) return } const data = require('minecraft-data')(bot.version) const movements = new Movements(bot, data) bot.pathfinder.setMovements(movements) const goal = new GoalFollow(player.entity, 2) bot.pathfinder.setGoal(goal, true) setInterval(() => { const playerFilter = p => p.type === "player" const playerr = bot.nearestEntity(playerFilter) bot.pvp.attack(player.entity) const sword = bot.inventory.items().find(item => item.name.includes('sword')) if(sword) bot.equip(sword, "hand") }, 2000); } }) } bot.on('spawn', lookAtPlayer) bot.on('spawn', followPlayer)
问题修复与功能实现方案
1. 解决远距离停止跟随问题
- 原始代码未加载
pathfinder等核心插件,导致寻路功能失效,需补充插件加载逻辑; - 避免在
chat事件内重复注册定时器,改用全局变量管理状态,防止多次触发逻辑冲突; - 新增玩家实体消失时的清理逻辑,确保机器人能重新响应指令。
2. 修复搭桥掉落问题
自定义Movements参数,优化搭桥行为:
- 调整
maxDropDown允许小幅度掉落,适配搭桥场景; - 开启
allowFreeMotion提升移动灵活性; - 确保
canPlaceBlocks为true,保证机器人能放置方块搭桥。
3. 实现跟随冲刺状态
通过physicTick事件实时检测机器人移动状态,控制冲刺按键:
- 当机器人处于移动状态且存在目标玩家时,持续按下冲刺键;
- 停止移动或目标消失时,关闭冲刺状态。
修改后的完整代码
const mineflayer = require('mineflayer') const { pathfinder, Movements, goals } = require('mineflayer-pathfinder') const GoalFollow = goals.GoalFollow const pvp = require('mineflayer-pvp').plugin const armorManager = require('mineflayer-armor-manager') const bot = mineflayer.createBot({ host: 'EventWG.aternos.me', username: 'bot' }) // 加载核心插件 bot.loadPlugin(pathfinder) bot.loadPlugin(pvp) bot.loadPlugin(armorManager) let targetPlayer = null let attackInterval = null // 自定义移动配置,修复搭桥掉落问题 function createCustomMovements() { const data = require('minecraft-data')(bot.version) const movements = new Movements(bot, data) movements.maxDropDown = 1 // 允许搭桥时小幅度掉落 movements.canPlaceBlocks = true // 允许放置方块搭桥 movements.allowFreeMotion = true // 提升移动灵活性 return movements } // 实时控制冲刺状态 function handleSprint() { const isMoving = bot.pathfinder.isMoving() bot.setControlState('sprint', targetPlayer && isMoving) } // 启动跟随与攻击逻辑 function startFollowAndAttack(player) { targetPlayer = player.entity const movements = createCustomMovements() bot.pathfinder.setMovements(movements) // 设置跟随目标,保持2格距离,持续追踪 const goal = new GoalFollow(targetPlayer, 2) bot.pathfinder.setGoal(goal, true) // 清理旧定时器,避免重复执行 if (attackInterval) clearInterval(attackInterval) // 高频执行攻击与装备逻辑(500ms间隔更流畅) attackInterval = setInterval(() => { if (!targetPlayer) return bot.pvp.attack(targetPlayer) const sword = bot.inventory.items().find(item => item.name.includes('sword')) if (sword) bot.equip(sword, 'hand') }, 500) // 绑定冲刺控制事件 bot.on('physicTick', handleSprint) } // 聊天指令触发 bot.on('chat', (name, msg) => { const player = bot.players[name] if (msg === 'attack') { bot.chat(`ok ${name}`) bot.chat('/effect give @p minecraft:resistance 111111 255') if (!player || !player.entity) { bot.chat(`我看不到你 ${name}`) return } startFollowAndAttack(player) } }) bot.on('spawn', () => { bot.chat('已上线,输入attack让我跟随攻击你') }) // 目标玩家消失时清理逻辑 bot.on('entityRemoved', (entity) => { if (entity === targetPlayer) { targetPlayer = null bot.pathfinder.stop() if (attackInterval) { clearInterval(attackInterval) attackInterval = null } bot.setControlState('sprint', false) bot.removeListener('physicTick', handleSprint) } })
内容的提问来源于stack exchange,提问作者Ahmed WG
相关产品推荐
相关产品推荐

