From 3fd2d350013ec77f50d4eee6a80f6f62e3ac9208 Mon Sep 17 00:00:00 2001 From: MaxRobinsonTheGreat Date: Mon, 17 Mar 2025 14:01:14 -0500 Subject: [PATCH] pathfinder stops if dont have right tools --- src/agent/library/skills.js | 36 ++++++++++++++++++++++++++++++------ src/agent/modes.js | 8 +++++++- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/src/agent/library/skills.js b/src/agent/library/skills.js index 86884df..51f9860 100644 --- a/src/agent/library/skills.js +++ b/src/agent/library/skills.js @@ -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; diff --git a/src/agent/modes.js b/src/agent/modes.js index 8747cf3..69b2f06 100644 --- a/src/agent/modes.js +++ b/src/agent/modes.js @@ -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!');