From df6ed989d09b6adc2ed321e826764cb581263fed Mon Sep 17 00:00:00 2001 From: Vitaly Date: Fri, 10 Nov 2023 12:21:19 +0300 Subject: [PATCH] feat: add `debugSceneChunks` and `debugChangedOptions` global variables that available via browser console fix: reset all options sometimes didn't work (don't mutate default options) --- README.MD | 4 +++- prismarine-viewer/viewer/lib/worldrenderer.ts | 11 +++++++++++ src/index.ts | 5 +++++ src/optionsStorage.ts | 15 ++++++++++----- 4 files changed, 29 insertions(+), 6 deletions(-) diff --git a/README.MD b/README.MD index 94c4987c..8297d2c0 100644 --- a/README.MD +++ b/README.MD @@ -53,11 +53,13 @@ There is world renderer playground ([link](https://mcon.vercel.app/playground.ht However, there are many things that can be done in online version. You can access some global variables in the console and useful examples: -- `localStorage.debug = '*'` - Enables all debug messages! +- `localStorage.debug = '*'` - Enables all debug messages! Warning: this will start all packets spam. - `bot` - Mineflayer bot instance. See Mineflayer documentation for more. - `viewer` - Three.js viewer instance, basically does all the rendering. - `viewer.world.sectionObjects` - Object with all active chunk sections (geometries) in the world. Each chunk section is a Three.js mesh or group. +- `debugSceneChunks` - The same as above, but relative to current bot position (e.g. 0,0 is the current chunk). +- `debugChangedOptions` - See what options are changed. Don't change options here. - `localServer` - Only for singleplayer mode/host. Flying Squid server instance, see it's documentation for more. - `localServer.overworld.storageProvider.regions` - See ALL LOADED region files with all raw data. diff --git a/prismarine-viewer/viewer/lib/worldrenderer.ts b/prismarine-viewer/viewer/lib/worldrenderer.ts index 79c7265d..d7cea5e9 100644 --- a/prismarine-viewer/viewer/lib/worldrenderer.ts +++ b/prismarine-viewer/viewer/lib/worldrenderer.ts @@ -9,6 +9,7 @@ import { dispose3 } from './dispose' import { toMajor } from './version.js' import PrismarineChatLoader from 'prismarine-chat' import { renderSign } from '../sign-renderer/' +import { chunkPos } from './simpleUtils' function mod (x, n) { return ((x % n) + n) % n @@ -33,6 +34,7 @@ export class WorldRenderer { workers: any[] = [] texturesVersion?: string + constructor (public scene: THREE.Scene, numWorkers = 4) { // init workers for (let i = 0; i < numWorkers; i++) { @@ -182,6 +184,15 @@ export class WorldRenderer { }) } + getLoadedChunksRelative (pos: Vec3) { + const [currentX, currentZ] = chunkPos(pos) + return Object.fromEntries(Object.entries(this.sectionObjects).map(([key, o]) => { + const [xRaw, yRaw, zRaw] = key.split(',').map(Number) + const [x, z] = chunkPos({x: xRaw, z: zRaw}) + return [`${x - currentX},${z - currentZ}`, o] + })) + } + addColumn (x, z, chunk) { this.loadedChunks[`${x},${z}`] = true for (const worker of this.workers) { diff --git a/src/index.ts b/src/index.ts index f6deda93..a22800d3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -105,6 +105,11 @@ document.body.appendChild(renderer.domElement) // Create viewer const viewer: import('prismarine-viewer/viewer/lib/viewer').Viewer = new Viewer(renderer, options.numWorkers) window.viewer = viewer +Object.defineProperty(window, 'debugSceneChunks', { + get () { + return viewer.world.getLoadedChunksRelative(bot.entity.position) + }, +}) viewer.entities.entitiesOptions = { fontFamily: 'mojangles' } diff --git a/src/optionsStorage.ts b/src/optionsStorage.ts index 986e17e7..f186072a 100644 --- a/src/optionsStorage.ts +++ b/src/optionsStorage.ts @@ -4,8 +4,6 @@ import { proxy, subscribe } from 'valtio/vanilla' // weird webpack configuration bug: it cant import valtio/utils in this file import { subscribeKey } from 'valtio/utils' -const mergeAny: (arg1: T, arg2: any) => T = Object.assign - const defaultOptions = { renderDistance: 2, multiplayerRenderDistance: 2, @@ -53,9 +51,10 @@ const defaultOptions = { export type AppOptions = typeof defaultOptions -export const options = proxy( - mergeAny(defaultOptions, JSON.parse(localStorage.options || '{}')) -) +export const options: AppOptions = proxy({ + ...defaultOptions, + ...JSON.parse(localStorage.options || '{}') +}) window.options = window.settings = options @@ -63,6 +62,12 @@ export const resetOptions = () => { Object.assign(options, defaultOptions) } +Object.defineProperty(window, 'debugChangedOptions', { + get () { + return Object.fromEntries(Object.entries(options).filter(([key, v]) => defaultOptions[key] !== v)) + }, +}) + subscribe(options, () => { localStorage.options = JSON.stringify(options) })