prevent nbt editing from causing crashes on foci and such

This commit is contained in:
yrsegal@gmail.com 2022-06-12 22:33:48 -04:00
parent fef331d8e0
commit 10c9d074b8
2 changed files with 57 additions and 52 deletions

View file

@ -143,7 +143,7 @@ class SpellDatum<T : Any> private constructor(val payload: T) {
fun fromNBT(nbt: CompoundTag, world: ServerLevel): SpellDatum<*> {
val keys = nbt.allKeys
if (keys.size != 1)
throw IllegalArgumentException("Expected exactly one kv pair: $nbt")
return SpellDatum(Widget.GARBAGE) // Invalid iota format
return when (val key = keys.iterator().next()) {
TAG_ENTITY -> {
@ -164,7 +164,7 @@ class SpellDatum<T : Any> private constructor(val payload: T) {
TAG_PATTERN -> {
SpellDatum(HexPattern.fromNBT(nbt.getCompound(TAG_PATTERN)))
}
else -> throw IllegalArgumentException("Unknown key $key: $nbt")
else -> SpellDatum(Widget.GARBAGE) // Invalid iota type
}
}
@ -181,62 +181,66 @@ class SpellDatum<T : Any> private constructor(val payload: T) {
@JvmStatic
fun displayFromNBT(nbt: CompoundTag): Component {
val keys = nbt.allKeys
if (keys.size != 1)
throw IllegalArgumentException("Expected exactly one kv pair: $nbt")
val out = "".asTextComponent
when (val key = keys.iterator().next()) {
TAG_DOUBLE -> out += String.format(
"%.4f",
nbt.getDouble(TAG_DOUBLE)
).green
TAG_VEC3 -> {
val vec = vecFromNBT(nbt.getLongArray(key))
// the focus color is really more red, but we don't want to show an error-y color
out += String.format(
"(%.2f, %.2f, %.2f)",
vec.x,
vec.y,
vec.z
).lightPurple
}
TAG_LIST -> {
out += "[".white
val arr = nbt.getList(key, Tag.TAG_COMPOUND)
for ((i, subtag) in arr.withIndex()) {
out += displayFromNBT(subtag.asCompound)
if (i != arr.lastIndex) {
out += ", ".white
}
if (keys.size != 1)
out += "hexcasting.spelldata.unknown".asTranslatedComponent.white
else {
when (val key = keys.iterator().next()) {
TAG_DOUBLE -> out += String.format(
"%.4f",
nbt.getDouble(TAG_DOUBLE)
).green
TAG_VEC3 -> {
val vec = vecFromNBT(nbt.getLongArray(key))
// the focus color is really more red, but we don't want to show an error-y color
out += String.format(
"(%.2f, %.2f, %.2f)",
vec.x,
vec.y,
vec.z
).lightPurple
}
TAG_LIST -> {
out += "[".white
out += "]".white
}
TAG_WIDGET -> {
val widget = Widget.fromString(nbt.getString(key))
val arr = nbt.getList(key, Tag.TAG_COMPOUND)
for ((i, subtag) in arr.withIndex()) {
out += displayFromNBT(subtag.asCompound)
if (i != arr.lastIndex) {
out += ", ".white
}
}
out += if (widget == Widget.GARBAGE)
"arimfexendrapuse".darkGray.obfuscated
else
widget.toString().darkPurple
out += "]".white
}
TAG_WIDGET -> {
val widget = Widget.fromString(nbt.getString(key))
out += if (widget == Widget.GARBAGE)
"arimfexendrapuse".darkGray.obfuscated
else
widget.toString().darkPurple
}
TAG_PATTERN -> {
val pat = HexPattern.fromNBT(nbt.getCompound(TAG_PATTERN))
var angleDesc = pat.anglesSignature()
if (angleDesc.isNotBlank()) angleDesc = " $angleDesc";
out += "HexPattern(".gold
out += "${pat.startDir}$angleDesc".white
out += ")".gold
}
TAG_ENTITY -> {
val subtag = nbt.getCompound(TAG_ENTITY)
val json = subtag.getString(TAG_ENTITY_NAME_CHEATY)
// handle pre-0.5.0 foci not having the tag
out += Component.Serializer.fromJson(json)?.aqua
?: "hexcasting.spelldata.entity.whoknows".asTranslatedComponent.white
}
else -> {
out += "hexcasting.spelldata.unknown".asTranslatedComponent.white
}
}
TAG_PATTERN -> {
val pat = HexPattern.fromNBT(nbt.getCompound(TAG_PATTERN))
var angleDesc = pat.anglesSignature()
if (angleDesc.isNotBlank()) angleDesc = " $angleDesc";
out += "HexPattern(".gold
out += "${pat.startDir}$angleDesc".white
out += ")".gold
}
TAG_ENTITY -> {
val subtag = nbt.getCompound(TAG_ENTITY)
val json = subtag.getString(TAG_ENTITY_NAME_CHEATY)
// handle pre-0.5.0 foci not having the tag
out += Component.Serializer.fromJson(json)?.aqua
?: "hexcasting.spelldata.entity.whoknows".asTranslatedComponent.white
}
else -> throw IllegalArgumentException("Unknown key $key: $nbt")
}
return out
}

View file

@ -135,6 +135,7 @@
"hexcasting.tooltip.brainsweep.product": "Mindless Body",
"hexcasting.spelldata.onitem": "Contains: %s",
"hexcasting.spelldata.anything": "Anything",
"hexcasting.spelldata.unknown": "Unknown data (this is a bug)",
"hexcasting.spelldata.entity.whoknows": "An Entity (this should only show up if this was stored before the 0.5.0 update, use Scribe's Reflection, Scribe's Gambit to fix)",
"hexcasting.spelldata.akashic.nopos": "The owning record does not know of any iota here (this is a bug)",