fix digdown

This commit is contained in:
MaxRobinsonTheGreat 2025-03-13 15:42:23 -05:00
parent cbe5804f73
commit ef37a400b0
2 changed files with 34 additions and 28 deletions

View file

@ -409,8 +409,8 @@ export const actionsList = [
}, },
{ {
name: '!digDown', name: '!digDown',
description: 'Digs down a specified distance.', description: 'Digs down a specified distance. Will stop if it reaches lava, water, or a fall of >=4 blocks below the bot.',
params: {'distance': { type: 'int', description: 'Distance to dig down'}}, params: {'distance': { type: 'int', description: 'Distance to dig down', domain: [1, Number.MAX_SAFE_INTEGER] }},
perform: runAsAction(async (agent, distance) => { perform: runAsAction(async (agent, distance) => {
await skills.digDown(agent.bot, distance) await skills.digDown(agent.bot, distance)
}) })

View file

@ -1379,51 +1379,57 @@ export async function activateNearestBlock(bot, type) {
export async function digDown(bot, distance = 10) { export async function digDown(bot, distance = 10) {
/** /**
* Digs down a specified distance. * Digs down a specified distance. Will stop if it reaches lava, water, or a fall of >=4 blocks below the bot.
* @param {MinecraftBot} bot, reference to the minecraft bot. * @param {MinecraftBot} bot, reference to the minecraft bot.
* @param {int} distance, distance to dig down. * @param {int} distance, distance to dig down.
* @returns {Promise<boolean>} true if successfully dug down. * @returns {Promise<boolean>} true if successfully dug all the way down.
* @example * @example
* await skills.digDown(bot, 10); * await skills.digDown(bot, 10);
**/ **/
for (let i = 0; i < distance; i++) { let start_block_pos = bot.blockAt(bot.entity.position).position;
const targetBlock = bot.blockAt(bot.entity.position.offset(0, -1, 0)); for (let i = 1; i <= distance; i++) {
const belowBlock = bot.blockAt(bot.entity.position.offset(0, -2, 0)); const targetBlock = bot.blockAt(start_block_pos.offset(0, -i, 0));
let belowBlock = bot.blockAt(start_block_pos.offset(0, -i-1, 0));
// Check for lava, water if (!targetBlock || !belowBlock) {
if (!targetBlock || targetBlock.name === 'lava' || targetBlock.name === 'water' || log(bot, `Dug down ${i-1} blocks, but reached the end of the world.`);
(belowBlock && (belowBlock.name === 'lava' || belowBlock.name === 'water'))) { return true;
console.log(`Dug down ${i} blocks, but reached ${belowBlock ? belowBlock.name : '(lava/water)'}`); }
log(bot, `Dug down ${i} blocks, but reached ${belowBlock ? belowBlock.name : '(lava/water)'}`)
return false; // Check for lava, water
} if (targetBlock.name === 'lava' || targetBlock.name === 'water' ||
belowBlock.name === 'lava' || belowBlock.name === 'water') {
// Check for a fall of more than 5 blocks below the bot log(bot, `Dug down ${i-1} blocks, but reached ${belowBlock ? belowBlock.name : '(lava/water)'}`)
let isSafe = false; return false;
for (let j = 1; j <= 5; j++) { }
const belowBlock = bot.blockAt(bot.entity.position.offset(0, -j-1, 0));
if (!belowBlock || belowBlock.name !== 'air') { const MAX_FALL_BLOCKS = 2;
isSafe = true; let num_fall_blocks = 0;
break; for (let j = 0; j <= MAX_FALL_BLOCKS; j++) {
} if (!belowBlock || (belowBlock.name !== 'air' && belowBlock.name !== 'cave_air')) {
} break;
}
if (!targetBlock || !isSafe) { num_fall_blocks++;
console.log(`Dug down ${i} blocks, but reached fall`); belowBlock = bot.blockAt(belowBlock.position.offset(0, -1, 0));
log(bot, `Dug down ${i} blocks, but reached fall`); }
return false; if (num_fall_blocks > MAX_FALL_BLOCKS) {
} log(bot, `Dug down ${i-1} blocks, but reached a drop below the next block.`);
return false;
if (bot.canDigBlock(targetBlock)) { }
await breakBlockAt(bot, bot.entity.position.x, bot.entity.position.y - 1, bot.entity.position.z);
await bot.waitForTicks(10); // wait for a short period to avoid issues if (targetBlock.name === 'air' || targetBlock.name === 'cave_air') {
await bot.entity.position.offset(0, -1, 0); log(bot, 'Skipping air block');
} else { console.log(targetBlock.position);
console.log('Cannot dig block at position:', bot.entity.position.offset(0, -1, 0)); continue;
log(bot, 'Cannot dig block at position:' + bot.entity.position.offset(0, -1, 0)) }
return false;
} let dug = await breakBlockAt(bot, targetBlock.position.x, targetBlock.position.y, targetBlock.position.z);
} if (!dug) {
log(bot, 'Failed to dig block at position:' + targetBlock.position);
return false;
}
}
log(bot, `Dug down ${distance} blocks.`);
return true; return true;
} }