Fix Sophisticated Storage shulkers/chests/barrels losing contents on transfer

Root cause: Sophisticated Storage uses its own ItemContentsStorage
(SavedData) for packed items, NOT BackpackStorage from Sophisticated
Backpacks. The code was calling BackpackStorage which returned empty
data for storage items.

Fixes:
- Use ItemContentsStorage.get().getOrCreateStorageContents() for save
- Use ItemContentsStorage.get().setStorageContents() for restore
- Add extractStorageUuid() for "storageUuid" key (SS uses this, not
  "contentsUuid" which is for backpacks only)
- Try both UUID keys when scanning inventory items
- Add sophisticatedstorage as compileOnly dependency

Vyrriox
This commit is contained in:
laforetbrut 2026-03-26 17:12:29 +01:00
parent 0c7026aa65
commit fc7d81f914
2 changed files with 37 additions and 18 deletions

View File

@ -117,6 +117,7 @@ dependencies {
compileOnly "curse.maven:curios-309927:6529130" compileOnly "curse.maven:curios-309927:6529130"
compileOnly "curse.maven:sophisticated-backpacks-422301:7169832" compileOnly "curse.maven:sophisticated-backpacks-422301:7169832"
compileOnly "curse.maven:sophisticated-core-618298:7168230" compileOnly "curse.maven:sophisticated-core-618298:7168230"
compileOnly "curse.maven:sophisticated-storage-619320:7744168"
compileOnly "thedarkcolour:kotlinforforge:5.10.0" compileOnly "thedarkcolour:kotlinforforge:5.10.0"
compileOnly "curse.maven:cobblemon-687131:7273151" compileOnly "curse.maven:cobblemon-687131:7273151"

View File

@ -288,18 +288,16 @@ public class ModsSupport {
ItemStack stack = inventory.getItem(i); ItemStack stack = inventory.getItem(i);
if (stack.isEmpty()) continue; if (stack.isEmpty()) continue;
// Check if this item is from the sophisticatedstorage namespace
String itemId = stack.getItem().toString();
if (!isSophisticatedStorageItem(stack)) continue; if (!isSophisticatedStorageItem(stack)) continue;
// Try to extract contentsUuid from the item's custom data // FIX: Extract UUID using both "contentsUuid" (backpacks) and "storageUuid" (storage items) keys
UUID contentsUuid = extractContentsUuid(stack); UUID contentsUuid = extractStorageUuid(stack);
if (contentsUuid == null) contentsUuid = extractContentsUuid(stack);
if (contentsUuid == null) continue; if (contentsUuid == null) continue;
try { try {
// Read the storage contents from the world save data via BackpackStorage // FIX: Use ItemContentsStorage (Sophisticated Storage's own SavedData), NOT BackpackStorage
// Sophisticated Storage uses the same BackpackStorage mechanism from sophisticatedcore CompoundTag storageNbt = net.p3pp3rf1y.sophisticatedstorage.block.ItemContentsStorage.get().getOrCreateStorageContents(contentsUuid);
CompoundTag storageNbt = net.p3pp3rf1y.sophisticatedbackpacks.backpack.BackpackStorage.get().getOrCreateBackpackContents(contentsUuid);
if (storageNbt != null && !storageNbt.isEmpty()) { if (storageNbt != null && !storageNbt.isEmpty()) {
saveStorageContents(contentsUuid, storageNbt); saveStorageContents(contentsUuid, storageNbt);
PlayerSync.LOGGER.info("Saved Sophisticated Storage item data for UUID {}", contentsUuid); PlayerSync.LOGGER.info("Saved Sophisticated Storage item data for UUID {}", contentsUuid);
@ -323,15 +321,19 @@ public class ModsSupport {
if (!isSophisticatedStorageItem(stack)) continue; if (!isSophisticatedStorageItem(stack)) continue;
UUID contentsUuid = extractContentsUuid(stack); // FIX: Try both UUID keys
UUID contentsUuid = extractStorageUuid(stack);
if (contentsUuid == null) contentsUuid = extractContentsUuid(stack);
if (contentsUuid == null) continue; if (contentsUuid == null) continue;
final UUID finalUuid = contentsUuid;
restoreStorageContents(contentsUuid, (nbt) -> { restoreStorageContents(contentsUuid, (nbt) -> {
try { try {
net.p3pp3rf1y.sophisticatedbackpacks.backpack.BackpackStorage.get().setBackpackContents(contentsUuid, nbt); // FIX: Use ItemContentsStorage, NOT BackpackStorage
PlayerSync.LOGGER.info("Restored Sophisticated Storage item data for UUID {}", contentsUuid); net.p3pp3rf1y.sophisticatedstorage.block.ItemContentsStorage.get().setStorageContents(finalUuid, nbt);
PlayerSync.LOGGER.info("Restored Sophisticated Storage item data for UUID {}", finalUuid);
} catch (Exception e) { } catch (Exception e) {
PlayerSync.LOGGER.error("Error restoring Sophisticated Storage data for UUID {}", contentsUuid, e); PlayerSync.LOGGER.error("Error restoring Sophisticated Storage data for UUID {}", finalUuid, e);
} }
}); });
} }
@ -350,26 +352,42 @@ public class ModsSupport {
} }
/** /**
* Extracts the contents UUID from an item's custom data (used by Sophisticated Core). * Extracts the contents UUID from an item's custom data.
* Both Sophisticated Backpacks and Sophisticated Storage store a "contentsUuid" in the item's NBT. * Used by Sophisticated Backpacks (key: "contentsUuid").
*/ */
private static UUID extractContentsUuid(ItemStack stack) { private static UUID extractContentsUuid(ItemStack stack) {
return extractUuidFromCustomData(stack, "contentsUuid");
}
/**
* Extracts the storage UUID from an item's custom data.
* Used by Sophisticated Storage items - shulkers, barrels, chests (key: "storageUuid").
*/
private static UUID extractStorageUuid(ItemStack stack) {
return extractUuidFromCustomData(stack, "storageUuid");
}
/**
* Generic UUID extraction from an item's CustomData by tag key name.
* Handles both UUID compound format (most/leastSignificantBits) and string format.
*/
private static UUID extractUuidFromCustomData(ItemStack stack, String tagKey) {
try { try {
if (!stack.has(DataComponents.CUSTOM_DATA)) return null; if (!stack.has(DataComponents.CUSTOM_DATA)) return null;
CustomData customData = stack.get(DataComponents.CUSTOM_DATA); CustomData customData = stack.get(DataComponents.CUSTOM_DATA);
if (customData == null) return null; if (customData == null) return null;
CompoundTag tag = customData.copyTag(); CompoundTag tag = customData.copyTag();
if (tag.hasUUID("contentsUuid")) { if (tag.hasUUID(tagKey)) {
return tag.getUUID("contentsUuid"); return tag.getUUID(tagKey);
} }
// Some versions use a string format // Some versions use a string format
if (tag.contains("contentsUuid")) { if (tag.contains(tagKey)) {
try { try {
return UUID.fromString(tag.getString("contentsUuid")); return UUID.fromString(tag.getString(tagKey));
} catch (IllegalArgumentException ignored) {} } catch (IllegalArgumentException ignored) {}
} }
} catch (Exception e) { } catch (Exception e) {
PlayerSync.LOGGER.debug("Could not extract contentsUuid from item: {}", e.getMessage()); PlayerSync.LOGGER.debug("Could not extract {} from item: {}", tagKey, e.getMessage());
} }
return null; return null;
} }