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

I've been at this for a while now, and I try to not ask for help usually, but this time I'm at an impasse.

 

I've created the command "count" which I want to get the item that the player is currently holding, then count the amount of that item in the players inventory, then send the player a message with the amount of that item.

 

        ItemStack heldStack = player.inventory.getCurrentItem();

        if (heldStack != null)  {
            for (int i = 0; i < player.inventory.getSizeInventory(); i++) {
                ItemStack cycledStack = player.inventory.getStackInSlot(i);

                if (cycledStack.getItem().equals(heldStack)) {
                    itemCount = itemCount + cycledStack.stackSize;
                }
            }
        }

 

I'm still fairly new to Java, so I'm assuming it's some stupid mistake that I somehow made. I don't want to be spoonfed code, I want to learn, so if you guys could help me out by pointing me in the right direction, that would be awesome.

 

I apologize if I've forgotten anything in this post, please let me know if I have.

 

Thanks!

 

So what's the problem? Are you getting NullPointerException (i.e. because you don't check if cycledStack is null before calling a method with it)? Is your count not incrementing (i.e. because your condition is never true - ItemStack != Item)? Is the message not getting sent (I don't see any code to send a chat message...)?

  • Author

Wow, forgot the error... Sorry...

 

Yes, it's a NullPointerException, and I feel more like an idiot because checking if it was null or not fixed that issue, and then it wasn't counting which I fixed by changing:

 

if (cycledStack.getItem().equals(heldStack))

 

To:

 

if (cycledStack.getItem().equals(heldStack.getItem()))

 

Thank you!

Np. Rookie mistakes (that every one of us has made) ;)

 

Btw, since Items are all instantiated as singletons, you can compare using identity '==' rather than the #equals method (which does check identity, but also potentially a lot of other stuff e.g. for complex objects that are separate instances but may still considered equal).

// this is the typical way of comparing Items and Blocks (but NOT ItemStacks if you care about damage value and/or NBT data)
if (cycledStack.getItem() == heldStack.getItem())

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.