import Translate from '@docusaurus/Translate'; import HeroBanner from '@site/src/components/HeroBanner'; import HeroBackground from '@site/static/img/site/split-background.webp'; import Layout from '@theme/Layout'; import styles from './utilities.module.scss'; import { useRef, useState } from 'react'; const ConfigEditorPage: React.FC = () => { const [config, setConfig] = useState(''); const [parsedConfig, setParsedConfig] = useState({}); const editedConfig = useRef({}); const uploadRef = useRef(null); const loadDefault = async () => { try { const response = await fetch('https://raw.githubusercontent.com/GeyserMC/Geyser/master/core/src/main/resources/config.yml'); if (!response.ok) { throw new Error('Network response was not ok'); } const text = await response.text(); handleConfigLoad(text); } catch (error) { console.error('Failed to fetch default config:', error); } } const handleUpload = async (e) => { const fileName = e.target.files[0]; // Makse sure the file is a .yml file if (!fileName.name.endsWith('.yml')) { alert('Please select a .yml config file!'); return; } // Read the file const fileReader = new FileReader(); fileReader.onload = (e) => { handleConfigLoad(e.target.result as string); } fileReader.readAsText(fileName); } const handleConfigLoad = (config: string) => { setConfig(config); setParsedConfig(loadConfig(config)); } const loadConfig = (config: string) => { const lines = config.split('\n'); let currentSection = ''; let currentComment = ''; const newConfig = {}; // Parse each line of the config lines.forEach(line => { // Reset the section if we are no indented if (!line.startsWith(' ')) { currentSection = ''; } // Ignore any empty lines line = line.trim() if (line === '') { currentComment = ''; return; } // Check if we are in a comment if (line.startsWith('#')) { currentComment += line.replace(/^# ?/i, '') + '
'; return; } // Ignore these lines since they will fail to parse if (!line.includes(':')) { return; } // Get the key and value const splitLine = line.split(':'); splitLine[0] = splitLine[0].trim(); splitLine[1] = splitLine[1].trim(); // Check if we are in a new section if (line.endsWith(':')) { currentSection = splitLine[0]; currentComment = ''; return; } // Ignore user auths section if (currentSection === 'userAuths' || currentSection === 'saved-user-logins') { return; } // Add the config option to the config object newConfig[currentSection] = newConfig[currentSection] || {}; newConfig[currentSection][splitLine[0]] = { desc: currentComment, value: splitLine[1] }; // Reset the comment currentComment = ''; }) return newConfig; } const generateHTML = (config: any) => { return Object.keys(config).map(configKey => (
{configKey === '' ? '.' : configKey}
{Object.keys(config[configKey]).map(configOption => { const configOptionInfo = config[configKey][configOption]; const configOptionKey = configKey === '' ? configOption : `${configKey}.${configOption}`; return (

{configOption}

{getInput(configOptionKey, configOptionInfo.value)}

) })}
)) } const handleExport = () => { let newConfig = config; Object.entries(editedConfig.current).forEach(([key, value]) => { const keys = key.split('.'); let currentObj = parsedConfig; keys.forEach((nestedKey, index) => { if (currentObj.hasOwnProperty(nestedKey)) { if (index === keys.length - 1) { const oldValue = currentObj[nestedKey].value; newConfig = newConfig.replace(` ${nestedKey}: ${oldValue}`, ` ${nestedKey}: ${value}`); } else { currentObj = currentObj[nestedKey]; } } }); }); // Trigger a download of the final config downloadString(newConfig, 'text/plain', 'config.yml'); } function getInput(name, value) { const handleChange = (event) => { const { id, value, type, checked } = event.target; editedConfig.current = { ...editedConfig.current, [id]: type === 'checkbox' ? checked.toString() : value }; } switch (name) { // Handle all the dropdowns case 'remote.auth-type': return ( ); case 'show-cooldown': return ( ); case 'emote-offhand-workaround': return ( ); case 'config-version': return ; case 'metrics.uuid': return ; default: // Handle all the other inputs if (value.toLowerCase() === 'true' || value.toLowerCase() === 'false') { // Boolean return (
); } else if (!isNaN(value)) { // Number return ; } else { // String return ; } } } // From https://gist.github.com/danallison/3ec9d5314788b337b682 function downloadString(text, fileType, fileName) { const blob = new Blob([text], { type: fileType }) const a = document.createElement('a') a.download = fileName a.href = URL.createObjectURL(blob) a.dataset.downloadurl = [fileType, a.download, a.href].join(':') a.style.display = 'none' document.body.appendChild(a) a.click() document.body.removeChild(a) setTimeout(() => { URL.revokeObjectURL(a.href) }, 1500) } const NoConfigSection = () => ( <>

No config selected.

Upload a file or get started with the default config!

) const EditorView = () => ( <>
{generateHTML(parsedConfig)} ) return ( <> Config Editor} subheading={Edit your Geyser configuration.} backgroundImage={HeroBackground} />
{Object.keys(parsedConfig).length === 0 ? : }
) } export default function ConfigEditor(): JSX.Element { return (
) }