Skip to content

Commit 125f19e

Browse files
committed
update SNBT & settings ui
1 parent a5fecb9 commit 125f19e

File tree

3 files changed

+53716
-53441
lines changed

3 files changed

+53716
-53441
lines changed

src/ui/dialogs/settings.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import { tl } from '../../util/intl'
44
import { ERROR } from '../../util/errors'
55
import events from '../../constants/events'
66
import React, { useEffect, useRef, useState } from 'react'
7-
import { DefaultSettings, settings, ForeignSettingTranslationKeys } from '../../settings'
7+
import {
8+
DefaultSettings,
9+
settings,
10+
ForeignSettingTranslationKeys,
11+
} from '../../settings'
812

913
const dialog = electron.dialog
1014
let updateSettingsUiActions = {}
@@ -185,6 +189,33 @@ const RenderTemplates = {
185189
</>
186190
)
187191
},
192+
number({ value, setValue, namespace, name, children, forceRerender }) {
193+
return (
194+
<>
195+
{children}
196+
<input
197+
type="number"
198+
id={`aj.setting.${namespace}.${name}`}
199+
value={value}
200+
onChange={(e) => {
201+
setValue(e.target.value)
202+
}}
203+
onBlur={(e) => {
204+
try {
205+
let value = Number(e.target.value)
206+
settings[namespace][name] = !Number.isNaN(value)
207+
? value
208+
: undefined
209+
} catch (e) {
210+
forceRerender()
211+
}
212+
}}
213+
className="dark_bordered"
214+
style={{ width: 'calc(100% - 18px)' }}
215+
/>
216+
</>
217+
)
218+
},
188219
}
189220
export function registerSettingRenderer(type, renderer) {
190221
if (Reflect.has(RenderTemplates, type)) {

src/util/SNBT.ts

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,112 @@ export class SNBTTag {
339339
return this.value ? 'true' : 'false'
340340
}
341341
}
342+
toHighlightString(
343+
highlighters: Record<string, (string) => string>,
344+
exclude_type?: true
345+
) {
346+
function NO_SYNTAX_HIGHLIGHTER(s) {
347+
return s
348+
}
349+
function getFormatter(name) {
350+
return name in highlighters
351+
? highlighters[name]
352+
: NO_SYNTAX_HIGHLIGHTER
353+
}
354+
const STRING_FORMATTER = getFormatter('string')
355+
const NUMBER_FORMATTER = getFormatter('number')
356+
const BOOLEAN_FORMATTER = getFormatter('boolean')
357+
const BRACKET_FORMATTER = getFormatter('brackets')
358+
const ARRAY_FORMATTER = getFormatter('list')
359+
const TYPE_BYTE = getFormatter('type_byte')
360+
const TYPE_SHORT = getFormatter('type_short')
361+
const TYPE_INT = getFormatter('type_int_list')
362+
switch (this.type) {
363+
case SNBTTagType.END:
364+
throw new Error('Cannot convert END tag to string')
365+
case SNBTTagType.BYTE:
366+
return (
367+
NUMBER_FORMATTER(this.value.toString()) +
368+
TYPE_BYTE(exclude_type ? '' : 'b')
369+
)
370+
case SNBTTagType.SHORT:
371+
return (
372+
NUMBER_FORMATTER(this.value.toString()) +
373+
TYPE_SHORT(exclude_type ? '' : 's')
374+
)
375+
case SNBTTagType.INT:
376+
return NUMBER_FORMATTER(this.value.toString())
377+
case SNBTTagType.LONG:
378+
return NUMBER_FORMATTER(this.value.toString())
379+
case SNBTTagType.FLOAT:
380+
return NUMBER_FORMATTER(this.value.toString())
381+
case SNBTTagType.DOUBLE:
382+
return NUMBER_FORMATTER(
383+
this.value.toString() + Number.isInteger(this.value)
384+
? '.0'
385+
: ''
386+
)
387+
case SNBTTagType.STRING:
388+
return STRING_FORMATTER(SNBTUtil.stringify(this.value))
389+
case SNBTTagType.COMPOUND:
390+
let entries = Object.entries(this.value as Record<any, SNBTTag>)
391+
if (!entries.length) return BRACKET_FORMATTER(`{}`)
392+
return (
393+
BRACKET_FORMATTER('{') +
394+
'\n' +
395+
entries
396+
.map(([key, value]) =>
397+
`${key}:${value.toHighlightString(highlighters)}`
398+
.split('\n')
399+
.map((_) => ` ${_}`)
400+
.join('\n')
401+
)
402+
.join(',\n') +
403+
'\n' +
404+
BRACKET_FORMATTER('}')
405+
)
406+
case SNBTTagType.INT_ARRAY: {
407+
let items = this.value.map((item) =>
408+
item.toHighlightString(highlighters)
409+
)
410+
let combined = items.join(',')
411+
let isIndented = items.indexOf('\n') > -1
412+
if (combined.length > 16) isIndented = true
413+
if (isIndented) {
414+
return `${ARRAY_FORMATTER('[')}${TYPE_INT('I')};\n${items
415+
.map((_) => ` ${_}`)
416+
.join(',\n')}\n${ARRAY_FORMATTER(']')}`
417+
}
418+
return (
419+
`${ARRAY_FORMATTER('[')}${TYPE_INT('I')};` +
420+
combined +
421+
ARRAY_FORMATTER(']')
422+
)
423+
}
424+
case SNBTTagType.BYTE_ARRAY:
425+
case SNBTTagType.LIST:
426+
case SNBTTagType.LONG_ARRAY: {
427+
let items = this.value.map((item) =>
428+
item.toHighlightString(highlighters)
429+
)
430+
let combined = items.join(', ')
431+
let isIndented = combined.indexOf('\n') > -1
432+
if (combined.length > 16) isIndented = true
433+
if (isIndented) {
434+
return `${ARRAY_FORMATTER('[')}\n${items
435+
.map((_) =>
436+
_.split('\n')
437+
.map((i) => ` ${i}`)
438+
.join('\n')
439+
)
440+
.join(',\n')}\n${ARRAY_FORMATTER(']')}`
441+
}
442+
return ARRAY_FORMATTER('[') + combined + ARRAY_FORMATTER(']')
443+
}
444+
case SNBTTagType.BOOLEAN:
445+
return BOOLEAN_FORMATTER(this.value ? 'true' : 'false')
446+
}
447+
}
342448
// clone the tag taking into account the type
343449
// the LIST, INT_ARRAY, BYTE_ARRAY, and LONG_ARRAY clone each item into a new list,
344450
// the COMPOUND copies each entry and makes a new compound

tests/SNBT.nnb

Lines changed: 53578 additions & 53440 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)