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
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.