From 10c9d074b8d2c5dceaf2467f61cbc1ef8cfdb0e8 Mon Sep 17 00:00:00 2001 From: "yrsegal@gmail.com" Date: Sun, 12 Jun 2022 22:33:48 -0400 Subject: [PATCH] prevent nbt editing from causing crashes on foci and such --- .../petrak/hexcasting/api/spell/SpellDatum.kt | 108 +++++++++--------- .../assets/hexcasting/lang/en_us.json | 1 + 2 files changed, 57 insertions(+), 52 deletions(-) diff --git a/Common/src/main/java/at/petrak/hexcasting/api/spell/SpellDatum.kt b/Common/src/main/java/at/petrak/hexcasting/api/spell/SpellDatum.kt index a7405ab0..a2edd790 100644 --- a/Common/src/main/java/at/petrak/hexcasting/api/spell/SpellDatum.kt +++ b/Common/src/main/java/at/petrak/hexcasting/api/spell/SpellDatum.kt @@ -143,7 +143,7 @@ class SpellDatum 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 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 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 } diff --git a/Common/src/main/resources/assets/hexcasting/lang/en_us.json b/Common/src/main/resources/assets/hexcasting/lang/en_us.json index 7a70ad00..3db36fae 100644 --- a/Common/src/main/resources/assets/hexcasting/lang/en_us.json +++ b/Common/src/main/resources/assets/hexcasting/lang/en_us.json @@ -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)",