fixed many skills

This commit is contained in:
Maximus 2023-11-27 21:07:52 -06:00
parent 0845ca1030
commit af980a3df8
5 changed files with 37 additions and 20 deletions

View file

@ -15,3 +15,8 @@ Start minecraft server on localhost port `55916`
Add `OPENAI_API_KEY` (and optionally `OPENAI_ORG_ID`) to your environment variables.
run `node main.js`
## Node Module Patches
Some of the node modules that we depend on have bugs in them. PRs have been submitted but currently you must manually implement these fixes in some node modules:
- In `mineflayer-collectblock/lib/CollectBlock.js`, replace `!block.canHarvest(bot.heldItem)` with `bot.heldItem !== null && !block.canHarvest(bot.heldItem.type)` (line 80?)

View file

@ -124,6 +124,7 @@ export class Coder {
this.agent.bot.interrupt_code = true;
this.agent.bot.collectBlock.cancelTask();
this.agent.bot.pathfinder.stop();
this.agent.bot.pvp.stop();
console.log('waiting for code to finish executing... interrupt:', this.agent.bot.interrupt_code);
await new Promise(resolve => setTimeout(resolve, 1000));
}

View file

@ -33,7 +33,7 @@
[
{"role": "user", "content": "hanky: kill that zombie!"},
{"role": "assistant", "content": "I'm attacking!\n```\n//I'm going to attack the nearest zombie.\nawait skills.attackMob(bot, 'zombie');\n```"},
{"role": "system", "content": "Code Output:\nNo zombie nearby\nCode execution failed!"},
{"role": "system", "content": "Code Output:\nNo zombie nearby"},
{"role": "assistant", "content": "I could not find a zombie nearby."}
],
@ -47,5 +47,10 @@
{"role": "assistant", "content": "```\n// I am navigating to the nearest oak log.\nawait skills.goToPosition(bot, nearestOakLog.x, nearestOakLog.y, nearestOakLog.z);\n```"},
{"role": "system", "content": "Code output:\n!!Code threw exception!! Error: ReferenceError: nearestOakLog is not defined\n Write code to fix the problem and try again."},
{"role": "assistant", "content": "Apologies, I made an error. Let me try that again.```\n// I am navigating to the nearest oak log.\nlet position = world.getNearestBlock(bot, 'oak_log', 20);\nawait skills.goToPosition(bot, position.x, position.y, position.z);\n```"}
],
[
{"role": "user", "content": "trixy88: craft some sticks"},
{"role": "assistant", "content": "```\n// I am going to craft 8 sticks\nawait skills.craftRecipe(bot, 'stick');\nawait skills.craftRecipe(bot, 'stick');```"}
]
]

View file

@ -1,11 +1,11 @@
import minecraftData from 'minecraft-data';
import { createBot } from 'mineflayer';
import { pathfinder } from 'mineflayer-pathfinder';
import pvp from 'mineflayer-pvp';
import { plugin } from 'mineflayer-collectblock';
import { plugin as pvp } from 'mineflayer-pvp';
import { plugin as collectblock } from 'mineflayer-collectblock';
const mc_version = '1.19.3'
let mcdata = minecraftData(mc_version);
const mcdata = minecraftData(mc_version);
export function initBot(username) {
@ -15,9 +15,9 @@ export function initBot(username) {
username: username,
version: mc_version,
});
bot.loadPlugin(pathfinder)
bot.loadPlugin(pvp.plugin)
bot.loadPlugin(plugin)
bot.loadPlugin(pathfinder);
bot.loadPlugin(pvp);
bot.loadPlugin(collectblock);
return bot;
}

View file

@ -8,18 +8,16 @@ export function log(bot, message) {
}
export async function craftItem(bot, itemName, num=1) {
export async function craftRecipe(bot, itemName) {
/**
* Attempt to craft the given item.
* Attempt to craft the given item name from a recipe. May craft many items.
* @param {MinecraftBot} bot, reference to the minecraft bot.
* @param {string} item_name, the item name to craft.
* @param {number} num, the number of items to craft. Defaults to 1.
* @param {string} itemName, the item name to craft.
* @returns {Promise<boolean>} true if the item was crafted, false otherwise.
* @example
* await skills.craftItem(bot, "wooden_pickaxe", 2);
* await skills.craftRecipe(bot, "stick");
**/
let recipes = bot.recipesFor(getItemId(itemName), null, num, null); // get recipes that don't require a crafting table
let recipes = bot.recipesFor(getItemId(itemName), null, 1, null); // get recipes that don't require a crafting table
let craftingTable = undefined;
if (!recipes || recipes.length === 0) {
craftingTable = getNearestBlock(bot, 'crafting_table', 6);
@ -27,16 +25,16 @@ export async function craftItem(bot, itemName, num=1) {
log(bot, `You either do not have enough resources to craft ${itemName} or it requires a crafting table, but there is none nearby.`)
return false;
}
recipes = bot.recipesFor(getItemId(itemName), null, num, craftingTable);
recipes = bot.recipesFor(getItemId(itemName), null, 1, craftingTable);
}
if (!recipes || recipes.length === 0) {
log(bot, `You do not have the resources to craft ${num} ${itemName}(s).`);
log(bot, `You do not have the resources to craft a ${itemName}.`);
return false;
}
const recipe = recipes[0];
console.log('crafting...');
await bot.craft(recipe, num, craftingTable);
await bot.craft(recipe, 1, craftingTable);
console.log('crafted');
return true;
}
@ -52,7 +50,7 @@ export async function attackMob(bot, mobType, kill=true) {
* @example
* await skills.attackMob(bot, "zombie", true);
**/
const mob = bot.nearestEntity(entity => entity.name.toLowerCase() === mobType.toLowerCase());
const mob = bot.nearestEntity(entity => entity.name && entity.name.toLowerCase() === mobType.toLowerCase());
const attackable = ['animal', 'monster', 'mob'];
if (mob && attackable.includes(mob.type)) {
let pos = mob.position;
@ -94,8 +92,16 @@ export async function collectBlock(bot, blockType) {
* @example
* await skills.collectBlock(bot, "oak_log");
**/
const block = getNearestBlock(bot, blockType);
const block = getNearestBlock(bot, blockType, 64);
if (block) {
// check if block is collectable
await bot.tool.equipForBlock(block);
const itemId = bot.heldItem ? bot.heldItem.type : null
if (!block.canHarvest(itemId)) {
log(bot, `Don't have right tools to harvest ${blockType}.`);
return false;
}
await bot.collectBlock.collect(block);
return true;
}
@ -137,7 +143,7 @@ export async function placeBlock(bot, blockType, x, y, z) {
* await skills.placeBlock(bot, "oak_log", position.x + 1, position.y - 1, position.x);
**/
const empty_blocks = ['air', 'water', 'lava'];
const empty_blocks = ['air', 'water', 'lava', 'grass', 'tall_grass', 'snow', 'dead_bush', 'fern'];
const targetBlock = bot.blockAt(new Vec3(x, y, z));
if (!empty_blocks.includes(targetBlock.name)) {
log(bot, `Cannot place block at ${targetBlock.position} because ${targetBlock.name} is in the way.`);