diff --git a/src/agent/agent.js b/src/agent/agent.js index 14a4e9c..534d582 100644 --- a/src/agent/agent.js +++ b/src/agent/agent.js @@ -2,7 +2,7 @@ import { initBot } from '../utils/mcdata.js'; import { sendRequest } from '../utils/gpt.js'; import { History } from './history.js'; import { Coder } from './coder.js'; -import { containsCommand, executeCommand } from './commands.js'; +import { containsCommand, commandExists, executeCommand } from './commands.js'; import { Events } from './events.js'; @@ -19,9 +19,7 @@ export class Agent { this.bot.on('login', async () => { await this.history.loadExamples(); - - if (!init_message) - this.bot.chat('Hello world! I am ' + this.name); + console.log(`${this.name} logged in.`); const ignore_messages = [ @@ -42,10 +40,17 @@ export class Agent { this.handleMessage(username, message); }); + // set the bot to automatically eat food when hungry + this.bot.autoEat.options = { + priority: 'foodPoints', + startAt: 14, + bannedFood: [] + }; if (init_message) { this.handleMessage('system', init_message); } else { + this.bot.chat('Hello world! I am ' + this.name); this.bot.emit('finished_executing'); } }); @@ -77,7 +82,11 @@ export class Agent { let command_name = containsCommand(res); if (command_name) { // contains query or command - console.log('Query/Command response:', res); + console.log('Command message:', res); + if (!commandExists(command_name)) { + this.history.add('system', `Command ${command_name} does not exist. Use !newAction to perform custom actions.`); + continue; + } let pre_message = res.substring(0, res.indexOf(command_name)).trim(); diff --git a/src/agent/commands.js b/src/agent/commands.js index 54e6764..2944b18 100644 --- a/src/agent/commands.js +++ b/src/agent/commands.js @@ -17,14 +17,17 @@ const argRegex = /(?:"[^"]*"|'[^']*'|[^,])+/g; export function containsCommand(message) { const commandMatch = message.match(commandRegex); - if (commandMatch) { - const commandName = "!"+commandMatch[1]; - if (commandList.some((command) => command.name === commandName)) - return commandName; - } + if (commandMatch) + return commandName; return null; } +export function commandExists(commandName) { + if (!commandName.startsWith("!")) + commandName = "!" + commandName; + return commandMap[commandName] !== undefined; +} + // todo: handle arrays? function parseCommandMessage(message) { const commandMatch = message.match(commandRegex); diff --git a/src/utils/mcdata.js b/src/utils/mcdata.js index 641e82d..057d706 100644 --- a/src/utils/mcdata.js +++ b/src/utils/mcdata.js @@ -3,6 +3,8 @@ import { createBot } from 'mineflayer'; import { pathfinder } from 'mineflayer-pathfinder'; import { plugin as pvp } from 'mineflayer-pvp'; import { plugin as collectblock } from 'mineflayer-collectblock'; +import { plugin as autoEat } from 'mineflayer-auto-eat'; + const mc_version = '1.19.3' const mcdata = minecraftData(mc_version); @@ -18,6 +20,8 @@ export function initBot(username) { bot.loadPlugin(pathfinder); bot.loadPlugin(pvp); bot.loadPlugin(collectblock); + bot.loadPlugin(autoEat); + return bot; } @@ -77,3 +81,8 @@ export function getAllBlockIds(ignore) { } return blockIds; } + +export function getBiomeName(bot) { + const biomeId = bot.world.getBiome(bot.entity.position); + return mcdata.biomes[biomeId].name; +}