mirror of
https://github.com/PrismarineJS/prismarine-web-client.git
synced 2025-08-28 21:28:44 -04:00
fix showing the loading screen + refactor
This commit is contained in:
parent
2b363f1515
commit
6e1eb77a5f
1 changed files with 18 additions and 17 deletions
35
index.js
35
index.js
|
@ -131,7 +131,13 @@ const optionsScrn = document.getElementById('options-screen')
|
||||||
const keyBindScrn = document.getElementById('keybinds-screen')
|
const keyBindScrn = document.getElementById('keybinds-screen')
|
||||||
const pauseMenu = document.getElementById('pause-screen')
|
const pauseMenu = document.getElementById('pause-screen')
|
||||||
|
|
||||||
const showEl = (str) => { document.getElementById(str).style = 'display:block' }
|
function setLoadingScreenStatus (status, isError = false) {
|
||||||
|
showModal(loadingScreen)
|
||||||
|
if (loadingScreen.hasError) return
|
||||||
|
loadingScreen.status = status
|
||||||
|
loadingScreen.hasError = isError
|
||||||
|
}
|
||||||
|
|
||||||
async function main () {
|
async function main () {
|
||||||
const menu = document.getElementById('play-screen')
|
const menu = document.getElementById('play-screen')
|
||||||
|
|
||||||
|
@ -176,7 +182,7 @@ async function connect (options) {
|
||||||
net.setProxy({ hostname: proxy, port: proxyport })
|
net.setProxy({ hostname: proxy, port: proxyport })
|
||||||
}
|
}
|
||||||
|
|
||||||
loadingScreen.status = 'Logging in'
|
setLoadingScreenStatus('Logging in')
|
||||||
|
|
||||||
const bot = mineflayer.createBot({
|
const bot = mineflayer.createBot({
|
||||||
host,
|
host,
|
||||||
|
@ -193,23 +199,17 @@ async function connect (options) {
|
||||||
|
|
||||||
bot.on('error', (err) => {
|
bot.on('error', (err) => {
|
||||||
console.log('Encountered error!', err)
|
console.log('Encountered error!', err)
|
||||||
showModal(loadingScreen)
|
setLoadingScreenStatus(`Error encountered. Error message: ${err}. Please reload the page`, true)
|
||||||
loadingScreen.status = `Error encountered. Error message: ${err}. Please reload the page`
|
|
||||||
loadingScreen.hasError = true
|
|
||||||
})
|
})
|
||||||
|
|
||||||
bot.on('kicked', (kickReason) => {
|
bot.on('kicked', (kickReason) => {
|
||||||
console.log('User was kicked!', kickReason)
|
console.log('User was kicked!', kickReason)
|
||||||
showModal(loadingScreen)
|
setLoadingScreenStatus(`The Minecraft server kicked you. Kick reason: ${kickReason}. Please reload the page to rejoin`, true)
|
||||||
loadingScreen.status = `The Minecraft server kicked you. Kick reason: ${kickReason}. Please reload the page to rejoin`
|
|
||||||
loadingScreen.hasError = true
|
|
||||||
})
|
})
|
||||||
|
|
||||||
bot.on('end', (endReason) => {
|
bot.on('end', (endReason) => {
|
||||||
console.log('disconnected for', endReason)
|
console.log('disconnected for', endReason)
|
||||||
showModal(loadingScreen)
|
setLoadingScreenStatus(`You have been disconnected from the server. End reason: ${endReason}. Please reload the page to rejoin`, true)
|
||||||
loadingScreen.status = `You have been disconnected from the server. End reason: ${endReason}. Please reload the page to rejoin`
|
|
||||||
loadingScreen.hasError = true
|
|
||||||
})
|
})
|
||||||
|
|
||||||
bot.once('login', () => {
|
bot.once('login', () => {
|
||||||
|
@ -219,13 +219,13 @@ async function connect (options) {
|
||||||
serverHistory.unshift(options.server)
|
serverHistory.unshift(options.server)
|
||||||
localStorage.setItem('serverHistory', JSON.stringify([...new Set(serverHistory)]))
|
localStorage.setItem('serverHistory', JSON.stringify([...new Set(serverHistory)]))
|
||||||
|
|
||||||
loadingScreen.status = 'Loading world'
|
setLoadingScreenStatus('Loading world')
|
||||||
})
|
})
|
||||||
|
|
||||||
bot.once('spawn', () => {
|
bot.once('spawn', () => {
|
||||||
const mcData = require('minecraft-data')(bot.version)
|
const mcData = require('minecraft-data')(bot.version)
|
||||||
|
|
||||||
loadingScreen.status = 'Placing blocks (starting viewer)'
|
setLoadingScreenStatus('Placing blocks (starting viewer)')
|
||||||
|
|
||||||
console.log('bot spawned - starting viewer')
|
console.log('bot spawned - starting viewer')
|
||||||
|
|
||||||
|
@ -285,7 +285,7 @@ async function connect (options) {
|
||||||
bot.on('move', botPosition)
|
bot.on('move', botPosition)
|
||||||
botPosition()
|
botPosition()
|
||||||
|
|
||||||
loadingScreen.status = 'Setting callbacks'
|
setLoadingScreenStatus('Setting callbacks')
|
||||||
|
|
||||||
function moveCallback (e) {
|
function moveCallback (e) {
|
||||||
if (!pointerLock.hasPointerLock) return
|
if (!pointerLock.hasPointerLock) return
|
||||||
|
@ -418,7 +418,7 @@ async function connect (options) {
|
||||||
})
|
})
|
||||||
}, false)
|
}, false)
|
||||||
|
|
||||||
loadingScreen.status = 'Done!'
|
setLoadingScreenStatus('Done!')
|
||||||
console.log('Done!')
|
console.log('Done!')
|
||||||
|
|
||||||
hud.init(renderer, bot, host)
|
hud.init(renderer, bot, host)
|
||||||
|
@ -427,7 +427,8 @@ async function connect (options) {
|
||||||
setTimeout(function () {
|
setTimeout(function () {
|
||||||
if (loadingScreen.hasError) return
|
if (loadingScreen.hasError) return
|
||||||
// remove loading screen, wait a second to make sure a frame has properly rendered
|
// remove loading screen, wait a second to make sure a frame has properly rendered
|
||||||
hideModal(loadingScreen)
|
loadingScreen.style.display = 'none'
|
||||||
|
activeModalStack.splice(0, activeModalStack.length)
|
||||||
}, 2500)
|
}, 2500)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -449,5 +450,5 @@ window.addEventListener('keydown', (e) => {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
showEl('title-screen')
|
showModal(document.getElementById('title-screen'))
|
||||||
main()
|
main()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue