Mineflayer 机器人在开始挖掘前崩溃

问题描述 投票:0回答:1
const mineflayer = require('mineflayer')
const pathfinder = require('mineflayer-pathfinder').pathfinder
const Movements = require('mineflayer-pathfinder').Movements
const { GoalNear } = require('mineflayer-pathfinder').goals
const inventoryViewer = require('mineflayer-web-inventory')

let options = {
    host: 'localhost',
    port: 50599,
    username: 'Specchiopaura',
    version: '1.19.2'
}

const bot = mineflayer.createBot(options)

inventoryViewer(bot)



bot.once('spawn', () => {
    console.log('-CONNESSO-')
    bot.loadPlugin(pathfinder)
})


const mcData = require('minecraft-data')(bot.version)
const defaultMove = new Movements(bot, mcData)




bot.on('chat', (username, message) =>{
    args = message.split(' ')


    if(args[0] == 'quitta'){bot.quit()}


    else if(args[0] == 'trova')
    {
        Blocchi(args[1], args[2])
    }

    else if(args[0] == 'aprichest')
    {
        let chest = bot.findBlock({matching: mcData.blocksByName.chest.id, maxDistance: 64})
        const goal = chest.position
        bot.pathfinder.setMovements(defaultMove)
        bot.pathfinder.setGoal(new GoalNear(chest.position.x, chest.position.y, chest.position.z, 1))
        bot.once('goal_reached', () => {
            bot.lookAt(chest.position.offset(0.5, 0.5, 0.5));
            bot.openChest(chest)
        })
    }
})

async function Blocchi(bloccoDaTrovare, contatore) {
    for (let i = 0; i < contatore; i++) {
        let block = bot.findBlock({
            matching: mcData.blocksByName[bloccoDaTrovare].id,
            maxDistance: 10
        })
        console.log(block.position)
        if (!block) { return }
        await bot.pathfinder.setMovements(defaultMove)
        await bot.pathfinder.setGoal(new GoalNear(block.position.x, block.position.y, block.position.z, 1))
        await new Promise(resolve => {
            bot.on('goal_reached', () => {
                bot.lookAt(block.position.offset(0.5, 0.5, 0.5))
                bot.waitForTicks(10)
                bot.dig(block)
                resolve()
            })
        })
    }
}

当我尝试通过提供一个方块和一个计数器来运行“Blocchi”功能时,机器人在开始挖掘之前就崩溃了

这是错误

1

通过给机器人一个方块和一个计数器,它应该找到 maxDistance 范围内的所有方块并挖掘所有这些方块。这没有用,因为机器人在挖掘前崩溃了。我试图将 maxdistance 从 10 减少到 2,但没有用。我也试着看看为什么它不能挖掘块并且控制台以“未知原因”响应。

node.js minecraft mineflayer
1个回答
0
投票

我的脚本也有同样的问题,所以我知道它是什么感觉。所以希望这会有所帮助:)

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

//Create Bot
const bot = mineflayer.createBot({
    host: 'localhost',
    port: 5555,
    username: 'StackOverFlow',
    version: '1.19.2'
})

//Load pathfinding plugin
bot.loadPlugin(pathfinder)


bot.on('chat', (username, message) =>{
    args = message.split(' ')


    if(args[0] == 'quitta'){bot.quit()}


    else if(args[0] == 'trova')
    {
        Blocchi(args[1], args[2])
    }

    // else if(args[0] == 'aprichest')
    // {
    //     let chest = bot.findBlock({matching:             
    //     mcData.blocksByName.chest.id, maxDistance: 64})
    //     const goal = chest.position
    //     bot.pathfinder.setMovements(defaultMove)
    //     bot.pathfinder.setGoal(new GoalNear(chest.position.x,                 
    //     chest.position.y, chest.position.z, 1))
    //     bot.once('goal_reached', () => {
    //         bot.lookAt(chest.position.offset(0.5, 0.5, 0.5));
    //         bot.openChest(chest)
    //     })
    // }
})



async function Blocchi(blockToFind, counter) {
    const mcData = require('minecraft-data')(bot.version)
    const defaultMove = new Movements(bot, mcData)

    for (let i = 0; i < counter; i++) {
        let block = bot.findBlock({
            matching: mcData.blocksByName[blockToFind].id,
            maxDistance: 10
        })

        console.log(block.position)

        if (!block) { return }
        
        bot.pathfinder.setMovements(defaultMove)
        bot.pathfinder.setGoal(new GoalNear(block.position.x,             
        block.position.y, block.position.z, 1))
        await new Promise(resolve => {
            bot.on('goal_reached', () => {
                bot.lookAt(block.position.offset(0.5, 0.5, 0.5))
                bot.waitForTicks(10)
                bot.dig(block)
                resolve()
            })
        })
    }
}

您可能还会注意到您的一些代码被注释掉了,这是因为我测试的只是 Blocchi 函数。

希望这有帮助! :)

© www.soinside.com 2019 - 2024. All rights reserved.