Display Name in If Statement Not Working

Discussion in 'Plugin Development' started by wiiboy123, Nov 17, 2015.

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

    wiiboy123

    Hello. I've got a pesky if statement here. I'm trying to check if the item in the player's hand is an emerald with the name:
    Code:
    ChatColor.GREEN + "Finalise Arena Settings"
    So I set up my if statements like this:
    Code:
    if (p.getItemInHand().getType() == Material.EMERALD) {
                    ItemStack handItem = p.getItemInHand();
                    if (handItem.hasItemMeta()) {
                        String handItemName = handItem.getItemMeta().getDisplayName();
                        String[] correctName = { ChatColor.GREEN + "Finalise Arena Settings"};
                        if (handItemName == correctName[0]) {
                            MessageManager.getInstance().info(p, "Are these the correct settings? They will be written to the disk.", true);
                            p.sendMessage(ChatColor.GREEN + "[" + ChatColor.YELLOW + "ARENA NAME" + ChatColor.GREEN + "] " + "Test");
                            p.sendMessage(ChatColor.GREEN + "[" + ChatColor.YELLOW + "ARENA BOUNDS" + ChatColor.GREEN + "] " + "(1,2,3) to (4,5,6)");
                            p.sendMessage(ChatColor.GREEN + "[" + ChatColor.YELLOW + "SPECTATOR SPAWN" + ChatColor.GREEN + "] " + "(7,8,9)");
                            p.sendMessage(ChatColor.GREEN + "[" + ChatColor.YELLOW + "PLAYER SPAWNS" + ChatColor.GREEN + "] " + "P1: (10,11,12) P2: (13,14,15)");
                        } else {
                            MessageManager.getInstance().severe(p, "This item is incorrect. It should have the name: " + handItem.getItemMeta().getDisplayName(), true); //used for testing purposes
                        }
                       
                    }
                }
    And I've tried modifying what the item display name should be. A lot. If passes the first check and then the second but just stumps on the third one, and I don't know why! It looks to me like all conditions are met! I just don't know what's wrong with it. I'll play around some more with it.
     
  2. Offline

    Koobaczech

    First off why are you creating strings and a string array to save one string?
    Second try comparing it to the string directly and with an actual comparator statement. Lastly remove the ChatColor.Green format and use the minecraft escape character for colors. With == you are comparing if one string equals another, not if the contents of one string equal the contents of another.
    Remember, §2 = chat color green, §a = chat color light green
    http://files.enjin.com.s3.amazonaws.com/164910/modules/forum/attachments/Colour_1372150223.jpg
    If this doesn't work try using getCustomName instead of getDisplayName. Hopefully issue is fixed :)
    Code:
    if (p.getItemInHand().getType() == Material.EMERALD) {
                    ItemStack handItem = p.getItemInHand();
                    if (handItem.hasItemMeta()) {
                        if (handItem.getItemMeta().getDisplayName().equalsIgnoreCase("§2Finalise Arena Settings")) {
                            MessageManager.getInstance().info(p, "Are these the correct settings? They will be written to the disk.", true);
                            p.sendMessage(ChatColor.GREEN + "[" + ChatColor.YELLOW + "ARENA NAME" + ChatColor.GREEN + "] " + "Test");
                            p.sendMessage(ChatColor.GREEN + "[" + ChatColor.YELLOW + "ARENA BOUNDS" + ChatColor.GREEN + "] " + "(1,2,3) to (4,5,6)");
                            p.sendMessage(ChatColor.GREEN + "[" + ChatColor.YELLOW + "SPECTATOR SPAWN" + ChatColor.GREEN + "] " + "(7,8,9)");
                            p.sendMessage(ChatColor.GREEN + "[" + ChatColor.YELLOW + "PLAYER SPAWNS" + ChatColor.GREEN + "] " + "P1: (10,11,12) P2: (13,14,15)");
                        } else {
                            MessageManager.getInstance().severe(p, "This item is incorrect. It should have the name: " + handItem.getItemMeta().getDisplayName(), true); //used for testing purposes
                        }
                 
                    }
                }
     
    Last edited: Nov 17, 2015
  3. Offline

    adam753

    Use .equals() instead of ==
     
  4. Offline

    wiiboy123

    @Koobaczech Thank you so much! It worked with §a, though.
     
  5. Offline

    Koobaczech

    No problem man! :]
     
Thread Status:
Not open for further replies.

Share This Page