diff --git a/gradle.properties b/gradle.properties index 131ba755..d3e7d827 100644 --- a/gradle.properties +++ b/gradle.properties @@ -13,7 +13,7 @@ loader_version_range=[4,) mod_id=sophisticatedstorage mod_name=Sophisticated Storage mod_license=GNU General Public License v3.0 -mod_version=0.10.51 +mod_version=0.10.52 mod_group_id=sophisticatedstorage mod_authors=P3pp3rF1y, Ridanisaurus mod_description=Fancy and functional storage containers. diff --git a/src/main/java/net/p3pp3rf1y/sophisticatedstorage/block/ChestBlock.java b/src/main/java/net/p3pp3rf1y/sophisticatedstorage/block/ChestBlock.java index 8f835a22..4774f496 100644 --- a/src/main/java/net/p3pp3rf1y/sophisticatedstorage/block/ChestBlock.java +++ b/src/main/java/net/p3pp3rf1y/sophisticatedstorage/block/ChestBlock.java @@ -197,7 +197,7 @@ public BlockState getStateForPlacement(BlockPlaceContext context) { StorageBlockItem.getMainColorFromStack(chestBeingPlaced).orElse(-1), StorageBlockItem.getAccentColorFromStack(chestBeingPlaced).orElse(-1), WoodStorageBlockItem.getWoodType(chestBeingPlaced).orElse(WoodType.ACACIA), - wrapper.getContentsUuid().isPresent() && InventoryHelper.isEmpty(wrapper.getUpgradeHandler())); + wrapper.hasContents() && InventoryHelper.isEmpty(wrapper.getUpgradeHandler())); } private BlockState getStateForPlacement(BlockPlaceContext context, Direction direction, FluidState fluidstate, int mainColor, int accentColor, WoodType woodType, boolean itemHasNoUpgrades) { diff --git a/src/main/java/net/p3pp3rf1y/sophisticatedstorage/block/StorageWrapper.java b/src/main/java/net/p3pp3rf1y/sophisticatedstorage/block/StorageWrapper.java index 2d34d440..fb7fa5c5 100644 --- a/src/main/java/net/p3pp3rf1y/sophisticatedstorage/block/StorageWrapper.java +++ b/src/main/java/net/p3pp3rf1y/sophisticatedstorage/block/StorageWrapper.java @@ -34,11 +34,13 @@ import java.util.function.Supplier; public abstract class StorageWrapper implements IStorageWrapper { - private static final String MAIN_COLOR_TAG = "mainColor"; - private static final String ACCENT_COLOR_TAG = "accentColor"; + public static final String MAIN_COLOR_TAG = "mainColor"; + public static final String ACCENT_COLOR_TAG = "accentColor"; private static final String UUID_TAG = "uuid"; private static final String OPEN_TAB_ID_TAG = "openTabId"; public static final String CONTENTS_TAG = "contents"; + public static final String NUMBER_OF_INVENTORY_SLOTS_TAG = "numberOfInventorySlots"; + public static final String NUMBER_OF_UPGRADE_SLOTS_TAG = "numberOfUpgradeSlots"; private final Supplier getSaveHandler; @Nullable @@ -175,10 +177,10 @@ CompoundTag saveData(CompoundTag tag) { tag.putInt("columnsTaken", columnsTaken); } if (numberOfInventorySlots > 0) { - tag.putInt("numberOfInventorySlots", numberOfInventorySlots); + tag.putInt(NUMBER_OF_INVENTORY_SLOTS_TAG, numberOfInventorySlots); } if (numberOfUpgradeSlots > -1) { - tag.putInt("numberOfUpgradeSlots", numberOfUpgradeSlots); + tag.putInt(NUMBER_OF_UPGRADE_SLOTS_TAG, numberOfUpgradeSlots); } if (mainColor != -1) { tag.putInt(MAIN_COLOR_TAG, mainColor); @@ -215,8 +217,8 @@ private void loadData(HolderLookup.Provider registries, CompoundTag tag) { } protected void loadSlotNumbers(CompoundTag tag) { - numberOfInventorySlots = NBTHelper.getInt(tag, "numberOfInventorySlots").orElse(0); - numberOfUpgradeSlots = NBTHelper.getInt(tag, "numberOfUpgradeSlots").orElse(-1); + numberOfInventorySlots = NBTHelper.getInt(tag, NUMBER_OF_INVENTORY_SLOTS_TAG).orElse(0); + numberOfUpgradeSlots = NBTHelper.getInt(tag, NUMBER_OF_UPGRADE_SLOTS_TAG).orElse(-1); } private void loadContents(CompoundTag tag) { diff --git a/src/main/java/net/p3pp3rf1y/sophisticatedstorage/item/StackStorageWrapper.java b/src/main/java/net/p3pp3rf1y/sophisticatedstorage/item/StackStorageWrapper.java index 4f826370..fd9114e4 100644 --- a/src/main/java/net/p3pp3rf1y/sophisticatedstorage/item/StackStorageWrapper.java +++ b/src/main/java/net/p3pp3rf1y/sophisticatedstorage/item/StackStorageWrapper.java @@ -20,10 +20,7 @@ public class StackStorageWrapper extends StorageWrapper { private ItemStack storageStack; public StackStorageWrapper(ItemStack storageStack) { - super(() -> () -> { - }, () -> { - }, () -> { - }); + super(() -> () -> {}, () -> {}, () -> {}); setStorageStack(storageStack); } @@ -50,6 +47,10 @@ public Optional getContentsUuid() { return Optional.ofNullable(contentsUuid); } + public boolean hasContents() { + return StorageBlockItem.getEntityWrapperTagFromStack(storageStack).isPresent() || contentsUuid != null; + } + @Override public void setContentsUuid(@Nullable UUID contentsUuid) { super.setContentsUuid(contentsUuid); @@ -69,10 +70,12 @@ public void setContentsUuid(@Nullable UUID contentsUuid) { @Override protected CompoundTag getContentsNbt() { - if (contentsUuid == null) { - contentsUuid = getNewUuid(); - } - return ItemContentsStorage.get().getOrCreateStorageContents(contentsUuid).getCompound(StorageBlockEntity.STORAGE_WRAPPER_TAG).getCompound(CONTENTS_TAG); + return StorageBlockItem.getEntityWrapperTagFromStack(storageStack).map(wrapperTag -> wrapperTag.getCompound(CONTENTS_TAG)).orElseGet(() -> { + if (contentsUuid == null) { + contentsUuid = getNewUuid(); + } + return ItemContentsStorage.get().getOrCreateStorageContents(contentsUuid).getCompound(StorageBlockEntity.STORAGE_WRAPPER_TAG).getCompound(CONTENTS_TAG); + }); } @Override @@ -87,8 +90,13 @@ public int getDefaultNumberOfInventorySlots() { @Override protected void loadSlotNumbers(CompoundTag tag) { - numberOfInventorySlots = storageStack.getOrDefault(ModCoreDataComponents.NUMBER_OF_INVENTORY_SLOTS, 0); - numberOfUpgradeSlots = storageStack.getOrDefault(ModCoreDataComponents.NUMBER_OF_UPGRADE_SLOTS, 0); + StorageBlockItem.getEntityWrapperTagFromStack(storageStack).ifPresentOrElse(wrapperTag -> { + numberOfInventorySlots = wrapperTag.getInt(StorageWrapper.NUMBER_OF_INVENTORY_SLOTS_TAG); + numberOfUpgradeSlots = wrapperTag.getInt(StorageWrapper.NUMBER_OF_UPGRADE_SLOTS_TAG); + }, () -> { + numberOfInventorySlots = storageStack.getOrDefault(ModCoreDataComponents.NUMBER_OF_INVENTORY_SLOTS, 0); + numberOfUpgradeSlots = storageStack.getOrDefault(ModCoreDataComponents.NUMBER_OF_UPGRADE_SLOTS, 0); + }); } @Override diff --git a/src/main/java/net/p3pp3rf1y/sophisticatedstorage/item/StorageBlockItem.java b/src/main/java/net/p3pp3rf1y/sophisticatedstorage/item/StorageBlockItem.java index f9641ffa..bf697de9 100644 --- a/src/main/java/net/p3pp3rf1y/sophisticatedstorage/item/StorageBlockItem.java +++ b/src/main/java/net/p3pp3rf1y/sophisticatedstorage/item/StorageBlockItem.java @@ -1,27 +1,40 @@ package net.p3pp3rf1y.sophisticatedstorage.item; +import net.minecraft.core.component.DataComponents; +import net.minecraft.nbt.CompoundTag; import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.component.CustomData; import net.minecraft.world.level.block.Block; import net.p3pp3rf1y.sophisticatedcore.init.ModCoreDataComponents; import net.p3pp3rf1y.sophisticatedcore.util.BlockItemBase; import net.p3pp3rf1y.sophisticatedstorage.block.ITintableBlockItem; +import net.p3pp3rf1y.sophisticatedstorage.block.StorageWrapper; import net.p3pp3rf1y.sophisticatedstorage.init.ModDataComponents; import java.util.Optional; +import static net.p3pp3rf1y.sophisticatedstorage.block.StorageBlockEntity.STORAGE_WRAPPER_TAG; + public class StorageBlockItem extends BlockItemBase implements ITintableBlockItem { public StorageBlockItem(Block block, Properties properties) { super(block, properties); } - public static Optional getMainColorFromStack(ItemStack storageStack) { - return Optional.ofNullable(storageStack.get(ModCoreDataComponents.MAIN_COLOR)); - } + public static Optional getEntityWrapperTagFromStack(ItemStack barrelStack) { + CustomData customData = barrelStack.get(DataComponents.BLOCK_ENTITY_DATA); + if (customData == null) { + return Optional.empty(); + } + return Optional.of(customData.copyTag().getCompound(STORAGE_WRAPPER_TAG)); + } - public static Optional getAccentColorFromStack(ItemStack storageStack) { - return Optional.ofNullable(storageStack.get(ModCoreDataComponents.ACCENT_COLOR)); - } + public static Optional getMainColorFromStack(ItemStack storageStack) { + return getEntityWrapperTagFromStack(storageStack).map(tag -> tag.getInt(StorageWrapper.MAIN_COLOR_TAG)).or(() -> Optional.ofNullable(storageStack.get(ModCoreDataComponents.MAIN_COLOR))); + } + public static Optional getAccentColorFromStack(ItemStack storageStack) { + return getEntityWrapperTagFromStack(storageStack).map(tag -> tag.getInt(StorageWrapper.ACCENT_COLOR_TAG)).or(() -> Optional.ofNullable(storageStack.get(ModCoreDataComponents.ACCENT_COLOR))); + } @Override public void setMainColor(ItemStack storageStack, int mainColor) {