Skip to content

Commit

Permalink
fix: 🐛 Fixed crash where chest copied with its upgrade contents in cr…
Browse files Browse the repository at this point in the history
…eative put right next to another one caused world to hang
  • Loading branch information
P3pp3rF1y committed Nov 10, 2024
1 parent d4319c2 commit 1857ef9
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 18 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ org.gradle.daemon=false

mod_id=sophisticatedstorage
mod_group_id=sophisticatedstorage
mod_version=0.10.47
mod_version=0.10.48
sonar_project_key=sophisticatedstorage:SophisticatedStorage
github_package_url=https://maven.pkg.github.com/P3pp3rF1y/SophisticatedStorage

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@
import net.p3pp3rf1y.sophisticatedstorage.Config;
import net.p3pp3rf1y.sophisticatedstorage.common.gui.StorageContainerMenu;
import net.p3pp3rf1y.sophisticatedstorage.init.ModBlocks;
import net.p3pp3rf1y.sophisticatedstorage.item.CapabilityStorageWrapper;
import net.p3pp3rf1y.sophisticatedstorage.item.ChestBlockItem;
import net.p3pp3rf1y.sophisticatedstorage.item.StorageBlockItem;
import net.p3pp3rf1y.sophisticatedstorage.item.WoodStorageBlockItem;
import net.p3pp3rf1y.sophisticatedstorage.item.*;
import org.joml.Vector3f;

import javax.annotation.Nullable;
Expand Down Expand Up @@ -196,7 +193,7 @@ public BlockState getStateForPlacement(BlockPlaceContext context) {

Direction direction = context.getHorizontalDirection().getOpposite();
return chestBeingPlaced.getCapability(CapabilityStorageWrapper.getCapabilityInstance())
.filter(wrapper -> wrapper.getContentsUuid().isPresent())
.filter(StackStorageWrapper::hasContents)
.map(wrapper ->
getStateForPlacement(context, direction, fluidstate,
StorageBlockItem.getMainColorFromStack(chestBeingPlaced).orElse(-1),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
public class CapabilityStorageWrapper {
private CapabilityStorageWrapper() {}

public static final Capability<StorageWrapper> STORAGE_WRAPPER_CAPABILITY = CapabilityManager.get(new CapabilityToken<>() {});
public static final Capability<StackStorageWrapper> STORAGE_WRAPPER_CAPABILITY = CapabilityManager.get(new CapabilityToken<>() {});

public static Capability<StorageWrapper> getCapabilityInstance() {
public static Capability<StackStorageWrapper> getCapabilityInstance() {
return STORAGE_WRAPPER_CAPABILITY;
}

public static void onRegister(RegisterCapabilitiesEvent event) {
event.register(StorageWrapper.class);
event.register(StackStorageWrapper.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import java.util.Optional;
import java.util.UUID;

abstract class StackStorageWrapper extends StorageWrapper {
public abstract class StackStorageWrapper extends StorageWrapper {
private static final String CONTENTS_TAG = "contents";
private final ItemStack storageStack;

Expand All @@ -33,6 +33,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 @@ -52,10 +56,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 @@ -70,8 +76,14 @@ public int getDefaultNumberOfInventorySlots() {

@Override
protected void loadSlotNumbers(CompoundTag tag) {
numberOfInventorySlots = NBTHelper.getInt(storageStack, "numberOfInventorySlots").orElse(0);
numberOfUpgradeSlots = NBTHelper.getInt(storageStack, "numberOfUpgradeSlots").orElse(0);
StorageBlockItem.getEntityWrapperTagFromStack(storageStack).ifPresentOrElse(wrapperTag -> {
numberOfInventorySlots = wrapperTag.getInt("numberOfInventorySlots");
numberOfUpgradeSlots = wrapperTag.getInt("numberOfUpgradeSlots");
}, () -> {
numberOfInventorySlots = NBTHelper.getInt(storageStack, "numberOfInventorySlots").orElse(0);
numberOfUpgradeSlots = NBTHelper.getInt(storageStack, "numberOfUpgradeSlots").orElse(0);
});

}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.p3pp3rf1y.sophisticatedstorage.item;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.Block;
import net.p3pp3rf1y.sophisticatedcore.util.BlockItemBase;
Expand All @@ -18,12 +19,16 @@ public StorageBlockItem(Block block, Properties properties) {
super(block, properties);
}

public static Optional<CompoundTag> getEntityWrapperTagFromStack(ItemStack barrelStack) {
return NBTHelper.getCompound(barrelStack, "BlockEntityTag").flatMap(tag -> NBTHelper.getCompound(tag, "storageWrapper"));
}

public static Optional<Integer> getMainColorFromStack(ItemStack barrelStack) {
return NBTHelper.getInt(barrelStack, MAIN_COLOR_TAG);
return getEntityWrapperTagFromStack(barrelStack).map(tag -> NBTHelper.getInt(tag, MAIN_COLOR_TAG)).orElse(NBTHelper.getInt(barrelStack, MAIN_COLOR_TAG));
}

public static Optional<Integer> getAccentColorFromStack(ItemStack barrelStack) {
return NBTHelper.getInt(barrelStack, ACCENT_COLOR_TAG);
return getEntityWrapperTagFromStack(barrelStack).map(tag -> NBTHelper.getInt(tag, ACCENT_COLOR_TAG)).orElse(NBTHelper.getInt(barrelStack, ACCENT_COLOR_TAG));
}

@Override
Expand Down

0 comments on commit 1857ef9

Please sign in to comment.