Solved Getting name of an item, and comparing it with the name in a config.yml file

Discussion in 'Plugin Development' started by mccrafter1212, May 27, 2017.

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

    mccrafter1212

    Wow, that was a long title... Anyways, I basically have my config.yml:

    Code:
    waypoints:
      Spawn:
        x: 0
        y: 50
        z: 0
        name: Spawn
      Test:
        x: 5
        y: 50
        z: 5
        name: Test
    I've got this code:
    Code:
    final String NAME_OF_THING = getConfig().getString("waypoints." + getConfig().getConfigurationSection("waypoints").getKeys(false).toArray()[i] + ".name");
                player.sendMessage("You're attempting to go to " + NAME_OF_THING + " and the item's name is " + e.getCurrentItem().getItemMeta().getDisplayName());
                if (e.getCurrentItem().getItemMeta().getDisplayName().equals(NAME_OF_THING) == true) { // The line above will give me You're attempting to go to Spawn and the item's name is Spawn (but the second "Spawn" is in purple). I'm not sure why, but this never seems to be true. I think it might be because the displayname has a color and NAME_OF_THING doesn't have a color. I'm not sure how to remove the color when checking though.
                  System.out.println("It matches!");
                }
    
    Any help's appreciated! Thanks :D
     
    Last edited by a moderator: May 28, 2017
  2. Offline

    mehboss

    Compare strings using .equals, not enums (==).
     
  3. Offline

    mccrafter1212

    Thanks for that tip but it really wasn't helpful towards getting my question answered.
     
  4. Online

    timtower Administrator Administrator Moderator

    @mccrafter1212 configurationsection.contains(item.getItemMeta().getDisplayName()) ?
     
  5. Offline

    mccrafter1212

    @timtower But isn't the displayName formatted with color, while the config.yml name doesn't use color? So, it won't be match. Right?
     
  6. Online

    timtower Administrator Administrator Moderator

  7. Offline

    mccrafter1212

    @timtower So, final would be like:

    getConfig().configurationsection.contains(ChatColor.stripColor(item.getItemMeta().getDisplayName()));

    Correct?
     
  8. Online

    timtower Administrator Administrator Moderator

    @mccrafter1212 Do get the correct configurationsection first though.
    And maybe move the displayname without colors to a variable after checking that it has a displayname etc.
     
  9. Offline

    mccrafter1212

    K so:

    String displayName = ChatColor.stripColor(item.getItemMeta().getDisplayName());
    if (getConfig().getConfigurationSection("waypoints").contains(displayName) == true) {
    // Code
    }

    Right? @timtower

    k, solved it @timtower
     
    Last edited: May 29, 2017
  10. Online

    timtower Administrator Administrator Moderator

Thread Status:
Not open for further replies.

Share This Page