Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Handle TypeError in #onError and removeAllListeners in digging.js #3577

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions lib/plugins/digging.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,16 @@ function inject (bot) {
}

bot.on('death', () => {
bot.removeAllListeners('diggingAborted')
bot.removeAllListeners('diggingCompleted')
bot.stopDigging()
// Check if there are listeners before removing
if (bot.listenerCount('diggingAborted') > 0) {
bot.removeAllListeners('diggingAborted');
}
if (bot.listenerCount('diggingCompleted') > 0) {
bot.removeAllListeners('diggingCompleted');
}
if (typeof bot.stopDigging === 'function') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In what situation is this not defined?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It happens when the bot dies. This is the full error:
200 | #onClose(e) {

201 | this.#onExitPromise = e.code, this.emit("exit", e.code);

202 | }

203 | #onError(event) {

204 | let error = event?.error;

205 | error = new Error(event.message, { cause: event });

                ^

error: 208 |

209 | await diggingTask.promise

210 | }

211 |

212 | bot.on('death', () => {

213 | bot.removeAllListeners('diggingAborted');

          ^

TypeError: undefined is not an object (evaluating 'this._events')

  at removeAllListeners (node:events:244:25)

  at <anonymous> (/home/dimonchi/Project-Archive-Minecraft/node_modules/mineflayer/lib/plugins/digging.js:213:9)

  at emit (node:events:84:22)

  at <anonymous> (/home/dimonchi/Project-Archive-Minecraft/node_modules/mineflayer/lib/plugins/health.js:25:13)

  at emit (node:events:90:22)

  at emitPacket (/home/dimonchi/Project-Archive-Minecraft/node_modules/minecraft-protocol/src/client.js:84:12)

  at <anonymous> (/home/dimonchi/Project-Archive-Minecraft/node_modules/minecraft-protocol/src/client.js:113:9)

  at emit (node:events:87:22)

  at addChunk (/home/dimonchi/Project-Archive-Minecraft/node_modules/readable-stream/lib/internal/streams/readable.js:323:12)

  at readableAddChunk (/home/dimonchi/Project-Archive-Minecraft/node_modules/readable-stream/lib/internal/streams/readable.js:300:9)



  at #onError (node:worker_threads:205:15)

Aborted (core dumped)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a problem with your code, not mineflayer

bot.stopDigging();
}
})

function canDigBlock (block) {
Expand Down
Loading