From c2da0579d329e36d274326794dcf3487767c1b1d Mon Sep 17 00:00:00 2001 From: alikindsys Date: Sat, 30 Mar 2024 21:47:19 -0300 Subject: [PATCH] Add custom handling for breaking empty shulker boxes --- .../bvextension/utils/Inventory.java | 62 ++++++++++++++++++- 1 file changed, 60 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/blocovermelho/bvextension/utils/Inventory.java b/src/main/java/org/blocovermelho/bvextension/utils/Inventory.java index ea505f7..5e84794 100644 --- a/src/main/java/org/blocovermelho/bvextension/utils/Inventory.java +++ b/src/main/java/org/blocovermelho/bvextension/utils/Inventory.java @@ -1,19 +1,30 @@ package org.blocovermelho.bvextension.utils; +import carpet.CarpetSettings; import net.minecraft.block.BlockState; +import net.minecraft.block.ShulkerBoxBlock; import net.minecraft.block.entity.BlockEntity; import net.minecraft.block.entity.ShulkerBoxBlockEntity; -import net.minecraft.entity.Entity; import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.item.Item; import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NbtCompound; import net.minecraft.server.world.ServerWorld; import net.minecraft.sound.SoundCategory; import net.minecraft.sound.SoundEvents; import net.minecraft.stat.Stats; +import net.minecraft.text.LiteralTextContent; +import net.minecraft.text.Text; +import net.minecraft.text.TranslatableTextContent; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; +import java.util.ArrayList; +import java.util.List; + +import static carpet.helpers.InventoryHelper.TAG_COMPOUND; +import static carpet.helpers.InventoryHelper.TAG_LIST; import static net.minecraft.block.Block.dropStack; import static net.minecraft.block.Block.getDroppedStacks; @@ -26,7 +37,15 @@ public static void putItem(BlockState state, World world, BlockPos pos, BlockEnt var count = is.getCount(); if (blockEntity instanceof ShulkerBoxBlockEntity sbbe) { - is.removeSubNbt("BlockEntityTag"); + if (sbbe.isEmpty()) { is.removeSubNbt("BlockEntityTag"); } + int candidate = SBox.getCandidate(sbbe, entity.getInventory()); + if (candidate != -1) { + ItemStack slot = entity.getInventory().getStack(candidate); + slot.increment(1); + entity.getInventory().setStack(candidate,slot); + entity.increaseStat(Stats.PICKED_UP.getOrCreateStat(item), count); + return; + } } if (entity.getInventory().insertStack(is)) { @@ -38,4 +57,43 @@ public static void putItem(BlockState state, World world, BlockPos pos, BlockEnt }); } } + + public class SBox { + public static boolean HasItem(ItemStack sboxItem) { + NbtCompound tag = sboxItem.getNbt(); + + if (tag == null || !tag.contains("BlockEntityTag", TAG_COMPOUND)) + return false; + + NbtCompound bet = tag.getCompound("BlockEntityTag"); + return bet.contains("Items", TAG_LIST) && !bet.getList("Items", TAG_COMPOUND).isEmpty(); + } + public static int getCandidate(ShulkerBoxBlockEntity box, net.minecraft.inventory.Inventory inventory) { + if (CarpetSettings.stackableShulkerBoxes.equals("false") + || CarpetSettings.shulkerBoxStackSize == 1 + || !box.isEmpty() + ) { + return -1; + } + + Item boxKind = ShulkerBoxBlock.getItemStack(box.getColor()).getItem(); + for (int i = 0; i < inventory.size(); i++) { + ItemStack stack = inventory.getStack(i); + if (stack.isEmpty()) continue; + if (!stack.isOf(boxKind)) continue; + if (SBox.HasItem(stack)) continue; + + Text stackName = stack.getName(); + Text boxName = box.getName(); + + if (!box.hasCustomName() && stackName.getContent() instanceof LiteralTextContent) continue; + if (box.hasCustomName() && !stackName.equals(boxName)) continue; + if (stack.getCount() + 1 > CarpetSettings.shulkerBoxStackSize) continue; + + return i; + } + return -1; + } + } + }