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 @@ -143,9 +143,9 @@ jobs:
if-no-files-found: error
retention-days: 30
path: |
build/libs/SkysBuildingPieces-0.3.10.2602002.jar
build/libs/SkysBuildingPieces-0.3.10.2602002-sources.jar
build/libs/SkysBuildingPieces-0.3.10.2602002-javadoc.jar
build/libs/SkysBuildingPieces-0.3.11.2602002.jar
build/libs/SkysBuildingPieces-0.3.11.2602002-sources.jar
build/libs/SkysBuildingPieces-0.3.11.2602002-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.11.2602002

- Leave unrelated storage inventories alone during legacy recovery.
- Preserve full item counts and custom data when recovering older pieces from large storage stacks.

## 0.3.10.2602002

- Snowy steps and corners cover the grass beneath them, and snow pieces can be dug by hand.
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,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.2602002
mod_version=0.3.11.2602002
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.getValue(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.getListOrEmpty("Items").getCompoundOrEmpty(0);
require(recoveredOuter.getCount() == 1895 && recoveredNested.getIntOr("count", 0) == 1895
&& recoveredNested.getIntOr("Slot", 0) == 7
&& recoveredNested.getStringOr("id", "").equals(BuiltInRegistries.ITEM.getKey(expected.getItem()).toString())
&& recoveredData.getStringOr("owner", "").equals(contents.getStringOr("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).nonEmptyItemCopyStream().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 @@ -25,6 +25,7 @@ private NativeLegacyChecks() {
}

public static void run(ServerLevel world) throws Exception {
LiveInventoryRecoveryChecks.run(world);
zone.moddev.mc.skysbuildingpieces.probe.legacy.NativeStagingChecks.run(world);
flattenedPieces();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public static void register(String module, Predicate<CompoundTag> relevant, Cons
RECOVERIES.register(module, relevant, recovery);
}

static boolean hasContributors() {
return RECOVERIES.hasContributors();
}

/** No copy or traversal is added to standalone runs without contributors. */
static CompoundTag prepareCopy(CompoundTag root) {
return RECOVERIES.prepareCopy(root);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,15 @@ public static boolean hasPendingReport() {
}
}

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);
}

public static void prepareStack(CompoundTag stack) {
prepareStacks(stack);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package zone.moddev.mc.skysbuildingpieces.legacy;

import java.util.function.Consumer;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtOps;
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) {
return convert(original, registries, Runnable::run);
}

static ItemStack convert(ItemStack original, HolderLookup.Provider registries, Consumer<Runnable> account) {
String registryId = BuiltInRegistries.ITEM.getKey(original.getItem()).toString();
if (original.getComponentsPatch().isEmpty() && !HistoricalItems.hasContributors()
&& !LegacyBridge.mightRecoverItem(registryId)) {
return original;
}

// Storage handlers can hold more items than the ordinary stack codec.
// Inspect a one-item copy and keep the handler's real count intact.
int storedCount = original.getCount();
var ops = registries.createSerializationContext(NbtOps.INSTANCE);
CompoundTag before = (CompoundTag) ItemStack.CODEC.encodeStart(ops, original.copyWithCount(1)).getOrThrow();
before.putInt("count", storedCount);
CompoundTag recovered = before.copy();
LegacyBridge.prepareStack(recovered, account);
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 at the codec's normal
// count, then restore the full storage count on the resulting stack.
recovered.putInt("count", 1);
ItemStack result = ItemStack.CODEC.parse(ops, recovered).getOrThrow(message ->
new LegacyBridge.RecoveryAbort("Cannot decode recovered item "
+ recovered.getStringOr("id", "") + ": " + message, null));
result.setCount(storedCount);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -382,22 +382,7 @@ private static ItemStack convert(ItemStack legacyStack, Level world, java.util.f
return legacyStack;
}

var ops = world.registryAccess().createSerializationContext(NbtOps.INSTANCE);
CompoundTag before = (CompoundTag) ItemStack.CODEC.encodeStart(ops, legacyStack).getOrThrow();
// 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, account);

if (legacyTileData.equals(before)) {
return legacyStack;
}
return LiveItemRecovery.convert(legacyStack, world.registryAccess(), account);

return ItemStack.CODEC.parse(ops, legacyTileData).getOrThrow(message ->
new LegacyBridge.RecoveryAbort("Cannot decode recovered item "
+ legacyTileData.getStringOr("id", "") + ": " + message, null));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
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.component.DataComponentMap;
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) {
prepareStone();
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) {
prepareStone();
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);
}

private static void prepareStone() {
MinecraftTestBootstrap.registerVanilla();
// Plain JUnit has no datapack component reload. The native server checks
// use the complete item defaults; this test needs stone's stack limit.
if (!Items.STONE.builtInRegistryHolder().areComponentsBound()) {
Items.STONE.builtInRegistryHolder().bindComponents(DataComponentMap.builder()
.set(DataComponents.MAX_STACK_SIZE, 64).build());
}
}
}
Loading