mirror of
https://github.com/misode/misode.github.io.git
synced 2026-04-23 07:10:41 +00:00
Add snbt output format
This commit is contained in:
@@ -153,7 +153,7 @@ export function SourcePanel({ name, model, blockStates, doCopy, doDownload, doIm
|
||||
useEffect(() => {
|
||||
if (!editor.current || !retransform.current) return
|
||||
if (!highlighting || braceLoaded) {
|
||||
editor.current.configure(indent, format)
|
||||
editor.current.configure(indent, format === 'snbt' ? 'yaml' : format)
|
||||
retransform.current()
|
||||
}
|
||||
}, [indent, format, highlighting, braceLoaded])
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { NbtByte, NbtCompound, NbtDouble, NbtInt, NbtList, NbtString, NbtTag } from 'deepslate'
|
||||
import yaml from 'js-yaml'
|
||||
import { Store } from '../Store.js'
|
||||
|
||||
@@ -26,6 +27,10 @@ const FORMATS: Record<string, {
|
||||
},
|
||||
stringify: (v, i) => (commentJson ?? JSON).stringify(v, null, i) + '\n',
|
||||
},
|
||||
snbt: {
|
||||
parse: async (v) => NbtTag.fromString(v).toSimplifiedJson(),
|
||||
stringify: (v, _i) => jsonToNbt(v).toPrettyString(),
|
||||
},
|
||||
yaml: {
|
||||
parse: async (v) => yaml.load(v),
|
||||
stringify: (v, i) => yaml.dump(v, {
|
||||
@@ -35,6 +40,28 @@ const FORMATS: Record<string, {
|
||||
},
|
||||
}
|
||||
|
||||
function jsonToNbt(value: unknown): NbtTag {
|
||||
if (typeof value === 'string') {
|
||||
return new NbtString(value)
|
||||
}
|
||||
if (typeof value === 'number') {
|
||||
return Number.isInteger(value) ? new NbtInt(value) : new NbtDouble(value)
|
||||
}
|
||||
if (typeof value === 'boolean') {
|
||||
return new NbtByte(value)
|
||||
}
|
||||
if (Array.isArray(value)) {
|
||||
return new NbtList(value.map(jsonToNbt))
|
||||
}
|
||||
if (typeof value === 'object' && value !== null) {
|
||||
return new NbtCompound(
|
||||
new Map(Object.entries(value ?? {})
|
||||
.map(([k, v]) => [k, jsonToNbt(v)]))
|
||||
)
|
||||
}
|
||||
throw new Error(`Could not convert ${value} to NBT`)
|
||||
}
|
||||
|
||||
export function stringifySource(data: unknown, format?: string, indent?: string) {
|
||||
return FORMATS[format ?? Store.getFormat()].stringify(data, INDENTS[indent ?? Store.getIndent()])
|
||||
}
|
||||
|
||||
@@ -48,6 +48,7 @@
|
||||
"fields": "Fields",
|
||||
"follow_reference": "Follow reference",
|
||||
"format.json": "JSON",
|
||||
"format.snbt": "SNBT",
|
||||
"format.yaml": "YAML",
|
||||
"generate_new_color": "Generate new color",
|
||||
"generate_new_seed": "Generate new seed",
|
||||
|
||||
Reference in New Issue
Block a user