Posted March 7, 20169 yr 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
March 7, 20169 yr 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.
March 7, 20169 yr 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
March 7, 20169 yr I guess it is crashign with an NullPointerException? You are never checking if the item stacks are null
March 7, 20169 yr 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.
March 7, 20169 yr 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
March 7, 20169 yr 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.
March 7, 20169 yr 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
March 7, 20169 yr 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.