fixed multiplayer chat, misc fixes

This commit is contained in:
MaxRobinsonTheGreat 2024-01-24 17:24:52 -06:00
parent e9abae74da
commit 8a40ff03c4
5 changed files with 33 additions and 10 deletions

View file

@ -67,6 +67,12 @@ export class Agent {
}); });
} }
cleanChat(message) {
// newlines are interpreted as separate chats, which triggers spam filters. replace them with spaces
message = message.replaceAll('\n', ' ');
return this.bot.chat(message);
}
async handleMessage(source, message) { async handleMessage(source, message) {
if (!!source && !!message) if (!!source && !!message)
await this.history.add(source, message); await this.history.add(source, message);
@ -82,7 +88,7 @@ export class Agent {
this.history.add(source, truncated_msg); this.history.add(source, truncated_msg);
} }
if (execute_res) if (execute_res)
this.bot.chat(execute_res); this.cleanChat(execute_res);
return; return;
} }
@ -103,7 +109,7 @@ export class Agent {
let pre_message = res.substring(0, res.indexOf(command_name)).trim(); let pre_message = res.substring(0, res.indexOf(command_name)).trim();
this.bot.chat(`${pre_message} *used ${command_name.substring(1)}*`); this.cleanChat(`${pre_message} *used ${command_name.substring(1)}*`);
let execute_res = await executeCommand(this, res); let execute_res = await executeCommand(this, res);
console.log('Agent executed:', command_name, 'and got:', execute_res); console.log('Agent executed:', command_name, 'and got:', execute_res);
@ -114,7 +120,7 @@ export class Agent {
break; break;
} }
else { // conversation response else { // conversation response
this.bot.chat(res); this.cleanChat(res);
console.log('Purely conversational response:', res); console.log('Purely conversational response:', res);
break; break;
} }
@ -125,13 +131,20 @@ export class Agent {
} }
startUpdateLoop() { startUpdateLoop() {
this.bot.on('end', () => { this.bot.on('error' , (err) => {
console.warn('Bot disconnected! Killing agent process.') console.error('Error event!', err);
});
this.bot.on('end', (reason) => {
console.warn('Bot disconnected! Killing agent process.', reason)
process.exit(1); process.exit(1);
}); });
this.bot.on('death', () => { this.bot.on('death', () => {
this.coder.stop(); this.coder.stop();
}); });
this.bot.on('kicked', (reason) => {
console.warn('Bot kicked!', reason);
process.exit(1);
});
this.bot.on('messagestr', async (message, _, jsonMsg) => { this.bot.on('messagestr', async (message, _, jsonMsg) => {
if (jsonMsg.translate && jsonMsg.translate.startsWith('death') && message.startsWith(this.name)) { if (jsonMsg.translate && jsonMsg.translate.startsWith('death') && message.startsWith(this.name)) {
console.log('Agent died: ', message); console.log('Agent died: ', message);

View file

@ -107,7 +107,7 @@ export const actionsList = [
description: 'Attack and kill the nearest entity of a given type.', description: 'Attack and kill the nearest entity of a given type.',
params: {'type': '(string) The type of entity to attack.'}, params: {'type': '(string) The type of entity to attack.'},
perform: wrapExecution(async (agent, type) => { perform: wrapExecution(async (agent, type) => {
await skills.attackMob(agent.bot, type, true); await skills.attackNearest(agent.bot, type, true);
}) })
}, },
{ {

View file

@ -19,7 +19,12 @@ export const queryList = [
res += `\n- Health: ${Math.round(bot.health)} / 20`; res += `\n- Health: ${Math.round(bot.health)} / 20`;
res += `\n- Hunger: ${Math.round(bot.food)} / 20`; res += `\n- Hunger: ${Math.round(bot.food)} / 20`;
res += `\n- Biome: ${getBiomeName(bot)}`; res += `\n- Biome: ${getBiomeName(bot)}`;
res += `\n- Weather: ${bot.weather}`; let weather = "clear";
if (bot.rainState > 0)
weather = "Rain";
if (bot.thunderState > 0)
weather = "Thunderstorm";
res += `\n- Weather: ${weather}`;
// let block = bot.blockAt(pos); // let block = bot.blockAt(pos);
// res += `\n- Artficial light: ${block.skyLight}`; // res += `\n- Artficial light: ${block.skyLight}`;
// res += `\n- Sky light: ${block.light}`; // res += `\n- Sky light: ${block.light}`;

View file

@ -53,6 +53,8 @@ const modes = [
let item = world.getNearestEntityWhere(agent.bot, entity => entity.name === 'item', 8); let item = world.getNearestEntityWhere(agent.bot, entity => entity.name === 'item', 8);
if (item) { if (item) {
execute(this, agent, async () => { execute(this, agent, async () => {
// wait 2 seconds for the item to settle
await new Promise(resolve => setTimeout(resolve, 2000));
await skills.pickupNearbyItem(agent.bot); await skills.pickupNearbyItem(agent.bot);
}); });
} }

View file

@ -186,7 +186,7 @@ export async function attackNearest(bot, mobType, kill=true) {
* @param {boolean} kill, whether or not to continue attacking until the mob is dead. Defaults to true. * @param {boolean} kill, whether or not to continue attacking until the mob is dead. Defaults to true.
* @returns {Promise<boolean>} true if the mob was attacked, false if the mob type was not found. * @returns {Promise<boolean>} true if the mob was attacked, false if the mob type was not found.
* @example * @example
* await skills.attackMob(bot, "zombie", true); * await skills.attackNearest(bot, "zombie", true);
**/ **/
const mob = bot.nearestEntity(entity => entity.name && entity.name.toLowerCase() === mobType.toLowerCase()); const mob = bot.nearestEntity(entity => entity.name && entity.name.toLowerCase() === mobType.toLowerCase());
if (mob) { if (mob) {
@ -248,7 +248,7 @@ export async function defendSelf(bot, range=8) {
let enemy = getNearestEntityWhere(bot, entity => isHostile(entity), range); let enemy = getNearestEntityWhere(bot, entity => isHostile(entity), range);
while (enemy) { while (enemy) {
equipHighestAttack(bot); equipHighestAttack(bot);
if (bot.entity.position.distanceTo(enemy.position) > 4 && enemy.name !== 'creeper') { if (bot.entity.position.distanceTo(enemy.position) > 4 && enemy.name !== 'creeper' && enemy.name !== 'phantom') {
try { try {
bot.pathfinder.setMovements(new pf.Movements(bot)); bot.pathfinder.setMovements(new pf.Movements(bot));
await bot.pathfinder.goto(new pf.goals.GoalFollow(enemy, 2), true); await bot.pathfinder.goto(new pf.goals.GoalFollow(enemy, 2), true);
@ -263,6 +263,7 @@ export async function defendSelf(bot, range=8) {
return false; return false;
} }
} }
bot.pvp.stop();
if (attacked) if (attacked)
log(bot, `Successfully defended self.`); log(bot, `Successfully defended self.`);
else else
@ -678,12 +679,14 @@ export async function goToBed(bot) {
export function isHuntable(mob) { export function isHuntable(mob) {
if (!mob || !mob.name) return false;
const animals = ['chicken', 'cod', 'cow', 'llama', 'mooshroom', 'pig', 'pufferfish', 'rabbit', 'salmon', 'sheep', 'squid', 'tropical_fish', 'turtle']; const animals = ['chicken', 'cod', 'cow', 'llama', 'mooshroom', 'pig', 'pufferfish', 'rabbit', 'salmon', 'sheep', 'squid', 'tropical_fish', 'turtle'];
return animals.includes(mob.name.toLowerCase()) && !mob.metadata[16]; // metadata 16 is not baby return animals.includes(mob.name.toLowerCase()) && !mob.metadata[16]; // metadata 16 is not baby
} }
export function isHostile(mob) { export function isHostile(mob) {
if (!mob || !mob.name) return false;
return (mob.type === 'mob' || mob.type === 'hostile') && mob.name !== 'iron_golem' && mob.name !== 'snow_golem'; return (mob.type === 'mob' || mob.type === 'hostile') && mob.name !== 'iron_golem' && mob.name !== 'snow_golem';
} }