Skip to content

Commit

Permalink
Should show error screen on error (#350)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
zardoy authored Sep 16, 2023
1 parent 856639d commit 2fba863
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
19 changes: 17 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
25 changes: 25 additions & 0 deletions scripts/patchPackages.js
Original file line number Diff line number Diff line change
@@ -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')

0 comments on commit 2fba863

Please sign in to comment.