From dbcfcbff486a1293569815cce67f87bc27a62cf8 Mon Sep 17 00:00:00 2001 From: Ruling-0 <47913168+Ruling-0@users.noreply.github.com> Date: Thu, 27 Aug 2026 22:44:03 -0500 Subject: [PATCH] Attach absent tile entity and entity lists to the chunk tag --- .../postea/api/ChunkTransformContext.java | 11 +++--- .../postea/utility/NbtLists.java | 21 +++++++++++ .../postea/utility/NbtListsTest.java | 35 +++++++++++++++++++ 3 files changed, 61 insertions(+), 6 deletions(-) create mode 100644 src/main/java/com/gtnewhorizons/postea/utility/NbtLists.java create mode 100644 src/test/java/com/gtnewhorizons/postea/utility/NbtListsTest.java diff --git a/src/main/java/com/gtnewhorizons/postea/api/ChunkTransformContext.java b/src/main/java/com/gtnewhorizons/postea/api/ChunkTransformContext.java index 8b2d7d2..d80f904 100644 --- a/src/main/java/com/gtnewhorizons/postea/api/ChunkTransformContext.java +++ b/src/main/java/com/gtnewhorizons/postea/api/ChunkTransformContext.java @@ -1,7 +1,5 @@ package com.gtnewhorizons.postea.api; -import static net.minecraftforge.common.util.Constants.NBT.TAG_COMPOUND; - import java.util.function.Consumer; import net.minecraft.nbt.NBTTagCompound; @@ -13,14 +11,15 @@ import net.minecraft.world.chunk.storage.ExtendedBlockStorage; 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 as handed to {@link IVersionedTransformer#transformChunk}: its block storage and the raw {@code Level} * tag as read from disk, before tile entities and entities are parsed. Coordinates are chunk-local * ({@code 0..15}, {@code y 0..255}); block ids and metadata are the world's numeric values at full EndlessIDs - * width. The world is not loaded yet: block access through {@link #world()} is not possible. Changing a block id - * does not recompute lighting or the height map; Postea flags the chunk for a relight when an id changed. + * width. The world is not loaded yet: block access through {@link #world()} is not possible. When a transformer + * changed a block id, Postea recomputes the height map and flags the chunk for a relight once all transformers ran. */ public final class ChunkTransformContext { @@ -68,11 +67,11 @@ public final class ChunkTransformContext { } public NBTTagList tileEntityTags() { - return levelTag.getTagList("TileEntities", TAG_COMPOUND); + return NbtLists.compoundList(levelTag, "TileEntities"); } public NBTTagList entityTags() { - return levelTag.getTagList("Entities", TAG_COMPOUND); + return NbtLists.compoundList(levelTag, "Entities"); } /** diff --git a/src/main/java/com/gtnewhorizons/postea/utility/NbtLists.java b/src/main/java/com/gtnewhorizons/postea/utility/NbtLists.java new file mode 100644 index 0000000..b39e713 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/postea/utility/NbtLists.java @@ -0,0 +1,21 @@ +package com.gtnewhorizons.postea.utility; + +import static net.minecraftforge.common.util.Constants.NBT.TAG_COMPOUND; +import static net.minecraftforge.common.util.Constants.NBT.TAG_LIST; + +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagList; + +public final class NbtLists { + + private NbtLists() {} + + /** + * The list of compounds stored under {@code key}, attached to {@code parent} first when absent so that tags + * appended to it are part of {@code parent}. + */ + public static NBTTagList compoundList(NBTTagCompound parent, String key) { + if (!parent.hasKey(key, TAG_LIST)) parent.setTag(key, new NBTTagList()); + return parent.getTagList(key, TAG_COMPOUND); + } +} diff --git a/src/test/java/com/gtnewhorizons/postea/utility/NbtListsTest.java b/src/test/java/com/gtnewhorizons/postea/utility/NbtListsTest.java new file mode 100644 index 0000000..b6c35dd --- /dev/null +++ b/src/test/java/com/gtnewhorizons/postea/utility/NbtListsTest.java @@ -0,0 +1,35 @@ +package com.gtnewhorizons.postea.utility; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertSame; + +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagList; + +import org.junit.jupiter.api.Test; + +class NbtListsTest { + + @Test + void anAbsentListIsCreatedAndAttachedSoAppendedTagsAreKept() { + NBTTagCompound level = new NBTTagCompound(); + + NbtLists.compoundList(level, "TileEntities") + .appendTag(new NBTTagCompound()); + + assertEquals( + 1, + level.getTagList("TileEntities", 10) + .tagCount()); + } + + @Test + void anExistingListIsReturnedAsIs() { + NBTTagCompound level = new NBTTagCompound(); + NBTTagList existing = new NBTTagList(); + existing.appendTag(new NBTTagCompound()); + level.setTag("Entities", existing); + + assertSame(existing, NbtLists.compoundList(level, "Entities")); + } +}