Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 2 additions & 0 deletions docs/WORLD-UPGRADES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public static void run(ServerLevel world) throws Exception {

DirtCoverageChecks.run(world);
storedInventories(world);
LiveInventoryRecoveryChecks.run(world);

if (!installed) {
unknownContentStops();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Loading