node.jsminecraftmineflayer

Mineflayer bot crashes before starting digging


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()
            })
        })
    }
}

when i try to run the "Blocchi" function by giving a block and a counter, the bot crashes before starting digging

this is the error

1

by giving a block and a counter to the bot, it should find all the blocks in the maxDistance range and dig all of them. This didn't work as the bot crashes before digging. I tried to reduce the maxdistance from 10 to 2, but it didn't work. I also tried to see why it cannot dig the block and the console responded with "unkown reasons".


Solution

  • I had this exact same problem with my script so I know how it feels. So hopefully this helps :)

    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()
                })
            })
        }
    }
    

    .

    Also you may notice some of your code is commented out and this is because all I tested was the Blocchi function.

    Hope this helps! :)

    EDIT

    Hi! Sorry I just tested the code again but with multiple chests and it didn't work so I fixed it and this is the new code:

    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], () => setTimeout(Blocchi, 20))
        }
    
        // 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++) {
            bot.waitForChunksToLoad()
            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, 3))
            await new Promise(resolve => {
                bot.on('goal_reached', async () => {
                    bot.lookAt(block.position)//.offset(0.5, 0.5, 0.5))
                    bot.waitForTicks(20)
                    let promise = bot.dig(block)
    
                    await promise
                    resolve()
                })
            })
        }
        bot.pathfinder.setGoal(null)
    }
    

    The way I fixed your code btw was by:

    1. Waiting for the new chunks to load before mining a new chest
    2. Setting the pathfinding goal to 'null' before setting a new goal
    3. Removing some of your unnecessary 'await's
    4. Adding a timeout to the Blocchi function so that the bot/world has time to reload (this shouldn't effect how quickly you can type the command in chat by what my testing shows me)

    So I hope this helps and it was fun to try and fix your code as it improved some bugs in my other scripts.

    Thanks and bye!