Skip to content

Commit

Permalink
feat: ✨ Added stashing to Shulker box minecarts so that items can eas…
Browse files Browse the repository at this point in the history
…ily be put in the minecart when it is in player's inventory
  • Loading branch information
P3pp3rF1y committed Jan 16, 2025
1 parent 75df195 commit 31a02d9
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 11 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=sophisticatedstorageinmotion
mod_name=Sophisticated Storage In Motion
mod_license=GNU General Public License v3.0
mod_version=0.8.0
mod_version=0.9.0
mod_group_id=sophisticatedstorageinmotion
mod_authors=P3pp3rF1y
mod_description=Sophisticated Storage on moving entities.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import net.neoforged.neoforge.event.level.LevelEvent;
import net.neoforged.neoforge.network.PacketDistributor;
import net.p3pp3rf1y.sophisticatedcore.client.render.ClientStorageContentsTooltipBase;
import net.p3pp3rf1y.sophisticatedstorageinmotion.entity.MovingStorageWrapper;
import net.p3pp3rf1y.sophisticatedstorageinmotion.item.MovingStorageItem;
import net.p3pp3rf1y.sophisticatedstorageinmotion.network.RequestMovingStorageInventoryContentsPayload;

Expand All @@ -24,7 +23,7 @@ public static void onWorldLoad(LevelEvent.Load event) {

@Override
public void renderImage(Font font, int leftX, int topY, GuiGraphics guiGraphics) {
renderTooltip(MovingStorageWrapper.fromStack(movingStorage, () -> {}, () -> {}), font, leftX, topY, guiGraphics);
renderTooltip(MovingStorageItem.getMovingStorageWrapper(movingStorage), font, leftX, topY, guiGraphics);
}

public ClientMovingStorageContentsTooltip(MovingStorageItem.MovingStorageContentsTooltip tooltip) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ private MovingStorageWrapper(ItemStack storageStack, Runnable onContentsChanged,
contentsChangeHandler = onContentsChanged;
stackChangeHandler = onStackChanged;
renderInfo = new MovingStorageRenderInfo(storageStack);
MovingStorageData.get(getContentsUuid().orElseGet(this::getNewUuid));

if (EntityStorageHolder.isLimitedBarrel(storageStack)) {
registerUpgradeDefaultsHandler(VoidUpgradeWrapper.class, LimitedBarrelBlockEntity.VOID_UPGRADE_VOIDING_OVERFLOW_OF_EVERYTHING_BY_DEFAULT);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package net.p3pp3rf1y.sophisticatedstorageinmotion.item;

import net.minecraft.client.gui.screens.inventory.CreativeModeInventoryScreen;
import net.minecraft.core.HolderLookup;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.SlotAccess;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.ClickAction;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.inventory.tooltip.TooltipComponent;
import net.minecraft.world.item.DyeColor;
import net.minecraft.world.item.Item;
Expand All @@ -27,6 +32,7 @@

import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.function.Consumer;

import static net.p3pp3rf1y.sophisticatedstorage.block.DecorationTableBlockEntity.STORAGE_DECORATOR;
Expand Down Expand Up @@ -104,7 +110,7 @@ public Component getName(ItemStack stack) {
@Override
public Optional<TooltipComponent> getTooltipImage(ItemStack stack) {
if (FMLEnvironment.dist.isClient()) {
return Optional.ofNullable(MovingStorageItemClient.getTooltipImage(getStorageItem(stack)));
return Optional.ofNullable(MovingStorageItemClient.getTooltipImage(stack));
}
return Optional.empty();
}
Expand All @@ -116,8 +122,8 @@ public Optional<TooltipComponent> getInventoryTooltip(ItemStack stack) {

@Override
public StashResult getItemStashable(HolderLookup.Provider registries, ItemStack storageStack, ItemStack stack) {
if (getStorageItemType(stack).map(item -> item instanceof ShulkerBoxItem).orElse(false)) {
MovingStorageWrapper wrapper = MovingStorageWrapper.fromStack(getStorageItem(storageStack), () -> {}, () -> {});
if (getStorageItemType(storageStack).map(item -> item instanceof ShulkerBoxItem).orElse(false)) {
MovingStorageWrapper wrapper = getMovingStorageWrapper(storageStack);

if (wrapper.getInventoryForUpgradeProcessing().insertItem(stack, true).getCount() == stack.getCount()) {
return StashResult.NO_SPACE;
Expand All @@ -132,6 +138,63 @@ public StashResult getItemStashable(HolderLookup.Provider registries, ItemStack
return StashResult.NO_SPACE;
}

public static MovingStorageWrapper getMovingStorageWrapper(ItemStack movingStorageStack) {
ItemStack storageItem = getStorageItem(movingStorageStack);
MovingStorageWrapper wrapper = MovingStorageWrapper.fromStack(storageItem, () -> {},
() -> movingStorageStack.set(ModDataComponents.STORAGE_ITEM, SimpleItemContent.copyOf(storageItem)));
return wrapper;
}

public ItemStack stash(ItemStack movingStorageStack, ItemStack stack, boolean simulate) {
MovingStorageWrapper wrapper = getMovingStorageWrapper(movingStorageStack);
if (wrapper.getContentsUuid().isEmpty()) {
wrapper.setContentsUuid(UUID.randomUUID());
}
return wrapper.getInventoryForUpgradeProcessing().insertItem(stack, simulate);
}

@Override
public boolean overrideStackedOnOther(ItemStack stack, Slot slot, ClickAction action, Player player) {
if (hasCreativeScreenContainerOpen(player) || stack.getCount() > 1 || !slot.mayPickup(player) || slot.getItem().isEmpty() || action != ClickAction.SECONDARY || !isShulkerBoxMovingStorage(stack)) {
return super.overrideStackedOnOther(stack, slot, action, player);
}

ItemStack stackToStash = slot.getItem();
ItemStack stashResult = stash(stack, stackToStash, true);
if (stashResult.getCount() != stackToStash.getCount()) {
int countToTake = stackToStash.getCount() - stashResult.getCount();
ItemStack takeResult = slot.safeTake(countToTake, countToTake, player);
stash(stack, takeResult, false);
return true;
}

return super.overrideStackedOnOther(stack, slot, action, player);
}

private boolean isShulkerBoxMovingStorage(ItemStack movingStorageStack) {
return getStorageItemType(movingStorageStack).map(item -> item instanceof ShulkerBoxItem).orElse(false);
}

@Override
public boolean overrideOtherStackedOnMe(ItemStack stack, ItemStack otherStack, Slot slot, ClickAction action, Player player, SlotAccess carriedAccess) {
if (hasCreativeScreenContainerOpen(player) || stack.getCount() > 1 || !slot.mayPlace(stack) || action != ClickAction.SECONDARY || !isShulkerBoxMovingStorage(stack)) {
return super.overrideOtherStackedOnMe(stack, otherStack, slot, action, player, carriedAccess);
}

ItemStack result = stash(stack, otherStack, false);
if (result.getCount() != otherStack.getCount()) {
carriedAccess.set(result);
slot.set(stack);
return true;
}

return super.overrideOtherStackedOnMe(stack, otherStack, slot, action, player, carriedAccess);
}

private boolean hasCreativeScreenContainerOpen(Player player) {
return player.level().isClientSide() && player.containerMenu instanceof CreativeModeInventoryScreen.ItemPickerMenu;
}

public record MovingStorageContentsTooltip(ItemStack movingStorage) implements TooltipComponent {
public ItemStack getMovingStorage() {
return movingStorage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.neoforged.neoforge.network.handling.IPayloadContext;
import net.p3pp3rf1y.sophisticatedcore.client.render.ClientStorageContentsTooltipBase;
import net.p3pp3rf1y.sophisticatedstorageinmotion.SophisticatedStorageInMotion;
import net.p3pp3rf1y.sophisticatedstorageinmotion.entity.MovingStorageData;

Expand All @@ -28,5 +29,6 @@ public Type<? extends CustomPacketPayload> type() {

public static void handlePayload(MovingStorageContentsPayload payload, IPayloadContext context) {
MovingStorageData.get(payload.storageUuid).setContents(payload.storageUuid, payload.contents);
ClientStorageContentsTooltipBase.refreshContents();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import net.neoforged.neoforge.network.handling.IPayloadContext;
import net.p3pp3rf1y.sophisticatedcore.inventory.InventoryHandler;
import net.p3pp3rf1y.sophisticatedcore.upgrades.UpgradeHandler;
import net.p3pp3rf1y.sophisticatedstorage.block.StorageWrapper;
import net.p3pp3rf1y.sophisticatedstorageinmotion.SophisticatedStorageInMotion;
import net.p3pp3rf1y.sophisticatedstorageinmotion.entity.MovingStorageData;

Expand All @@ -29,19 +30,25 @@ public Type<? extends CustomPacketPayload> type() {
}

public static void handlePayload(RequestMovingStorageInventoryContentsPayload payload, IPayloadContext context) {
CompoundTag movingStorageContents = MovingStorageData.get(payload.storageUuid).getContents();
CompoundTag baseContentsTag = MovingStorageData.get(payload.storageUuid).getContents();
if (!baseContentsTag.contains(StorageWrapper.CONTENTS_TAG)) {
return;
}
CompoundTag contentsTag = baseContentsTag.getCompound(StorageWrapper.CONTENTS_TAG);

CompoundTag inventoryContents = new CompoundTag();
Tag inventoryNbt = movingStorageContents.get(InventoryHandler.INVENTORY_TAG);
Tag inventoryNbt = contentsTag.get(InventoryHandler.INVENTORY_TAG);
if (inventoryNbt != null) {
inventoryContents.put(InventoryHandler.INVENTORY_TAG, inventoryNbt);
}
Tag upgradeNbt = movingStorageContents.get(UpgradeHandler.UPGRADE_INVENTORY_TAG);
Tag upgradeNbt = contentsTag.get(UpgradeHandler.UPGRADE_INVENTORY_TAG);
if (upgradeNbt != null) {
inventoryContents.put(UpgradeHandler.UPGRADE_INVENTORY_TAG, upgradeNbt);
}
CompoundTag newBaseContentsTag = new CompoundTag();
newBaseContentsTag.put(StorageWrapper.CONTENTS_TAG, inventoryContents);
if (context.player() instanceof ServerPlayer serverPlayer) {
PacketDistributor.sendToPlayer(serverPlayer, new MovingStorageContentsPayload(payload.storageUuid, inventoryContents));
PacketDistributor.sendToPlayer(serverPlayer, new MovingStorageContentsPayload(payload.storageUuid, newBaseContentsTag));
}
}
}

0 comments on commit 31a02d9

Please sign in to comment.