Files
Postea/src/main/java/com/gtnewhorizons/postea/utility/VersionStamps.java
T
2026-11-25 14:45:57 -06:00

42 lines
1.7 KiB
Java

package com.gtnewhorizons.postea.utility;
import static net.minecraftforge.common.util.Constants.NBT.TAG_COMPOUND;
import static net.minecraftforge.common.util.Constants.NBT.TAG_INT;
import net.minecraft.nbt.NBTTagCompound;
import com.gtnewhorizons.postea.api.IVersionedTransformer;
/// The `POSTEA_VERSIONS` compound Postea keeps on data handled by [IVersionedTransformer]s, with one int per
/// transformer key.
///
/// Keys of transformers not registered in the running session are carried through unchanged.
public final class VersionStamps {
public static final String KEY = "POSTEA_VERSIONS";
public static final int UNSTAMPED = -1;
private VersionStamps() {}
/// The stamps stored on `container`, or null if it carries none.
public static NBTTagCompound read(NBTTagCompound container) {
return container.hasKey(KEY, TAG_COMPOUND) ? container.getCompoundTag(KEY) : null;
}
/// The version stored for `key` in `stamps`, or [#UNSTAMPED]. `stamps` may be null.
public static int stored(NBTTagCompound stamps, String key) {
return stamps != null && stamps.hasKey(key, TAG_INT) ? stamps.getInteger(key) : UNSTAMPED;
}
/// Writes the stamps onto `container`: `carried`, the stamps the data was read with or null, with every
/// transformer's key set to its current version.
public static void write(NBTTagCompound container, NBTTagCompound carried,
Iterable<IVersionedTransformer> transformers) {
NBTTagCompound stamps = carried == null ? new NBTTagCompound() : (NBTTagCompound) carried.copy();
for (IVersionedTransformer transformer : transformers) {
stamps.setInteger(transformer.key(), transformer.currentVersion());
}
container.setTag(KEY, stamps);
}
}