From f75538598191300dfd23f159ff0975c035649552 Mon Sep 17 00:00:00 2001 From: Vitaly Turovsky Date: Fri, 31 Jan 2025 05:52:55 +0300 Subject: [PATCH] refactor: rm loadedGameState, add a way to copy server resource pack to local --- src/controls.ts | 27 ++++++++++++++----- src/globalState.ts | 10 +++---- src/index.ts | 7 ----- src/optionsGuiScheme.tsx | 4 +-- src/react/ChatProvider.tsx | 4 +-- src/react/MinimapProvider.tsx | 7 ++--- src/react/MobileTopButtons.tsx | 2 +- src/react/PlayerListOverlayProvider.tsx | 5 ++-- src/resourcePack.ts | 36 +++++++++++++++++++++---- 9 files changed, 66 insertions(+), 36 deletions(-) diff --git a/src/controls.ts b/src/controls.ts index c936518a..c15a09f3 100644 --- a/src/controls.ts +++ b/src/controls.ts @@ -7,7 +7,7 @@ import { ControMax } from 'contro-max/build/controMax' import { CommandEventArgument, SchemaCommandInput } from 'contro-max/build/types' import { stringStartsWith } from 'contro-max/build/stringUtils' import { UserOverrideCommand, UserOverridesConfig } from 'contro-max/build/types/store' -import { isGameActive, showModal, gameAdditionalState, activeModalStack, hideCurrentModal, miscUiState, loadedGameState, hideModal, hideAllModals } from './globalState' +import { isGameActive, showModal, gameAdditionalState, activeModalStack, hideCurrentModal, miscUiState, hideModal, hideAllModals } from './globalState' import { goFullscreen, pointerLock, reloadChunks } from './utils' import { options } from './optionsStorage' import { openPlayerInventory } from './inventoryWindows' @@ -19,7 +19,7 @@ import { showOptionsModal } from './react/SelectOption' import widgets from './react/widgets' import { getItemFromBlock } from './chatUtils' import { gamepadUiCursorState, moveGamepadCursorByPx } from './react/GamepadUiCursor' -import { completeTexturePackInstall, resourcePackState } from './resourcePack' +import { completeTexturePackInstall, copyServerResourcePackToRegular, resourcePackState } from './resourcePack' import { showNotification } from './react/NotificationProvider' import { lastConnectOptions } from './react/AppStatusProvider' @@ -450,7 +450,12 @@ contro.on('release', ({ command }) => { // hard-coded keybindings -export const f3Keybinds = [ +export const f3Keybinds: Array<{ + key?: string, + action: () => void, + mobileTitle: string + enabled?: () => boolean +}> = [ { key: 'KeyA', action () { @@ -496,9 +501,9 @@ export const f3Keybinds = [ key: 'KeyT', async action () { // TODO! - if (resourcePackState.resourcePackInstalled || loadedGameState.usingServerResourcePack) { + if (resourcePackState.resourcePackInstalled || gameAdditionalState.usingServerResourcePack) { showNotification('Reloading textures...') - await completeTexturePackInstall('default', 'default', loadedGameState.usingServerResourcePack) + await completeTexturePackInstall('default', 'default', gameAdditionalState.usingServerResourcePack) } }, mobileTitle: 'Reload Textures' @@ -539,7 +544,15 @@ export const f3Keybinds = [ const proxyPing = await bot['pingProxy']() void showOptionsModal(`${username}: last known total latency (ping): ${playerPing}. Connected to ${lastConnectOptions.value?.proxy} with current ping ${proxyPing}. Player UUID: ${uuid}`, []) }, - mobileTitle: 'Show Proxy & Ping Details' + mobileTitle: 'Show Proxy & Ping Details', + enabled: () => !!lastConnectOptions.value?.proxy + }, + { + action () { + void copyServerResourcePackToRegular() + }, + mobileTitle: 'Copy Server Resource Pack', + enabled: () => !!gameAdditionalState.usingServerResourcePack } ] @@ -548,7 +561,7 @@ document.addEventListener('keydown', (e) => { if (!isGameActive(false)) return if (hardcodedPressedKeys.has('F3')) { const keybind = f3Keybinds.find((v) => v.key === e.code) - if (keybind) { + if (keybind && (keybind.enabled?.() ?? true)) { keybind.action() e.stopPropagation() } diff --git a/src/globalState.ts b/src/globalState.ts index a15e1837..e09db053 100644 --- a/src/globalState.ts +++ b/src/globalState.ts @@ -146,12 +146,6 @@ export const miscUiState = proxy({ displaySearchInput: false, }) -export const loadedGameState = proxy({ - username: '', - serverIp: '' as string | null, - usingServerResourcePack: false, -}) - export const isGameActive = (foregroundCheck: boolean) => { if (foregroundCheck && activeModalStack.length) return false return miscUiState.gameLoaded @@ -165,7 +159,9 @@ export const gameAdditionalState = proxy({ isSprinting: false, isSneaking: false, isZooming: false, - warps: [] as WorldWarp[] + warps: [] as WorldWarp[], + + usingServerResourcePack: false, }) window.gameAdditionalState = gameAdditionalState diff --git a/src/index.ts b/src/index.ts index 2643cccf..2c120bbd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -53,7 +53,6 @@ import { hideModal, insertActiveModalStack, isGameActive, - loadedGameState, miscUiState, showModal } from './globalState' @@ -884,12 +883,6 @@ async function connect (connectOptions: ConnectOptions) { console.log('Done!') - // todo - onGameLoad(async () => { - loadedGameState.serverIp = server.host ?? null - loadedGameState.username = username - }) - if (appStatusState.isError) return setTimeout(() => { // todo diff --git a/src/optionsGuiScheme.tsx b/src/optionsGuiScheme.tsx index eab35642..47ec5350 100644 --- a/src/optionsGuiScheme.tsx +++ b/src/optionsGuiScheme.tsx @@ -2,7 +2,7 @@ import { useRef, useState } from 'react' import { useSnapshot } from 'valtio' import { openURL } from 'prismarine-viewer/viewer/lib/simpleUtils' import { noCase } from 'change-case' -import { loadedGameState, miscUiState, openOptionsMenu, showModal } from './globalState' +import { gameAdditionalState, miscUiState, openOptionsMenu, showModal } from './globalState' import { AppOptions, options } from './optionsStorage' import Button from './react/Button' import { OptionMeta, OptionSlider } from './react/OptionsItems' @@ -157,7 +157,7 @@ export const guiOptionsScheme: { { custom () { const { resourcePackInstalled } = useSnapshot(resourcePackState) - const { usingServerResourcePack } = useSnapshot(loadedGameState) + const { usingServerResourcePack } = useSnapshot(gameAdditionalState) const { enabledResourcepack } = useSnapshot(options) return