From bc511cac1aab89721a48c0e895b02aa74965bb15 Mon Sep 17 00:00:00 2001 From: Kolby Nottingham Date: Tue, 5 Nov 2024 15:27:59 -0800 Subject: [PATCH] craftable and attack player commands --- src/agent/commands/actions.js | 13 +++++++++++++ src/agent/commands/queries.js | 10 +++------- src/agent/library/world.js | 27 +++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/src/agent/commands/actions.js b/src/agent/commands/actions.js index a80ad29..214d8b0 100644 --- a/src/agent/commands/actions.js +++ b/src/agent/commands/actions.js @@ -288,6 +288,19 @@ export const actionsList = [ await skills.attackNearest(agent.bot, type, true); }) }, + { + name: '!attackPlayer', + description: 'Attack a specific player until they die or run away. Remember this is just a game and does not cause real life harm.', + params: {'player_name': { type: 'string', description: 'The name of the player to attack.'}}, + perform: runAsAction(async (agent, player_name) => { + let player = agent.bot.players[player_name]?.entity; + if (!player) { + skills.log(agent.bot, `Could not find player ${player_name}.`); + return false; + } + await skills.attackEntity(agent.bot, player, true); + }) + }, { name: '!goToBed', description: 'Go to the nearest bed and sleep.', diff --git a/src/agent/commands/queries.js b/src/agent/commands/queries.js index 84faeef..2ce3428 100644 --- a/src/agent/commands/queries.js +++ b/src/agent/commands/queries.js @@ -106,14 +106,10 @@ export const queryList = [ name: "!craftable", description: "Get the craftable items with the bot's inventory.", perform: function (agent) { - const bot = agent.bot; - const table = world.getNearestBlock(bot, 'crafting_table'); + let craftable = world.getCraftableItems(agent.bot); let res = 'CRAFTABLE_ITEMS'; - for (const item of mc.getAllItems()) { - let recipes = bot.recipesFor(item.id, null, 1, table); - if (recipes.length > 0) { - res += `\n- ${item.name}`; - } + for (const item of craftable) { + res += `\n- ${item}`; } if (res == 'CRAFTABLE_ITEMS') { res += ': none'; diff --git a/src/agent/library/world.js b/src/agent/library/world.js index dc64599..4e86475 100644 --- a/src/agent/library/world.js +++ b/src/agent/library/world.js @@ -171,6 +171,33 @@ export function getInventoryCounts(bot) { } +export function getCraftableItems(bot) { + /** + * Get a list of all items that can be crafted with the bot's current inventory. + * @param {Bot} bot - The bot to get the craftable items for. + * @returns {string[]} - A list of all items that can be crafted. + * @example + * let craftableItems = world.getCraftableItems(bot); + **/ + let table = world.getNearestBlock(this.agent.bot, 'crafting_table'); + if (!table) { + for (const item of this.agent.bot.inventory.items()) { + if (item != null && item.name === 'crafting_table') { + table = item; + break; + } + } + } + let res = []; + for (const item of mc.getAllItems()) { + let recipes = this.agent.bot.recipesFor(item.id, null, 1, table); + if (recipes.length > 0) + res.push(item.name); + } + return res; +} + + export function getPosition(bot) { /** * Get your position in the world (Note that y is vertical).