Improve empty shulker box merging a bit

- Prevents boxes with different nbt from stacking. Fixes #1596
- Makes stacks partially be able merge to max size when sum would be greater than it (by keeping both entities around, but lowering its count)

Pending for the future: Checking why client doesn't render stack size changes
This commit is contained in:
altrisi authored and GitHub committed 2022-11-22 12:48:04 +01:00
1 parent 2fd48c31ff
commit 176b576c30
1 file changed
+8 -11
@@ -12,6 +12,7 @@ import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level; import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.ShulkerBoxBlock; import net.minecraft.world.level.block.ShulkerBoxBlock;
import carpet.fakes.ItemEntityInterface; import carpet.fakes.ItemEntityInterface;
import java.util.Objects;
import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.At;
@@ -22,8 +23,6 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@Mixin(ItemEntity.class) @Mixin(ItemEntity.class)
public abstract class ItemEntityMixin extends Entity implements ItemEntityInterface public abstract class ItemEntityMixin extends Entity implements ItemEntityInterface
{ {
private static final int SHULKERBOX_MAX_STACK_AMOUNT = 64;
@Shadow private int age; @Shadow private int age;
@Shadow private int pickupDelay; @Shadow private int pickupDelay;
@@ -51,12 +50,10 @@ public abstract class ItemEntityMixin extends Entity implements ItemEntityInterf
private void removeEmptyShulkerBoxTags(Level worldIn, double x, double y, double z, ItemStack stack, CallbackInfo ci) private void removeEmptyShulkerBoxTags(Level worldIn, double x, double y, double z, ItemStack stack, CallbackInfo ci)
{ {
if (CarpetSettings.shulkerBoxStackSize > 1 if (CarpetSettings.shulkerBoxStackSize > 1
&& stack.getItem() instanceof BlockItem && stack.getItem() instanceof BlockItem bi
&& ((BlockItem)stack.getItem()).getBlock() instanceof ShulkerBoxBlock) && bi.getBlock() instanceof ShulkerBoxBlock)
{ {
if (InventoryHelper.cleanUpShulkerBoxTag(stack)) { InventoryHelper.cleanUpShulkerBoxTag(stack);
((ItemEntity) (Object) this).setItem(stack);
}
} }
} }
@@ -83,7 +80,7 @@ public abstract class ItemEntityMixin extends Entity implements ItemEntityInterf
{ {
ItemEntity self = (ItemEntity)(Object)this; ItemEntity self = (ItemEntity)(Object)this;
ItemStack selfStack = self.getItem(); ItemStack selfStack = self.getItem();
if (CarpetSettings.shulkerBoxStackSize == 1 || !(selfStack.getItem() instanceof BlockItem) || !(((BlockItem)selfStack.getItem()).getBlock() instanceof ShulkerBoxBlock)) { if (CarpetSettings.shulkerBoxStackSize == 1 || !(selfStack.getItem() instanceof BlockItem bi) || !(bi.getBlock() instanceof ShulkerBoxBlock)) {
return; return;
} }
@@ -91,8 +88,8 @@ public abstract class ItemEntityMixin extends Entity implements ItemEntityInterf
if (selfStack.getItem() == otherStack.getItem() if (selfStack.getItem() == otherStack.getItem()
&& !InventoryHelper.shulkerBoxHasItems(selfStack) && !InventoryHelper.shulkerBoxHasItems(selfStack)
&& !InventoryHelper.shulkerBoxHasItems(otherStack) && !InventoryHelper.shulkerBoxHasItems(otherStack)
&& selfStack.hasTag() == otherStack.hasTag() && Objects.equals(selfStack.getTag(), otherStack.getTag()) // empty block entity tags are cleaned up when spawning
&& selfStack.getCount() + otherStack.getCount() <= CarpetSettings.shulkerBoxStackSize) && selfStack.getCount() != CarpetSettings.shulkerBoxStackSize)
{ {
int amount = Math.min(otherStack.getCount(), CarpetSettings.shulkerBoxStackSize - selfStack.getCount()); int amount = Math.min(otherStack.getCount(), CarpetSettings.shulkerBoxStackSize - selfStack.getCount());
@@ -105,7 +102,7 @@ public abstract class ItemEntityMixin extends Entity implements ItemEntityInterf
otherStack.shrink(amount); otherStack.shrink(amount);
if (otherStack.isEmpty()) if (otherStack.isEmpty())
{ {
other.discard(); // discard remove(); other.discard();
} }
else else
{ {