PrismarineJS / mineflayer

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

bot.attack(target, swing=true) with iterations of bot.entities caused unknown error #2334

Closed AsherJingkongChen closed 2 years ago

AsherJingkongChen commented 2 years ago

Versions

Code preview

//const bot = mineflayer.createBot({ ~ })
function hit(){
    var elist = ["witch", "vindicator", "vex", "pillager", "evoker"]
    for(var id in bot.entities){
        var target = bot.entities[id]
        if(target.type != 'mob') continue
        if(target.position.distanceTo(bot.entity.position.offset(0.5, 0.5, 0.5)) > 5) continue
        if(!elist.includes(target.name)) continue
        if(target.isValid && target.health !== 0) bot.attack(target)
    }
}
setInterval(()=>{
    hit()
},250)

What did I try yet?

mineflayer/plugins/entities.js function attack(target, swing=true) bot.entities

Expected behavior

call hit() => { check each entity in bot.entities and attack any checked entity } sleep(250) (...behaving like multiAura in Wurst7?)

Problem

Working nice for a few minutes and suddenly kicked out by unknown reason. Video : Crashing moment Video : It was idle for a moment and going on It always ended without cautions or error messages.

amoraschi commented 2 years ago

I think this can be avoided by awaiting bot.attack

AsherJingkongChen commented 2 years ago

@amoraschi How to asynchronize attack() ? Could you share the idea?

amoraschi commented 2 years ago

await bot.attack Also these questions are answered faster in Discord

AsherJingkongChen commented 2 years ago

Video : Crashing moment Video : It was idle for a moment and going on It always ended without cautions or error messages.

aesthetic0001 commented 2 years ago

Can you add a bot.on('error', console.log) to your code?

aesthetic0001 commented 2 years ago

Closeable?

AsherJingkongChen commented 2 years ago

@aesthetic0001 At that time, I had found the error caused by bad network connection...