PrismarineJS / mineflayer-collectblock

A simple utility plugin for Mineflayer that add a higher level API for collecting blocks.
MIT License
38 stars 23 forks source link

Fix collect blocks that require tools #132

Closed MaxRobinsonTheGreat closed 9 months ago

MaxRobinsonTheGreat commented 10 months ago

Currently the bot will not collect blocks that require tools (stone, obsidian, etc) even when it has the correct tools. This was because the canHarvest check was being given the equipped item object instead of the id. This has been fixed. I was using this to test:

const mineflayer = require("mineflayer")
const bot = mineflayer.createBot({
  host: 'localhost',
  username: 'Player',
})

// Load collect block
bot.loadPlugin(require('mineflayer-collectblock').plugin)

// Listen for when a player says "collect [something]" in chat
bot.on('chat', async (username, message) => {
    const args = message.split(' ')
    if (args[0] !== 'collect') return

    // Get the correct block type
    const blockType = bot.registry.blocksByName[args[1]]
    if (!blockType) {
      bot.chat("I don't know any blocks with that name.")
      return
    }

    bot.chat('Collecting the nearest ' + blockType.name)

    // Try and find that block type in the world
    const block = bot.findBlock({
      matching: blockType.id,
      maxDistance: 64
    })

    if (!block) {
      bot.chat("I don't see that block nearby.")
      return
    }

    // Collect the block if we found one
    await bot.collectBlock.collect(block);
    bot.chat('Done.')
  })

To test, give the bot a pickaxe and ask it to collect stone. I think this fixes #129

SilkePilon commented 9 months ago

Looks good