Jump to content

[1.11.2] [SOLVED] Comparing NBT Data of Two ItemStacks


Draconwolver

Recommended Posts

I'm having trouble comparing the name and lore of an item to a name and lore that I specify. Here is some code that demonstrates my issue:

public static final String NAME = TextFormatting.LIGHT_PURPLE + "Magic";

public NBTTagList lore = new NBTTagList();
lore.appendTag(new NBTTagString(TextFormatting.DARK_PURPLE + "This does magic stuff!"));
lore.appendTag(new NBTTagString(TextFormatting.RED + "Magic stuff is magical."));

public ItemStack itemStack = // some ItemStack that has a display name of NAME and a lore of lore

public NBTTagList getLore(ItemStack itemStack)
{
	NBTTagCompound nbt = itemStack.getTagCompound();
	if (nbt != null) {
		NBTTagCompound display = nbt.getCompoundTag("display");
		if (display != null) {
			NBTTagList lore = display.getTagList("Lore", 8);
			if (lore != null)
			{
				return lore;
			}
		}
	}
	
	return new NBTTagList();
}

public boolean displayNamesEqual = itemStack.getDisplayName() == NAME;
public boolean loresEqual = getLore(itemStack).toString() == lore.toString();

// displayNamesEqual and loresEqual both evaluate to false

Does it have to do something with the color/style of this information?

Edited by Draconwolver
Link to comment
Share on other sites

7 hours ago, Draconwolver said:

I don't. I check the NBT of the item I'm currently holding at certain times.

Well, if you've never set the NBT then it's never going to be equal to the condition you're checking for. It won't set itself to the exact text you want unless you tell it to.

 

You're also using the == operator which checks for object identity. So unless you are checking references to the exact same object, they will return false even if they seem to be the same. Instead you should use equals or an equivalent method to check for matching content.

Edited by Jay Avery
  • Like 1
Link to comment
Share on other sites

8 hours ago, Jay Avery said:

Well, if you've never set the NBT then it's never going to be equal to the condition you're checking for. It won't set itself to the exact text you want unless you tell it to.

 

You're also using the == operator which checks for object identity. So unless you are checking references to the exact same object, they will return false even if they seem to be the same. Instead you should use equals or an equivalent method to check for matching content.

Thank you, the equals method worked perfectly for both the display name and lore.

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.