From 72ecfe85053df0ec0bacfb7e7ed781474d2f0e3c Mon Sep 17 00:00:00 2001 From: JohnBraham Date: Tue, 24 Nov 2026 15:58:00 +0000 Subject: [PATCH] Preserve full counts during live item recovery --- CHANGELOG.md | 4 + gradle.properties | 2 +- .../probe/LiveInventoryRecoveryChecks.java | 88 +++++++++++++++++++ .../probe/RuntimeProbe.java | 2 + .../legacy/LegacyBridge.java | 6 +- .../legacy/LiveItemRecovery.java | 35 ++++++++ .../legacy/MigrationEvents.java | 12 +-- .../legacy/LiveStackCountTest.java | 26 ++++++ 8 files changed, 162 insertions(+), 13 deletions(-) create mode 100644 src/integrationTest/java/zone/moddev/mc/skysbuildingpieces/probe/LiveInventoryRecoveryChecks.java create mode 100644 src/main/java/zone/moddev/mc/skysbuildingpieces/legacy/LiveItemRecovery.java create mode 100644 src/test/java/zone/moddev/mc/skysbuildingpieces/legacy/LiveStackCountTest.java diff --git a/CHANGELOG.md b/CHANGELOG.md index dbfb8cfed..fad963298 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 0.3.11.112021 + +- Preserve full item counts and custom data when recovering older pieces from large storage stacks. + ## 0.3.10.112021 - Stop snow layers hovering over bottom slabs, steps, corners and lowered paths. diff --git a/gradle.properties b/gradle.properties index ec14c002b..9447df8f2 100644 --- a/gradle.properties +++ b/gradle.properties @@ -20,7 +20,7 @@ release_enabled=true curseforge_project_id=1754162 mod_name=Sky's Building Pieces mod_license=LGPL-2.1-only -mod_version=0.3.10.112021 +mod_version=0.3.11.112021 mod_group=zone.moddev.mc.skysbuildingpieces mod_authors=SkyBlade1978 mod_description=Extra building shapes for vanilla materials, with built-in Mineralogy support when installed. No world generation. diff --git a/src/integrationTest/java/zone/moddev/mc/skysbuildingpieces/probe/LiveInventoryRecoveryChecks.java b/src/integrationTest/java/zone/moddev/mc/skysbuildingpieces/probe/LiveInventoryRecoveryChecks.java new file mode 100644 index 000000000..3ffe8b558 --- /dev/null +++ b/src/integrationTest/java/zone/moddev/mc/skysbuildingpieces/probe/LiveInventoryRecoveryChecks.java @@ -0,0 +1,88 @@ +package zone.moddev.mc.skysbuildingpieces.probe; + +import net.minecraft.init.Blocks; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagList; +import net.minecraft.world.WorldServer; +import zone.moddev.mc.skysbuildingpieces.legacy.LegacyBridge; +import zone.moddev.mc.skysbuildingpieces.legacy.MigrationEvents; + +/** Real storage stacks may hold more items than the saved byte-sized Count. */ +final class LiveInventoryRecoveryChecks { + private LiveInventoryRecoveryChecks() { + } + + static void run(WorldServer world) { + for (int count : new int[] {1, 64, 99, 100, 127, 128, 255, 256, 1895, Integer.MAX_VALUE}) { + ItemStack ordinary = new ItemStack(Blocks.STONE, count); + require(MigrationEvents.convert(ordinary, world) == ordinary, + "Leave unrelated storage stacks unchanged"); + + ItemStack saved = historical(count); + ItemStack original = saved.copy(); + ItemStack converted = MigrationEvents.convert(saved, world); + require(converted != saved && converted.getItem().getRegistryName().getNamespace() + .equals("skysbuildingpieces") && converted.getCount() == count, + "Recover the piece without narrowing its storage count: " + count); + require(converted.getTagCompound().equals(original.getTagCompound()), + "Keep names and unrelated item NBT"); + require(saved.getCount() == count && saved.getItem() == original.getItem() + && saved.getTagCompound().equals(original.getTagCompound()), + "Never mutate the handler's original stack"); + require(MigrationEvents.convert(converted, world) == converted, + "Recovered pieces remain stable on subsequent scans"); + } + + ItemStack container = new ItemStack(Blocks.CHEST, 1895); + NBTTagCompound data = new NBTTagCompound(); + data.setString("owner", "Keep this storage data"); + NBTTagList contents = new NBTTagList(); + NBTTagCompound nested = historical(17).writeToNBT(new NBTTagCompound()); + nested.setByte("Slot", (byte) 7); + contents.appendTag(nested); + data.setTag("Items", contents); + container.setTagCompound(data); + ItemStack recovered = MigrationEvents.convert(container, world); + NBTTagCompound recoveredItem = recovered.getTagCompound().getTagList("Items", 10).getCompoundTagAt(0); + require(recovered.getCount() == 1895 && recoveredItem.getByte("Count") == 17 + && recoveredItem.getByte("Slot") == 7 + && recoveredItem.getString("id").startsWith("skysbuildingpieces:") + && recovered.getTagCompound().getString("owner").equals(data.getString("owner")), + "Keep outer counts, nested counts, slots and custom data"); + require(container.getCount() == 1895 && container.getTagCompound().equals(data), + "Nested recovery leaves the original container unchanged"); + require(MigrationEvents.convert(recovered, world) == recovered, "Nested recovery is idempotent"); + + NBTTagCompound unsafe = nested.copy(); + unsafe.getCompoundTag("tag").setString("material", "missing:unknown"); + contents.appendTag(unsafe); + NBTTagCompound beforeFailure = data.copy(); + boolean stopped = false; + try { + MigrationEvents.convert(container, world); + } catch (LegacyBridge.RecoveryAbort expected) { + stopped = true; + } + require(stopped && container.getTagCompound().equals(beforeFailure), + "An unknown nested material stops before any source data changes"); + System.out.println("BUILDING_PIECES_LIVE_STORAGE_PASS counts=1,64,99,100,127,128,255,256,1895,2147483647 nested=true atomic=true"); + } + + private static ItemStack historical(int count) { + ItemStack stack = new ItemStack(Item.getByNameOrId("buildingbricks:rock_step"), count, 327); + NBTTagCompound data = new NBTTagCompound(); + data.setString("material", "minecraft:stone"); + data.setString("owner", "Keep this piece"); + stack.setTagCompound(data); + stack.setStackDisplayName("Stored stone step"); + return stack; + } + + private static void require(boolean condition, String message) { + if (!condition) { + throw new AssertionError(message); + } + } +} diff --git a/src/integrationTest/java/zone/moddev/mc/skysbuildingpieces/probe/RuntimeProbe.java b/src/integrationTest/java/zone/moddev/mc/skysbuildingpieces/probe/RuntimeProbe.java index e3da04d52..fd53ab283 100644 --- a/src/integrationTest/java/zone/moddev/mc/skysbuildingpieces/probe/RuntimeProbe.java +++ b/src/integrationTest/java/zone/moddev/mc/skysbuildingpieces/probe/RuntimeProbe.java @@ -356,6 +356,8 @@ public void tick(TickEvent.ServerTickEvent event) { LegacyBridge.prepareStack(crate); require(before.equals(crate), "nested recovery idempotence"); + LiveInventoryRecoveryChecks.run(world); + Properties props = new Properties(); File marker = new File(world.getSaveHandler().getWorldDirectory(), "skysbuildingpieces-integration.properties"); diff --git a/src/main/java/zone/moddev/mc/skysbuildingpieces/legacy/LegacyBridge.java b/src/main/java/zone/moddev/mc/skysbuildingpieces/legacy/LegacyBridge.java index 62b7911a6..290b66d21 100644 --- a/src/main/java/zone/moddev/mc/skysbuildingpieces/legacy/LegacyBridge.java +++ b/src/main/java/zone/moddev/mc/skysbuildingpieces/legacy/LegacyBridge.java @@ -111,6 +111,10 @@ public static boolean hasPendingReport() { } } + static int stackCount(NBTTagCompound stack) { + return stack.hasKey("Count", 1) ? stack.getByte("Count") & 255 : stack.getInteger("Count"); + } + public static void prepareStack(NBTTagCompound stack) { if (ready) { zone.moddev.mc.skysbuildingpieces.compat.MineralogyCompat.prepareStacks(stack); @@ -133,7 +137,7 @@ private static void applyStacks(List changes) { if (worldDirectory != null) { pendingItems.computeIfAbsent(change.module, key -> new java.util.concurrent.atomic.AtomicLong()) - .addAndGet(change.stack.getByte("Count") & 255); + .addAndGet(stackCount(change.stack)); } } } diff --git a/src/main/java/zone/moddev/mc/skysbuildingpieces/legacy/LiveItemRecovery.java b/src/main/java/zone/moddev/mc/skysbuildingpieces/legacy/LiveItemRecovery.java new file mode 100644 index 000000000..babf40275 --- /dev/null +++ b/src/main/java/zone/moddev/mc/skysbuildingpieces/legacy/LiveItemRecovery.java @@ -0,0 +1,35 @@ +package zone.moddev.mc.skysbuildingpieces.legacy; + +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; + +/** Recover live inventory identities without using the save format's byte-sized count. */ +final class LiveItemRecovery { + private LiveItemRecovery() { + } + + static ItemStack convert(ItemStack original) { + int storedCount = original.getCount(); + NBTTagCompound before = original.writeToNBT(new NBTTagCompound()); + // This integer exists only during recovery. Saved vanilla stacks retain + // their usual byte Count, including stacks nested inside item NBT. + before.setInteger("Count", storedCount); + NBTTagCompound recovered = before.copy(); + LegacyBridge.prepareStack(recovered); + if (recovered.equals(before)) { + return original; + } + if (LegacyBridge.stackCount(recovered) != storedCount) { + throw new LegacyBridge.RecoveryAbort("Item recovery changed the stored count for " + + before.getString("id"), null); + } + + recovered.setByte("Count", (byte) 1); + ItemStack result = new ItemStack(recovered); + if (result.isEmpty()) { + throw new LegacyBridge.RecoveryAbort("Cannot decode recovered item " + recovered.getString("id"), null); + } + result.setCount(storedCount); + return result; + } +} diff --git a/src/main/java/zone/moddev/mc/skysbuildingpieces/legacy/MigrationEvents.java b/src/main/java/zone/moddev/mc/skysbuildingpieces/legacy/MigrationEvents.java index 28daeee8c..f52fab6f2 100644 --- a/src/main/java/zone/moddev/mc/skysbuildingpieces/legacy/MigrationEvents.java +++ b/src/main/java/zone/moddev/mc/skysbuildingpieces/legacy/MigrationEvents.java @@ -289,17 +289,7 @@ public static ItemStack convert(ItemStack legacyStack, net.minecraft.world.World return legacyStack; } - NBTTagCompound before = legacyStack.writeToNBT(new NBTTagCompound()); - NBTTagCompound legacyTileData = before.copy(); + return LiveItemRecovery.convert(legacyStack); - LegacyBridge.prepareStack(legacyTileData); - - if (legacyTileData.equals(before)) { - return legacyStack; - } - - ItemStack updated = new ItemStack(legacyTileData); - - return updated; } } diff --git a/src/test/java/zone/moddev/mc/skysbuildingpieces/legacy/LiveStackCountTest.java b/src/test/java/zone/moddev/mc/skysbuildingpieces/legacy/LiveStackCountTest.java new file mode 100644 index 000000000..ef8fd2edd --- /dev/null +++ b/src/test/java/zone/moddev/mc/skysbuildingpieces/legacy/LiveStackCountTest.java @@ -0,0 +1,26 @@ +package zone.moddev.mc.skysbuildingpieces.legacy; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import net.minecraft.nbt.NBTTagCompound; +import org.junit.jupiter.api.Test; + +class LiveStackCountTest { + @Test + void savedBytesKeepTheirUnsignedMeaning() { + NBTTagCompound saved = new NBTTagCompound(); + saved.setByte("Count", (byte) 255); + + assertEquals(255, LegacyBridge.stackCount(saved)); + } + + @Test + void liveCountsKeepAllTheirBits() { + for (int count : new int[] {1, 127, 128, 255, 256, 1895, Integer.MAX_VALUE}) { + NBTTagCompound live = new NBTTagCompound(); + live.setInteger("Count", count); + + assertEquals(count, LegacyBridge.stackCount(live)); + } + } +}