@@ -4,6 +4,7 @@ import { getThreeJsRendererMethods } from 'renderer/viewer/three/threeJsMethods'
44import { options } from './optionsStorage'
55import { jeiCustomCategories } from './inventoryWindows'
66import { registerIdeChannels } from './core/ideChannels'
7+ import { serverSafeSettings } from './defaultOptions'
78
89export default ( ) => {
910 customEvents . on ( 'mineflayerBotCreated' , async ( ) => {
@@ -17,6 +18,7 @@ export default () => {
1718 registerWaypointChannels ( )
1819 registerFireworksChannels ( )
1920 registerIdeChannels ( )
21+ registerServerSettingsChannel ( )
2022 } )
2123 } )
2224}
@@ -525,6 +527,67 @@ const addTestVideo = (rotation = 0 as 0 | 1 | 2 | 3, scale = 1, isImage = false)
525527}
526528window . addTestVideo = addTestVideo
527529
530+ const registerServerSettingsChannel = ( ) => {
531+ const CHANNEL_NAME = 'minecraft-web-client:server-settings'
532+ const packetStructure = [
533+ 'container' ,
534+ [
535+ {
536+ name : 'settingsJson' ,
537+ type : [ 'pstring' , { countType : 'i16' } ]
538+ } ,
539+ ]
540+ ]
541+
542+ registerChannel ( CHANNEL_NAME , packetStructure , ( data ) => {
543+ try {
544+ const settings = JSON . parse ( data . settingsJson )
545+
546+ if ( typeof settings !== 'object' || settings === null || Array . isArray ( settings ) ) {
547+ console . warn ( 'Invalid settings format: expected an object' )
548+ return
549+ }
550+
551+ let appliedCount = 0
552+ let skippedCount = 0
553+
554+ for ( const [ key , value ] of Object . entries ( settings ) ) {
555+ // Only apply settings that are in the safe list
556+ if ( ! ( key in serverSafeSettings ) ) {
557+ console . warn ( `Skipping unsafe setting: ${ key } ` )
558+ skippedCount ++
559+ continue
560+ }
561+
562+ // Validate that the setting exists in options
563+ if ( ! ( key in options ) ) {
564+ console . warn ( `Setting does not exist: ${ key } ` )
565+ skippedCount ++
566+ continue
567+ }
568+
569+ // Validate type matches
570+ const currentValue = options [ key ]
571+
572+ // For union types, check if value is valid
573+ if ( Array . isArray ( currentValue ) && ! Array . isArray ( value ) ) {
574+ console . warn ( `Type mismatch for setting ${ key } : expected array` )
575+ skippedCount ++
576+ continue
577+ }
578+
579+ // Apply the setting
580+ options [ key ] = value
581+ appliedCount ++
582+ }
583+
584+ console . debug ( `Applied ${ appliedCount } server settings${ skippedCount > 0 ? `, skipped ${ skippedCount } unsafe/invalid settings` : '' } ` )
585+ } catch ( error ) {
586+ console . error ( 'Failed to parse or apply server settings:' , error )
587+ }
588+ } , false ) // Don't wait for world, settings can be applied before world loads
589+ }
590+
528591function getCurrentTopDomain ( ) : string {
529592 const { hostname } = location
530593 // Split hostname into parts
0 commit comments