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
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.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.
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -133,7 +137,7 @@ private static void applyStacks(List<StackChange> 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));
}
}
}
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.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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
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.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));
}
}
}
Loading