link-discord / mineflayer-auto-eat

Auto eat plugin for mineflayer
https://www.npmjs.com/package/mineflayer-auto-eat
MIT License
33 stars 4 forks source link

How to make bot auto-eat when the food on off-hand #14

Closed wubushanyan closed 1 year ago

wubushanyan commented 2 years ago

I wana make bot eat whem it get hurt by zombified_piglin, meanwhile the bot is attacking the zombified_piglin, So I think the hold food on off-hand is better than taking food from inventory when bot attacking How to automatically eat when bot attack and not affect attacking?

When the food on hand it can eat but then the bot will attacking with food When the food onoff-hand it cant eat

//auto take some food on off-hand
bot.on('playerCollect', (collector, itemDrop) => {
  if (collector !== bot.entity) return

  setTimeout(() => {
    const cooked_porkchop = bot.inventory.items().find(item => item.name.includes('cooked_porkchop'))
    if (cooked_porkchop) bot.equip(cooked_porkchop, 'off-hand')
  }, 250)
})

bot.once('spawn', () => {
  bot.autoEat.options = {
    priority: 'saturation',
    startAt: 16,
    bannedFood: []
  }
})

//auto-eat when hunger
bot.on("health", () => {
  if (bot.food === 20) bot.autoEat.disable()
  else bot.autoEat.enable() 
})

//get order to attack
var id
bot.on('chat', (username, message) => {
   if (message=== "attack")
   id=setInterval(()=>{hit()},650)
   console.log(message)
    }
 if(message ==="unattack")  clearInterval(id)
})
async function hit(){
    var elist = ["witch", "vindicator", "vex", "pillager", "evoker","zombified_piglin"]
    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)
    }
}
link-discord commented 1 year ago

It doesn't support offhand eating atm but I'm doing a rewrite of the plugin that will have it

link-discord commented 1 year ago

New version has offhand eating now.