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

So basically I need to call a function on an Item (happens to be a piece of armor) from a completely separate class, which (as far as I know, only has access to the ItemStack)

 

This is actually being called from my tickhandler, which should be every X ticks calling a function on that specific piece of armor if a player is wearing it.

 

The problem is, I can't figure out how to get the ItemStack object returned from .getCurrentItemOrArmor(X); Which returns an Itemstack, but I cannot find a way to call the Custom Object's functions from that.

Since getCurrentItemOrArmor() returns an ItemStack which has a method called getItem(), you need to cast the result of that into your custom object (which must either extend or implement Item):

 

MyObject obj = (MyObject)object.getCurrentItemOrArmor(X).getItem();

if (obj != null)
    obj.myCustomFunction();

  • Author

Right, I understand that (at least mostly)  and have tried similar (casting stuff)

 

The problem is that eclipse still claims that ItemStack cannot be cast to ShieldArmor in this case.

 

This is even though:

ShieldArmor extends my custom class "ElectricArmor"

Which Extends ItemArmor

Which itself extends Item.

 

However, it doesn't allow casting from ItemStack to ShieldArmor, even though this is the case.

 

Still thanks, btw

Right, I understand that (at least mostly)  and have tried similar (casting stuff)

 

The problem is that eclipse still claims that ItemStack cannot be cast to ShieldArmor in this case.

 

This is even though:

ShieldArmor extends my custom class "ElectricArmor"

Which Extends ItemArmor

Which itself extends Item.

 

However, it doesn't allow casting from ItemStack to ShieldArmor, even though this is the case.

 

Still thanks, btw

 

Don't try to cast the result of getCurrentItemOrArmor(), that's an ItemStack, you want to use getItem() on the ItemStack returned and then cast that...

 

Here it is in long format:

 

ItemStack stack = something.getCurrentItemOrArmor(x);
Item tmp = stack.getItem();
MyObject obj = (MyObj)tmp;

 

So to be concise you can simply write:

 

MyObject obj = (MyObj)something.getCurrentItemOrArmor(x).getItem();

if (obj != null)
{
    // do stuff
}
else
{
    // oops
}

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.