From 8f327c34368fe6bcb1bc877e0b2663ae95694110 Mon Sep 17 00:00:00 2001 From: RainbowBird Date: Mon, 6 Jan 2025 21:42:02 +0800 Subject: [PATCH] fix: type module --- services/minecraft/src/components/follow.ts | 22 +++++++++---------- .../minecraft/src/components/pathfinder.ts | 12 +++++----- services/minecraft/src/main.ts | 2 +- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/services/minecraft/src/components/follow.ts b/services/minecraft/src/components/follow.ts index 76c4d48f0..0888c53c9 100644 --- a/services/minecraft/src/components/follow.ts +++ b/services/minecraft/src/components/follow.ts @@ -2,21 +2,19 @@ import type { BotContext, ComponentLifecycle } from '@/composables/bot' import type { CommandContext } from '@/middlewares/command' import { registerCommand } from '@/composables/command' import { useLogg } from '@guiiai/logg' -import { goals, Movements, pathfinder } from 'mineflayer-pathfinder' +import pathfinderModel from 'mineflayer-pathfinder' -interface FollowContext { - following: string | null - movements: Movements -} +const { goals, Movements, pathfinder } = pathfinderModel -export function createFollowComponent(ctx: BotContext): ComponentLifecycle { - const RANGE_GOAL = 1 // get within this radius of the player +export function createFollowComponent(ctx: BotContext, config?: { + rangeGoal: number +}): ComponentLifecycle { const logger = useLogg('follow').useGlobalConfig() ctx.bot.loadPlugin(pathfinder) - const state: FollowContext = { - following: null, + const state = { + following: undefined as string | undefined, movements: new Movements(ctx.bot), } @@ -27,7 +25,7 @@ export function createFollowComponent(ctx: BotContext): ComponentLifecycle { } function stopFollow(): void { - state.following = null + state.following = undefined logger.log('Stopping follow') ctx.bot.pathfinder.stop() } @@ -39,14 +37,14 @@ export function createFollowComponent(ctx: BotContext): ComponentLifecycle { const target = ctx.bot.players[state.following]?.entity if (!target) { ctx.bot.chat('I lost sight of you!') - state.following = null + state.following = undefined return } const { x: playerX, y: playerY, z: playerZ } = target.position ctx.bot.pathfinder.setMovements(state.movements) - ctx.bot.pathfinder.setGoal(new goals.GoalNear(playerX, playerY, playerZ, RANGE_GOAL)) + ctx.bot.pathfinder.setGoal(new goals.GoalNear(playerX, playerY, playerZ, config?.rangeGoal ?? 1)) } registerCommand('follow', (commandCtx: CommandContext) => { diff --git a/services/minecraft/src/components/pathfinder.ts b/services/minecraft/src/components/pathfinder.ts index 0c74a69ae..3d7d8746e 100644 --- a/services/minecraft/src/components/pathfinder.ts +++ b/services/minecraft/src/components/pathfinder.ts @@ -2,17 +2,19 @@ import type { BotContext, ComponentLifecycle } from '@/composables/bot' import type { CommandContext } from '@/middlewares/command' import { registerCommand } from '@/composables/command' import { useLogg } from '@guiiai/logg' -import { goals, Movements, pathfinder } from 'mineflayer-pathfinder' +import pathfinderModel from 'mineflayer-pathfinder' -export function createPathFinderComponent(ctx: BotContext): ComponentLifecycle { - const RANGE_GOAL = 1 // get within this radius of the player +const { goals, Movements, pathfinder } = pathfinderModel +export function createPathFinderComponent(ctx: BotContext, config?: { + rangeGoal: number +}): ComponentLifecycle { const logger = useLogg('pathfinder').useGlobalConfig() logger.log('Loading pathfinder plugin') ctx.bot.loadPlugin(pathfinder) - let defaultMove: Movements + let defaultMove: any const handleCome = (commandCtx: CommandContext) => { const username = commandCtx.sender @@ -31,7 +33,7 @@ export function createPathFinderComponent(ctx: BotContext): ComponentLifecycle { const { x: playerX, y: playerY, z: playerZ } = target.position ctx.bot.pathfinder.setMovements(defaultMove) - ctx.bot.pathfinder.setGoal(new goals.GoalNear(playerX, playerY, playerZ, RANGE_GOAL)) + ctx.bot.pathfinder.setGoal(new goals.GoalNear(playerX, playerY, playerZ, config?.rangeGoal ?? 1)) } defaultMove = new Movements(ctx.bot) diff --git a/services/minecraft/src/main.ts b/services/minecraft/src/main.ts index b58f327e7..ad7a96663 100644 --- a/services/minecraft/src/main.ts +++ b/services/minecraft/src/main.ts @@ -29,7 +29,7 @@ async function main() { registerComponent('command', createCommandComponent) }) - // initAgent() + initAgent() process.on('SIGINT', () => { cleanup()