Posted July 18, 201411 yr 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)); } }
July 18, 201411 yr 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/
July 18, 201411 yr Author Thanks for the reply, it all works now. I tried it without the empty string earlier but received an error, probably because I can't use the chat formatting next to a field by itself.
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.