From cb9de90d11304b48ae42287a2f9a3a8b356fc473 Mon Sep 17 00:00:00 2001 From: Kolby Nottingham Date: Tue, 5 Mar 2024 12:50:13 -0800 Subject: [PATCH] smelting command, minor cleanup --- bots/andy/assist.json | 6 ------ src/agent/coder.js | 2 +- src/agent/commands/actions.js | 15 ++++++++++++--- src/agent/history.js | 2 +- src/agent/library/skills.js | 26 +++++++++++++++++++++++--- src/agent/library/world.js | 1 - src/utils/mcdata.js | 14 +++++++++++--- 7 files changed, 48 insertions(+), 18 deletions(-) delete mode 100644 bots/andy/assist.json diff --git a/bots/andy/assist.json b/bots/andy/assist.json deleted file mode 100644 index 680b4be..0000000 --- a/bots/andy/assist.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "name": "andy", - "bio": "You are playing minecraft and assisting other players in tasks.", - "memory": "", - "turns": [] -} \ No newline at end of file diff --git a/src/agent/coder.js b/src/agent/coder.js index 68457ea..90e3c9d 100644 --- a/src/agent/coder.js +++ b/src/agent/coder.js @@ -146,12 +146,12 @@ export class Coder { } async executeResume(func=null, name=null, timeout=10) { - console.log('resuming code...') if (func != null) { this.resume_func = func; this.resume_name = name; } if (this.resume_func != null && this.agent.isIdle()) { + console.log('resuming code...') this.interruptible = true; let res = await this.execute(this.resume_func, timeout); this.interruptible = false; diff --git a/src/agent/commands/actions.js b/src/agent/commands/actions.js index c69baef..0b8943d 100644 --- a/src/agent/commands/actions.js +++ b/src/agent/commands/actions.js @@ -144,9 +144,18 @@ export const actionsList = [ 'num': '(number) The number of times to craft the recipe. This is NOT the number of output items, as it may craft many more items depending on the recipe.' }, perform: wrapExecution(async (agent, recipe_name, num) => { - for (let i=0; i { + await skills.smeltItem(agent.bot, recipe_name, num); }) }, { diff --git a/src/agent/history.js b/src/agent/history.js index fe53e4a..7a6c52e 100644 --- a/src/agent/history.js +++ b/src/agent/history.js @@ -1,4 +1,4 @@ -import { writeFileSync, readFileSync, mkdirSync } from 'fs'; +import { writeFileSync, readFileSync } from 'fs'; export class History { diff --git a/src/agent/library/skills.js b/src/agent/library/skills.js index 1befd7d..2c4be54 100644 --- a/src/agent/library/skills.js +++ b/src/agent/library/skills.js @@ -40,7 +40,7 @@ async function equipHighestAttack(bot) { } -export async function craftRecipe(bot, itemName) { +export async function craftRecipe(bot, itemName, num=1) { /** * Attempt to craft the given item name from a recipe. May craft many items. * @param {MinecraftBot} bot, reference to the minecraft bot. @@ -90,7 +90,7 @@ export async function craftRecipe(bot, itemName) { const recipe = recipes[0]; console.log('crafting...'); - await bot.craft(recipe, 1, craftingTable); + await bot.craft(recipe, num, craftingTable); log(bot, `Successfully crafted ${itemName}, you now have ${world.getInventoryCounts(bot)[itemName]} ${itemName}.`); if (placedTable) { await collectBlock(bot, 'crafting_table', 1); @@ -116,10 +116,21 @@ export async function smeltItem(bot, itemName, num=1) { return false; } // TODO: allow cobblestone, sand, clay, etc. + let placedFurnace = false; let furnaceBlock = undefined; furnaceBlock = world.getNearestBlock(bot, 'furnace', 6); if (!furnaceBlock){ - log(bot, `There is no furnace nearby.`) + // Try to place furnace + let hasFurnace = world.getInventoryCounts(bot)['furnace'] > 0; + if (hasFurnace) { + let pos = world.getNearestFreeSpace(bot, 1, 6); + await placeBlock(bot, 'furnace', pos.x, pos.y, pos.z); + furnaceBlock = world.getNearestBlock(bot, 'furnace', 6); + placedFurnace = true; + } + } + if (!furnaceBlock){ + log(bot, `There is no furnace nearby and you have no furnace.`) return false; } await bot.lookAt(furnaceBlock.position); @@ -132,12 +143,16 @@ export async function smeltItem(bot, itemName, num=1) { // TODO: check if furnace is currently burning fuel. furnace.fuel is always null, I think there is a bug. // This only checks if the furnace has an input item, but it may not be smelting it and should be cleared. log(bot, `The furnace is currently smelting ${mc.getItemName(input_item.type)}.`); + if (placedFurnace) + await collectBlock(bot, 'furnace', 1); return false; } // check if the bot has enough items to smelt let inv_counts = world.getInventoryCounts(bot); if (!inv_counts[itemName] || inv_counts[itemName] < num) { log(bot, `You do not have enough ${itemName} to smelt.`); + if (placedFurnace) + await collectBlock(bot, 'furnace', 1); return false; } @@ -147,6 +162,8 @@ export async function smeltItem(bot, itemName, num=1) { let put_fuel = Math.ceil(num / 8); if (!fuel || fuel.count < put_fuel) { log(bot, `You do not have enough coal or charcoal to smelt ${num} ${itemName}, you need ${put_fuel} coal or charcoal`); + if (placedFurnace) + await collectBlock(bot, 'furnace', 1); return false; } await furnace.putFuel(fuel.type, null, put_fuel); @@ -180,6 +197,9 @@ export async function smeltItem(bot, itemName, num=1) { } } + if (placedFurnace) { + await collectBlock(bot, 'furnace', 1); + } if (total === 0) { log(bot, `Failed to smelt ${itemName}.`); return false; diff --git a/src/agent/library/world.js b/src/agent/library/world.js index 50cdfcc..2bb00bb 100644 --- a/src/agent/library/world.js +++ b/src/agent/library/world.js @@ -172,7 +172,6 @@ export function getInventoryCounts(bot) { inventory[item.name] += item.count; } } - console.log(inventory) return inventory; } diff --git a/src/utils/mcdata.js b/src/utils/mcdata.js index 7c8a0a9..0a76316 100644 --- a/src/utils/mcdata.js +++ b/src/utils/mcdata.js @@ -42,12 +42,20 @@ export function isHostile(mob) { return (mob.type === 'mob' || mob.type === 'hostile') && mob.name !== 'iron_golem' && mob.name !== 'snow_golem'; } -export function getItemId(item) { - return mcdata.itemsByName[item].id; +export function getItemId(itemName) { + let item = mcdata.itemsByName[itemName]; + if (item) { + return item.id; + } + return null; } export function getItemName(itemId) { - return mcdata.items[itemId].name; + let item = mcdata.items[itemId] + if (item) { + return item.name; + } + return null; } export function getAllItems(ignore) {