Skip to content

Commit

Permalink
adding more events
Browse files Browse the repository at this point in the history
  • Loading branch information
GenerelSchwerz committed Feb 20, 2024
1 parent 776204c commit 08c50b7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
15 changes: 10 additions & 5 deletions src/ThePathfinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export class ThePathfinder {
public abortCalculation = false
private userAborted = false

private currentGoal?: goals.Goal
private currentGotoGoal?: goals.Goal
private currentExecutingPath?: Move[]
private currentMove?: Move
private currentExecutor?: MovementExecutor
Expand Down Expand Up @@ -357,7 +357,6 @@ export class ThePathfinder {
async * getPathFromTo (startPos: Vec3, startVel: Vec3, goal: goals.Goal, settings = this.defaultMoveSettings): PathGenerator {
this.abortCalculation = false
delete this.resetReason
this.currentGoal = goal

this.currentMove = Move.startMove(new IdleMovement(this.bot, this.world), startPos.clone(), startVel.clone(), this.getScaffoldCount())
this.currentExecutor = new IdleMovementExecutor(this.bot, this.world, this.defaultMoveSettings)
Expand Down Expand Up @@ -393,7 +392,7 @@ export class ThePathfinder {
}

this.bot.on(goal.eventKey, list1)
goal.cleanup = (reason) => {
goal.cleanup = () => {
this.bot.off(goal.eventKey, list1)
delete goal.cleanup
}
Expand Down Expand Up @@ -472,6 +471,10 @@ export class ThePathfinder {
}
this.executeTask = new Task()

this.currentGotoGoal = goal

this.bot.emit('goalSet', goal)

const doForever = !!(goal instanceof goals.GoalDynamic && goal.neverfinish && goal.dynamic)

do {
Expand Down Expand Up @@ -711,7 +714,7 @@ export class ThePathfinder {

async cleanupAll (goal: goals.Goal, lastMove?: MovementExecutor): Promise<void> {
if (goal instanceof goals.GoalDynamic && goal.dynamic) {
goal.cleanup?.('normal')
goal.cleanup?.()
}

await this.cleanupBot()
Expand All @@ -727,9 +730,11 @@ export class ThePathfinder {

delete this.resetReason

delete this.currentGoal
delete this.currentGotoGoal
delete this.currentExecutingPath
delete this.currentMove
delete this.currentExecutor

this.bot.emit('goalFinished', goal)
}
}
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import utilPlugin from '@nxg-org/mineflayer-util-plugin'
import { Block, PlaceBlockOptions, ResetReason } from './types'
import { PathingUtil } from './PathingUtil'

export * as goals from './mineflayer-specific/goals'
import * as goals from './mineflayer-specific/goals'

export function createPlugin (settings?: any) {
return function (bot: Bot) {
Expand All @@ -31,5 +31,9 @@ declare module 'mineflayer' {
resetPath: (reason: ResetReason) => void
enteredRecovery: (errorCount: number) => void
exitedRecovery: (errorCount: number) => void
goalSet: (goal: goals.Goal) => void
goalFinished: (goal: goals.Goal) => void
}
}

export * as goals from './mineflayer-specific/goals'
2 changes: 1 addition & 1 deletion src/mineflayer-specific/goals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export abstract class GoalDynamic<Key extends keyof BotEvents> extends Goal {
readonly abstract eventKey: Key
abstract hasChanged (...args: Parameters<BotEvents[Key]>): boolean
abstract update (): void
cleanup?: (reason: 'cancel' | 'normal') => void // will be assigned later.
cleanup?: () => void // will be assigned later.
}
/**
* A goal to be directly at a specific coordinate.
Expand Down

0 comments on commit 08c50b7

Please sign in to comment.