PrismarineJS / mineflayer

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

Change the code so that the bot digs several blocks, and exactly how much I tell him. #3458

Open Sweety4ka opened 2 months ago

Sweety4ka commented 2 months ago

I took the code, but it breaks only one block, for example: collect dirt. He will only break 1 block, but I want me to write: collect dirt 64, and he dug 64 dirt. Please help me! Code: let mcData bot.once('spawn', () => { mcData = require('minecraft-data')(bot.version); })

bot.on('chat', async (username, message) => { const args = message.split(' ') if (args[0] !== 'collect') return

let count = 1 if (args.length === 3) count = parseInt(args[1])

let type = args[1] if (args.length === 3) type = args[2]

const blockType = mcData.blocksByName[type] if (!blockType) { return }

const blocks = bot.findBlocks({ matching: blockType.id, maxDistance: 64, count: count })

if (blocks.length === 0) { bot.chat("I don't see that block nearby.") return }

const targets = [] for (let i = 0; i < Math.min(blocks.length, count); i++) { targets.push(bot.blockAt(blocks[i])) }

bot.chat(Found ${targets.length} ${type}(s))

try { await bot.collectBlock.collect(targets) // All blocks have been collected. bot.chat('Done') } catch (err) { // An error occurred, report it. bot.chat(err.message) console.log(err) }

})