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 @@ -192,9 +192,9 @@ jobs:
if-no-files-found: error
retention-days: 30
path: |
build/libs/SkysBuildingPieces-0.3.10.120011.jar
build/libs/SkysBuildingPieces-0.3.10.120011-sources.jar
build/libs/SkysBuildingPieces-0.3.10.120011-javadoc.jar
build/libs/SkysBuildingPieces-0.3.11.120011.jar
build/libs/SkysBuildingPieces-0.3.11.120011-sources.jar
build/libs/SkysBuildingPieces-0.3.11.120011-javadoc.jar
build/release/SHA256SUMS
CHANGELOG.md

Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.3.11.120011

- Preserve full item counts and custom data when recovering older pieces from large storage stacks.

## 0.3.10.120011

- Snowy steps and corners also cover the grass beneath them, without leaving a floating snow layer.
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,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.120011
mod_version=0.3.11.120011
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.
1 change: 1 addition & 0 deletions gradle/native-bootstrap.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def compileBootstrap = tasks.register('compileNativeBootstrapProbe', JavaCompile
source file('src/integrationTest/java/zone/moddev/mc/skysbuildingpieces/probe/MineralogyChecks.java')
source file('src/integrationTest/java/zone/moddev/mc/skysbuildingpieces/probe/RecipeChecks.java')
source file('src/integrationTest/java/zone/moddev/mc/skysbuildingpieces/probe/NativeLegacyChecks.java')
source file('src/integrationTest/java/zone/moddev/mc/skysbuildingpieces/probe/LiveInventoryRecoveryChecks.java')
source file('src/integrationTest/java/zone/moddev/mc/skysbuildingpieces/probe/NativeBiomeChecks.java')
source file('src/integrationTest/java/zone/moddev/mc/skysbuildingpieces/probe/WaterloggingChecks.java')
source file('src/integrationTest/java/zone/moddev/mc/skysbuildingpieces/probe/DirtCoverageChecks.java')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package zone.moddev.mc.skysbuildingpieces.probe;

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

/** Live storage counts must not be narrowed to the saved byte-sized Count. */
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(), "Use real registered legacy and current pieces");

for (int count : new int[] {1, 64, 99, 100, 127, 128, 255, 256, 1895, Integer.MAX_VALUE}) {
ItemStack ordinary = new ItemStack(Items.STONE, count);
require(MigrationEvents.convert(ordinary, world) == ordinary, "Leave unrelated storage stacks alone");
ItemStack current = expected.copyWithCount(count);
require(MigrationEvents.convert(current, world) == current, "Current pieces are already recovered");
ItemStack saved = new ItemStack(oldItem, count);
saved.setHoverName(Component.literal("Stored stone corner"));
saved.getOrCreateTag().putString("owner", "Keep this storage data");
ItemStack before = saved.copy();
ItemStack converted = MigrationEvents.convert(saved, world);
require(converted != saved && converted.is(expected.getItem()) && converted.getCount() == count,
"Recover the piece without narrowing its storage count: " + count);
require(converted.getTag().equals(before.getTag()), "Keep names and unrelated item NBT");
require(ItemStack.matches(before, saved), "Never mutate the handler's original stack");
require(MigrationEvents.convert(converted, world) == converted, "Recovered storage remains stable");
}

CompoundTag nested = new CompoundTag();
nested.putString("id", oldId);
nested.putByte("Count", (byte) 17);
nested.putShort("Damage", (short) 0);
nested.putInt("Slot", 7);
ListTag contents = new ListTag();
contents.add(nested);
ItemStack container = new ItemStack(Items.CHEST, 1895);
container.getOrCreateTag().put("Items", contents);
container.getOrCreateTag().putString("owner", "A sealed storage item");
ItemStack before = container.copy();
ItemStack recovered = MigrationEvents.convert(container, world);
CompoundTag recoveredItem = recovered.getTag().getList("Items", 10).getCompound(0);
require(recovered.getCount() == 1895 && recoveredItem.getByte("Count") == 17
&& recoveredItem.getInt("Slot") == 7
&& recoveredItem.getString("id").equals(BuiltInRegistries.ITEM.getKey(expected.getItem()).toString())
&& recovered.getTag().getString("owner").equals(before.getTag().getString("owner")),
"Keep outer counts, nested counts, slots and custom data");
require(ItemStack.matches(before, container), "Nested recovery leaves its source unchanged");
require(MigrationEvents.convert(recovered, world) == recovered, "Nested recovery is idempotent");

CompoundTag invalid = nested.copy();
invalid.putString("id", "buildingbricks:rock_step");
CompoundTag material = new CompoundTag();
material.putString("material", "missing:unknown");
invalid.put("tag", material);
contents.add(invalid);
ItemStack beforeFailure = container.copy();
boolean stopped = false;
try {
MigrationEvents.convert(container, world);
} catch (LegacyBridge.RecoveryAbort expectedFailure) {
stopped = true;
}
require(stopped && ItemStack.matches(beforeFailure, container), "Unknown content stops without changing its source");
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 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 @@ -470,6 +470,10 @@ public static boolean hasPendingReport() {
}
}

static int stackCount(CompoundTag stack) {
return stack.contains("Count", 1) ? stack.getByte("Count") & 255 : stack.getInt("Count");
}

public static void prepareStack(CompoundTag stack) {
prepareStacks(stack);
}
Expand Down Expand Up @@ -510,7 +514,7 @@ private static void applyStacks(List<StackChange> changes) {

if (worldDirectory != null && change.module != null) {
pendingItems.computeIfAbsent(change.module, key -> new java.util.concurrent.atomic.AtomicLong())
.addAndGet(change.stack.getByte("Count") & 255);
.addAndGet(stackCount(change.stack));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package zone.moddev.mc.skysbuildingpieces.legacy;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.item.ItemStack;

/** Recover live storage identities without narrowing the container's item count. */
final class LiveItemRecovery {
private LiveItemRecovery() {
}

static ItemStack convert(ItemStack original) {
int storedCount = original.getCount();
CompoundTag before = original.save(new CompoundTag());
// Keep the live count as an integer only while resolving identities.
// Vanilla saves and nested item tags retain their original Count format.
before.putInt("Count", storedCount);
CompoundTag 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.putByte("Count", (byte) 1);
ItemStack result = ItemStack.of(recovered);
if (result.isEmpty()) {
throw 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 @@ -338,17 +338,7 @@ public static ItemStack convert(ItemStack legacyStack, net.minecraft.world.level
return legacyStack;
}

CompoundTag before = legacyStack.save(new CompoundTag());
CompoundTag legacyTileData = before.copy();
return LiveItemRecovery.convert(legacyStack);

LegacyBridge.prepareStack(legacyTileData);

if (legacyTileData.equals(before)) {
return legacyStack;
}

ItemStack updated = ItemStack.of(legacyTileData);

return updated;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package zone.moddev.mc.skysbuildingpieces.legacy;

import static org.junit.jupiter.api.Assertions.assertEquals;

import net.minecraft.nbt.CompoundTag;
import org.junit.jupiter.api.Test;

class LiveStackCountTest {
@Test
void savedBytesKeepTheirUnsignedMeaning() {
CompoundTag saved = new CompoundTag();
saved.putByte("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}) {
CompoundTag live = new CompoundTag();
live.putInt("Count", count);

assertEquals(count, LegacyBridge.stackCount(live));
}
}
}
Loading