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.

Featured Replies

Posted

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");

}

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.

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.

  • Author

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!

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.

  • Author

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

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.

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.