From 360ed68d9875df44dbfa5692bedff3aa663d3e72 Mon Sep 17 00:00:00 2001 From: Vitaly Date: Mon, 4 Sep 2023 07:57:23 +0300 Subject: [PATCH] singlePlayerServer -> localServer (intention: will allow other players to connect soon) --- src/builtinCommands.ts | 12 ++++++------ src/globalState.js | 4 ++-- src/globals.d.ts | 2 +- src/globals.js | 2 +- src/index.js | 14 ++++++++------ src/utils.js | 4 ++-- 6 files changed, 20 insertions(+), 18 deletions(-) diff --git a/src/builtinCommands.ts b/src/builtinCommands.ts index 61de3366..15f2df8a 100644 --- a/src/builtinCommands.ts +++ b/src/builtinCommands.ts @@ -31,7 +31,7 @@ async function addFolderToZip(folderPath, zip, relativePath) { const exportWorld = async () => { // todo issue into chat warning if fs is writable! const zip = new JSZip() - let worldFolder: string = singlePlayerServer.options.worldFolder + let worldFolder: string = localServer.options.worldFolder if (!worldFolder.startsWith('/')) worldFolder = `/${worldFolder}` await addFolderToZip(worldFolder, zip, '') @@ -66,9 +66,9 @@ const commands = [ if (fsState.isReadonly || !fsState.syncFs) return // todo for testing purposes sessionStorage.oldData = localStorage - singlePlayerServer.quit() + localServer.quit() // todo browserfs bug - fs.rmdirSync(singlePlayerServer.options.worldFolder, { recursive: true }) + fs.rmdirSync(localServer.options.worldFolder, { recursive: true }) } }, { @@ -80,7 +80,7 @@ const commands = [ ] export const tryHandleBuiltinCommand = (message) => { - if (!singlePlayerServer) return + if (!localServer) return for (const command of commands) { if (command.command.includes(message)) { @@ -91,10 +91,10 @@ export const tryHandleBuiltinCommand = (message) => { } export const saveWorld = async () => { - for (const player of window.singlePlayerServer.players) { + for (const player of localServer.players) { await player.save() } - const worlds = [singlePlayerServer.overworld] + const worlds = [localServer.overworld] for (const world of worlds) { await world.storageProvider.close() } diff --git a/src/globalState.js b/src/globalState.js index 7a500e4b..7d83340b 100644 --- a/src/globalState.js +++ b/src/globalState.js @@ -124,8 +124,8 @@ export const gameAdditionalState = proxy({ window.gameAdditionalState = gameAdditionalState const savePlayers = () => { - if (!window.singlePlayerServer) return - for (const player of window.singlePlayerServer.players) { + if (!window.localServer) return + for (const player of window.localServer.players) { player.save() } } diff --git a/src/globals.d.ts b/src/globals.d.ts index cc13b180..ecad1636 100644 --- a/src/globals.d.ts +++ b/src/globals.d.ts @@ -3,7 +3,7 @@ declare const THREE: typeof import('three') // todo declare const bot: import('mineflayer').Bot -declare const singlePlayerServer: any +declare const localServer: any declare interface Document { getElementById(id): any diff --git a/src/globals.js b/src/globals.js index 24185356..4362604c 100644 --- a/src/globals.js +++ b/src/globals.js @@ -2,4 +2,4 @@ window.bot = undefined window.THREE = undefined -window.singlePlayerServer = undefined +window.localServer = undefined diff --git a/src/index.js b/src/index.js index 71abd0b3..5ba19919 100644 --- a/src/index.js +++ b/src/index.js @@ -306,7 +306,7 @@ async function connect (connectOptions) { let bot const destroyAll = () => { viewer.resetAll() - window.singlePlayerServer = undefined + window.localServer = undefined // simple variant, still buggy postRenderFrameFn = () => { } @@ -354,7 +354,7 @@ async function connect (connectOptions) { }, { signal: errorAbortController.signal }) - let singlePlayerServer + let localServer try { Object.assign(serverOptions, _.defaultsDeep({}, connectOptions.serverOverrides ?? {}, options.localServerOptions, serverOptions)) let version = connectOptions.botVersion ?? serverOptions.version @@ -378,13 +378,13 @@ async function connect (connectOptions) { setLoadingScreenStatus('Starting local server') window.serverDataChannel ??= {} window.worldLoaded = false - singlePlayerServer = window.singlePlayerServer = startLocalServer() + localServer = window.localServer = startLocalServer() // todo need just to call quit if started // loadingScreen.maybeRecoverable = false // init world, todo: do it for any async plugins - if (!singlePlayerServer.worldsReady) { + if (!localServer.worldsReady) { await new Promise(resolve => { - singlePlayerServer.once('worldsReady', resolve) + localServer.once('worldsReady', resolve) }) } } @@ -466,10 +466,12 @@ async function connect (connectOptions) { const center = bot.entity.position + /** @type {import('../prismarine-viewer/viewer/lib/worldView').WorldView} */ const worldView = new WorldView(bot.world, singeplayer ? renderDistance : Math.min(renderDistance, maxMultiplayerRenderDistance), center) if (singeplayer) { + let prevRenderDistance = renderDistance const d = subscribeKey(options, 'renderDistance', () => { - singlePlayerServer.options['view-distance'] = options.renderDistance + localServer.options['view-distance'] = options.renderDistance worldView.viewDistance = options.renderDistance window.onPlayerChangeRenderDistance?.(options.renderDistance) }) diff --git a/src/utils.js b/src/utils.js index f4831392..ae6ae43c 100644 --- a/src/utils.js +++ b/src/utils.js @@ -149,9 +149,9 @@ export const setLoadingScreenStatus = function (/** @type {string} */status, isE export const disconnect = async () => { - if (window.singlePlayerServer) { + if (window.localServer) { await saveWorld() - singlePlayerServer.quit() + localServer.quit() } bot._client.emit('end') miscUiState.gameLoaded = false