PrismarineJS / mineflayer-collectblock

A simple utility plugin for Mineflayer that add a higher level API for collecting blocks.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Bot walks very slow & doesn't mine

nabilhayek opened this issue · comments

commented

The bot is suppose to find a diamond block and mine it. The code was copied and modified a bit from examples/collector.js to ensure that it wasn't only my code that didn't work.

Code:

const mineflayer = require('mineflayer');
const collectBlock = require('mineflayer-collectblock').plugin;

const bot = mineflayer.createBot({
  host: 'minetime.com',
  username: 'farucci',
  password: 'censored',
});

bot.loadPlugin(collectBlock);

let mcData;
bot.once('spawn', () => {
  mcData = require('minecraft-data')(bot.version);

  //Pull up compass menu
  bot.activateItem();
});

bot.on('windowOpen', async (e) => {
  //Select Prison server
  await bot.clickWindow(1, 1, 0, () => {});
});

bot.on('chat', (username, message) => {
  if (message !== 'me] start') return;

  let count = 1;
  let type = 'diamond_block';

  const blockType = mcData.blocksByName[type];
  if (!blockType) {
    console.log(`"I don't know any blocks named ${type}.`);
    return;
  }

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

  if (blocks.length === 0) {
    console.log("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]));
  }

  console.log(`Found ${targets.length} ${type}(s)`);

  bot.collectBlock.collect(targets, (err) => {
    if (err) {
      // An error occurred, report it.
      console.log(err.message);
      console.log(err);
    } else {
      // All blocks have been collected.
      console.log('Done');
    }
  });
});

Video demonstration:

https://youtu.be/3ZULtC2P-Sk

It looks like the bot releases mine button too early

The slow walking and the mining issues are two separate bugs. The slow walking is likely caused by how the pathfinder plugin is set up. Make sure you're using the latest version and not modifying it to cause that effect.

For the mining issue, this is a known bug that is sometimes triggered when the block breaks before the bot finishes mining it, and the bot gets stuck in a loop trying to mine the air block but cannot.

The issue seems to be because the bot's world isn't updated with the current state during collectblock. Try doing a collectblock on a single target, when successful, call your collectBlock again with the next target, poping from the list of targets and carry on recursing through until targets is finished.

Haven't had any more sticking issues after the above. (ensure you check if the target to collect still exists)