From 1597acca727192688dd6361461a81a8cb993f850 Mon Sep 17 00:00:00 2001 From: jay Date: Mon, 22 Mar 2021 17:32:56 +0500 Subject: [PATCH] fix(eater): :goal_net: catch async error on full food MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Attempt to fix async error returned by `bot.consume`. Fixed by wrapping in a `try{}` block and using `.catch`. Still don't know why or how this works 🤷. --- lib/plugins/eater.js | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/lib/plugins/eater.js b/lib/plugins/eater.js index 8347e7b..c9abe21 100644 --- a/lib/plugins/eater.js +++ b/lib/plugins/eater.js @@ -2,11 +2,7 @@ let cfg = {} let bot = {} let isEating = false -function callbackHandle(err) { - if (err) console.error(err) -} - -function eat(callback) { +function eat(callback = e => e && console.error(e)) { isEating = true const foodNames = require('minecraft-data')(bot.version).foodsArray.map((item) => item.name) @@ -37,23 +33,23 @@ function eat(callback) { bot.equip(best_food, 'hand', function (error) { if (error) { - console.error(error) + console.warn(error, best_food) isEating = false bot.emit('eat_stop') } else { - bot.consume(function (err) { - if (err) { - console.error(err) - isEating = false - bot.emit('eat_stop') - return callback(err) - } else { + try { + bot.consume().catch(error => { + if (error.message === "Food is full") { + console.warn(error, best_food) + } else { + return callback({ error, best_food }) + } + }).finally(() => { isEating = false bot.emit('eat_stop') - callback(null) - if (!bot.food === 20) eat(callbackHandle) - } - }) + }) + } catch { } + if (bot.food !== 20) eat(callback) } }) } @@ -76,7 +72,7 @@ function checkFood() { // TODO implement better idle state ) || true // idle most likely ) { - eat(callbackHandle) + eat() } } }