PrismarineJS / mineflayer

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

Error: Event windowOpen did not fire within timeout of 20000ms #3360

Closed chiteroman closed 5 months ago

chiteroman commented 5 months ago

Versions

Detailed description of a problem

After a real player disconnect from the server the bot can't open chests.

What did you try yet?

Downgrading mineflayer version, chaning Minecraft server version

Did you try any method from the API? No Did you try any example? Any error from those? No

Your current code

const mineflayer = require('mineflayer')
const autoeat = require('mineflayer-auto-eat').plugin
const { Telegraf } = require('telegraf')
const telegram = new Telegraf('*******')

async function send(msg) {
  await telegram.telegram.sendMessage('*******', msg)
}

const bot = mineflayer.createBot({
  host: '*******',
  username: '*******',
  auth: 'offline',
  version: '1.19.4'
})

async function collect() {
  try {

    await bot.waitForChunksToLoad()

    await send('Empezando a recolectar y vender...')

    let blocks = bot.findBlocks({
      matching: block => {
        return block.name === 'chest'
      },
      useExtraInfo: block => {
        return block.name === 'chest'
      },
      count: 8
    })

    if (blocks.length < 1) {
      send('No se ha encontrado ningún cofre :(')
      return
    }

    await send(String('Se han encontrado ' + blocks.length + ' cofres'))

    let items = 0;

    for (let i = 0; i < blocks.length; i++) {

      let vec = blocks[i]

      let block = bot.blockAt(vec)

      if (block) {
        let chest = await bot.openChest(block)

        let slots = chest.slots

        for (let i = 0; i < slots.length; i++) {
          let slot = slots[i]
          if (slot) {
            items += slot.count
            await bot.putAway(i)
            await new Promise(resolve => setTimeout(resolve, 200))
          }
        }

        chest.close()

        bot.chat('/sell')
        bot.chat('/is deposit all')
      }
    }

    send('Se han recolectado ' + items + ' objetos')

  } catch (e) {
    console.log(e)
    send(e)
  }
}

bot.loadPlugin(autoeat)

bot.autoEat.options.priority = 'foodPoints'
bot.autoEat.options.startAt = 19
bot.autoEat.options.bannedFood.push('golden_apple')

telegram.on('text', async (ctx) => {
  if (ctx.update.message.chat.id.toString() === '******') {
    const msg = String(ctx.update.message.text)

    if (msg === 'RECOLECTAR') {
      collect()
      return
    }

    bot.chat(msg)
  }
})

telegram.launch()

bot.on('kicked', console.log)

bot.on('error', (err) => {
  send(err)
  console.log(err)
})

bot.on('end', () => {
  send('Bot desconectado :(')
})

bot.on('login', () => {
  bot.chat('/login *******')
})

bot.on('autoeat_started', () => {
  console.log('Auto Eat ha empezado')
})

bot.on('autoeat_stopped', () => {
  console.log('Auto Eat paro')
})

bot.once('spawn', async () => {

  await bot.waitForChunksToLoad()

  bot.once('windowOpen', async function open() {
    await bot.clickWindow(12, 0, 0)
    await send('Bot conectado')
    bot.removeListener('windowOpen', open)
  })

  bot.setQuickBarSlot(0)
  bot.activateItem()

  await bot.waitForChunksToLoad()

  setInterval(collect, 5 * 60 * 1000);
})

Expected behavior

Open chest, recollect items and sell them without the presence of real player

chiteroman commented 5 months ago

Fixed adding 1 second delay between opening chests.

await new Promise(resolve => setTimeout(resolve, 1000))