Jump to content

Determine if an Item is an Instance of a Class [1.8.9][SOLVED]


Athire

Recommended Posts

Hello all!

 

Me again! It's been awhile but I'm continuing on with my mod. Here's my dilemna:

 

I have a tool class that is going to be used to create 7 or 8 different instances of that tool with different properties (below).

 

//This will be the basic bone dagger class, used to create all tiers of necromancer dagger.

public class simpleBoneDagger extends ItemSword

{

public int buffAmount;

 

public simpleBoneDagger(String unlocalizedName, ToolMaterial material, int buffVal) {

super(material);

this.setUnlocalizedName(unlocalizedName);

this.buffAmount=buffVal;

}

 

}

 

In particular, the material and the buffVal will differ between instances of this class. My goal is that when a player has this weapon in hand, it increases an extended player property by the buffVal. My issue is I don't want to check player.getCurrentEquippedItem().getItem() 7 or 8 times to hit every tool. Is there a way to check if an item is one of the various instances of a class like that? I've been looking but haven't come across anything yet.

 

The other issue I run into is that I need the buffVal of the particular instance of the class the player has equipped. Is there an easy way to get that without hard coding in every value and comparing 8 times?

 

Here is some pseudocode to illustrate what I mean:

 

 

//If magic weapon equipped, increase max mana;

if(this.player.getCurrentEquippedItem() != null)

{

Item item= this.player.getCurrentEquippedItem().getItem();

if(item == <instance of simpleBoneDagger class>)

{      //check if weapon was just equipped, or has been in hand for a while

if(this.prevWeapon == false && this.weaponEquiped == true)

{

this.prevWeapon = true;

 

this.maxMana += <Particular instance of class>.buffVal;

}

}

 

 

Thanks in advance for any help you guys can provide! I'm trying to code this smartly (for a change  ;D) and it's throwing me for a loop!

Link to comment
Share on other sites

Use instanceof and casting:

if item instanceof SimpleBoneDagger then
    mana += ((SimpleBoneDagger) item).buffVal

 

PS: I suggest you use the official code conventions: http://www.oracle.com/technetwork/java/codeconventions-135099.html

esp. on your class names (SimpleBoneDagger instead of simpleBoneDagger)

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

Link to comment
Share on other sites

If the OP needs to check for a particular instance (one with a particular buf value), then one needs more than a class check.

 

I suppose the value could be copied into the player, which in recent versions would utilize the capability system. I'm not sure about 1.8.9; it might have been using extended entity properties. If 1.8.9 is the old way, then the mod might want to upgrade to 1.10.2 or 1.11 before coding in an obsolete paradigm.

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

Link to comment
Share on other sites

Thank you guys! SanAndreasP's solution seems to have worked. I was sure there must be a way to do that, but Java isn't my Forte so wasn't sure of the syntax. On the convention note, I learned camelCase (as opposed to CamelCase) at my university for other languages and it's become a bit of a bad habit. I'll try to get better at sticking to the java coding conventions :)

 

Thanks again, you guys rock! Marking as solved!

Link to comment
Share on other sites

On the convention note, I learned camelCase (as opposed to CamelCase) at my university

 

You mean "TitleCase"?

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

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.