mirror of
https://github.com/GTNewHorizons/Postea.git
synced 2026-11-29 00:09:01 +00:00
Add nullability annotations to the versioned transformer API
This commit is contained in:
10 files changed
+60
-34
No files matched your search
@@ -4,6 +4,9 @@ import static net.minecraftforge.common.util.Constants.NBT.TAG_STRING;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.block.material.Material;
|
||||
import net.minecraft.init.Blocks;
|
||||
@@ -363,7 +366,7 @@ abstract class ExampleMigrators {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String key() {
|
||||
public @Nonnull String key() {
|
||||
return "examplemod:gemIndex";
|
||||
}
|
||||
|
||||
@@ -373,7 +376,7 @@ abstract class ExampleMigrators {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void transformChunk(ChunkTransformContext ctx) {
|
||||
public void transformChunk(@Nonnull ChunkTransformContext ctx) {
|
||||
if (shiftsGems(ctx.storedVersion())) {
|
||||
int gemBlockId = Block.getIdFromBlock(Blocks.wool);
|
||||
ctx.forEachBlock((x, y, z, id, meta) -> { if (id == gemBlockId) ctx.setBlock(x, y, z, id, meta + 1); });
|
||||
@@ -382,7 +385,7 @@ abstract class ExampleMigrators {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Consumer<NBTTagCompound> stackTransform(int storedVersion) {
|
||||
protected @Nullable Consumer<NBTTagCompound> stackTransform(int storedVersion) {
|
||||
if (!shiftsGems(storedVersion)) return null;
|
||||
int dyeId = Item.getIdFromItem(Items.dye);
|
||||
return stack -> {
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
package com.gtnewhorizons.postea.api;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
/// A block's namespaced id and meta value, as returned by [BlockReplacementManager#transformBlock(String, int)].
|
||||
public final class BlockNameAndMeta {
|
||||
|
||||
/// The namespaced id of the block.
|
||||
public final String name;
|
||||
public final @Nonnull String name;
|
||||
/// The meta value of the block.
|
||||
public final int metadata;
|
||||
|
||||
public BlockNameAndMeta(String name, int metadata) {
|
||||
public BlockNameAndMeta(@Nonnull String name, int metadata) {
|
||||
this.name = name;
|
||||
this.metadata = metadata;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@ package com.gtnewhorizons.postea.api;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.nbt.NBTTagList;
|
||||
import net.minecraft.world.World;
|
||||
@@ -28,18 +30,19 @@ public final class ChunkTransformContext {
|
||||
private final SubChunkAccess[] sections = new SubChunkAccess[16];
|
||||
private boolean blockIdsChanged;
|
||||
|
||||
public ChunkTransformContext(Chunk chunk, World world, NBTTagCompound levelTag, int storedVersion) {
|
||||
public ChunkTransformContext(@Nonnull Chunk chunk, @Nonnull World world, @Nonnull NBTTagCompound levelTag,
|
||||
int storedVersion) {
|
||||
this.chunk = chunk;
|
||||
this.world = world;
|
||||
this.levelTag = levelTag;
|
||||
this.storedVersion = storedVersion;
|
||||
}
|
||||
|
||||
public Chunk chunk() {
|
||||
public @Nonnull Chunk chunk() {
|
||||
return chunk;
|
||||
}
|
||||
|
||||
public World world() {
|
||||
public @Nonnull World world() {
|
||||
return world;
|
||||
}
|
||||
|
||||
@@ -52,15 +55,15 @@ public final class ChunkTransformContext {
|
||||
}
|
||||
|
||||
/// The chunk's `Level` tag. Changes to its `TileEntities` and `Entities` lists are loaded by the game.
|
||||
public NBTTagCompound levelTag() {
|
||||
public @Nonnull NBTTagCompound levelTag() {
|
||||
return levelTag;
|
||||
}
|
||||
|
||||
public NBTTagList tileEntityTags() {
|
||||
public @Nonnull NBTTagList tileEntityTags() {
|
||||
return NbtLists.compoundList(levelTag, "TileEntities");
|
||||
}
|
||||
|
||||
public NBTTagList entityTags() {
|
||||
public @Nonnull NBTTagList entityTags() {
|
||||
return NbtLists.compoundList(levelTag, "Entities");
|
||||
}
|
||||
|
||||
@@ -90,7 +93,7 @@ public final class ChunkTransformContext {
|
||||
}
|
||||
|
||||
/// Visits every non-air block of every present section.
|
||||
public void forEachBlock(BlockVisitor visitor) {
|
||||
public void forEachBlock(@Nonnull BlockVisitor visitor) {
|
||||
ExtendedBlockStorage[] storage = chunk.getBlockStorageArray();
|
||||
for (int index = 0; index < storage.length; index++) {
|
||||
if (storage[index] == null) continue;
|
||||
@@ -109,7 +112,7 @@ public final class ChunkTransformContext {
|
||||
|
||||
/// Visits every serialized item stack held by the chunk's tile entities and entities. See
|
||||
/// [NbtItemStacks#forEach].
|
||||
public void forEachItemStackTag(Consumer<NBTTagCompound> visitor) {
|
||||
public void forEachItemStackTag(@Nonnull Consumer<NBTTagCompound> visitor) {
|
||||
NbtItemStacks.forEach(tileEntityTags(), visitor);
|
||||
NbtItemStacks.forEach(entityTags(), visitor);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
package com.gtnewhorizons.postea.api;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
|
||||
import com.gtnewhorizons.postea.utility.IDRegistry;
|
||||
@@ -23,7 +26,7 @@ public abstract class CustomDataReplacementManager {
|
||||
///
|
||||
/// A failing transformer throws [net.minecraft.util.ReportedException] and leaves `root` as read, without stamps
|
||||
/// added. A call before the world's info and saved id map have loaded throws [IllegalStateException].
|
||||
public static void transform(String storageId, NBTTagCompound root) {
|
||||
public static void transform(@Nonnull String storageId, @Nonnull NBTTagCompound root) {
|
||||
if (!serverRunning()) return;
|
||||
if (!IDRegistry.isMappingApplied()) {
|
||||
throw new IllegalStateException(
|
||||
@@ -46,14 +49,14 @@ public abstract class CustomDataReplacementManager {
|
||||
}
|
||||
|
||||
/// Same as [#stamp(NBTTagCompound, NBTTagCompound)] with a null `carried`.
|
||||
public static void stamp(NBTTagCompound root) {
|
||||
public static void stamp(@Nonnull NBTTagCompound root) {
|
||||
stamp(root, null);
|
||||
}
|
||||
|
||||
/// Stamps `root` with every registered transformer's current version, keeping the stamps in `carried` for
|
||||
/// transformers not registered now. A non-null `carried` replaces any stamps already on `root`. A null `carried`
|
||||
/// keeps them.
|
||||
public static void stamp(NBTTagCompound root, NBTTagCompound carried) {
|
||||
public static void stamp(@Nonnull NBTTagCompound root, @Nullable NBTTagCompound carried) {
|
||||
if (!serverRunning()) return;
|
||||
stamp(root, carried, VersionedReplacementManager.transformers());
|
||||
}
|
||||
@@ -63,7 +66,7 @@ public abstract class CustomDataReplacementManager {
|
||||
}
|
||||
|
||||
/// 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) {
|
||||
public static @Nullable NBTTagCompound savedStamps(@Nonnull NBTTagCompound root) {
|
||||
NBTTagCompound stamps = VersionStamps.read(root);
|
||||
return stamps == null ? null : (NBTTagCompound) stamps.copy();
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@ package com.gtnewhorizons.postea.api;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
|
||||
import com.github.bsideup.jabel.Desugar;
|
||||
@@ -15,11 +17,11 @@ import com.gtnewhorizons.postea.utility.NbtItemStacks;
|
||||
/// @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) {
|
||||
public record CustomDataTransformContext(@Nonnull String storageId, @Nonnull NBTTagCompound root, int storedVersion) {
|
||||
|
||||
/// Visits every serialized item stack in the storage, including stacks whose `id` is a string name. See
|
||||
/// [NbtItemStacks#forEach].
|
||||
public void forEachItemStackTag(Consumer<NBTTagCompound> visitor) {
|
||||
public void forEachItemStackTag(@Nonnull Consumer<NBTTagCompound> visitor) {
|
||||
NbtItemStacks.forEach(root, visitor, true);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.gtnewhorizons.postea.api;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
/// 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
|
||||
@@ -25,6 +27,7 @@ public interface IVersionedTransformer {
|
||||
|
||||
/// The stamp key, unique across all registered transformers, e.g. `"examplemod:idList"`. Must return the same
|
||||
/// value for the transformer's lifetime.
|
||||
@Nonnull
|
||||
String key();
|
||||
|
||||
/// The version the transformer's output is written under. Read on every load and save, and may differ between
|
||||
@@ -32,12 +35,12 @@ public interface IVersionedTransformer {
|
||||
int currentVersion();
|
||||
|
||||
/// Brings a chunk stamped at [ChunkTransformContext#storedVersion()] to [#currentVersion()].
|
||||
void transformChunk(ChunkTransformContext ctx);
|
||||
void transformChunk(@Nonnull ChunkTransformContext ctx);
|
||||
|
||||
/// Brings a player's saved data stamped at [PlayerDataTransformContext#storedVersion()] to [#currentVersion()].
|
||||
void transformPlayer(PlayerDataTransformContext ctx);
|
||||
void transformPlayer(@Nonnull PlayerDataTransformContext ctx);
|
||||
|
||||
/// Brings custom world storage stamped at [CustomDataTransformContext#storedVersion()] to [#currentVersion()].
|
||||
/// Stacks in such storage may carry a string `id` instead of a numeric one.
|
||||
void transformCustomData(CustomDataTransformContext ctx);
|
||||
void transformCustomData(@Nonnull CustomDataTransformContext ctx);
|
||||
}
|
||||
@@ -2,6 +2,8 @@ package com.gtnewhorizons.postea.api;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayerMP;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
|
||||
@@ -13,10 +15,11 @@ import com.gtnewhorizons.postea.utility.NbtItemStacks;
|
||||
/// 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.
|
||||
@Desugar
|
||||
public record PlayerDataTransformContext(EntityPlayerMP player, NBTTagCompound playerTag, int storedVersion) {
|
||||
public record PlayerDataTransformContext(@Nonnull EntityPlayerMP player, @Nonnull NBTTagCompound playerTag,
|
||||
int storedVersion) {
|
||||
|
||||
/// Visits every serialized item stack anywhere in the player's data. See [NbtItemStacks#forEach].
|
||||
public void forEachItemStackTag(Consumer<NBTTagCompound> visitor) {
|
||||
public void forEachItemStackTag(@Nonnull Consumer<NBTTagCompound> visitor) {
|
||||
NbtItemStacks.forEach(playerTag, visitor);
|
||||
}
|
||||
}
|
||||
@@ -5,13 +5,15 @@ import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
/// Registry of [IVersionedTransformer]s. See that interface for the execution contract.
|
||||
public abstract class VersionedReplacementManager {
|
||||
|
||||
private static final List<IVersionedTransformer> TRANSFORMERS = new CopyOnWriteArrayList<>();
|
||||
|
||||
/// Registers a transformer. Throws [IllegalArgumentException] when its key is empty or already registered.
|
||||
public static void register(IVersionedTransformer transformer) {
|
||||
public static void register(@Nonnull IVersionedTransformer transformer) {
|
||||
String key = Objects.requireNonNull(transformer.key(), "key must not be null");
|
||||
if (key.isEmpty()) throw new IllegalArgumentException("Versioned transformer key must not be empty");
|
||||
for (IVersionedTransformer existing : TRANSFORMERS) {
|
||||
@@ -24,7 +26,7 @@ public abstract class VersionedReplacementManager {
|
||||
}
|
||||
|
||||
/// The registered transformers in registration order, as an unmodifiable view.
|
||||
public static List<IVersionedTransformer> transformers() {
|
||||
public static @Nonnull List<IVersionedTransformer> transformers() {
|
||||
return Collections.unmodifiableList(TRANSFORMERS);
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,9 @@ package com.gtnewhorizons.postea.api;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
|
||||
/// A versioned transformer that rewrites every saved item stack, in chunks, player data and custom world storage alike.
|
||||
@@ -13,22 +16,22 @@ public abstract class VersionedStackTransformer implements IVersionedTransformer
|
||||
/// The rewrite applied to each stack tag in data stamped at `storedVersion`, or null when that data needs none. In
|
||||
/// custom world storage the rewrite also receives stacks whose `id` is a string name. A rewrite that matches only
|
||||
/// numeric ids leaves those stacks unchanged.
|
||||
protected abstract Consumer<NBTTagCompound> stackTransform(int storedVersion);
|
||||
protected abstract @Nullable Consumer<NBTTagCompound> stackTransform(int storedVersion);
|
||||
|
||||
@Override
|
||||
public void transformChunk(ChunkTransformContext ctx) {
|
||||
public void transformChunk(@Nonnull ChunkTransformContext ctx) {
|
||||
Consumer<NBTTagCompound> transform = stackTransform(ctx.storedVersion());
|
||||
if (transform != null) ctx.forEachItemStackTag(transform);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void transformPlayer(PlayerDataTransformContext ctx) {
|
||||
public void transformPlayer(@Nonnull PlayerDataTransformContext ctx) {
|
||||
Consumer<NBTTagCompound> transform = stackTransform(ctx.storedVersion());
|
||||
if (transform != null) ctx.forEachItemStackTag(transform);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void transformCustomData(CustomDataTransformContext ctx) {
|
||||
public void transformCustomData(@Nonnull CustomDataTransformContext ctx) {
|
||||
Consumer<NBTTagCompound> transform = stackTransform(ctx.storedVersion());
|
||||
if (transform != null) ctx.forEachItemStackTag(transform);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@ package com.gtnewhorizons.postea.api;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
/// An [IVersionedTransformer] at a version the test sets, running the test's custom data transform.
|
||||
public final class StubTransformer implements IVersionedTransformer {
|
||||
|
||||
@@ -15,7 +17,7 @@ public final class StubTransformer implements IVersionedTransformer {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String key() {
|
||||
public @Nonnull String key() {
|
||||
return key;
|
||||
}
|
||||
|
||||
@@ -25,13 +27,13 @@ public final class StubTransformer implements IVersionedTransformer {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void transformChunk(ChunkTransformContext ctx) {}
|
||||
public void transformChunk(@Nonnull ChunkTransformContext ctx) {}
|
||||
|
||||
@Override
|
||||
public void transformPlayer(PlayerDataTransformContext ctx) {}
|
||||
public void transformPlayer(@Nonnull PlayerDataTransformContext ctx) {}
|
||||
|
||||
@Override
|
||||
public void transformCustomData(CustomDataTransformContext ctx) {
|
||||
public void transformCustomData(@Nonnull CustomDataTransformContext ctx) {
|
||||
customData.accept(ctx);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user