diff --git a/src/agent/commands/actions.js b/src/agent/commands/actions.js index 0b8943d..a57a464 100644 --- a/src/agent/commands/actions.js +++ b/src/agent/commands/actions.js @@ -182,6 +182,14 @@ export const actionsList = [ await skills.goToBed(agent.bot); }) }, + { + name: '!activate', + description: 'Activate the nearest object of a given type.', + params: {'type': '(string) The type of object to activate.'}, + perform: wrapExecution(async (agent, type) => { + await skills.activateNearestBlock(agent.bot, type); + }) + }, { name: '!stay', description: 'Stay in the current location no matter what. Pauses all modes.', diff --git a/src/agent/library/skills.js b/src/agent/library/skills.js index cf440af..f72b785 100644 --- a/src/agent/library/skills.js +++ b/src/agent/library/skills.js @@ -912,3 +912,27 @@ export async function tillAndSow(bot, x, y, z, seedType=null) { } return true; } + +export async function activateNearestBlock(bot, type) { + /** + * Activate the nearest block of the given type. + * @param {MinecraftBot} bot, reference to the minecraft bot. + * @param {string} type, the type of block to activate. + * @returns {Promise} true if the block was activated, false otherwise. + * @example + * await skills.activateNearestBlock(bot, "lever"); + * **/ + let block = world.getNearestBlock(bot, type, 16); + if (!block) { + log(bot, `Could not find any ${type} to activate.`); + return false; + } + if (bot.entity.position.distanceTo(block.position) > 4.5) { + let pos = block.position; + bot.pathfinder.setMovements(new pf.Movements(bot)); + await bot.pathfinder.goto(new pf.goals.GoalNear(pos.x, pos.y, pos.z, 4)); + } + await bot.activateBlock(block); + log(bot, `Activated ${type} at x:${block.position.x.toFixed(1)}, y:${block.position.y.toFixed(1)}, z:${block.position.z.toFixed(1)}.`); + return true; +} diff --git a/src/agent/modes.js b/src/agent/modes.js index 78b62a6..0298e21 100644 --- a/src/agent/modes.js +++ b/src/agent/modes.js @@ -87,7 +87,7 @@ const modes = [ // TODO: check light level instead of nearby torches, block.light is broken const near_torch = world.getNearestBlock(agent.bot, 'torch', 6); if (!near_torch) { - let torches = agent.bot.inventory.items().filter(item => item.name.includes('torch')); + let torches = agent.bot.inventory.items().filter(item => item.name === 'torch'); if (torches.length > 0) { const torch = torches[0]; const pos = agent.bot.entity.position;