mirror of
https://github.com/GTNewHorizons/Postea.git
synced 2026-11-25 22:44:51 +00:00
Keep version stamps of absent transformers on rebuilt custom data roots
This commit is contained in:
4 files changed
+95
-45
No files matched your search
@@ -14,27 +14,30 @@ import cpw.mods.fml.common.FMLCommonHandler;
|
||||
/// Applies [IVersionedTransformer]s to a mod's custom storage files.
|
||||
///
|
||||
/// Call [#transform] once on the root read from disk, before consuming it, and persist that root, stamps included,
|
||||
/// when writing the storage back. A save path that rebuilds its NBT from scratch calls [#stamp] on the fresh root
|
||||
/// instead.
|
||||
/// when writing the storage back. A save path that rebuilds its NBT from scratch keeps [#savedStamps] from the read and
|
||||
/// passes them to [#stamp(NBTTagCompound, NBTTagCompound)] on the fresh root, so the stamps of transformers missing at
|
||||
/// save time survive.
|
||||
///
|
||||
/// Custom data belongs to a world, so both methods do nothing unless a server runs in this JVM.
|
||||
/// Custom data belongs to a world, so [#transform] and [#stamp] do nothing unless a server runs in this JVM.
|
||||
public abstract class CustomDataReplacementManager {
|
||||
|
||||
/// Transforms `root` in place through every registered transformer whose stamp differs from its current
|
||||
/// version, then writes the updated stamps onto `root`. Throws [IllegalStateException] when called before the
|
||||
/// world's saved id mappings were applied.
|
||||
public static void transform(String storageId, NBTTagCompound root) {
|
||||
// Custom data is only transformed while a server runs. Not getEffectiveSide(): mods save from worker
|
||||
// threads, which 1.7.10 reports as client.
|
||||
if (FMLCommonHandler.instance()
|
||||
.getMinecraftServerInstance() == null) return;
|
||||
if (!serverRunning()) return;
|
||||
if (!IDRegistry.isMappingApplied()) {
|
||||
throw new IllegalStateException(
|
||||
"Custom data " + storageId
|
||||
+ " was offered for transformation before the world's id mappings were applied");
|
||||
}
|
||||
transform(storageId, root, VersionedReplacementManager.transformers());
|
||||
}
|
||||
|
||||
/// [#transform(String, NBTTagCompound)] through `transformers`, without the server and id mapping checks.
|
||||
static void transform(String storageId, NBTTagCompound root, Iterable<IVersionedTransformer> transformers) {
|
||||
NBTTagCompound stamps = VersionStamps.read(root);
|
||||
for (IVersionedTransformer transformer : VersionedReplacementManager.transformers()) {
|
||||
for (IVersionedTransformer transformer : transformers) {
|
||||
int current = transformer.currentVersion();
|
||||
int stored = VersionStamps.stored(stamps, transformer.key());
|
||||
if (stored == current) continue;
|
||||
@@ -47,13 +50,34 @@ public abstract class CustomDataReplacementManager {
|
||||
ChunkFixerUtility.describe(t, transformer, stored, current, "Custom data", storageId));
|
||||
}
|
||||
}
|
||||
VersionStamps.write(root, stamps, VersionedReplacementManager.transformers());
|
||||
VersionStamps.write(root, stamps, transformers);
|
||||
}
|
||||
|
||||
/// Stamps a freshly built root with every registered transformer's current version.
|
||||
/// Same as [#stamp(NBTTagCompound, NBTTagCompound)] with a null `carried`.
|
||||
public static void stamp(NBTTagCompound root) {
|
||||
if (FMLCommonHandler.instance()
|
||||
.getMinecraftServerInstance() == null) return;
|
||||
VersionStamps.write(root, VersionStamps.read(root), VersionedReplacementManager.transformers());
|
||||
stamp(root, null);
|
||||
}
|
||||
|
||||
/// Stamps `root` with every registered transformer's current version, keeping the stamps in `carried` for
|
||||
/// transformers not registered now. A null `carried` keeps the stamps already on `root` instead.
|
||||
public static void stamp(NBTTagCompound root, NBTTagCompound carried) {
|
||||
if (!serverRunning()) return;
|
||||
stamp(root, carried, VersionedReplacementManager.transformers());
|
||||
}
|
||||
|
||||
static void stamp(NBTTagCompound root, NBTTagCompound carried, Iterable<IVersionedTransformer> transformers) {
|
||||
VersionStamps.write(root, carried == null ? VersionStamps.read(root) : carried, transformers);
|
||||
}
|
||||
|
||||
/// A copy of the stamps on a root read from disk, or null when it carries none. Call after [#transform].
|
||||
public static NBTTagCompound savedStamps(NBTTagCompound root) {
|
||||
NBTTagCompound stamps = VersionStamps.read(root);
|
||||
return stamps == null ? null : (NBTTagCompound) stamps.copy();
|
||||
}
|
||||
|
||||
private static boolean serverRunning() {
|
||||
// Not getEffectiveSide(): mods save from worker threads, which 1.7.10 reports as client.
|
||||
return FMLCommonHandler.instance()
|
||||
.getMinecraftServerInstance() != null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.gtnewhorizons.postea.api;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.gtnewhorizons.postea.utility.VersionStamps;
|
||||
|
||||
class CustomDataReplacementManagerTest {
|
||||
|
||||
@Test
|
||||
void aRebuiltRootKeepsTheStampOfATransformerThatIsNotRegistered() {
|
||||
StubTransformer present = new StubTransformer("mod:present", 2);
|
||||
StubTransformer absent = new StubTransformer("mod:absent", 5);
|
||||
NBTTagCompound original = new NBTTagCompound();
|
||||
CustomDataReplacementManager.transform("mod:storage", original, Arrays.asList(present, absent));
|
||||
NBTTagCompound saved = CustomDataReplacementManager.savedStamps(original);
|
||||
|
||||
present.version = 3;
|
||||
NBTTagCompound rebuilt = new NBTTagCompound();
|
||||
CustomDataReplacementManager.stamp(rebuilt, saved, Collections.singletonList(present));
|
||||
|
||||
NBTTagCompound stamps = VersionStamps.read(rebuilt);
|
||||
assertEquals(3, VersionStamps.stored(stamps, "mod:present"));
|
||||
assertEquals(5, VersionStamps.stored(stamps, "mod:absent"));
|
||||
assertEquals(2, VersionStamps.stored(saved, "mod:present"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.gtnewhorizons.postea.api;
|
||||
|
||||
/// An [IVersionedTransformer] that changes nothing, at a version the test sets.
|
||||
public final class StubTransformer implements IVersionedTransformer {
|
||||
|
||||
private final String key;
|
||||
public int version;
|
||||
|
||||
public StubTransformer(String key, int version) {
|
||||
this.key = key;
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String key() {
|
||||
return key;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int currentVersion() {
|
||||
return version;
|
||||
}
|
||||
}
|
||||
@@ -9,25 +9,10 @@ import net.minecraft.nbt.NBTTagCompound;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.gtnewhorizons.postea.api.IVersionedTransformer;
|
||||
import com.gtnewhorizons.postea.api.StubTransformer;
|
||||
|
||||
class VersionStampsTest {
|
||||
|
||||
private static IVersionedTransformer transformer(String key, int version) {
|
||||
return new IVersionedTransformer() {
|
||||
|
||||
@Override
|
||||
public String key() {
|
||||
return key;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int currentVersion() {
|
||||
return version;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
void anUnstampedContainerReadsAsUnstamped() {
|
||||
NBTTagCompound container = new NBTTagCompound();
|
||||
@@ -36,26 +21,11 @@ class VersionStampsTest {
|
||||
assertEquals(VersionStamps.UNSTAMPED, VersionStamps.stored(null, "mod:key"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void writeStampsRegisteredKeysAtTheirCurrentVersionAndCarriesUnknownKeys() {
|
||||
NBTTagCompound carried = new NBTTagCompound();
|
||||
carried.setInteger("absentmod:key", 7);
|
||||
carried.setInteger("mod:key", 2);
|
||||
NBTTagCompound container = new NBTTagCompound();
|
||||
|
||||
VersionStamps.write(container, carried, Collections.singletonList(transformer("mod:key", 3)));
|
||||
|
||||
NBTTagCompound stamps = VersionStamps.read(container);
|
||||
assertEquals(3, VersionStamps.stored(stamps, "mod:key"));
|
||||
assertEquals(7, VersionStamps.stored(stamps, "absentmod:key"));
|
||||
assertEquals(2, carried.getInteger("mod:key"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void writeWithoutCarriedStampsWritesOnlyRegisteredKeys() {
|
||||
NBTTagCompound container = new NBTTagCompound();
|
||||
|
||||
VersionStamps.write(container, null, Collections.singletonList(transformer("mod:key", 1)));
|
||||
VersionStamps.write(container, null, Collections.singletonList(new StubTransformer("mod:key", 1)));
|
||||
|
||||
assertEquals(1, VersionStamps.stored(VersionStamps.read(container), "mod:key"));
|
||||
assertEquals(VersionStamps.UNSTAMPED, VersionStamps.stored(VersionStamps.read(container), "other:key"));
|
||||
|
||||
Reference in new issue
Block a user