From f45817bfd68bc6e30e7194770f6826aa2af65d62 Mon Sep 17 00:00:00 2001 From: Sollace Date: Wed, 21 Sep 2022 17:12:01 +0200 Subject: [PATCH] Add handling for inserting items directly into trinket slots --- .../mixin/trinkets/MixinScreenHandler.java | 10 ++++++ .../mixin/trinkets/MixinTrinketInventory.java | 33 +++++++++++++++++++ src/main/resources/unicopia.mixin.json | 1 + 3 files changed, 44 insertions(+) create mode 100644 src/main/java/com/minelittlepony/unicopia/mixin/trinkets/MixinTrinketInventory.java diff --git a/src/main/java/com/minelittlepony/unicopia/mixin/trinkets/MixinScreenHandler.java b/src/main/java/com/minelittlepony/unicopia/mixin/trinkets/MixinScreenHandler.java index 9c4be2ed..2a6bf29a 100644 --- a/src/main/java/com/minelittlepony/unicopia/mixin/trinkets/MixinScreenHandler.java +++ b/src/main/java/com/minelittlepony/unicopia/mixin/trinkets/MixinScreenHandler.java @@ -52,6 +52,16 @@ abstract class MixinScreenHandler { return sender.isEmpty() || (canApply(currentSlot) && (currentSlot.getStack().getCount() + sender.getCount()) <= currentSlot.getMaxItemCount(sender)); } + @Redirect(method = "canInsertItemIntoSlot", + at = @At( + value = "INVOKE", + target = "Lnet/minecraft/item/ItemStack;getMaxCount()I" + ) + ) + private static int onCanInsertItemIntoSlot(ItemStack sender, @Nullable Slot slot) { + return canApply(slot) ? slot.getMaxItemCount(sender) : sender.getMaxCount(); + } + private static boolean canApply(Slot slot) { return slot instanceof TrinketSlot; } diff --git a/src/main/java/com/minelittlepony/unicopia/mixin/trinkets/MixinTrinketInventory.java b/src/main/java/com/minelittlepony/unicopia/mixin/trinkets/MixinTrinketInventory.java new file mode 100644 index 00000000..c6a5f2bf --- /dev/null +++ b/src/main/java/com/minelittlepony/unicopia/mixin/trinkets/MixinTrinketInventory.java @@ -0,0 +1,33 @@ +package com.minelittlepony.unicopia.mixin.trinkets; + +import org.spongepowered.asm.mixin.*; + +import com.minelittlepony.unicopia.trinkets.TrinketsDelegateImpl; +import dev.emi.trinkets.api.*; +import net.minecraft.inventory.Inventory; +import net.minecraft.item.ItemStack; + +@Mixin(TrinketInventory.class) +abstract class MixinTrinketInventory implements Inventory { + + @Override + public boolean isValid(int slot, ItemStack stack) { + ItemStack existingStack = getStack(slot); + SlotReference ref = new SlotReference((TrinketInventory)(Object)this, slot); + + + int max = Math.min( + existingStack.isEmpty() ? 64 : TrinketsDelegateImpl.getMaxCount(existingStack, ref, existingStack.getMaxCount()), + stack.isEmpty() ? 64 : TrinketsDelegateImpl.getMaxCount(stack, ref, stack.getMaxCount()) + ); + + int combinedCount = stack.getCount(); + + if (ItemStack.canCombine(existingStack, stack)) { + combinedCount += existingStack.getCount(); + } + + return combinedCount <= max; + } + +} diff --git a/src/main/resources/unicopia.mixin.json b/src/main/resources/unicopia.mixin.json index 7b1a80e1..a58652d9 100644 --- a/src/main/resources/unicopia.mixin.json +++ b/src/main/resources/unicopia.mixin.json @@ -36,6 +36,7 @@ "trinkets.MixinTrinketSurvivalSlot", "trinkets.MixinTrinketCreativeSlot", "trinkets.MixinTrinketItem", + "trinkets.MixinTrinketInventory", "trinkets.MixinScreenHandler" ], "client": [