From e8087638badc14c2f340e4160c317566f3f742dd Mon Sep 17 00:00:00 2001 From: Vitaly Date: Sat, 2 Sep 2023 03:37:53 +0300 Subject: [PATCH] refactor to utils for fixes --- src/menus/pause_screen.js | 8 ++------ src/utils.js | 27 ++++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/menus/pause_screen.js b/src/menus/pause_screen.js index aecdf6ccf..a863198b4 100644 --- a/src/menus/pause_screen.js +++ b/src/menus/pause_screen.js @@ -6,6 +6,7 @@ const { fsState } = require('../loadFolder') const { subscribe } = require('valtio') const { saveWorld } = require('../builtinCommands') const { notification } = require('./notification') +const { disconnect } = require('../utils') class PauseScreen extends LitElement { static get styles () { @@ -71,12 +72,7 @@ class PauseScreen extends LitElement { showModal(document.getElementById('options-screen'))}> { - if (window.singlePlayerServer) { - await saveWorld() - singlePlayerServer.quit() - } - bot._client.emit('end') - miscUiState.gameLoaded = false + disconnect() }}> ` diff --git a/src/utils.js b/src/utils.js index 411e92763..f48313929 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,9 +1,10 @@ //@ts-check -import { activeModalStack, miscUiState } from './globalState' +import { activeModalStack, miscUiState, showModal } from './globalState' import { notification } from './menus/notification' import * as crypto from 'crypto' import UUID from 'uuid-1345' import { options } from './optionsStorage' +import { saveWorld } from './builtinCommands' export const goFullscreen = async (doToggle = false) => { if (!document.fullscreenElement) { @@ -132,6 +133,30 @@ function javaUUID (s) { export function nameToMcOfflineUUID (name) { return (new UUID(javaUUID('OfflinePlayer:' + name))).toString() } + +export const setLoadingScreenStatus = function (/** @type {string} */status, isError = false) { + const loadingScreen = document.getElementById('loading-error-screen') + + // todo update in component instead + showModal(loadingScreen) + if (loadingScreen.hasError) { + miscUiState.gameLoaded = false + return + } + loadingScreen.hasError = isError + loadingScreen.status = status +} + + +export const disconnect = async () => { + if (window.singlePlayerServer) { + await saveWorld() + singlePlayerServer.quit() + } + bot._client.emit('end') + miscUiState.gameLoaded = false +} + export const loadScript = async function (/** @type {string} */ scriptSrc) { if (document.querySelector(`script[src="${scriptSrc}"]`)) { return