Skip to content

Commit 10a8ef9

Browse files
committed
NEW add a custom packet minecraft-web-client:server-settings that can change some client settings from a server
1 parent f6cbd58 commit 10a8ef9

2 files changed

Lines changed: 106 additions & 0 deletions

File tree

src/customChannels.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { getThreeJsRendererMethods } from 'renderer/viewer/three/threeJsMethods'
44
import { options } from './optionsStorage'
55
import { jeiCustomCategories } from './inventoryWindows'
66
import { registerIdeChannels } from './core/ideChannels'
7+
import { serverSafeSettings } from './defaultOptions'
78

89
export 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
}
526528
window.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+
528591
function getCurrentTopDomain (): string {
529592
const { hostname } = location
530593
// Split hostname into parts

src/defaultOptions.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,46 @@ function getTouchControlsSize () {
158158
sneak: 36,
159159
}
160160
}
161+
162+
/**
163+
* Map of settings that are safe to change from the server.
164+
* These are rendering/display/input preferences that don't affect critical client functionality.
165+
* Settings like modsSupport, customChannels, or security-related options are excluded.
166+
*/
167+
export const serverSafeSettings: Partial<Record<keyof typeof defaultOptions, true>> = {
168+
renderEars: true,
169+
viewBobbing: true,
170+
mouseRawInput: true,
171+
preciseMouseInput: true,
172+
showHand: true,
173+
fov: true,
174+
defaultPerspective: true,
175+
volume: true,
176+
musicVolume: true,
177+
enableMusic: true,
178+
smoothLighting: true,
179+
starfieldRendering: true,
180+
defaultSkybox: true,
181+
dayCycleAndLighting: true,
182+
showChunkBorders: true,
183+
renderDebug: true,
184+
highlightBlockColor: true,
185+
displayBossBars: true,
186+
showMinimap: true,
187+
autoJump: true,
188+
chatVanillaRestrictions: true,
189+
chatPingExtension: true,
190+
chatSpellCheckEnabled: true,
191+
renderEntities: true,
192+
displayRecordButton: true,
193+
topRightTimeDisplay: true,
194+
guiScale: true,
195+
chatWidth: true,
196+
chatHeight: true,
197+
chatScale: true,
198+
loadPlayerSkins: true,
199+
disableBlockEntityTextures: true,
200+
neighborChunkUpdates: true,
201+
newVersionsLighting: true,
202+
showCursorBlockInSpectator: true,
203+
}

0 commit comments

Comments
 (0)