Jump to content

[Solved] How to get Object from Itemstack


1n5aN1aC

Recommended Posts

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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
}

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.

×
×
  • Create New...

Important Information

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