Jump to content

[1.8.9][SOLVED]How to tell if an item is already in custom inventory?


Looke81

Recommended Posts

Your post title and what you said in the description do not match.

Do you want to restrict your custom inventory slots to be able to hold just one type of item, or you want to check if an item is part of a inventory.

I want to make sure not more than one type of item can be placed in the inventory.

 

Loop all other slots and check if the Item is already there.

Probably not like this because this crashes?

@Override
public boolean isItemValid(ItemStack itemstack) {
if (itemstack.getItem() instanceof IBauble ){
	for (int a = 0; a < inventory.INV_SIZE; a++) {
		if(itemstack.getIsItemStackEqual(inventory.getStackInSlot(a))){
			return true;
		}
	}
	return false;
}
	return false;
}

BioWarfare Mod: http://goo.gl/BYWQty

Link to comment
Share on other sites

Does itemstack has a method called "getIsItemStackEqual" ?

It probably does not. I doubt minecraft would use such ridiculous naming for their methods.

 

From ItemStack Class

  @SideOnly(Side.CLIENT)
    public boolean getIsItemStackEqual(ItemStack p_179549_1_)
    {
        return this.isItemStackEqual(p_179549_1_);
    }

  private boolean isItemStackEqual(ItemStack other)
    {
        return this.stackSize != other.stackSize ? false : (this.item != other.item ? false : (this.itemDamage != other.itemDamage ? false : (this.stackTagCompound == null && other.stackTagCompound != null ? false : this.stackTagCompound == null || this.stackTagCompound.equals(other.stackTagCompound))));
    }

 

 

 

I guess it is crashign with an NullPointerException? You are never checking if the item stacks are null

Nope

http://pastebin.com/HkrTfcia

BioWarfare Mod: http://goo.gl/BYWQty

Link to comment
Share on other sites

ItemStack#getIsItemStackEqual

is client-only, using it in common code will crash the dedicated server.

 

If you only care about the

Item

, use

ItemStack#getItem

and compare the

Item

s directly. If you care about the

Item

and metadata, use

ItemStack.areItemsEqual

. If you care about the

Item

, metadata and NBT, use

ItemStack.areItemStacksEqual

.

 

 

I guess it is crashign with an NullPointerException? You are never checking if the item stacks are null

Nope

http://pastebin.com/HkrTfcia

 

That is a

NullPointerException

.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites


@Override
public boolean isItemValid(ItemStack itemstack) {
if (itemstack.getItem() instanceof IBauble ){
	for (int a = 0; a < inventory.INV_SIZE; a++) {
		if(itemstack.areItemStacksEqual(itemstack, inventory.getStackInSlot(a))){
			if(inventory.getStackInSlot(a) != null){
				return true;
			}

		}
	}

}
	return false;
}

 

Thanks. Except now I cant put an item in the inventory at all.

BioWarfare Mod: http://goo.gl/BYWQty

Link to comment
Share on other sites

You're returning

true

(item is valid for slot) when there's already an item of that type in the inventory and

false

(item isn't valid for slot) when there isn't.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

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.