pathfinder stops if dont have right tools

This commit is contained in:
MaxRobinsonTheGreat 2025-03-17 14:01:14 -05:00
parent 54c57acc93
commit 3fd2d35001
2 changed files with 37 additions and 7 deletions

View file

@ -79,7 +79,7 @@ export async function craftRecipe(bot, itemName, num=1) {
}
}
if (!recipes || recipes.length === 0) {
log(bot, `You do not have the resources to craft a ${itemName}. It requires: ${Object.entries(mc.getItemCraftingRecipes(itemName)[0][0]).map(([key, value]) => `${key}: ${value}`).join(', ')}.`);
log(bot, `You do not have the resources to craft a ${itemName}.`);
if (placedTable) {
await collectBlock(bot, 'crafting_table', 1);
}
@ -1002,10 +1002,34 @@ export async function goToPosition(bot, x, y, z, min_distance=2) {
log(bot, `Teleported to ${x}, ${y}, ${z}.`);
return true;
}
bot.pathfinder.setMovements(new pf.Movements(bot));
await bot.pathfinder.goto(new pf.goals.GoalNear(x, y, z, min_distance));
log(bot, `You have reached at ${x}, ${y}, ${z}.`);
return true;
const movements = new pf.Movements(bot);
bot.pathfinder.setMovements(movements);
const checkProgress = () => {
if (bot.targetDigBlock) {
const targetBlock = bot.targetDigBlock;
const itemId = bot.heldItem ? bot.heldItem.type : null;
if (!targetBlock.canHarvest(itemId)) {
log(bot, `Pathfinding stopped: Cannot break ${targetBlock.name} with current tools.`);
bot.pathfinder.stop();
bot.stopDigging();
}
}
};
const progressInterval = setInterval(checkProgress, 1000);
try {
await bot.pathfinder.goto(new pf.goals.GoalNear(x, y, z, min_distance));
log(bot, `You have reached at ${x}, ${y}, ${z}.`);
return true;
} catch (err) {
log(bot, `Pathfinding stopped: ${err.message}.`);
return false;
} finally {
clearInterval(progressInterval);
}
}
export async function goToNearestBlock(bot, blockType, min_distance=2, range=64) {
@ -1029,7 +1053,7 @@ export async function goToNearestBlock(bot, blockType, min_distance=2, range=64
log(bot, `Could not find any ${blockType} in ${range} blocks.`);
return false;
}
log(bot, `Found ${blockType} at ${block.position}.`);
log(bot, `Found ${blockType} at ${block.position}. Navigating...`);
await goToPosition(bot, block.position.x, block.position.y, block.position.z, min_distance);
return true;

View file

@ -83,6 +83,7 @@ const modes_list = [
stuck_time: 0,
last_time: Date.now(),
max_stuck_time: 20,
prev_dig_block: null,
update: async function (agent) {
if (agent.isIdle()) {
this.prev_location = null;
@ -90,12 +91,17 @@ const modes_list = [
return; // don't get stuck when idle
}
const bot = agent.bot;
if (this.prev_location && this.prev_location.distanceTo(bot.entity.position) < this.distance) {
const cur_dig_block = bot.targetDigBlock;
if (cur_dig_block && !this.prev_dig_block) {
this.prev_dig_block = cur_dig_block;
}
if (this.prev_location && this.prev_location.distanceTo(bot.entity.position) < this.distance && cur_dig_block == this.prev_dig_block) {
this.stuck_time += (Date.now() - this.last_time) / 1000;
}
else {
this.prev_location = bot.entity.position.clone();
this.stuck_time = 0;
this.prev_dig_block = null;
}
if (this.stuck_time > this.max_stuck_time) {
say(agent, 'I\'m stuck!');