Skip to content

Commit 2fba863

Browse files
authored
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
1 parent 856639d commit 2fba863

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

index.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,17 @@ async function connect (options) {
176176

177177
loadingScreen.status = 'Logging in'
178178

179+
const errorAbortController = new AbortController()
180+
window.addEventListener('unhandledrejection', (e) => {
181+
handleError(e.reason)
182+
}, {
183+
signal: errorAbortController
184+
})
185+
window.addEventListener('error', (e) => {
186+
handleError(e.message)
187+
}, {
188+
signal: errorAbortController.signal
189+
})
179190
const bot = mineflayer.createBot({
180191
host,
181192
port,
@@ -189,12 +200,13 @@ async function connect (options) {
189200
})
190201
hud.preload(bot)
191202

192-
bot.on('error', (err) => {
203+
const handleError = (err) => {
193204
console.log('Encountered error!', err)
194205
loadingScreen.status = `Error encountered. Error message: ${err}. Please reload the page`
195206
loadingScreen.style = 'display: block;'
196207
loadingScreen.hasError = true
197-
})
208+
}
209+
bot.on('error', handleError)
198210

199211
bot.on('kicked', (kickReason) => {
200212
console.log('User was kicked!', kickReason)
@@ -398,6 +410,9 @@ async function connect (options) {
398410
hud.style.display = 'block'
399411

400412
setTimeout(function () {
413+
// game in playable state show errors in console instead
414+
errorAbortController.abort()
415+
if (loadingScreen.hasError) return
401416
// remove loading screen, wait a second to make sure a frame has properly rendered
402417
loadingScreen.style = 'display: none;'
403418
}, 2500)

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"description": "A minecraft client running in a browser",
55
"main": "index.js",
66
"scripts": {
7+
"postinstall": "node scripts/patchPackages.js",
78
"build": "webpack --config webpack.prod.js",
89
"build-dev": "webpack --config webpack.dev.js",
910
"start": "node --max-old-space-size=8192 server.js 8080 dev",

scripts/patchPackages.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// @ts-check
2+
const path = require('path')
3+
const dataPath = path.join(require.resolve('minecraft-data'), '../data.js')
4+
5+
const fs = require('fs')
6+
7+
const lines = fs.readFileSync(dataPath, 'utf8').split('\n')
8+
if (lines[0] === '//patched') {
9+
console.log('Already patched')
10+
process.exit(0)
11+
}
12+
13+
function removeLinesBetween (start, end) {
14+
const startIndex = lines.findIndex(line => line === start)
15+
if (startIndex === -1) return
16+
const endIndex = startIndex + lines.slice(startIndex).findIndex(line => line === end)
17+
// insert block comments
18+
lines.splice(startIndex, 0, '/*')
19+
lines.splice(endIndex + 2, 0, '*/')
20+
}
21+
22+
removeLinesBetween(" 'bedrock': {", ' }')
23+
24+
lines.unshift('//patched')
25+
fs.writeFileSync(dataPath, lines.join('\n'), 'utf8')

0 commit comments

Comments
 (0)