PrismarineJS / mineflayer

Create Minecraft bots with a powerful, stable, and high level JavaScript API.
https://prismarinejs.github.io/mineflayer/
MIT License
5.15k stars 926 forks source link

How i can catch a Digging Eror? #2678

Closed V4nden closed 2 years ago

V4nden commented 2 years ago

Here's piece of my code `try{ if (this.bot.blockAt(v1.offset(1, 0, 0)).name != "air") { this.bot.dig(this.bot.blockAt(v1.offset(1, 0, 0)), false) await this.bot.waitForTicks(3) } if (this.bot.blockAt(v1.offset(1, 1, 0)).name != "air") { this.bot.dig(this.bot.blockAt(v1.offset(1, 1, 0)), false) await this.bot.waitForTicks(3) } if (this.bot.blockAt(v1.offset(1, 2, 0)).name != "air") { this.bot.dig(this.bot.blockAt(v1.offset(1, 2, 0)), false) await this.bot.waitForTicks(3) } if (this.bot.blockAt(v1.offset(1, 3, 0)).name != "air") { this.bot.dig(this.bot.blockAt(v1.offset(1, 3, 0)), false) await this.bot.waitForTicks(3) } if (this.bot.blockAt(v1.offset(0, 0, 1)).name != "air") { this.bot.dig(this.bot.blockAt(v1.offset(0, 0, 1)), false) await this.bot.waitForTicks(3) } if (this.bot.blockAt(v1.offset(0, 1, 1)).name != "air") { this.bot.dig(this.bot.blockAt(v1.offset(0, 1, 1)), false) await this.bot.waitForTicks(3) } if (this.bot.blockAt(v1.offset(0, 2, 1)).name != "air") { this.bot.dig(this.bot.blockAt(v1.offset(0, 2, 1)), false) await this.bot.waitForTicks(3) } if (this.bot.blockAt(v1.offset(0, 3, 1)).name != "air") { this.bot.dig(this.bot.blockAt(v1.offset(0, 3, 1)), false) await this.bot.waitForTicks(3) }

    if (this.bot.blockAt(v1.offset(-1, 0, 0)).name != "air") {
        this.bot.dig(this.bot.blockAt(v1.offset(-1, 0, 0)), false)
        await this.bot.waitForTicks(3)
    }
    if (this.bot.blockAt(v1.offset(-1, 1, 0)).name != "air") {
        this.bot.dig(this.bot.blockAt(v1.offset(-1, 1, 0)), false)
        await this.bot.waitForTicks(3)
    }
    if (this.bot.blockAt(v1.offset(-1, 2, 0)).name != "air") {
        this.bot.dig(this.bot.blockAt(v1.offset(-1, 2, 0)), false)
        await this.bot.waitForTicks(3)
    }
    if (this.bot.blockAt(v1.offset(-1, 3, 0)).name != "air") {
        this.bot.dig(this.bot.blockAt(v1.offset(-1, 3, 0)), false)
        await this.bot.waitForTicks(3)
    }

    if (this.bot.blockAt(v1.offset(0, 0, -1)).name != "air") {
        this.bot.dig(this.bot.blockAt(v1.offset(0, 0, -1)), false)
        await this.bot.waitForTicks(3)
    }
    if (this.bot.blockAt(v1.offset(0, 1, -1)).name != "air") {
        this.bot.dig(this.bot.blockAt(v1.offset(0, 1, -1)), false)
        await this.bot.waitForTicks(3)
    }
    if (this.bot.blockAt(v1.offset(0, 2, -1)).name != "air") {
        this.bot.dig(this.bot.blockAt(v1.offset(0, 2, -1)), false)
        await this.bot.waitForTicks(3)
    }
    if (this.bot.blockAt(v1.offset(0, 3, -1)).name != "air") {
        this.bot.dig(this.bot.blockAt(v1.offset(0, 3, -1)), false)
        await this.bot.waitForTicks(3)
    }

} catch (e) { console.log("qq") }`

Every time when i cant break block i get this error: diggingTask.cancel(new Error('Digging aborted')) ^

Error: Digging aborted at bot.stopDigging (C:\Users\vande\Desktop\Skyblock autofarm\Bots\node_modules\mineflayer\lib\plugins\digging.js:147:26) at EventEmitter.dig (C:\Users\vande\Desktop\Skyblock autofarm\Bots\node_modules\mineflayer\lib\plugins\digging.js:25:33) at Bot1.go (file:///C:/Users/vande/Desktop/Skyblock%20autofarm/Bots/bot1.mjs:94:34) at processTicksAndRejections (node:internal/process/task_queues:95:5) at runNextTicks (node:internal/process/task_queues:64:3) at process.processTimers (node:internal/timers:504:9)

amoraschi commented 2 years ago

You have to await all those this.bot.dig(...)

These articles should help you with Promise Rejections: https://flaviocopes.com/javascript-promises-rejection/ http://thecodebarbarian.com/unhandled-promise-rejections-in-node.js.html https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch

PS: I think there's probably a way to make that code look cleaner

V4nden commented 2 years ago

You have to await all those this.bot.dig(...)

These articles should help you with Promise Rejections: https://flaviocopes.com/javascript-promises-rejection/ http://thecodebarbarian.com/unhandled-promise-rejections-in-node.js.html https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch

PS: I think there's probably a way to make that code look cleaner

Thank you, i'll try it. I'm start learn js only for mineflayer, not know too much, but i optimized it with array and for cycle

nildotdev commented 2 years ago

Close the issue if resolved.