Jump to content

[1.7.10] Printing Custom Name To Chat Problem


qpwoeiruty

Recommended Posts

When you shift right click I want my custom entities name to appear on the first line and then its xp (my custom field) on the next. That works fine, but if it doesn't have a name I just want it to print the line with the xp, but it doesn't it prints a blank line, then the xp, as if it tries to print the custom name even though I have specified it to only print one line if there is not custom name.

 

if(this.func_152114_e(entityPlayer) && entityPlayer.isSneaking() && !this.worldObj.isRemote) {

		if(this.getCustomNameTag() != null) {

			entityPlayer.addChatMessage(new ChatComponentText(EnumChatFormatting.GOLD + "" + this.getCustomNameTag()));
			entityPlayer.addChatMessage(new ChatComponentText(EnumChatFormatting.GOLD + "" + this.currentXP));

		} else if (this.getCustomNameTag() == null) {

			entityPlayer.addChatMessage(new ChatComponentText(EnumChatFormatting.GOLD + "" + this.currentXP));

		}

	}

Link to comment
Share on other sites

A couple comments:

 

First of all, if you have an else statement you don't need to test the condition again (you shouldn't need the second if because that is what else already does).

 

Secondly, I don't think that the custom name will be null if there is no name, it might be an empty string instead "".  So you might have to test for empty string, or test if the length of the name is 0.  I believe this is your problem (that the name isn't actually null when there is no name).

 

Thirdly, I don't think you need the empty string in your chat messages.  I could be wrong, but I don't think you need the "".

 

Lastly, with string values you usually shouldn't use == to test them in Java.  Instead you should use the equals() method for strings.

 

Anyway, try something more like this:

 

if(this.func_152114_e(entityPlayer) && entityPlayer.isSneaking() && !this.worldObj.isRemote) {

		if(this.getCustomNameTag().length > 0) {

			entityPlayer.addChatMessage(new ChatComponentText(EnumChatFormatting.GOLD + this.getCustomNameTag()));
			entityPlayer.addChatMessage(new ChatComponentText(EnumChatFormatting.GOLD + this.currentXP));

		} else {

			entityPlayer.addChatMessage(new ChatComponentText(EnumChatFormatting.GOLD + this.currentXP));

		}

	}

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

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.

Announcements



×
×
  • Create New...

Important Information

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