Jump to content

How to check for meta data in an itemstack. (Any Version)[SOLVED]


Koopamillion

Recommended Posts

EDIT: Use:

                                                                                                          V finds meta data

if(itemstack != null && itemstack.getItem() == Items.whatever && itemstack.getItemDamage() == 0){

 

 

 

}

 

 

 

 

This is my method to check for charge levels of a battery... Unfortunately, it dosen't detects ANY item, no matter the meta data/count. If you can fix this, thanks in advance!

 

private void updateBattery() {

 

 

ItemStack itemstack = this.getStackInSlot(1);

 

if(itemstack == new ItemStack(Mitems.basicBattery, 1, 1)){

System.out.println(itemstack);

}else{

System.out.println("fail");

}

Link to comment
Share on other sites

This is a basic Java error.

==

checks reference equality, i.e. whether the operands are the same object. An

ItemStack

from an inventory will never be the same object as an

ItemStack

you just created.

 

Some classes override

Object#equals

to check value equality, i.e. whether the two operands represent the same value.

ItemStack

does not do this, so

itemStack1.equals(itemStack2)

still checks reference equality.

 

You need to get the

Item

and metadata from the

ItemStack

using the appropriate getter methods and compare those to the values you want to check.

 

If you already had two

ItemStack

s and wanted to check if they were equal, the

ItemStack

class provides several static and non-static methods to check various forms of equality (e.g.

Item

and metadata;

Item

, metadata and NBT). In general, you shouldn't call these if you have to create the second

ItemStack

at the time of the call.

 

Don't bump your threads. It will only annoy the people who can help you, it won't hasten their response.

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

OK, but I'm a bit of a noob with coding, but you said to get the item? So I did .getItem() and it asks for != null check, infact, it asks for that if I just do 1 =... Do you know anyway of fixing this? All I want to do is have it check if the item has the correct metadata.

Link to comment
Share on other sites

If an inventory slot is empty,

IInventory#getStackInSlot

will return

null

. You need to check that the returned

ItemStack

isn't

null

before you can safely call methods on it.

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

I'm so sorry for wasting your time, but I kind of understand what you're saying yet I don't know how to type it out...

 

I've done this:

 

ERROR

 

if(itemstack != null && itemstack = new ItemStack(Mitems.basicBattery, 1, 1))

                                                    ^ You said to do only one.

 

Should I tell it to get the item like this?

 

Item item = itemstack.getItem();

 

NO ERROR BUT IT DOSENT WORK

 

if(itemstack != null && item == Mitems.basicBattery){

 

Or should I plus the meta (what I need):

 

ERROR

 

Item item = itemstack.getItem();

int meta = item.getMetadata(0);

 

if(itemstack != null && item == Mitems.basicBattery.getMetadata(meta))

 

Thanks!

Link to comment
Share on other sites

I never told you to use the assignment (

=

) operator or create a new

ItemStack

.

 

To check the

Item

and metadata of an

ItemStack

, you need to do three things:

  • Check that the
    ItemStack

    isn't

    null


  • Check that the return value of
    ItemStack#getItem

    is equal to (

    ==

    ) the right

    Item


  • Check that the return value of
    ItemStack#getMetadata

    is equal to (

    ==

    ) the right metadata

 

Do not call either overload of

Item#getMetadata

yourself.

Item#getMetadata(int)

is used to determine the block metadata to place,

Item#getMetadata(ItemStack)

is used by

ItemStack#getMetadata

to determine the stack's metadata.

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

OK... But there is no itemstack.getMetadata, if you meant item.getmetadata(itemstack) it just errors saying int, so I think you meant the itemstack version (if there is one)... This is my code so far using 2/3 possible check list items:

 

 

if(itemstack != null && itemstack.getItem() == item){

It crashes upon placing the tile entity, so I think by the "right item" you didn't mean the variable (Itemstack.getitem()) I made? Or did you mean the Mitems.basicBattery? I'm not that good at understanding code.

 

But Thanks for not just saying "go learn java".

Link to comment
Share on other sites

That's because Choonster isn't a dick like I am.

 

Use your god damned IDE to examine the ItemStack class and figure out what method will do what you want.

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.

Announcements



×
×
  • Create New...

Important Information

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