fix(minecraft): implement proper interrupt method to stop bot actions
- Add `interrupt()` method to Mineflayer class with comprehensive action stopping
- Stop pathfinder, pvp, digging, item usage, and control states
- Replace non-working `emit('interrupt')` with `interrupt()` call in stop tool
- Add logging for interrupt requests and completion
- Remove FIXME comment as issue is now resolved
This commit is contained in:
@@ -90,7 +90,7 @@ export const actionsList: Action[] = [
|
||||
// await ctx.actions.stop()
|
||||
// ctx.clearBotLogs()
|
||||
// ctx.actions.cancelResume()
|
||||
mineflayer.emit('interrupt') // FIXME: this doesn't work!
|
||||
mineflayer.interrupt('stop tool called')
|
||||
const msg = 'Agent stopped.'
|
||||
// if (mineflayer.self_prompter.on)
|
||||
// msg += ' Self-prompting still active.'
|
||||
|
||||
@@ -53,6 +53,45 @@ export class Mineflayer extends EventEmitter<EventHandlers> {
|
||||
})
|
||||
}
|
||||
|
||||
public interrupt(reason?: string) {
|
||||
this.logger.withFields({ reason }).log('Interrupt requested')
|
||||
|
||||
try {
|
||||
(this.bot as any).pathfinder?.stop?.()
|
||||
}
|
||||
catch { }
|
||||
|
||||
try {
|
||||
(this.bot as any).pvp?.stop?.()
|
||||
}
|
||||
catch { }
|
||||
|
||||
try {
|
||||
; (this.bot as any).stopDigging?.()
|
||||
}
|
||||
catch { }
|
||||
|
||||
try {
|
||||
; (this.bot as any).deactivateItem?.()
|
||||
}
|
||||
catch { }
|
||||
|
||||
try {
|
||||
if (typeof (this.bot as any).clearControlStates === 'function') {
|
||||
; (this.bot as any).clearControlStates()
|
||||
}
|
||||
else {
|
||||
; (['forward', 'back', 'left', 'right', 'jump', 'sprint', 'sneak'] as const).forEach((control) => {
|
||||
this.bot.setControlState(control as any, false)
|
||||
})
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
|
||||
this.logger.withFields({ reason }).log('Interrupted')
|
||||
this.emit('interrupt')
|
||||
}
|
||||
|
||||
public static async asyncBuild(options: MineflayerOptions) {
|
||||
const mineflayer = new Mineflayer(options)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user