Trim the versioned transformer context API

This commit is contained in:
Ruling-0 committed 2026-11-25 14:45:58 -06:00
1 parent 67c695f79d
commit f1b35bb96f
8 files changed
+24 -88

No files matched your search

@@ -371,7 +371,7 @@ abstract class ExampleMigrators {
@Override
public void transformChunk(ChunkTransformContext ctx) {
if (ctx.storedVersion() == ChunkTransformContext.UNSTAMPED || ctx.storedVersion() == 1) {
if (ctx.storedVersion() == UNSTAMPED || ctx.storedVersion() == 1) {
int gemBlockId = Block.getIdFromBlock(Blocks.wool);
ctx.forEachBlock((x, y, z, id, meta) -> { if (id == gemBlockId) ctx.setBlock(x, y, z, id, meta + 1); });
ctx.forEachItemStackTag(stack -> {
@@ -384,7 +384,7 @@ abstract class ExampleMigrators {
@Override
public void transformPlayer(PlayerDataTransformContext ctx) {
if (ctx.storedVersion() == PlayerDataTransformContext.UNSTAMPED || ctx.storedVersion() == 1) {
if (ctx.storedVersion() == UNSTAMPED || ctx.storedVersion() == 1) {
ctx.forEachItemStackTag(stack -> {
if (IDExtenderCompat.getItemStackID(stack) == Item.getIdFromItem(Items.dye)) {
stack.setShort("Damage", (short) (stack.getShort("Damage") + 1));
@@ -12,7 +12,6 @@ import com.gtnewhorizons.postea.compat.Compat;
import com.gtnewhorizons.postea.compat.SubChunkAccess;
import com.gtnewhorizons.postea.utility.NbtItemStacks;
import com.gtnewhorizons.postea.utility.NbtLists;
import com.gtnewhorizons.postea.utility.VersionStamps;
/// A chunk's block storage and raw `Level` tag, as handed to [IVersionedTransformer#transformChunk].
///
@@ -22,23 +21,18 @@ import com.gtnewhorizons.postea.utility.VersionStamps;
/// recomputes the height map and flags the chunk for a relight after all transformers ran.
public final class ChunkTransformContext {
public static final int UNSTAMPED = VersionStamps.UNSTAMPED;
private final Chunk chunk;
private final World world;
private final NBTTagCompound levelTag;
private final int storedVersion;
private final int currentVersion;
private final SubChunkAccess[] sections = new SubChunkAccess[16];
private boolean blockIdsChanged;
public ChunkTransformContext(Chunk chunk, World world, NBTTagCompound levelTag, int storedVersion,
int currentVersion) {
public ChunkTransformContext(Chunk chunk, World world, NBTTagCompound levelTag, int storedVersion) {
this.chunk = chunk;
this.world = world;
this.levelTag = levelTag;
this.storedVersion = storedVersion;
this.currentVersion = currentVersion;
}
public Chunk chunk() {
@@ -70,15 +64,11 @@ public final class ChunkTransformContext {
return NbtLists.compoundList(levelTag, "Entities");
}
/// The version stamped under the transformer's key when the chunk was saved, or [#UNSTAMPED].
/// The version stamped under the transformer's key when the chunk was saved, or [IVersionedTransformer#UNSTAMPED].
public int storedVersion() {
return storedVersion;
}
public int currentVersion() {
return currentVersion;
}
/// The block id at a chunk-local position, or 0 for a missing section.
public int getBlockId(int x, int y, int z) {
SubChunkAccess access = section(y >> 4, false);
@@ -44,7 +44,7 @@ public abstract class CustomDataReplacementManager {
VersionedTransformerLog.announce(transformer, stored, current);
Postea.LOG.debug("{}: custom data {} from version {} to {}", transformer.key(), storageId, stored, current);
try {
transformer.transformCustomData(new CustomDataTransformContext(storageId, root, stored, current));
transformer.transformCustomData(new CustomDataTransformContext(storageId, root, stored));
} catch (Throwable t) {
throw new ReportedException(
ChunkFixerUtility.describe(t, transformer, stored, current, "Custom data", storageId));
@@ -4,44 +4,17 @@ import java.util.function.Consumer;
import net.minecraft.nbt.NBTTagCompound;
import com.github.bsideup.jabel.Desugar;
import com.gtnewhorizons.postea.utility.NbtItemStacks;
import com.gtnewhorizons.postea.utility.VersionStamps;
/// A mod's custom world storage, as handed to [IVersionedTransformer#transformCustomData]. Stacks here may have
/// string `id`s instead of numeric ones, and [#forEachItemStackTag] visits both.
public final class CustomDataTransformContext {
public static final int UNSTAMPED = VersionStamps.UNSTAMPED;
private final String storageId;
private final NBTTagCompound root;
private final int storedVersion;
private final int currentVersion;
public CustomDataTransformContext(String storageId, NBTTagCompound root, int storedVersion, int currentVersion) {
this.storageId = storageId;
this.root = root;
this.storedVersion = storedVersion;
this.currentVersion = currentVersion;
}
/// The storage's identity as its owner registered it, e.g. `"betterquesting:questDatabase"`.
public String storageId() {
return storageId;
}
public NBTTagCompound root() {
return root;
}
/// The version stamped under the transformer's key when the storage was written, or [#UNSTAMPED].
public int storedVersion() {
return storedVersion;
}
public int currentVersion() {
return currentVersion;
}
///
/// @param storageId the storage's identity as its owner registered it, e.g. `"betterquesting:questDatabase"`
/// @param storedVersion the version stamped under the transformer's key when the storage was written, or
/// [IVersionedTransformer#UNSTAMPED]
@Desugar
public record CustomDataTransformContext(String storageId, NBTTagCompound root, int storedVersion) {
/// Visits every serialized item stack in the storage, including stacks whose `id` is a string name. See
/// [NbtItemStacks#forEach].
@@ -1,11 +1,12 @@
package com.gtnewhorizons.postea.api;
import com.gtnewhorizons.postea.utility.VersionStamps;
/// Upgrades saved chunks, players and custom world storage written under another version of a mod's content.
///
/// Postea stamps everything it saves with [#currentVersion()] under [#key()]. On read, the transformer runs for
/// every stored stamp that differs from [#currentVersion()], older or newer. The transformer decides what a newer
/// version means and throws if it cannot handle it. Data saved before the transformer existed reads as
/// [ChunkTransformContext#UNSTAMPED].
/// Postea stamps everything it saves with [#currentVersion()] under [#key()]. On read, the transformer runs for every
/// stored stamp that differs from [#currentVersion()], older or newer. The transformer decides what a newer version
/// means and throws if it cannot handle it. Data saved before the transformer existed reads as [#UNSTAMPED].
///
/// Register with [VersionedReplacementManager#register(IVersionedTransformer)] by postInit. Transformers run in
/// registration order, before every other Postea pass. One whose output depends on another mod's versioned data
@@ -18,6 +19,8 @@ package com.gtnewhorizons.postea.api;
/// [#transformCustomData].
public interface IVersionedTransformer {
int UNSTAMPED = VersionStamps.UNSTAMPED;
/// The stamp key, unique across all registered transformers, e.g. `"examplemod:idList"`. Must return the same
/// value for the transformer's lifetime.
String key();
@@ -5,45 +5,15 @@ import java.util.function.Consumer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.nbt.NBTTagCompound;
import com.github.bsideup.jabel.Desugar;
import com.gtnewhorizons.postea.utility.NbtItemStacks;
import com.gtnewhorizons.postea.utility.VersionStamps;
/// A player's saved data, as handed to [IVersionedTransformer#transformPlayer].
///
/// The root tag holds the inventory, ender chest, `ForgeData` and every extended-properties compound. The player
/// object is constructed but not yet loaded from the tag.
public final class PlayerDataTransformContext {
public static final int UNSTAMPED = VersionStamps.UNSTAMPED;
private final EntityPlayerMP player;
private final NBTTagCompound playerTag;
private final int storedVersion;
private final int currentVersion;
public PlayerDataTransformContext(EntityPlayerMP player, NBTTagCompound playerTag, int storedVersion,
int currentVersion) {
this.player = player;
this.playerTag = playerTag;
this.storedVersion = storedVersion;
this.currentVersion = currentVersion;
}
public EntityPlayerMP player() {
return player;
}
public NBTTagCompound playerTag() {
return playerTag;
}
public int storedVersion() {
return storedVersion;
}
public int currentVersion() {
return currentVersion;
}
@Desugar
public record PlayerDataTransformContext(EntityPlayerMP player, NBTTagCompound playerTag, int storedVersion) {
/// Visits every serialized item stack anywhere in the player's data. See [NbtItemStacks#forEach].
public void forEachItemStackTag(Consumer<NBTTagCompound> visitor) {
@@ -75,7 +75,7 @@ public class ChunkFixerUtility {
int current = transformer.currentVersion();
int stored = VersionStamps.stored(stamps, transformer.key());
if (stored == current) continue;
ChunkTransformContext ctx = new ChunkTransformContext(chunk, world, tag, stored, current);
ChunkTransformContext ctx = new ChunkTransformContext(chunk, world, tag, stored);
VersionedTransformerLog.announce(transformer, stored, current);
try {
transformer.transformChunk(ctx);
@@ -25,7 +25,7 @@ public final class PlayerDataFixerUtility {
if (stored == current) continue;
VersionedTransformerLog.announce(transformer, stored, current);
try {
transformer.transformPlayer(new PlayerDataTransformContext(player, tag, stored, current));
transformer.transformPlayer(new PlayerDataTransformContext(player, tag, stored));
} catch (Throwable t) {
throw new ReportedException(
ChunkFixerUtility.describe(