[1.13] How to check for colours in name of item?

Discussion in 'Spigot Plugin Development' started by archiebakery, Aug 3, 2018.

  1. Hey guys
    Sorry if this is in the wrong section.


    The problem im having is i've updated my server to 1.13 and the below code no longer works

    event.getItem().getItemMeta().getDisplayName().equals("§F§l" + "ItemName");


    What would be the alternative to check for colours in item names?


    Thankyou :)
     
  2. Not sure why 1.13 would've made a difference, but in general it's not good practice to use that character in code. Try something like ChatColor.COLOR_CHAR + "F" + ChatColor.COLOR_CHAR + "I" + "ItemName".

    Or, better use the corresponding ChatColor constants, but I was too lazy to translate them for you :)
     
  3. I'm not sure what changed, but have you tried stripping the color off the item name, and then comparing it, if that's an option?
    Code (Text):
    String itemName = ChatColor.stripColor(item.getItemMeta().getDisplayName());
    if (itemName.equals("ItemName")) { ... }
     

  4. Hey i've tried both but neither seem to work :/


    This is a possible solution but i dont want users renaming items in similar fashion and the plugin thinking they have a different "item".
     
  5. Have you tried printing the item name to console to see what changed? Knowing if and how it changed would be illuminating. Perhaps color codes were simply changed to lower case (§f§i).

    In which case btw, you might just want to use the ChatColor enum. To my knowledge, their toString methods turn into lower case raw text.

    Code (Text):
    item.getName().equals(ChatColor.WHITE + ChatColor.ITALIC + "ItemName");
    Come to think of it, none of the color codes uses the "I" as a letter... I assume that's your own magic code. In that case, refer to nathanwolf's response. Or perhaps it got filtered out as an invalid color code? Yeah, definitely print it out and see what's in that String. :)
     
    • Agree Agree x 1
  6. Hey guys thanks for all your help.


    I was using an "l/L" but i can understand since i used a capital F it would look like an (eye) my bad on that one...


    Anyhow it seems like the colour codes changed between 1.12 and 1.13 so all i've had to do is change it to the bold formatting code.


    Thanks for both of your help
     
    • Friendly Friendly x 1