Skip to content

Commit

Permalink
feat: ✨ Minor change to storage tier upgrade recipes to allow upgradi…
Browse files Browse the repository at this point in the history
…ng packed storages. Especially useful when dropPacked config is set to true and thus all storages drop as packed.
  • Loading branch information
P3pp3rF1y committed Jan 9, 2025
1 parent 219c5f3 commit 11b92ac
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 98 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=1.1.1
mod_version=1.1.2
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 @@ -141,8 +141,8 @@ public void setPlacedBy(Level level, BlockPos pos, BlockState state, @Nullable L
storageWrapper.setAccentColor(shulkerBoxItem.getAccentColor(stack).orElse(-1));
InventoryHandler inventoryHandler = storageWrapper.getInventoryHandler();
UpgradeHandler upgradeHandler = storageWrapper.getUpgradeHandler();
storageWrapper.changeSize(shulkerBoxItem.getNumberOfInventorySlots(stack) - inventoryHandler.getSlots(),
shulkerBoxItem.getNumberOfUpgradeSlots(stack) - upgradeHandler.getSlots());
storageWrapper.changeSize(shulkerBoxItem.getNumberOfInventorySlotsOrDefault(stack) - inventoryHandler.getSlots(),
shulkerBoxItem.getNumberOfUpgradeSlotsOrDefault(stack) - upgradeHandler.getSlots());
}

be.getStorageWrapper().onInit();
Expand Down Expand Up @@ -238,8 +238,8 @@ private void addBasicPropertiesToStack(ItemStack stack, StorageBlockEntity be, S
if (accentColor > -1) {
shulkerBoxItem.setAccentColor(stack, accentColor);
}
shulkerBoxItem.setNumberOfInventorySlots(stack, storageWrapper.getInventoryHandler().getSlots());
shulkerBoxItem.setNumberOfUpgradeSlots(stack, storageWrapper.getUpgradeHandler().getSlots());
StorageBlockItem.setNumberOfInventorySlots(stack, storageWrapper.getInventoryHandler().getSlots());
StorageBlockItem.setNumberOfUpgradeSlots(stack, storageWrapper.getUpgradeHandler().getSlots());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import net.minecraft.world.level.block.state.properties.WoodType;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.HitResult;
import net.p3pp3rf1y.sophisticatedcore.inventory.InventoryHandler;
import net.p3pp3rf1y.sophisticatedcore.upgrades.UpgradeHandler;
import net.p3pp3rf1y.sophisticatedcore.util.ColorHelper;
import net.p3pp3rf1y.sophisticatedcore.util.InventoryHelper;
import net.p3pp3rf1y.sophisticatedcore.util.NBTHelper;
Expand Down Expand Up @@ -72,8 +74,8 @@ public void addDropData(ItemStack stack, StorageBlockEntity be) {
}
WoodStorageBlockItem.setPacked(stack, true);
StorageBlockItem.setShowsTier(stack, be.shouldShowTier());
WoodStorageBlockItem.setNumberOfInventorySlots(stack, storageWrapper.getInventoryHandler().getSlots());
WoodStorageBlockItem.setNumberOfUpgradeSlots(stack, storageWrapper.getUpgradeHandler().getSlots());
StorageBlockItem.setNumberOfInventorySlots(stack, storageWrapper.getInventoryHandler().getSlots());
StorageBlockItem.setNumberOfUpgradeSlots(stack, storageWrapper.getUpgradeHandler().getSlots());
}
}
}
Expand Down Expand Up @@ -150,6 +152,8 @@ public void setPlacedBy(Level level, BlockPos pos, BlockState state, @Nullable L
be.setBeingUpgraded(true);
be.load(itemContentsStorage.getOrCreateStorageContents(uuid));
itemContentsStorage.removeStorageContents(uuid);

setNewSize(stack, be);
});

if (stack.hasCustomHoverName()) {
Expand All @@ -167,6 +171,14 @@ public void setPlacedBy(Level level, BlockPos pos, BlockState state, @Nullable L
});
}

private void setNewSize(ItemStack stack, WoodStorageBlockEntity be) {
StorageWrapper storageWrapper = be.getStorageWrapper();
InventoryHandler inventoryHandler = storageWrapper.getInventoryHandler();
UpgradeHandler upgradeHandler = storageWrapper.getUpgradeHandler();
storageWrapper.changeSize(StorageBlockItem.getNumberOfInventorySlots(stack) - inventoryHandler.getSlots(),
StorageBlockItem.getNumberOfUpgradeSlots(stack) - upgradeHandler.getSlots());
}

protected void setRenderBlockRenderProperties(ItemStack stack, WoodStorageBlockEntity be) {
WoodStorageBlockItem.getWoodType(stack).ifPresent(be::setWoodType);
StorageBlockItem.getMainColorFromStack(stack).ifPresent(be.getStorageWrapper()::setMainColor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.RecipeSerializer;
import net.minecraft.world.item.crafting.ShapedRecipe;
import net.minecraft.world.level.Level;
import net.p3pp3rf1y.sophisticatedcore.crafting.IWrapperRecipe;
import net.p3pp3rf1y.sophisticatedcore.crafting.RecipeWrapperSerializer;
import net.p3pp3rf1y.sophisticatedstorage.block.IStorageBlock;
import net.p3pp3rf1y.sophisticatedstorage.init.ModBlocks;
import net.p3pp3rf1y.sophisticatedstorage.item.CapabilityStorageWrapper;
import net.p3pp3rf1y.sophisticatedstorage.item.ShulkerBoxItem;
import net.p3pp3rf1y.sophisticatedstorage.item.WoodStorageBlockItem;
import net.p3pp3rf1y.sophisticatedstorage.item.StackStorageWrapper;
import net.p3pp3rf1y.sophisticatedstorage.item.StorageBlockItem;

import java.util.LinkedHashSet;
import java.util.Optional;
Expand All @@ -30,11 +28,6 @@ public StorageTierUpgradeRecipe(ShapedRecipe compose) {
REGISTERED_RECIPES.add(compose.getId());
}

@Override
public boolean matches(CraftingContainer pInv, Level pLevel) {
return super.matches(pInv, pLevel) && getOriginalStorage(pInv).map(storage -> !(storage.getItem() instanceof WoodStorageBlockItem) || !WoodStorageBlockItem.isPacked(storage)).orElse(false);
}

@Override
public ShapedRecipe getCompose() {
return compose;
Expand All @@ -44,11 +37,10 @@ public ShapedRecipe getCompose() {
public ItemStack assemble(CraftingContainer inv, RegistryAccess registryAccess) {
ItemStack upgradedStorage = super.assemble(inv, registryAccess);
getOriginalStorage(inv).ifPresent(originalStorage -> upgradedStorage.setTag(originalStorage.getTag()));
if (upgradedStorage.getItem() instanceof ShulkerBoxItem shulkerBoxItem) {
upgradedStorage.getCapability(CapabilityStorageWrapper.getCapabilityInstance()).ifPresent(wrapper -> {
shulkerBoxItem.setNumberOfInventorySlots(upgradedStorage, wrapper.getDefaultNumberOfInventorySlots());
shulkerBoxItem.setNumberOfUpgradeSlots(upgradedStorage, wrapper.getDefaultNumberOfUpgradeSlots());
});
if (StorageBlockItem.getContentsUuid(upgradedStorage).isPresent()) {
StackStorageWrapper storageWrapper = new StackStorageWrapper(upgradedStorage);
StorageBlockItem.setNumberOfInventorySlots(upgradedStorage, storageWrapper.getDefaultNumberOfInventorySlots());
StorageBlockItem.setNumberOfUpgradeSlots(upgradedStorage, storageWrapper.getDefaultNumberOfUpgradeSlots());
}
return upgradedStorage;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.RecipeSerializer;
import net.minecraft.world.item.crafting.ShapelessRecipe;
import net.minecraft.world.level.Level;
import net.p3pp3rf1y.sophisticatedcore.crafting.IWrapperRecipe;
import net.p3pp3rf1y.sophisticatedcore.crafting.RecipeWrapperSerializer;
import net.p3pp3rf1y.sophisticatedstorage.block.IStorageBlock;
import net.p3pp3rf1y.sophisticatedstorage.init.ModBlocks;
import net.p3pp3rf1y.sophisticatedstorage.item.CapabilityStorageWrapper;
import net.p3pp3rf1y.sophisticatedstorage.item.ShulkerBoxItem;
import net.p3pp3rf1y.sophisticatedstorage.item.WoodStorageBlockItem;
import net.p3pp3rf1y.sophisticatedstorage.item.StackStorageWrapper;
import net.p3pp3rf1y.sophisticatedstorage.item.StorageBlockItem;

import java.util.LinkedHashSet;
import java.util.Optional;
Expand All @@ -30,11 +28,6 @@ public StorageTierUpgradeShapelessRecipe(ShapelessRecipe compose) {
REGISTERED_RECIPES.add(compose.getId());
}

@Override
public boolean matches(CraftingContainer pInv, Level pLevel) {
return super.matches(pInv, pLevel) && getOriginalStorage(pInv).map(storage -> !(storage.getItem() instanceof WoodStorageBlockItem) || !WoodStorageBlockItem.isPacked(storage)).orElse(false);
}

@Override
public ShapelessRecipe getCompose() {
return compose;
Expand All @@ -44,11 +37,10 @@ public ShapelessRecipe getCompose() {
public ItemStack assemble(CraftingContainer inv, RegistryAccess registryAccess) {
ItemStack upgradedStorage = super.assemble(inv, registryAccess);
getOriginalStorage(inv).ifPresent(originalStorage -> upgradedStorage.setTag(originalStorage.getTag()));
if (upgradedStorage.getItem() instanceof ShulkerBoxItem shulkerBoxItem) {
upgradedStorage.getCapability(CapabilityStorageWrapper.getCapabilityInstance()).ifPresent(wrapper -> {
shulkerBoxItem.setNumberOfInventorySlots(upgradedStorage, wrapper.getDefaultNumberOfInventorySlots());
shulkerBoxItem.setNumberOfUpgradeSlots(upgradedStorage, wrapper.getDefaultNumberOfUpgradeSlots());
});
if (StorageBlockItem.getContentsUuid(upgradedStorage).isPresent()) {
StackStorageWrapper storageWrapper = new StackStorageWrapper(upgradedStorage);
StorageBlockItem.setNumberOfInventorySlots(upgradedStorage, storageWrapper.getDefaultNumberOfInventorySlots());
StorageBlockItem.setNumberOfUpgradeSlots(upgradedStorage, storageWrapper.getDefaultNumberOfUpgradeSlots());
}
return upgradedStorage;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@
import net.p3pp3rf1y.sophisticatedcore.settings.memory.MemorySettingsCategory;
import net.p3pp3rf1y.sophisticatedcore.util.InventoryHelper;
import net.p3pp3rf1y.sophisticatedcore.util.NBTHelper;
import net.p3pp3rf1y.sophisticatedstorage.Config;
import net.p3pp3rf1y.sophisticatedstorage.block.ItemContentsStorage;
import net.p3pp3rf1y.sophisticatedstorage.block.ShulkerBoxBlock;
import net.p3pp3rf1y.sophisticatedstorage.block.StorageBlockEntity;
import net.p3pp3rf1y.sophisticatedstorage.block.StorageWrapper;
import net.p3pp3rf1y.sophisticatedstorage.client.render.ShulkerBoxItemRenderer;
Expand Down Expand Up @@ -129,23 +127,7 @@ public <T> LazyOptional<T> getCapability(Capability<T> cap, @Nullable Direction
private void initWrapper() {
if (wrapper == null) {
UUID uuid = getContentsUuid(stack).orElse(null);
StorageWrapper storageWrapper = new StackStorageWrapper(stack) {
@Override
public String getStorageType() {
return "shulker_box";
}

@Override
public Component getDisplayName() {
return Component.translatable(ShulkerBoxItem.this.getDescriptionId());
}

@Override
protected boolean isAllowedInStorage(ItemStack stack) {
Block block = Block.byItem(stack.getItem());
return !(block instanceof ShulkerBoxBlock) && !(block instanceof net.minecraft.world.level.block.ShulkerBoxBlock) && !Config.SERVER.shulkerBoxDisallowedItems.isItemDisallowed(stack.getItem());
}
};
StorageWrapper storageWrapper = new StackStorageWrapper(stack);
if (uuid != null) {
CompoundTag compoundtag = ItemContentsStorage.get().getOrCreateStorageContents(uuid).getCompound(StorageBlockEntity.STORAGE_WRAPPER_TAG);
storageWrapper.load(compoundtag);
Expand All @@ -157,10 +139,6 @@ protected boolean isAllowedInStorage(ItemStack stack) {
};
}

private Optional<UUID> getContentsUuid(ItemStack stack) {
return NBTHelper.getUniqueId(stack, "uuid");
}

@Override
public Optional<TooltipComponent> getInventoryTooltip(ItemStack stack) {
return Optional.of(new StorageContentsTooltip(stack));
Expand Down Expand Up @@ -189,24 +167,16 @@ public StashResult getItemStashable(ItemStack storageStack, ItemStack stack) {
}).orElse(StashResult.NO_SPACE);
}

public void setNumberOfInventorySlots(ItemStack shulkerBoxStack, int numberOfInventorySlots) {
NBTHelper.putInt(shulkerBoxStack.getOrCreateTag(), "numberOfInventorySlots", numberOfInventorySlots);
}

public int getNumberOfInventorySlots(ItemStack shulkerBoxStack) {
public int getNumberOfInventorySlotsOrDefault(ItemStack shulkerBoxStack) {
int defaultNumberOfInventorySlots = shulkerBoxStack.getCapability(CapabilityStorageWrapper.getCapabilityInstance()).map(StorageWrapper::getDefaultNumberOfInventorySlots).orElse(1);
return NBTHelper.getInt(shulkerBoxStack, "numberOfInventorySlots").map(inventorySlots -> Math.max(inventorySlots, defaultNumberOfInventorySlots)).orElse(defaultNumberOfInventorySlots);
}

public int getNumberOfUpgradeSlots(ItemStack shulkerBoxStack) {
public int getNumberOfUpgradeSlotsOrDefault(ItemStack shulkerBoxStack) {
int defaultNumberOfUpgradeSlots = shulkerBoxStack.getCapability(CapabilityStorageWrapper.getCapabilityInstance()).map(StorageWrapper::getDefaultNumberOfUpgradeSlots).orElse(1);
return NBTHelper.getInt(shulkerBoxStack, "numberOfUpgradeSlots").map(numberOfUpgradeSlots -> Math.max(numberOfUpgradeSlots, defaultNumberOfUpgradeSlots)).orElse(defaultNumberOfUpgradeSlots);
}

public void setNumberOfUpgradeSlots(ItemStack shulkerBoxStack, int numberOfUpgradeSlots) {
NBTHelper.putInt(shulkerBoxStack.getOrCreateTag(), "numberOfUpgradeSlots", numberOfUpgradeSlots);
}

@Override
public boolean overrideStackedOnOther(ItemStack storageStack, Slot slot, ClickAction action, Player player) {
if (storageStack.getCount() > 1 || !slot.mayPickup(player) || slot.getItem().isEmpty() || action != ClickAction.SECONDARY) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package net.p3pp3rf1y.sophisticatedstorage.item;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.Block;
import net.p3pp3rf1y.sophisticatedcore.util.BlockItemBase;
import net.p3pp3rf1y.sophisticatedcore.util.NBTHelper;
import net.p3pp3rf1y.sophisticatedstorage.block.IStorageBlock;
import net.p3pp3rf1y.sophisticatedstorage.block.ItemContentsStorage;
import net.p3pp3rf1y.sophisticatedstorage.block.StorageBlockEntity;
import net.p3pp3rf1y.sophisticatedstorage.block.StorageWrapper;
import net.p3pp3rf1y.sophisticatedstorage.Config;
import net.p3pp3rf1y.sophisticatedstorage.block.*;

import javax.annotation.Nullable;
import java.util.Optional;
import java.util.UUID;

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

Expand Down Expand Up @@ -90,4 +90,24 @@ protected void loadSlotNumbers(CompoundTag tag) {
public int getDefaultNumberOfUpgradeSlots() {
return storageStack.getItem() instanceof BlockItemBase blockItem && blockItem.getBlock() instanceof IStorageBlock storageBlock ? storageBlock.getNumberOfUpgradeSlots() : 0;
}

@Override
public String getStorageType() {
return "irrelevant"; //because this is only used when determining upgrade errors in gui which storage stacks can't have open
}

@Override
public Component getDisplayName() {
return Component.empty(); //because this is only used when determining upgrade errors in gui which storage stacks can't have open
}

@Override
protected boolean isAllowedInStorage(ItemStack stack) {
if (!(storageStack.getItem() instanceof ShulkerBoxItem)) {
return false;
}

Block block = Block.byItem(stack.getItem());
return !(block instanceof ShulkerBoxBlock) && !(block instanceof net.minecraft.world.level.block.ShulkerBoxBlock) && !Config.SERVER.shulkerBoxDisallowedItems.isItemDisallowed(stack.getItem());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import net.p3pp3rf1y.sophisticatedstorage.block.ITintableBlockItem;

import java.util.Optional;
import java.util.UUID;

public class StorageBlockItem extends BlockItemBase implements ITintableBlockItem {

Expand All @@ -31,6 +32,22 @@ public static Optional<Integer> getAccentColorFromStack(ItemStack barrelStack) {
return getEntityWrapperTagFromStack(barrelStack).map(tag -> NBTHelper.getInt(tag, ACCENT_COLOR_TAG)).orElse(NBTHelper.getInt(barrelStack, ACCENT_COLOR_TAG));
}

public static void setNumberOfInventorySlots(ItemStack storageStack, int numberOfInventorySlots) {
NBTHelper.setInteger(storageStack, StackStorageWrapper.NUMBER_OF_INVENTORY_SLOTS_TAG, numberOfInventorySlots);
}

public static void setNumberOfUpgradeSlots(ItemStack storageStack, int numberOfUpgradeSlots) {
NBTHelper.setInteger(storageStack, StackStorageWrapper.NUMBER_OF_UPGRADE_SLOTS_TAG, numberOfUpgradeSlots);
}

public static int getNumberOfInventorySlots(ItemStack storageStack) {
return NBTHelper.getInt(storageStack, StackStorageWrapper.NUMBER_OF_INVENTORY_SLOTS_TAG).orElse(0);
}

public static int getNumberOfUpgradeSlots(ItemStack storageStack) {
return NBTHelper.getInt(storageStack, StackStorageWrapper.NUMBER_OF_UPGRADE_SLOTS_TAG).orElse(0);
}

@Override
public void setMainColor(ItemStack storageStack, int mainColor) {
storageStack.getOrCreateTag().putInt(MAIN_COLOR_TAG, mainColor);
Expand Down Expand Up @@ -72,4 +89,8 @@ public static void setShowsTier(ItemStack stack, boolean showsTier) {
stack.getOrCreateTag().putBoolean(SHOWS_TIER_TAG, false);
}
}

public static Optional<UUID> getContentsUuid(ItemStack stack) {
return NBTHelper.getUniqueId(stack, "uuid");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,23 +123,8 @@ public <T> LazyOptional<T> getCapability(Capability<T> cap, @Nullable Direction

private void initWrapper() {
if (wrapper == null) {
UUID uuid = NBTHelper.getUniqueId(stack, "uuid").orElse(null);
StorageWrapper storageWrapper = new StackStorageWrapper(stack) {
@Override
public String getStorageType() {
return "wood_storage"; //isn't really relevant because wooden storage can't have its gui open when in item form
}

@Override
public Component getDisplayName() {
return Component.empty(); //isn't really relevant because wooden storage can't have its gui open when in item form
}

@Override
protected boolean isAllowedInStorage(ItemStack stack) {
return false;
}
};
UUID uuid = getContentsUuid(stack).orElse(null);
StorageWrapper storageWrapper = new StackStorageWrapper(stack);
if (uuid != null) {
CompoundTag compoundtag = ItemContentsStorage.get().getOrCreateStorageContents(uuid).getCompound(StorageBlockEntity.STORAGE_WRAPPER_TAG);
storageWrapper.load(compoundtag);
Expand Down Expand Up @@ -167,12 +152,4 @@ public static Component getDisplayName(String descriptionId, @Nullable WoodType
}
return Component.translatable(descriptionId, Component.translatable("wood_name.sophisticatedstorage." + woodType.name().toLowerCase(Locale.ROOT)), " ");
}

public static void setNumberOfInventorySlots(ItemStack storageStack, int numberOfInventorySlots) {
NBTHelper.putInt(storageStack.getOrCreateTag(), "numberOfInventorySlots", numberOfInventorySlots);
}

public static void setNumberOfUpgradeSlots(ItemStack storageStack, int numberOfUpgradeSlots) {
NBTHelper.putInt(storageStack.getOrCreateTag(), "numberOfUpgradeSlots", numberOfUpgradeSlots);
}
}

0 comments on commit 11b92ac

Please sign in to comment.