Skip to content

Commit

Permalink
Merge pull request #528 from P3pp3rF1y/1.21.x-dev
Browse files Browse the repository at this point in the history
fix: 🐛 Fixed crash where chest copied with its upgrade contents in cr…
  • Loading branch information
P3pp3rF1y authored Nov 10, 2024
2 parents 13d3180 + 77a326f commit e008998
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 24 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Runnable> getSaveHandler;

@Nullable
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ public class StackStorageWrapper extends StorageWrapper {
private ItemStack storageStack;

public StackStorageWrapper(ItemStack storageStack) {
super(() -> () -> {
}, () -> {
}, () -> {
});
super(() -> () -> {}, () -> {}, () -> {});
setStorageStack(storageStack);
}

Expand All @@ -50,6 +47,10 @@ public Optional<UUID> 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);
Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Integer> getMainColorFromStack(ItemStack storageStack) {
return Optional.ofNullable(storageStack.get(ModCoreDataComponents.MAIN_COLOR));
}
public static Optional<CompoundTag> 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<Integer> getAccentColorFromStack(ItemStack storageStack) {
return Optional.ofNullable(storageStack.get(ModCoreDataComponents.ACCENT_COLOR));
}
public static Optional<Integer> 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<Integer> 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) {
Expand Down

0 comments on commit e008998

Please sign in to comment.