diff --git a/src/main/java/com/gtnewhorizons/postea/utility/VersionedPass.java b/src/main/java/com/gtnewhorizons/postea/utility/VersionedPass.java index 9e3dd90..457bc0f 100644 --- a/src/main/java/com/gtnewhorizons/postea/utility/VersionedPass.java +++ b/src/main/java/com/gtnewhorizons/postea/utility/VersionedPass.java @@ -24,8 +24,8 @@ public final class VersionedPass { } /// Runs `step` for every transformer whose stamp on `root` differs from its current version. The steps work on a - /// copy of `root`, which replaces the contents of `root` once every step has succeeded. A failing step throws the - /// [#failure] for `subjectName` and `subject`, and leaves `root` as read. + /// copy of `root`, which is stamped with the current versions and replaces the contents of `root` once every step + /// has succeeded. A failing step throws the [#failure] for `subjectName` and `subject`, and leaves `root` as read. /// /// @return a copy of the stamps `root` was read with, or null if unstamped public static NBTTagCompound runAtomically(NBTTagCompound root, Iterable transformers, @@ -47,7 +47,10 @@ public final class VersionedPass { throw failure(t, transformer, stored, current, subjectName, subject); } } - if (working != null) replaceContents(root, working); + if (working != null) { + VersionStamps.write(working, carried, transformers); + replaceContents(root, working); + } return carried; } diff --git a/src/test/java/com/gtnewhorizons/postea/api/VersionedPassTest.java b/src/test/java/com/gtnewhorizons/postea/api/VersionedPassTest.java new file mode 100644 index 0000000..6cf8d28 --- /dev/null +++ b/src/test/java/com/gtnewhorizons/postea/api/VersionedPassTest.java @@ -0,0 +1,33 @@ +package com.gtnewhorizons.postea.api; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.Collections; + +import net.minecraft.nbt.NBTTagCompound; + +import org.junit.jupiter.api.Test; + +import com.gtnewhorizons.postea.utility.VersionStamps; +import com.gtnewhorizons.postea.utility.VersionedPass; + +class VersionedPassTest { + + /// Data saved while a later step of the same load fails must not be transformed a second time on the next load. + @Test + void aTransformedRootCarriesTheCurrentStamps() { + StubTransformer transformer = new StubTransformer("mod:t", 2); + NBTTagCompound root = new NBTTagCompound(); + root.setString("data", "old"); + + VersionedPass.runAtomically( + root, + Collections.singletonList(transformer), + "Player", + "someone", + (t, data, stored) -> data.setString("data", "new")); + + assertEquals("new", root.getString("data")); + assertEquals(2, VersionStamps.stored(VersionStamps.read(root), "mod:t")); + } +}