Posted September 14, 201312 yr 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.
September 14, 201312 yr 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();
September 14, 201312 yr 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
September 14, 201312 yr 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.