From 7d0e84a04ee5aebcac72177c930d7a03fd59c8f1 Mon Sep 17 00:00:00 2001 From: JohnBraham Date: Tue, 24 Nov 2026 13:49:22 +0000 Subject: [PATCH] Preserve oversized stacks during legacy item recovery --- .github/workflows/ci.yml | 6 +- CHANGELOG.md | 5 + docs/WORLD-UPGRADES.md | 2 + gradle.properties | 2 +- .../probe/LiveInventoryRecoveryChecks.java | 127 ++++++++++++++++++ .../probe/NativeLegacyChecks.java | 1 + .../probe/RecombinationChecks.java | 9 +- .../SkysBuildingPieces.java | 2 +- .../legacy/HistoricalItems.java | 4 + .../legacy/LegacyBridge.java | 9 ++ .../legacy/LiveItemRecovery.java | 42 ++++++ .../legacy/MigrationEvents.java | 16 +-- .../legacy/LiveItemRecoveryTest.java | 51 +++++++ 13 files changed, 254 insertions(+), 22 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/LiveItemRecoveryTest.java diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b3064d9e3..2dbdee699 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -137,9 +137,9 @@ jobs: if-no-files-found: error retention-days: 30 path: | - build/libs/SkysBuildingPieces-0.3.11.121012.jar - build/libs/SkysBuildingPieces-0.3.11.121012-sources.jar - build/libs/SkysBuildingPieces-0.3.11.121012-javadoc.jar + build/libs/SkysBuildingPieces-0.3.12.121012.jar + build/libs/SkysBuildingPieces-0.3.12.121012-sources.jar + build/libs/SkysBuildingPieces-0.3.12.121012-javadoc.jar build/release/SHA256SUMS CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md index 97c9e24b1..1f5f4e5a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## 0.3.12.121012 + +- Fix a crash when checking large item stacks in storage mods such as Sophisticated Storage. +- Keep stored counts, names and custom data intact when recovering older building pieces, including pieces inside other items. + ## 0.3.11.121012 - Fix fuel checks for stone, soil and other non-wood pieces in recipe viewers. This also covers the BOP and Mineralogy pieces. diff --git a/docs/WORLD-UPGRADES.md b/docs/WORLD-UPGRADES.md index f9ba37dda..5734fcc42 100644 --- a/docs/WORLD-UPGRADES.md +++ b/docs/WORLD-UPGRADES.md @@ -58,6 +58,8 @@ Editable item handlers are updated as their containers load. Serialized item stacks are also recovered before the game reads them, preserving counts, enchantments and other custom data. Unopened loot containers are not opened to perform migration. +Storage mods can hold more items in a slot than an ordinary inventory. Recovery +keeps those larger counts and leaves unrelated stacks unchanged. Saved contents of sealed item containers are recovered without opening them. Their container identity, seal and unrelated custom data are retained. diff --git a/gradle.properties b/gradle.properties index d7ba4ee05..da5c619e1 100644 --- a/gradle.properties +++ b/gradle.properties @@ -21,7 +21,7 @@ release_enabled=true curseforge_project_id=1754162 mod_name=Sky's Building Pieces mod_license=LGPL-2.1-only -mod_version=0.3.11.121012 +mod_version=0.3.12.121012 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..72bf9f2dc --- /dev/null +++ b/src/integrationTest/java/zone/moddev/mc/skysbuildingpieces/probe/LiveInventoryRecoveryChecks.java @@ -0,0 +1,127 @@ +package zone.moddev.mc.skysbuildingpieces.probe; + +import java.util.List; +import net.minecraft.core.component.DataComponents; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.nbt.ListTag; +import net.minecraft.network.chat.Component; +import net.minecraft.server.level.ServerLevel; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.Items; +import net.minecraft.world.item.component.CustomData; +import net.minecraft.world.item.component.ItemContainerContents; +import zone.moddev.mc.skysbuildingpieces.catalogue.Shape; +import zone.moddev.mc.skysbuildingpieces.content.PieceBlock; +import zone.moddev.mc.skysbuildingpieces.content.Pieces; +import zone.moddev.mc.skysbuildingpieces.legacy.LegacyBridge; +import zone.moddev.mc.skysbuildingpieces.legacy.MigrationEvents; + +/** Checks live storage counts as well as the early raw-NBT recovery fixtures. */ +final class LiveInventoryRecoveryChecks { + private LiveInventoryRecoveryChecks() { + } + + static void run(ServerLevel world) { + PieceBlock palette = Pieces.BLOCKS.values().stream() + .filter(piece -> piece.palette.shape == Shape.CORNER + && piece.palette.materials.contains("minecraft:stone")) + .findFirst().orElseThrow(); + String oldId = BuiltInRegistries.BLOCK.getKey(palette).toString(); + var oldItem = BuiltInRegistries.ITEM.get(BuiltInRegistries.BLOCK.getKey(palette)); + ItemStack expected = Pieces.legacyItem(oldId, 0, 1); + require(oldItem != Items.AIR && !expected.isEmpty(), "The saved-palette fixture needs real registered items"); + + for (int count : new int[] {1, 64, 99, 100, 1895, Integer.MAX_VALUE}) { + ItemStack ordinary = new ItemStack(Items.STONE, count); + require(MigrationEvents.convert(ordinary, world) == ordinary, + "Unrelated storage stacks should retain their original object"); + + ItemStack current = expected.copyWithCount(count); + require(MigrationEvents.convert(current, world) == current, + "Current pieces should not be migrated again"); + + ItemStack saved = new ItemStack(oldItem, count); + saved.set(DataComponents.CUSTOM_NAME, Component.literal("Saved stone corner")); + CompoundTag custom = new CompoundTag(); + custom.putString("owner", "Keep this storage data"); + custom.putInt("unrelatedCount", 75000); + saved.set(DataComponents.CUSTOM_DATA, CustomData.of(custom)); + ItemStack before = saved.copy(); + ItemStack converted = MigrationEvents.convert(saved, world); + require(converted != saved && converted.is(expected.getItem()) && converted.getCount() == count, + "Recover the material without limiting the storage count"); + require(converted.getComponentsPatch().equals(saved.getComponentsPatch()), + "Recovered pieces must retain their names and custom components"); + require(ItemStack.matches(before, saved), "Recovery must not mutate a handler's original stack"); + require(MigrationEvents.convert(converted, world) == converted, + "A recovered storage stack must be stable on the next scan"); + } + + CompoundTag oldNested = new CompoundTag(); + oldNested.putString("id", oldId); + oldNested.putInt("count", 1895); + oldNested.putShort("Damage", (short) 0); + oldNested.putInt("Slot", 7); + ListTag nestedItems = new ListTag(); + nestedItems.add(oldNested); + CompoundTag contents = new CompoundTag(); + contents.putString("owner", "A sealed storage item"); + contents.put("Items", nestedItems); + ItemStack outer = new ItemStack(Items.STONE, 1895); + outer.set(DataComponents.CUSTOM_DATA, CustomData.of(contents)); + ItemStack untouchedOuter = outer.copy(); + ItemStack recoveredOuter = MigrationEvents.convert(outer, world); + CompoundTag recoveredData = recoveredOuter.get(DataComponents.CUSTOM_DATA).copyTag(); + CompoundTag recoveredNested = recoveredData.getList("Items", 10).getCompound(0); + require(recoveredOuter.getCount() == 1895 && recoveredNested.getInt("count") == 1895 + && recoveredNested.getInt("Slot") == 7 + && recoveredNested.getString("id").equals(BuiltInRegistries.ITEM.getKey(expected.getItem()).toString()) + && recoveredData.getString("owner").equals(contents.getString("owner")), + "Nested raw storage must retain inner and outer counts, slots and unrelated data"); + require(ItemStack.matches(untouchedOuter, outer), "Nested recovery must leave the original container untouched"); + require(MigrationEvents.convert(recoveredOuter, world) == recoveredOuter, "Nested recovery must be idempotent"); + + ItemStack nativeContainer = new ItemStack(Items.CHEST); + nativeContainer.set(DataComponents.CONTAINER, + ItemContainerContents.fromItems(List.of(new ItemStack(oldItem, 17)))); + ItemStack originalContainer = nativeContainer.copy(); + ItemStack recoveredContainer = MigrationEvents.convert(nativeContainer, world); + ItemStack nested = recoveredContainer.get(DataComponents.CONTAINER).stream().findFirst().orElseThrow(); + require(nested.is(expected.getItem()) && nested.getCount() == 17, + "Typed container components must still recover saved pieces"); + require(ItemStack.matches(originalContainer, nativeContainer), "Typed recovery must not mutate the source"); + + CompoundTag invalid = new CompoundTag(); + invalid.putString("id", "buildingbricks:rock_step"); + invalid.putInt("count", 1895); + CompoundTag material = new CompoundTag(); + material.putString("material", "missing:unknown"); + CompoundTag components = new CompoundTag(); + components.put("minecraft:custom_data", material); + invalid.put("components", components); + ListTag mixed = new ListTag(); + mixed.add(oldNested.copy()); + mixed.add(invalid); + CompoundTag invalidContents = new CompoundTag(); + invalidContents.put("Items", mixed); + ItemStack invalidContainer = new ItemStack(Items.CHEST); + invalidContainer.set(DataComponents.CUSTOM_DATA, CustomData.of(invalidContents)); + ItemStack beforeFailure = invalidContainer.copy(); + boolean stopped = false; + try { + MigrationEvents.convert(invalidContainer, world); + } catch (LegacyBridge.RecoveryAbort expectedFailure) { + stopped = expectedFailure.getMessage().contains("missing:unknown"); + } + require(stopped && ItemStack.matches(beforeFailure, invalidContainer), + "An unsafe nested target must stop before a valid sibling or the original stack changes"); + System.out.println("BUILDING_PIECES_LIVE_STORAGE_PASS counts=1,64,99,100,1895,2147483647 nested=true atomic=true"); + } + + 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/NativeLegacyChecks.java b/src/integrationTest/java/zone/moddev/mc/skysbuildingpieces/probe/NativeLegacyChecks.java index ae9275cbc..c56d9340e 100644 --- a/src/integrationTest/java/zone/moddev/mc/skysbuildingpieces/probe/NativeLegacyChecks.java +++ b/src/integrationTest/java/zone/moddev/mc/skysbuildingpieces/probe/NativeLegacyChecks.java @@ -68,6 +68,7 @@ public static void run(ServerLevel world) throws Exception { DirtCoverageChecks.run(world); storedInventories(world); + LiveInventoryRecoveryChecks.run(world); if (!installed) { unknownContentStops(); diff --git a/src/integrationTest/java/zone/moddev/mc/skysbuildingpieces/probe/RecombinationChecks.java b/src/integrationTest/java/zone/moddev/mc/skysbuildingpieces/probe/RecombinationChecks.java index b3445afcc..a020f2314 100644 --- a/src/integrationTest/java/zone/moddev/mc/skysbuildingpieces/probe/RecombinationChecks.java +++ b/src/integrationTest/java/zone/moddev/mc/skysbuildingpieces/probe/RecombinationChecks.java @@ -160,9 +160,14 @@ private static int pair(ServerLevel world, String material, Shape shape, ItemSta PieceBlock other = Pieces.playableBlocks().stream() .filter(candidate -> candidate != block && candidate.palette.shape == shape) + // A retired material can resolve to the very same item as a + // current one. This negative case needs two different items. + .filter(candidate -> !Pieces.stack(candidate.defaultBlockState(), 1).is(ingredient.getItem())) .findFirst().orElseThrow(); - mixed.setItem(2, Pieces.stack(other.defaultBlockState(), 1)); - require(result(mixed, world).isEmpty(), "different materials do not recombine"); + ItemStack otherMaterial = Pieces.stack(other.defaultBlockState(), 1); + mixed.setItem(2, otherMaterial); + require(result(mixed, world).isEmpty(), + "different materials do not recombine: " + ingredient + " / " + otherMaterial); PieceBlock carrier = Pieces.BLOCKS.values().stream() .filter(candidate -> candidate.palette.shape == shape) diff --git a/src/main/java/zone/moddev/mc/skysbuildingpieces/SkysBuildingPieces.java b/src/main/java/zone/moddev/mc/skysbuildingpieces/SkysBuildingPieces.java index a70964b71..a9a4b43c6 100644 --- a/src/main/java/zone/moddev/mc/skysbuildingpieces/SkysBuildingPieces.java +++ b/src/main/java/zone/moddev/mc/skysbuildingpieces/SkysBuildingPieces.java @@ -23,7 +23,7 @@ @Mod(SkysBuildingPieces.ID) public final class SkysBuildingPieces { public static final String ID = "skysbuildingpieces"; - public static final String VERSION = "0.3.11.121012"; + public static final String VERSION = "0.3.12.121012"; public static final Logger LOGGER = LogManager.getLogger(); public static boolean forceReplace; public static Block HOLDER; diff --git a/src/main/java/zone/moddev/mc/skysbuildingpieces/legacy/HistoricalItems.java b/src/main/java/zone/moddev/mc/skysbuildingpieces/legacy/HistoricalItems.java index 2a8e63433..8c1a5eef7 100644 --- a/src/main/java/zone/moddev/mc/skysbuildingpieces/legacy/HistoricalItems.java +++ b/src/main/java/zone/moddev/mc/skysbuildingpieces/legacy/HistoricalItems.java @@ -30,6 +30,10 @@ static CompoundTag prepareCopy(CompoundTag root) { return RECOVERIES.prepareCopy(root); } + static boolean hasContributors() { + return RECOVERIES.hasContributors(); + } + static void finish(CompoundTag root, int targetVersion) { // ChunkLoader first fixes old chunks to 1493, then to the current version. Keep selected names through that // intermediate tag, since the stone slab rename runs at 1802. 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 26e01f7d3..feb4ca03c 100644 --- a/src/main/java/zone/moddev/mc/skysbuildingpieces/legacy/LegacyBridge.java +++ b/src/main/java/zone/moddev/mc/skysbuildingpieces/legacy/LegacyBridge.java @@ -471,6 +471,15 @@ public static void prepareStack(CompoundTag stack) { prepareStacks(stack); } + static boolean mightRecoverItem(String registryId) { + PieceItem item = Pieces.pieceItem(registryId); + if (item != null) { + return Catalogue.INSTANCE.materials.get(item.material).replacement() != null; + } + return palette(registryId) != null || HistoricalFullBlocks.historical(registryId) + || LegacyMapping.owned(registryId) || LegacyPreflight.skyOwned(registryId); + } + private static boolean prepareStacks(CompoundTag stack) { if (!ready) { return false; 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..916faa1f8 --- /dev/null +++ b/src/main/java/zone/moddev/mc/skysbuildingpieces/legacy/LiveItemRecovery.java @@ -0,0 +1,42 @@ +package zone.moddev.mc.skysbuildingpieces.legacy; + +import net.minecraft.core.HolderLookup; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.world.item.ItemStack; + +/** Inventory recovery after a container has created its live item stacks. */ +final class LiveItemRecovery { + private LiveItemRecovery() { + } + + static ItemStack convert(ItemStack original, HolderLookup.Provider registries) { + String registryId = BuiltInRegistries.ITEM.getKey(original.getItem()).toString(); + if (original.getComponentsPatch().isEmpty() && !HistoricalItems.hasContributors() + && !LegacyBridge.mightRecoverItem(registryId)) { + return original; + } + + // Storage handlers can legitimately hold more than the ordinary item + // codec's 99-item limit. Inspect a copy, keeping the real count intact. + int storedCount = original.getCount(); + CompoundTag before = (CompoundTag) original.copyWithCount(1).save(registries); + before.putInt("count", storedCount); + CompoundTag recovered = before.copy(); + LegacyBridge.prepareStack(recovered); + if (recovered.equals(before)) { + return original; + } + + if (HistoricalItems.stackCount(recovered) != storedCount) { + throw new LegacyBridge.RecoveryAbort("Item recovery changed the stored count for " + registryId, null); + } + // Decode the recovered identity and components without imposing the + // ordinary codec's count limit on the storage mod's inventory. + recovered.putInt("count", 1); + ItemStack result = ItemStack.parse(registries, recovered).orElseThrow(() -> + 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 af07b0a47..6e8086130 100644 --- a/src/main/java/zone/moddev/mc/skysbuildingpieces/legacy/MigrationEvents.java +++ b/src/main/java/zone/moddev/mc/skysbuildingpieces/legacy/MigrationEvents.java @@ -341,20 +341,6 @@ public static ItemStack convert(ItemStack legacyStack, Level world) { return legacyStack; } - CompoundTag before = (CompoundTag) legacyStack.save(world.registryAccess()); - // The current stack codec may omit count=1; this is a known stack, not an entity tag. - if (!before.contains("count")) { - before.putInt("count", legacyStack.getCount()); - } - CompoundTag legacyTileData = before.copy(); - - LegacyBridge.prepareStack(legacyTileData); - - if (legacyTileData.equals(before)) { - return legacyStack; - } - - return ItemStack.parse(world.registryAccess(), legacyTileData).orElseThrow(() -> - new LegacyBridge.RecoveryAbort("Cannot decode recovered item " + legacyTileData.getString("id"), null)); + return LiveItemRecovery.convert(legacyStack, world.registryAccess()); } } diff --git a/src/test/java/zone/moddev/mc/skysbuildingpieces/legacy/LiveItemRecoveryTest.java b/src/test/java/zone/moddev/mc/skysbuildingpieces/legacy/LiveItemRecoveryTest.java new file mode 100644 index 000000000..de6a5d6a4 --- /dev/null +++ b/src/test/java/zone/moddev/mc/skysbuildingpieces/legacy/LiveItemRecoveryTest.java @@ -0,0 +1,51 @@ +package zone.moddev.mc.skysbuildingpieces.legacy; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import net.minecraft.core.HolderLookup; +import net.minecraft.core.RegistryAccess; +import net.minecraft.core.component.DataComponents; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.network.chat.Component; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.Items; +import net.minecraft.world.item.component.CustomData; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import zone.moddev.mc.skysbuildingpieces.MinecraftTestBootstrap; + +class LiveItemRecoveryTest { + @ParameterizedTest + @ValueSource(ints = {1, 64, 99, 100, 1895, Integer.MAX_VALUE}) + void unrelatedStorageStacksKeepTheirIdentityAndFullCount(int count) { + MinecraftTestBootstrap.registerVanilla(); + ItemStack original = new ItemStack(Items.STONE, count); + + assertSame(original, LiveItemRecovery.convert(original, registries())); + assertEquals(count, original.getCount()); + } + + @ParameterizedTest + @ValueSource(ints = {1, 99, 100, 1895, Integer.MAX_VALUE}) + void namesAndForeignCustomDataDoNotMakeLargeStacksInvalid(int count) { + MinecraftTestBootstrap.registerVanilla(); + ItemStack original = new ItemStack(Items.STONE, count); + original.set(DataComponents.CUSTOM_NAME, Component.literal("Stored stone")); + CompoundTag custom = new CompoundTag(); + custom.putString("owner", "A storage mod"); + custom.putInt("storedCount", 50000); + original.set(DataComponents.CUSTOM_DATA, CustomData.of(custom)); + ItemStack before = original.copy(); + + assertSame(original, LiveItemRecovery.convert(original, registries())); + assertTrue(ItemStack.matches(before, original)); + assertEquals(custom, original.get(DataComponents.CUSTOM_DATA).copyTag()); + } + + private static HolderLookup.Provider registries() { + return RegistryAccess.fromRegistryOfRegistries(BuiltInRegistries.REGISTRY); + } +}