From 2fba863d662cb895339c3eda905a4f1edc8d5db6 Mon Sep 17 00:00:00 2001 From: Vitaly Date: Sat, 16 Sep 2023 08:30:52 +0300 Subject: [PATCH] Should show error screen on error (#350) * Should show error screen on error * not sure how its there ; * remove bedrock files as doesnt seem to be supported anyway * lint fix * handle non-promise variant as well --- index.js | 19 +++++++++++++++++-- package.json | 1 + scripts/patchPackages.js | 25 +++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 scripts/patchPackages.js diff --git a/index.js b/index.js index 0339782..027ce4d 100644 --- a/index.js +++ b/index.js @@ -176,6 +176,17 @@ async function connect (options) { loadingScreen.status = 'Logging in' + const errorAbortController = new AbortController() + window.addEventListener('unhandledrejection', (e) => { + handleError(e.reason) + }, { + signal: errorAbortController + }) + window.addEventListener('error', (e) => { + handleError(e.message) + }, { + signal: errorAbortController.signal + }) const bot = mineflayer.createBot({ host, port, @@ -189,12 +200,13 @@ async function connect (options) { }) hud.preload(bot) - bot.on('error', (err) => { + const handleError = (err) => { console.log('Encountered error!', err) loadingScreen.status = `Error encountered. Error message: ${err}. Please reload the page` loadingScreen.style = 'display: block;' loadingScreen.hasError = true - }) + } + bot.on('error', handleError) bot.on('kicked', (kickReason) => { console.log('User was kicked!', kickReason) @@ -398,6 +410,9 @@ async function connect (options) { hud.style.display = 'block' setTimeout(function () { + // game in playable state show errors in console instead + errorAbortController.abort() + if (loadingScreen.hasError) return // remove loading screen, wait a second to make sure a frame has properly rendered loadingScreen.style = 'display: none;' }, 2500) diff --git a/package.json b/package.json index 9700a30..3628f9a 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "description": "A minecraft client running in a browser", "main": "index.js", "scripts": { + "postinstall": "node scripts/patchPackages.js", "build": "webpack --config webpack.prod.js", "build-dev": "webpack --config webpack.dev.js", "start": "node --max-old-space-size=8192 server.js 8080 dev", diff --git a/scripts/patchPackages.js b/scripts/patchPackages.js new file mode 100644 index 0000000..d75753f --- /dev/null +++ b/scripts/patchPackages.js @@ -0,0 +1,25 @@ +// @ts-check +const path = require('path') +const dataPath = path.join(require.resolve('minecraft-data'), '../data.js') + +const fs = require('fs') + +const lines = fs.readFileSync(dataPath, 'utf8').split('\n') +if (lines[0] === '//patched') { + console.log('Already patched') + process.exit(0) +} + +function removeLinesBetween (start, end) { + const startIndex = lines.findIndex(line => line === start) + if (startIndex === -1) return + const endIndex = startIndex + lines.slice(startIndex).findIndex(line => line === end) + // insert block comments + lines.splice(startIndex, 0, '/*') + lines.splice(endIndex + 2, 0, '*/') +} + +removeLinesBetween(" 'bedrock': {", ' }') + +lines.unshift('//patched') +fs.writeFileSync(dataPath, lines.join('\n'), 'utf8')