From a0bd6e22acaf6856ab5a246df011bd3a55f4ac3f Mon Sep 17 00:00:00 2001 From: gnembon <41132274+gnembon@users.noreply.github.com> Date: Thu, 29 Feb 2024 11:11:18 +0100 Subject: [PATCH] simplify stackableShulkerBoxes rule --- .../java/carpet/helpers/InventoryHelper.java | 49 ------------ ...actContainerMenu_stackableSBoxesMixin.java | 33 -------- .../java/carpet/mixins/ItemEntityMixin.java | 75 ------------------- .../ItemStack_stackableShulkerBoxesMixin.java | 28 +++++++ .../mixins/Slot_stackableSBoxesMixin.java | 31 -------- src/main/resources/carpet.mixins.json | 3 +- 6 files changed, 29 insertions(+), 190 deletions(-) delete mode 100644 src/main/java/carpet/helpers/InventoryHelper.java delete mode 100644 src/main/java/carpet/mixins/AbstractContainerMenu_stackableSBoxesMixin.java create mode 100644 src/main/java/carpet/mixins/ItemStack_stackableShulkerBoxesMixin.java delete mode 100644 src/main/java/carpet/mixins/Slot_stackableSBoxesMixin.java diff --git a/src/main/java/carpet/helpers/InventoryHelper.java b/src/main/java/carpet/helpers/InventoryHelper.java deleted file mode 100644 index 4eaa276c..00000000 --- a/src/main/java/carpet/helpers/InventoryHelper.java +++ /dev/null @@ -1,49 +0,0 @@ -package carpet.helpers; - -import net.minecraft.nbt.CompoundTag; -import net.minecraft.world.item.ItemStack; - -import static net.minecraft.nbt.Tag.TAG_COMPOUND; -import static net.minecraft.nbt.Tag.TAG_LIST; - -public class InventoryHelper -{ - public static boolean cleanUpShulkerBoxTag(ItemStack stack) - { - boolean changed = false; - CompoundTag tag = stack.getTag(); - - if (tag == null || !tag.contains("BlockEntityTag", TAG_COMPOUND)) - return false; - - CompoundTag bet = tag.getCompound("BlockEntityTag"); - if (bet.contains("Items", TAG_LIST) && bet.getList("Items", TAG_COMPOUND).isEmpty()) - { - bet.remove("Items"); - changed = true; - } - - if (bet.isEmpty() || (bet.size() == 1 && bet.getString("id").equals("minecraft:shulker_box"))) - { - tag.remove("BlockEntityTag"); - changed = true; - } - if (tag.isEmpty()) - { - stack.setTag(null); - changed = true; - } - return changed; - } - - public static boolean shulkerBoxHasItems(ItemStack stack) - { - CompoundTag tag = stack.getTag(); - - if (tag == null || !tag.contains("BlockEntityTag", TAG_COMPOUND)) - return false; - - CompoundTag bet = tag.getCompound("BlockEntityTag"); - return bet.contains("Items", TAG_LIST) && !bet.getList("Items", TAG_COMPOUND).isEmpty(); - } -} diff --git a/src/main/java/carpet/mixins/AbstractContainerMenu_stackableSBoxesMixin.java b/src/main/java/carpet/mixins/AbstractContainerMenu_stackableSBoxesMixin.java deleted file mode 100644 index bb7dea28..00000000 --- a/src/main/java/carpet/mixins/AbstractContainerMenu_stackableSBoxesMixin.java +++ /dev/null @@ -1,33 +0,0 @@ -package carpet.mixins; - -import carpet.CarpetSettings; -import carpet.helpers.InventoryHelper; -import net.minecraft.world.inventory.AbstractContainerMenu; -import net.minecraft.world.inventory.Slot; -import net.minecraft.world.item.BlockItem; -import net.minecraft.world.item.ItemStack; -import net.minecraft.world.level.block.ShulkerBoxBlock; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Redirect; - -@Mixin(AbstractContainerMenu.class) -public class AbstractContainerMenu_stackableSBoxesMixin -{ - @Redirect(method = "doClick", at = @At( - value = "INVOKE", - target = "Lnet/minecraft/world/inventory/Slot;getMaxStackSize(Lnet/minecraft/world/item/ItemStack;)I" - )) - private int getMaxCountForSboxesInScreenHander(Slot slot, ItemStack stack) - { - if (CarpetSettings.shulkerBoxStackSize > 1 && - stack.getItem() instanceof BlockItem && - ((BlockItem)stack.getItem()).getBlock() instanceof ShulkerBoxBlock && - !InventoryHelper.shulkerBoxHasItems(stack) - ) - { - return CarpetSettings.shulkerBoxStackSize; - } - return slot.getMaxStackSize(stack); - } -} diff --git a/src/main/java/carpet/mixins/ItemEntityMixin.java b/src/main/java/carpet/mixins/ItemEntityMixin.java index 04731b21..dbb247dd 100644 --- a/src/main/java/carpet/mixins/ItemEntityMixin.java +++ b/src/main/java/carpet/mixins/ItemEntityMixin.java @@ -1,24 +1,15 @@ package carpet.mixins; import carpet.CarpetSettings; -import carpet.helpers.InventoryHelper; import net.minecraft.server.level.ServerLevel; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.LightningBolt; import net.minecraft.world.entity.item.ItemEntity; -import net.minecraft.world.item.BlockItem; -import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.Level; -import net.minecraft.world.level.block.ShulkerBoxBlock; import carpet.fakes.ItemEntityInterface; -import java.util.Objects; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.Redirect; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @Mixin(ItemEntity.class) public abstract class ItemEntityMixin extends Entity implements ItemEntityInterface @@ -45,70 +36,4 @@ public abstract class ItemEntityMixin extends Entity implements ItemEntityInterf public int getPickupDelayCM() { return this.pickupDelay; } - - @Inject(method="(Lnet/minecraft/world/level/Level;DDDLnet/minecraft/world/item/ItemStack;)V", at = @At("RETURN")) - private void removeEmptyShulkerBoxTags(Level worldIn, double x, double y, double z, ItemStack stack, CallbackInfo ci) - { - if (CarpetSettings.shulkerBoxStackSize > 1 - && stack.getItem() instanceof BlockItem bi - && bi.getBlock() instanceof ShulkerBoxBlock) - { - InventoryHelper.cleanUpShulkerBoxTag(stack); - } - } - - @Redirect( - method = "isMergable()Z", - at = @At( - value = "INVOKE", - target = "Lnet/minecraft/world/item/ItemStack;getMaxStackSize()I" - ) - ) - private int getItemStackMaxAmount(ItemStack stack) { - if (CarpetSettings.shulkerBoxStackSize > 1 && stack.getItem() instanceof BlockItem && ((BlockItem)stack.getItem()).getBlock() instanceof ShulkerBoxBlock) - return CarpetSettings.shulkerBoxStackSize; - - return stack.getMaxStackSize(); - } - - @Inject( - method = "tryToMerge(Lnet/minecraft/world/entity/item/ItemEntity;)V", - at = @At("HEAD"), - cancellable = true - ) - private void tryStackShulkerBoxes(ItemEntity other, CallbackInfo ci) - { - ItemEntity self = (ItemEntity)(Object)this; - ItemStack selfStack = self.getItem(); - if (CarpetSettings.shulkerBoxStackSize == 1 || !(selfStack.getItem() instanceof BlockItem bi) || !(bi.getBlock() instanceof ShulkerBoxBlock)) { - return; - } - - ItemStack otherStack = other.getItem(); - if (selfStack.getItem() == otherStack.getItem() - && !InventoryHelper.shulkerBoxHasItems(selfStack) - && !InventoryHelper.shulkerBoxHasItems(otherStack) - && Objects.equals(selfStack.getTag(), otherStack.getTag()) // empty block entity tags are cleaned up when spawning - && selfStack.getCount() != CarpetSettings.shulkerBoxStackSize) - { - int amount = Math.min(otherStack.getCount(), CarpetSettings.shulkerBoxStackSize - selfStack.getCount()); - - selfStack.grow(amount); - self.setItem(selfStack); - - this.pickupDelay = Math.max(((ItemEntityInterface)other).getPickupDelayCM(), this.pickupDelay); - this.age = Math.min(other.getAge(), this.age); - - otherStack.shrink(amount); - if (otherStack.isEmpty()) - { - other.discard(); - } - else - { - other.setItem(otherStack); - } - ci.cancel(); - } - } } diff --git a/src/main/java/carpet/mixins/ItemStack_stackableShulkerBoxesMixin.java b/src/main/java/carpet/mixins/ItemStack_stackableShulkerBoxesMixin.java new file mode 100644 index 00000000..4f11e6de --- /dev/null +++ b/src/main/java/carpet/mixins/ItemStack_stackableShulkerBoxesMixin.java @@ -0,0 +1,28 @@ +package carpet.mixins; + +import carpet.CarpetSettings; +import net.minecraft.core.component.DataComponents; +import net.minecraft.world.item.BlockItem; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.component.ItemContainerContents; +import net.minecraft.world.level.block.ShulkerBoxBlock; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +@Mixin(ItemStack.class) +public class ItemStack_stackableShulkerBoxesMixin +{ + @Inject(method = "getMaxStackSize", at = @At("HEAD"), cancellable = true) + private void getCMMAxStackSize(CallbackInfoReturnable cir) + { + if (CarpetSettings.shulkerBoxStackSize > 1 + && ((ItemStack)((Object)this)).getItem() instanceof BlockItem blockItem + && blockItem.getBlock() instanceof ShulkerBoxBlock + && ((ItemStack) ((Object) this)).getOrDefault(DataComponents.CONTAINER, ItemContainerContents.EMPTY).stream().findAny().isEmpty() + ) { + cir.setReturnValue(CarpetSettings.shulkerBoxStackSize); + } + } +} diff --git a/src/main/java/carpet/mixins/Slot_stackableSBoxesMixin.java b/src/main/java/carpet/mixins/Slot_stackableSBoxesMixin.java deleted file mode 100644 index 51971583..00000000 --- a/src/main/java/carpet/mixins/Slot_stackableSBoxesMixin.java +++ /dev/null @@ -1,31 +0,0 @@ -package carpet.mixins; - -import carpet.CarpetSettings; -import carpet.helpers.InventoryHelper; -import net.minecraft.world.inventory.Slot; -import net.minecraft.world.item.BlockItem; -import net.minecraft.world.item.ItemStack; -import net.minecraft.world.level.block.ShulkerBoxBlock; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Redirect; - -@Mixin(Slot.class) -public class Slot_stackableSBoxesMixin { - @Redirect(method = "safeInsert(Lnet/minecraft/world/item/ItemStack;I)Lnet/minecraft/world/item/ItemStack;", at = @At( - value = "INVOKE", - target = "Lnet/minecraft/world/inventory/Slot;getMaxStackSize(Lnet/minecraft/world/item/ItemStack;)I" - )) - private int getMaxCountForSboxesInSlot(Slot slot, ItemStack stack) - { - if (CarpetSettings.shulkerBoxStackSize > 1 && - stack.getItem() instanceof BlockItem && - ((BlockItem)stack.getItem()).getBlock() instanceof ShulkerBoxBlock && - !InventoryHelper.shulkerBoxHasItems(stack) - ) - { - return CarpetSettings.shulkerBoxStackSize; - } - return slot.getMaxStackSize(stack); - } -} diff --git a/src/main/resources/carpet.mixins.json b/src/main/resources/carpet.mixins.json index c43e4b5a..1e5b1b3f 100644 --- a/src/main/resources/carpet.mixins.json +++ b/src/main/resources/carpet.mixins.json @@ -101,8 +101,7 @@ "AbstractContainerMenu_ctrlQCraftingMixin", "Connection_packetCounterMixin", "AbstractCauldronBlock_stackableSBoxesMixin", - "Slot_stackableSBoxesMixin", - "AbstractContainerMenu_stackableSBoxesMixin", + "ItemStack_stackableShulkerBoxesMixin", "ItemEntityMixin", "TntBlock_noUpdateMixin", "Explosion_optimizedTntMixin",