From bc511cac1aab89721a48c0e895b02aa74965bb15 Mon Sep 17 00:00:00 2001 From: Kolby Nottingham Date: Tue, 5 Nov 2024 15:27:59 -0800 Subject: [PATCH 1/4] 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). From 867f253b14760841da9590c27d92290539e26998 Mon Sep 17 00:00:00 2001 From: Kolby Nottingham Date: Tue, 5 Nov 2024 15:30:12 -0800 Subject: [PATCH 2/4] craftable library fix --- src/agent/library/world.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/agent/library/world.js b/src/agent/library/world.js index 4e86475..01d54c3 100644 --- a/src/agent/library/world.js +++ b/src/agent/library/world.js @@ -179,9 +179,9 @@ export function getCraftableItems(bot) { * @example * let craftableItems = world.getCraftableItems(bot); **/ - let table = world.getNearestBlock(this.agent.bot, 'crafting_table'); + let table = getNearestBlock(bot, 'crafting_table'); if (!table) { - for (const item of this.agent.bot.inventory.items()) { + for (const item of bot.inventory.items()) { if (item != null && item.name === 'crafting_table') { table = item; break; @@ -190,7 +190,7 @@ export function getCraftableItems(bot) { } let res = []; for (const item of mc.getAllItems()) { - let recipes = this.agent.bot.recipesFor(item.id, null, 1, table); + let recipes = bot.recipesFor(item.id, null, 1, table); if (recipes.length > 0) res.push(item.name); } From f78047324996e48cbcedf057be234a437eead909 Mon Sep 17 00:00:00 2001 From: Kolby Nottingham Date: Tue, 5 Nov 2024 15:44:40 -0800 Subject: [PATCH 3/4] fix translate error --- src/agent/agent.js | 11 +++++++---- src/agent/commands/actions.js | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/agent/agent.js b/src/agent/agent.js index 5be2385..9938e08 100644 --- a/src/agent/agent.js +++ b/src/agent/agent.js @@ -56,10 +56,6 @@ export class Agent { if (ignore_messages.some((m) => message.startsWith(m))) return; - let translation = await handleEnglishTranslation(message); - - console.log('received message from', username, ':', translation); - this.shut_up = false; this.handleMessage(username, translation); @@ -123,6 +119,7 @@ export class Agent { let self_prompt = source === 'system' || source === this.name; + // First check for user commands if (!self_prompt) { const user_command_name = containsCommand(message); if (user_command_name) { @@ -143,6 +140,11 @@ export class Agent { } } + // Now translate the message + message = await handleEnglishTranslation(message); + console.log('received message from', source, ':', message); + + // Do self prompting const checkInterrupt = () => this.self_prompter.shouldInterrupt(self_prompt) || this.shut_up; let behavior_log = this.bot.modes.flushBehaviorLog(); @@ -155,6 +157,7 @@ export class Agent { await this.history.add('system', behavior_log); } + // Handle other user messages await this.history.add(source, message); this.history.save(); diff --git a/src/agent/commands/actions.js b/src/agent/commands/actions.js index 214d8b0..f1d675f 100644 --- a/src/agent/commands/actions.js +++ b/src/agent/commands/actions.js @@ -292,7 +292,7 @@ export const actionsList = [ 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) => { + perform: wrapExecution(async (agent, player_name) => { let player = agent.bot.players[player_name]?.entity; if (!player) { skills.log(agent.bot, `Could not find player ${player_name}.`); From 111929cc54fd3100e5142af8eb2d35e1b27e858d Mon Sep 17 00:00:00 2001 From: Kolby Nottingham Date: Tue, 5 Nov 2024 15:48:37 -0800 Subject: [PATCH 4/4] finished merge with main --- src/agent/agent.js | 2 +- src/agent/commands/actions.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/agent/agent.js b/src/agent/agent.js index 8f9014c..87a0d2f 100644 --- a/src/agent/agent.js +++ b/src/agent/agent.js @@ -64,7 +64,7 @@ export class Agent { this.shut_up = false; - this.handleMessage(username, translation); + this.handleMessage(username, message); }); // set the bot to automatically eat food when hungry diff --git a/src/agent/commands/actions.js b/src/agent/commands/actions.js index e1ed67f..8728ad5 100644 --- a/src/agent/commands/actions.js +++ b/src/agent/commands/actions.js @@ -294,7 +294,7 @@ export const actionsList = [ 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: wrapExecution(async (agent, player_name) => { + 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}.`);