smelting command, minor cleanup

This commit is contained in:
Kolby Nottingham 2024-03-05 12:50:13 -08:00
parent 5448b018c7
commit cb9de90d11
7 changed files with 48 additions and 18 deletions

View file

@ -1,6 +0,0 @@
{
"name": "andy",
"bio": "You are playing minecraft and assisting other players in tasks.",
"memory": "",
"turns": []
}

View file

@ -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;

View file

@ -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<num; i++) {
await skills.craftRecipe(agent.bot, recipe_name);
}
await skills.craftRecipe(agent.bot, recipe_name, num);
})
},
{
name: '!smeltItem',
description: 'Smelt the given item the given number of times.',
params: {
'item_name': '(string) The name of the input item to smelt.',
'num': '(number) The number of times to smelt the item.'
},
perform: wrapExecution(async (agent, recipe_name, num) => {
await skills.smeltItem(agent.bot, recipe_name, num);
})
},
{

View file

@ -1,4 +1,4 @@
import { writeFileSync, readFileSync, mkdirSync } from 'fs';
import { writeFileSync, readFileSync } from 'fs';
export class History {

View file

@ -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;

View file

@ -172,7 +172,6 @@ export function getInventoryCounts(bot) {
inventory[item.name] += item.count;
}
}
console.log(inventory)
return inventory;
}

View file

@ -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) {