From 6863ebed7561c5e0497edcc823b1c168a2ad9788 Mon Sep 17 00:00:00 2001 From: FirstMegaGame4 <84094287+FirstMegaGame4@users.noreply.github.com> Date: Mon, 26 Feb 2024 19:39:01 +0100 Subject: [PATCH] Add CustomItemAcceptingStacks --- .../items/CustomItemAcceptingStacks.java | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 src/main/java/com/mmodding/mmodding_lib/library/items/CustomItemAcceptingStacks.java diff --git a/src/main/java/com/mmodding/mmodding_lib/library/items/CustomItemAcceptingStacks.java b/src/main/java/com/mmodding/mmodding_lib/library/items/CustomItemAcceptingStacks.java new file mode 100644 index 0000000..9fecb34 --- /dev/null +++ b/src/main/java/com/mmodding/mmodding_lib/library/items/CustomItemAcceptingStacks.java @@ -0,0 +1,71 @@ +package com.mmodding.mmodding_lib.library.items; + +import com.mmodding.mmodding_lib.library.utils.FilterList; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.inventory.StackReference; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.screen.slot.Slot; +import net.minecraft.sound.SoundEvent; +import net.minecraft.util.ClickType; + +import java.util.concurrent.atomic.AtomicBoolean; + +public abstract class CustomItemAcceptingStacks extends Item implements ItemRegistrable { + + private final AtomicBoolean registered = new AtomicBoolean(false); + + public CustomItemAcceptingStacks(Settings settings) { + super(settings); + } + + @Override + public boolean onClickedOnOther(ItemStack thisStack, Slot otherSlot, ClickType clickType, PlayerEntity player) { + if (clickType != ClickType.RIGHT) { + return false; + } else { + ItemStack itemStack = otherSlot.getStack(); + if (this.getFilter().check(itemStack.getItem())) { + boolean sound = otherSlot.getStack().getCount() > 0; + this.receiveStack(otherSlot.takeStack(1)); + if (sound) { + this.playReceiveStackSound(player); + } + } + + return true; + } + } + + @Override + public boolean onClicked(ItemStack thisStack, ItemStack otherStack, Slot thisSlot, ClickType clickType, PlayerEntity player, StackReference cursorStackReference) { + if (clickType == ClickType.RIGHT && thisSlot.canTakePartial(player)) { + if (this.getFilter().check(otherStack.getItem())) { + boolean sound = otherStack.getCount() > 0; + if (sound) { + this.receiveStack(otherStack); + otherStack.decrement(otherStack.getCount()); + } + } + return true; + } else { + return false; + } + } + + public abstract void receiveStack(ItemStack stack); + + public abstract void playReceiveStackSound(PlayerEntity player); + + public abstract FilterList getFilter(); + + @Override + public boolean isNotRegistered() { + return !this.registered.get(); + } + + @Override + public void setRegistered() { + this.registered.set(true); + } +}