Trouble checking custom item names

Discussion in 'Plugin Development' started by broswen, May 4, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    broswen

    I am renaming item so i can use them as custom tools/weapons. When i check the display name for the item it says it's different than what i check it for, even though it's exactly the same.

    this is the item that i give the player
    Code:java
    1. public ItemStack thiefstick = new ItemStack(Material.BOWL);
    2. ItemMeta im = thiefstick.getItemMeta();
    3. String thiefItemName = ChatColor.RED + "Bowl of Theft";
    4. im.setDisplayName(thiefItemName);
    5. thiefstick.setItemMeta(im);
    6. player.getInventory().addItem(thiefstick);


    and when i check it with this method it says "false" which means the items are not named the same thing

    Code:java
    1. if(!(player.getInventory().getItemInHand().getItemMeta().getDisplayName() == ChatColor.RED + "Bowl of Theft")){
    2. player.sendMessage("false");
    3. return;
    4. }


    here is an image of the item in game
    [​IMG]
     
  2. Offline

    kennethbgoodin

    When you check the name, use ChatColor.stripColor() to remove the color, rather than check it with the color. You should also be using .equals() or .equalsIgnoreCase(). So something along the lines of:

    Code:
    if(!(ChatColor.stripColor(player.getInventory().getItemInHand().getItemMeta().getDisplayName()).equals("Bowl of Theft"))) {
     
    }
     
  3. Offline

    SaxSalute

    You can't compare objects with ==. If you do that, you're just comparing memory addresses. The == just works with primitives. With strings, you need to use string1.equals(string2) which will give you the same boolean true/false that you were looking for with ==.
     
  4. Offline

    paully104

    Not 100% sure on this but i'll have to look on my code for the .equals you need to have .equals("ChatColor.RED + "Bowl of Theft") the chat color effects the name? If you do just "Bowl of Theft" I don't think it will work. Once again i'll have to test again not 100% sure on that statement.
     
  5. Offline

    kennethbgoodin

    paully104 Yeah you would, if I hadn't used ChatColor.stripColor() on the item's name (which I did in the code I provided, and mentioned in the sentences above it), which strips any ChatColor formatting, and just leaves the string.
     
  6. Offline

    paully104

    Ah my fault, I just quickly read the code and didn't read what you posted, thanks for the clarification.
     
  7. Offline

    SaxSalute

    Regardless of if the color is stripped or not, nothing will work right with == on two strings.
     
  8. Offline

    kennethbgoodin

    SaxSalute Yes, and I mentioned that in my first post.
     
  9. Offline

    xXSniperzzXx_SD

    SaxSalute

    try:

    Code:
    if(!(ChatColor.stripColor(player.getInventory().getItemInHand().getItemMeta().getDisplayName()).equals("Bowl of Theft")){
            player.sendMessage("false");
            return;
    }
     
  10. Offline

    broswen

    Thanks for all the responses, I got the plugin working properly.
     
Thread Status:
Not open for further replies.

Share This Page