fix: emptyShulkerBoxStack

This commit is contained in:
zly2006 committed 2024-07-11 12:14:02 +08:00
1 parent 43a6b49e9e
commit dd7d6e9de4
6 files changed
+93 -24

No files matched your search

@@ -1,23 +1,23 @@
package com.github.zly2006.carpetslsaddition.mixin.shulker;
import com.github.zly2006.carpetslsaddition.SLSCarpetSettings;
import com.github.zly2006.carpetslsaddition.util.ShulkerBoxItemUtil;
import net.minecraft.item.ItemStack;
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;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@Mixin(ItemStack.class)
public class MixinItemStack {
@Inject(
method = "getMaxCount",
method = "capCount",
at = @At("HEAD"),
cancellable = true
)
private void getMaxCount(CallbackInfoReturnable<Integer> cir) {
if (SLSCarpetSettings.emptyShulkerBoxStack && ShulkerBoxItemUtil.isEmptyShulkerBoxItem((ItemStack) (Object) this)) {
cir.setReturnValue(ShulkerBoxItemUtil.SHULKERBOX_MAX_STACK_AMOUNT);
private void capCount(int maxCount, CallbackInfo ci) {
if (ShulkerBoxItemUtil.isEmptyShulkerBoxItem((ItemStack) (Object) this)) {
ci.cancel();
}
}
}
@@ -0,0 +1,35 @@
package com.github.zly2006.carpetslsaddition.mixin.shulker;
import com.github.zly2006.carpetslsaddition.SLSCarpetSettings;
import com.github.zly2006.carpetslsaddition.util.ShulkerBoxItemUtil;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemStack;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
@Mixin(PlayerInventory.class)
public abstract class MixinPlayerInventory implements Inventory {
@Redirect(
method = "canStackAddMore",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/item/ItemStack;isStackable()Z"
)
)
private boolean isStackable(ItemStack instance) {
if (SLSCarpetSettings.emptyShulkerBoxStack && ShulkerBoxItemUtil.isEmptyShulkerBoxItem(instance)) {
return true;
}
return instance.isStackable();
}
@Override
public int getMaxCount(ItemStack stack) {
if (SLSCarpetSettings.emptyShulkerBoxStack && ShulkerBoxItemUtil.isEmptyShulkerBoxItem(stack)) {
return ShulkerBoxItemUtil.SHULKERBOX_MAX_STACK_AMOUNT;
}
return Inventory.super.getMaxCount(stack);
}
}
@@ -1,26 +1,35 @@
package com.github.zly2006.carpetslsaddition.mixin.shulker;
import com.github.zly2006.carpetslsaddition.util.ShulkerBoxItemUtil;
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import net.minecraft.item.ItemStack;
import net.minecraft.screen.ScreenHandler;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
@Mixin(ScreenHandler.class)
public abstract class MixinScreenHandler {
@WrapOperation(
method = "internalOnSlotClick",
@Redirect(
method = "*",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/item/ItemStack;areItemsAndComponentsEqual(Lnet/minecraft/item/ItemStack;Lnet/minecraft/item/ItemStack;)Z"
)
)
private boolean checkEqual(ItemStack stack, ItemStack otherStack, Operation<Boolean> original) {
if (ShulkerBoxItemUtil.shulkerBoxEqual(stack, otherStack)) {
return true;
}
return original.call(stack, otherStack);
private boolean checkEqual(ItemStack stack, ItemStack otherStack) {
if (ShulkerBoxItemUtil.shulkerBoxEqual(stack, otherStack)) return true;
return ItemStack.areItemsAndComponentsEqual(stack, otherStack);
}
@Redirect(
method = "*",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/item/ItemStack;isStackable()Z"
)
)
private boolean isStackable(ItemStack stack) {
if (ShulkerBoxItemUtil.isEmptyShulkerBoxItem(stack)) return true;
return stack.isStackable();
}
}
@@ -0,0 +1,26 @@
package com.github.zly2006.carpetslsaddition.mixin.shulker;
import com.github.zly2006.carpetslsaddition.SLSCarpetSettings;
import com.github.zly2006.carpetslsaddition.util.ShulkerBoxItemUtil;
import net.minecraft.item.ItemStack;
import net.minecraft.server.network.ServerPlayNetworkHandler;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
@Mixin(ServerPlayNetworkHandler.class)
public class MixinServerPlayNetworkHandler {
@Redirect(
method = "onCreativeInventoryAction",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/item/ItemStack;getMaxCount()I"
)
)
private int getMaxCount(ItemStack instance) {
if (SLSCarpetSettings.emptyShulkerBoxStack && ShulkerBoxItemUtil.isEmptyShulkerBoxItem(instance)) {
return ShulkerBoxItemUtil.SHULKERBOX_MAX_STACK_AMOUNT;
}
return instance.getMaxCount();
}
}
@@ -2,11 +2,8 @@ package com.github.zly2006.carpetslsaddition.util;
import net.minecraft.block.ShulkerBoxBlock;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.NbtComponent;
import net.minecraft.item.BlockItem;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtList;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import java.util.Objects;
@@ -19,8 +16,8 @@ public class ShulkerBoxItemUtil {
return true;
}
return Objects.equals(
stack.getComponents().filtered(s -> s != DataComponentTypes.BLOCK_ENTITY_DATA),
stack.getComponents().filtered(s -> s != DataComponentTypes.BLOCK_ENTITY_DATA)
stack.getComponents().filtered(s -> s != DataComponentTypes.CONTAINER),
stack.getComponents().filtered(s -> s != DataComponentTypes.CONTAINER)
);
}
return ItemStack.areItemsAndComponentsEqual(stack, otherStack);
@@ -29,11 +26,11 @@ public class ShulkerBoxItemUtil {
public static boolean isEmptyShulkerBoxItem(ItemStack itemStack) {
if (itemStack.getItem() instanceof BlockItem &&
((BlockItem) itemStack.getItem()).getBlock() instanceof ShulkerBoxBlock) {
NbtComponent tag = itemStack.getComponents().get(DataComponentTypes.BLOCK_ENTITY_DATA);
if (tag != null && tag.contains("Items")) {
return tag.getNbt().getList("Items", 10).isEmpty();
if (itemStack.getComponents().contains(DataComponentTypes.CONTAINER)) {
return !itemStack.getComponents().get(DataComponentTypes.CONTAINER).iterateNonEmpty().iterator().hasNext();
} else {
return true;
}
return true;
} else {
return false;
}
@@ -25,7 +25,9 @@
"screen.MixinAnvilScreenHandler",
"server.MixinMinecraftServer",
"shulker.MixinItemStack",
"shulker.MixinPlayerInventory",
"shulker.MixinScreenHandler",
"shulker.MixinServerPlayNetworkHandler",
"shulker.MixinSlot",
"world.MixinSleepManager",
"world.MixinSpawnHelper",