From 52508aba5ffcd83d85db5353ad1519f50cb5175d Mon Sep 17 00:00:00 2001 From: RainbowBird Date: Sun, 5 Jan 2025 23:04:17 +0800 Subject: [PATCH] chore: rename bot --- services/minecraft/src/components/echo.ts | 10 ++--- services/minecraft/src/components/follow.ts | 43 ++++++------------- .../minecraft/src/components/patchfinder.ts | 23 +++++----- 3 files changed, 29 insertions(+), 47 deletions(-) diff --git a/services/minecraft/src/components/echo.ts b/services/minecraft/src/components/echo.ts index 7d8f5aba5..c66c77206 100644 --- a/services/minecraft/src/components/echo.ts +++ b/services/minecraft/src/components/echo.ts @@ -4,20 +4,20 @@ import { useLogg } from '@guiiai/logg' const logger = useLogg('echo').useGlobalConfig() -export function createEchoComponent(botInstance: Bot): ComponentLifecycle { +export function createEchoComponent(bot: Bot): ComponentLifecycle { const onChat = (username: string, message: string) => { - if (username === botInstance.username) + if (username === bot.username) return logger.withFields({ username, message }).log('Chat message received') - botInstance.chat(message) + bot.chat(message) } - botInstance.on('chat', onChat) + bot.on('chat', onChat) return { cleanup: () => { - botInstance.removeListener('chat', onChat) + bot.removeListener('chat', onChat) }, } } diff --git a/services/minecraft/src/components/follow.ts b/services/minecraft/src/components/follow.ts index 063697782..8b6315e8f 100644 --- a/services/minecraft/src/components/follow.ts +++ b/services/minecraft/src/components/follow.ts @@ -3,29 +3,13 @@ import type { ComponentLifecycle } from '../bot' import { useLogg } from '@guiiai/logg' import { goals, Movements, pathfinder } from 'mineflayer-pathfinder' -interface Context { - fromUsername?: string - fromEntity?: any - fromMessage?: string - - isBot: () => boolean - isCommand: () => boolean -} - -function newContext(botInstance: Bot, message: string, sender: string, entity: any): Context { - return { - isBot: () => sender === botInstance.username, - isCommand: () => message.startsWith('#'), - } -} - -export function createFollowComponent(botInstance: Bot): ComponentLifecycle { +export function createFollowComponent(bot: Bot): ComponentLifecycle { const RANGE_GOAL = 2 // get within this radius of the player const logger = useLogg('follow').useGlobalConfig() logger.log('Loading follow plugin') - botInstance.loadPlugin(pathfinder) + bot.loadPlugin(pathfinder) let defaultMove: Movements let following: string | null = null @@ -34,21 +18,21 @@ export function createFollowComponent(botInstance: Bot): ComponentLifecycle { if (!following) return - const target = botInstance.players[following]?.entity + const target = bot.players[following]?.entity if (!target) { - botInstance.chat('I lost sight of you!') + bot.chat('I lost sight of you!') following = null return } const { x: playerX, y: playerY, z: playerZ } = target.position - botInstance.pathfinder.setMovements(defaultMove) - botInstance.pathfinder.setGoal(new goals.GoalNear(playerX, playerY, playerZ, RANGE_GOAL)) + bot.pathfinder.setMovements(defaultMove) + bot.pathfinder.setGoal(new goals.GoalNear(playerX, playerY, playerZ, RANGE_GOAL)) } const onChat = (username: string, message: string) => { - if (username === botInstance.username) + if (username === bot.username) return if (message === 'follow') { @@ -59,14 +43,13 @@ export function createFollowComponent(botInstance: Bot): ComponentLifecycle { else if (message === 'stop') { following = null logger.log('Stopping follow') - botInstance.pathfinder.stop() + bot.pathfinder.stop() } } - botInstance.once('spawn', () => { - logger.log('Spawning bot') - defaultMove = new Movements(botInstance) - botInstance.on('chat', onChat) + bot.once('spawn', () => { + defaultMove = new Movements(bot) + bot.on('chat', onChat) // Continuously update path to follow player const followInterval = setInterval(() => { @@ -74,14 +57,14 @@ export function createFollowComponent(botInstance: Bot): ComponentLifecycle { followPlayer() }, 1000) - botInstance.once('end', () => { + bot.once('end', () => { clearInterval(followInterval) }) }) return { cleanup: () => { - botInstance.removeListener('chat', onChat) + bot.removeListener('chat', onChat) }, } } diff --git a/services/minecraft/src/components/patchfinder.ts b/services/minecraft/src/components/patchfinder.ts index 6468da80b..f65d4bf75 100644 --- a/services/minecraft/src/components/patchfinder.ts +++ b/services/minecraft/src/components/patchfinder.ts @@ -5,44 +5,43 @@ import type { ComponentLifecycle } from '../bot' import { useLogg } from '@guiiai/logg' import { goals, Movements, pathfinder } from 'mineflayer-pathfinder' -export function createPathFinderComponent(botInstance: Bot): ComponentLifecycle { +export function createPathFinderComponent(bot: Bot): ComponentLifecycle { const RANGE_GOAL = 1 // get within this radius of the player const logger = useLogg('pathfinder').useGlobalConfig() logger.log('Loading pathfinder plugin') - botInstance.loadPlugin(pathfinder) + bot.loadPlugin(pathfinder) let defaultMove: Movements const onChat = (username: string, message: string) => { - if (username === botInstance.username) + if (username === bot.username) return if (message !== 'come') return logger.withFields({ username, message }).log('Chat message received') - const target = botInstance.players[username]?.entity + const target = bot.players[username]?.entity if (!target) { - botInstance.chat('I don\'t see you !') + bot.chat('I don\'t see you !') return } const { x: playerX, y: playerY, z: playerZ } = target.position - botInstance.pathfinder.setMovements(defaultMove) - botInstance.pathfinder.setGoal(new goals.GoalNear(playerX, playerY, playerZ, RANGE_GOAL)) + bot.pathfinder.setMovements(defaultMove) + bot.pathfinder.setGoal(new goals.GoalNear(playerX, playerY, playerZ, RANGE_GOAL)) } - botInstance.once('spawn', () => { - logger.log('Spawning bot') - defaultMove = new Movements(botInstance) - botInstance.on('chat', onChat) + bot.once('spawn', () => { + defaultMove = new Movements(bot) + bot.on('chat', onChat) }) return { cleanup: () => { - botInstance.removeListener('chat', onChat) + bot.removeListener('chat', onChat) }, } }