Jump to content

[1.12.2] Can't find IItemHandler isItemValid


cptcorndog

Recommended Posts

Other sources have suggested overriding the isItemValid method of IItemHandler to check if an item can be placed in a slot (e.g., only allow smeltable items in a furnace). I don't see this method available in documentation

 

Should I be performing this check inside an overridden insertItem method instead?

 

Thanks! brand new to modding so bear with me

Link to comment
Share on other sites

3 hours ago, cptcorndog said:

Other sources have suggested overriding the isItemValid method of IItemHandler to check if an item can be placed in a slot (e.g., only allow smeltable items in a furnace). I don't see this method available in documentation

 

Should I be performing this check inside an overridden insertItem method instead?

 

Thanks! brand new to modding so bear with me

You should make a custom ItemStackHandler that extends ItemStackHandler in which you overwrite insertItem and extractItem, and check if the itemstack is valid. I have a small example of what I mean here:

    protected boolean canInsert(ItemStack stack, int slot) {
        return true;
    }

    protected boolean canExtract(ItemStack stack, int slot, int amount) {
        return true;
    }

    @Override
    public boolean isItemValid(int slot, @Nonnull ItemStack stack) {
        return this.canInsert(stack, slot);
    }

    @Nonnull
    @Override
    public ItemStack insertItem(int slot, @Nonnull ItemStack stack, boolean simulate) {
        if (this.canInsert(stack, slot)) {
            return super.insertItem(slot, stack, simulate);
        } else {
            return stack;
        }
    }

    @Nonnull
    @Override
    public ItemStack extractItem(int slot, int amount, boolean simulate) {
        if (this.canExtract(this.getStackInSlot(slot), slot, amount)) {
            return super.extractItem(slot, amount, simulate);
        } else {
            return ItemStack.EMPTY;
        }
    }

But this isn't necessarily needed, as you should be able to modify isItemValid when instantiating the ItemStackHandler:

    public final ItemStackHandler inv = new ItemStackHandler(1) {
        @Override
        public boolean isItemValid(int slot, @Nonnull ItemStack stack) {
            return super.isItemValid(slot, stack); //check stack.
        }
    };

Also, make sure you are exposing the ItemHandler capability, or your inventory will not work properly.

  • Thanks 1
Link to comment
Share on other sites

47 minutes ago, unassigned said:

You should make a custom ItemStackHandler that extends ItemStackHandler in which you overwrite insertItem and extractItem, and check if the itemstack is valid. I have a small example of what I mean here:

But this isn't necessarily needed, as you should be able to modify isItemValid when instantiating the ItemStackHandler:


    public final ItemStackHandler inv = new ItemStackHandler(1) {
        @Override
        public boolean isItemValid(int slot, @Nonnull ItemStack stack) {
            return super.isItemValid(slot, stack); //check stack.
        }
    };

Also, make sure you are exposing the ItemHandler capability, or your inventory will not work properly.

 

The latter is what I'm trying to accomplish but Eclipse is raising an error when I try to override this method, and when I search through ItemStackHandler and IItemHandler I don't see this method declaration...

Link to comment
Share on other sites

Careful with ItemHandlerSubclasses that override insertion/extraction behavior. Sometimes you want your machine to be able to insert things and not external forces, such as the player or hoppers. If you use a subclass that prevents insertion categorically, you can't add items to it even when you want to.

 

For such things I use a wrapper that I expose with GetCapability and have an internal regular handler that works as standard:

 

https://github.com/Draco18s/ReasonableRealism/blob/master/src/main/java/com/draco18s/hardlib/api/internal/inventory/OutputItemStackHandler.java

 

Note that all interaction is either passed to the internal wrapped handler or special logic is applied. This allows a machine to insert items into its own output slot, but nothing else can.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

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.



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Hello! I am attempting to play on a Minecraft server with a few friends, this server has loaded before but crashed and brought me to this issue where it is continuously freezing at version checking. The mod it version checks that it gets stuck on is inconsistent, and removing the mods it gets stuck on does not seem to help. Linked is the latest log given, and the debug log. Latest Log - https://paste.ee/p/HuPB2 Debug Log - https://paste.ee/p/2MwuF Server Hoster: GPortal Minecraft Vers: 1.20.1 Forge Vers: 47.3.0
    • "I had a distressing experience last year when I lost access to my cryptocurrency wallet due to a technical issue. After trying unsuccessfully to recover it myself, I was skeptical about finding a trustworthy service to help. That's when I discovered Fast fund Recovery. From the outset, they impressed me with their professionalism and expertise. Their team took the time to understand my situation and promptly outlined a recovery plan. They maintained clear communication throughout the process, informing me of progress and any challenges they encountered. This transparency was reassuring, especially considering the sensitive nature of cryptocurrency recovery. Fast fund Recovery utilized advanced techniques and tools specific to blockchain technology, demonstrating a deep understanding of how cryptocurrencies operate. They approached my case with diligence and persistence, never giving up despite the complexities involved. After several weeks of meticulous effort, I was ecstatic to receive the news that Fast fund Recovery had successfully recovered access to my wallet. It was a huge relief and a testament to their skill and dedication. They restored my access and provided valuable advice on improving security measures to prevent future issues. I am immensely grateful to Fast fund Recovery for their exceptional service and would highly recommend them to anyone facing similar challenges in the crypto world. Their professionalism, expertise, and commitment to customer satisfaction truly set them apart." E-m ail: Fastfundrecovery8 (at)g mail com  
    • I recently tried to load forge version 51.0.17 for 1.21 and, every time I tried to load it it would crash before even launching the game.
    • I am trying to do the same thing but it's telling me that "level" in player.level.levelinfo.dimension. is private. I am on 1.20.1 with parchment mappings 2023.09.03-1.20.1 Thanks in advance for any help!
×
×
  • Create New...

Important Information

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