For some reason when setting a item's name/lore using custom ChatColors via the Bukkit API(aka ItemMeta#setDisplayName()
) and then later checking its NBT, it formats the NBT using redundant formatting like italic: false
, obfusicated: false
, ...
This creates a issue when comparing the item with the same item but without these extra tags, altough the items are the same, they do not match since the NBT isn't similar because the redundant formatting.
I looked online and I found that this may be related to how Spigot serializes and deserializes the colors. https://hub.spigotmc.org/jira/browse/SPIGOT-5063 Though, the seloution posted by Pitrex111 doesn't seem to fix it.
Then I found the Adventure libary has a way to directly modify the NBT instead via ItemMeta.setDisplayName()
which might solve the issue since the Adventure API doesn't add the extra formatting when parsing color codes, altough I don't want to use this approuch unless I really have to.
Any other seloutions for this?
There is multiple ways to compare some items.
Use ItemStack#isSimilar
that will compare 2 items. Internally, spigot will compare 2 serialized items, so both will have all redudant content.
Check yourself by comparing type, amount, item meta etc... This will take time, and it's not recommended. Can be better in some case.
Adventure is a great solution, mostly if you're using paper server. All paper server since a long time are using Adventure. You can check if they are equals, but also serialize both and compare, like that:
// convert both
String itemNameA = LegacyComponentSerializer.legacySection().serialize(itemA);
String itemNameB = LegacyComponentSerializer.legacySection().serialize(itemB);
// now compare
itemNameA.equals(itemNameB);