blob: e773927491030e36a64cc7a70f63cdd88ae521e5 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
package moe.nea.firmament.util.mc
import net.minecraft.nbt.NbtByte
import net.minecraft.nbt.NbtByteArray
import net.minecraft.nbt.NbtCompound
import net.minecraft.nbt.NbtDouble
import net.minecraft.nbt.NbtElement
import net.minecraft.nbt.NbtEnd
import net.minecraft.nbt.NbtFloat
import net.minecraft.nbt.NbtInt
import net.minecraft.nbt.NbtIntArray
import net.minecraft.nbt.NbtList
import net.minecraft.nbt.NbtLong
import net.minecraft.nbt.NbtLongArray
import net.minecraft.nbt.NbtShort
import net.minecraft.nbt.NbtString
import net.minecraft.nbt.visitor.NbtElementVisitor
class SNbtFormatter private constructor() : NbtElementVisitor {
private val result = StringBuilder()
private var indent = 0
private fun writeIndent() {
result.append("\t".repeat(indent))
}
private fun pushIndent() {
indent++
}
private fun popIndent() {
indent--
}
fun apply(element: NbtElement): StringBuilder {
element.accept(this)
return result
}
override fun visitString(element: NbtString) {
result.append(NbtString.escape(element.asString()))
}
override fun visitByte(element: NbtByte) {
result.append(element.numberValue()).append("b")
}
override fun visitShort(element: NbtShort) {
result.append(element.shortValue()).append("s")
}
override fun visitInt(element: NbtInt) {
result.append(element.intValue())
}
override fun visitLong(element: NbtLong) {
result.append(element.longValue()).append("L")
}
override fun visitFloat(element: NbtFloat) {
result.append(element.floatValue()).append("f")
}
override fun visitDouble(element: NbtDouble) {
result.append(element.doubleValue()).append("d")
}
private fun visitArrayContents(array: List<NbtElement>) {
array.forEachIndexed { index, element ->
writeIndent()
element.accept(this)
if (array.size != index + 1) {
result.append(",")
}
result.append("\n")
}
}
private fun writeArray(arrayTypeTag: String, array: List<NbtElement>) {
result.append("[").append(arrayTypeTag).append("\n")
pushIndent()
visitArrayContents(array)
popIndent()
writeIndent()
result.append("]")
}
override fun visitByteArray(element: NbtByteArray) {
writeArray("B;", element)
}
override fun visitIntArray(element: NbtIntArray) {
writeArray("I;", element)
}
override fun visitLongArray(element: NbtLongArray) {
writeArray("L;", element)
}
override fun visitList(element: NbtList) {
writeArray("", element)
}
override fun visitCompound(compound: NbtCompound) {
result.append("{\n")
pushIndent()
val keys = compound.keys.sorted()
keys.forEachIndexed { index, key ->
writeIndent()
val element = compound[key] ?: error("Key '$key' found but not present in compound: $compound")
val escapedName = if (key.matches(SIMPLE_NAME)) key else NbtString.escape(key)
result.append(escapedName).append(": ")
element.accept(this)
if (keys.size != index + 1) {
result.append(",")
}
result.append("\n")
}
popIndent()
writeIndent()
result.append("}")
}
override fun visitEnd(element: NbtEnd) {
result.append("END")
}
companion object {
fun prettify(nbt: NbtElement): String {
return SNbtFormatter().apply(nbt).toString()
}
fun NbtElement.toPrettyString() = prettify(this)
private val SIMPLE_NAME = "[A-Za-z0-9._+-]+".toRegex()
}
}
|