PrismarineJS / mineflayer

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

TypeError: Cannot set properties of undefined (setting 'heldItem') #3040

Closed Fadnix closed 1 year ago

Fadnix commented 1 year ago

Versions

node_modules\mineflayer\lib\plugins\inventory.js:366 bot.entity.heldItem = bot.heldItem ^

TypeError: Cannot set properties of undefined (setting 'heldItem') at EventEmitter.updateHeldItem (node_modules\mineflayer\lib\plugins\inventory.js:366:25) at EventEmitter.setQuickBarSlot (node_modules\mineflayer\lib\plugins\simple_inventory.js:59:9) at listOnTimeout (node:internal/timers:569:17) at process.processTimers (node:internal/timers:512:7)

Your current code

function daddy() {
  setTimeout(() => {
    const playerHead = bot.inventory.items().find(item => item.name === 'player_head');
    if (playerHead) {
      bot.setQuickBarSlot(4);
      console.log('%cdaddy occurred (4)', 'color: red');
    } else {
      bot.setQuickBarSlot(0);
      console.log('%cdaddy occurred (0)', 'color: red');
    }
    setTimeout(() => {
      bot.activateItem();
    }, 6000);
  }, 12000);
}
setInterval(daddy, 18000);
daddy();
/*
*/

Expected behavior

It is supposed to set quick bar slot to 4 if the bot has a player_head in his inventory, else it will set it to 0

Additional context

usually the code works but its giving that error randomly like every 15-20 minutes

zefir-git commented 1 year ago

It looks like bot.entity is undefined. Is your bot logged into a server?

extremeheat commented 1 year ago

Reopen if you have more information