Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

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

Featured Replies

Posted

I want to make sure you can only put one type of any item in my custom inventory. I know i need to use the isItemValid method but I don't know how to check if that item already exists in my inventory.

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

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.

  • Author

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

Does itemstack has a method called "getIsItemStackEqual" ?

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

 

EDIT: Apparentely there is. Never used this before. My bad.

  • Author

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

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.

  • Author


@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

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.

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

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.