Jump to content

[1.20.1] How to limit a slot to only 1 item?


Recommended Posts

I am trying to make a machine that has an upgrade slot that has an item limit of 1. I've tried overriding a lot of functions and even making a custom SlotItemHandler class, but nothing worked.

How would one go about making this? 

Link to comment
Share on other sites

i have not test it but 
you must extend the slot class overriding the max stack size to 1 

	public int getMaxStackSize() {
return this.container.getMaxStackSize();
}
	

 

then in the menu class use your custome slot class to declare the slot 

 

this.addSlot(new SlotItemHandler(handler00, 6, 62, 35));//slot zero

this.addSlot(new ArmourSlotItemHandler(handler00, null,5, 80, 8));//head

 

Link to comment
Share on other sites

Posted (edited)

Got it to work, for anyone that has this same question, here's the code:
- In the EntityMenu

this.blockEntity.getCapability(ForgeCapabilities.ITEM_HANDLER).ifPresent(iItemHandler -> {
            this.addSlot(new SlotItemHandler(iItemHandler, 0, 12, 15) {
                @Override
                public int getMaxStackSize() {
                    return 1;
                }

                @Override
                public int getMaxStackSize(@NotNull ItemStack stack) {
                    return 1;
                }

                @Override
                public void set(@NotNull ItemStack stack) {
                    ItemStack copiedItemStack = stack.copyWithCount(1);
                    super.set(copiedItemStack);
                    return;
                }
            });
            this.addSlot(new SlotItemHandler(iItemHandler, 1, 86, 15));
            this.addSlot(new SlotItemHandler(iItemHandler, 2, 86, 60));
        });

 

Adding an anonymous class to the slot you want to have this, and in the class you override the three methods.

Now if you want other number of items, replace the 1's in the methods with the number you want.

PS. The return line at the end of set() is not needed.

Edited by IlluminatiJoe
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.