aboutsummaryrefslogtreecommitdiff
path: root/src/test/kotlin/testutil/ItemResources.kt
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/kotlin/testutil/ItemResources.kt')
-rw-r--r--src/test/kotlin/testutil/ItemResources.kt52
1 files changed, 46 insertions, 6 deletions
diff --git a/src/test/kotlin/testutil/ItemResources.kt b/src/test/kotlin/testutil/ItemResources.kt
index 8631c21..17198f1 100644
--- a/src/test/kotlin/testutil/ItemResources.kt
+++ b/src/test/kotlin/testutil/ItemResources.kt
@@ -1,13 +1,23 @@
package moe.nea.firmament.test.testutil
+import com.mojang.datafixers.DSL
+import com.mojang.datafixers.DataFixUtils
+import com.mojang.datafixers.types.templates.Named
+import com.mojang.serialization.Dynamic
+import com.mojang.serialization.JsonOps
+import net.minecraft.SharedConstants
+import net.minecraft.datafixer.Schemas
+import net.minecraft.datafixer.TypeReferences
import net.minecraft.item.ItemStack
import net.minecraft.nbt.NbtCompound
import net.minecraft.nbt.NbtElement
import net.minecraft.nbt.NbtOps
+import net.minecraft.nbt.NbtString
import net.minecraft.nbt.StringNbtReader
import net.minecraft.registry.RegistryOps
import net.minecraft.text.Text
import net.minecraft.text.TextCodecs
+import moe.nea.firmament.features.debug.ExportedTestConstantMeta
import moe.nea.firmament.test.FirmTestBootstrap
import moe.nea.firmament.util.MC
@@ -28,15 +38,45 @@ object ItemResources {
}
fun getNbtOps(): RegistryOps<NbtElement> = MC.currentOrDefaultRegistries.getOps(NbtOps.INSTANCE)
+ fun tryMigrateNbt(
+ nbtCompound: NbtCompound,
+ typ: DSL.TypeReference,
+ ): NbtElement {
+ val source = nbtCompound.get("source", ExportedTestConstantMeta.CODEC)
+ nbtCompound.remove("source")
+ if (source.isPresent) {
+ val wrappedNbtSource = if (typ == TypeReferences.TEXT_COMPONENT && source.get().dataVersion < 4325) {
+ // Per 1.21.5 text components are wrapped in a string, which firmament unwrapped in the snbt files
+ NbtString.of(
+ NbtOps.INSTANCE.convertTo(JsonOps.INSTANCE, nbtCompound)
+ .toString())
+ } else {
+ nbtCompound
+ }
+ return Schemas.getFixer()
+ .update(
+ typ,
+ Dynamic(NbtOps.INSTANCE, wrappedNbtSource),
+ source.get().dataVersion,
+ SharedConstants.getGameVersion().saveVersion.id
+ ).value
+ }
+ return nbtCompound
+ }
+
fun loadText(name: String): Text {
- return TextCodecs.CODEC.parse(getNbtOps(), loadSNbt("testdata/chat/$name.snbt"))
- .getOrThrow { IllegalStateException("Could not load test chat '$name': $it") }
+ return TextCodecs.CODEC.parse(
+ getNbtOps(),
+ tryMigrateNbt(loadSNbt("testdata/chat/$name.snbt"), TypeReferences.TEXT_COMPONENT)
+ ).getOrThrow { IllegalStateException("Could not load test chat '$name': $it") }
}
fun loadItem(name: String): ItemStack {
- // TODO: make the load work with enchantments
- // TODO: use DFU to load older items
- return ItemStack.CODEC.parse(getNbtOps(), loadSNbt("testdata/items/$name.snbt"))
- .getOrThrow { IllegalStateException("Could not load test item '$name': $it") }
+ try {
+ val itemNbt = loadSNbt("testdata/items/$name.snbt")
+ return ItemStack.CODEC.parse(getNbtOps(), tryMigrateNbt(itemNbt, TypeReferences.ITEM_STACK)).orThrow
+ } catch (ex: Exception) {
+ throw RuntimeException("Could not load item resource '$name'", ex)
+ }
}
}