Solved Getting an item display name? (User Friendly)

Discussion in 'Plugin Development' started by TheTinySpider, Jan 1, 2013.

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

    TheTinySpider

    I've been trying to get the name of an item usign ItemMeta, does this only work when the item has got a set DisplayName?

    This is the code I used:

    Code:
            String name = "";
            ItemMeta im = handItem.getItemMeta();
            name = im.getDisplayName();
            return name;
    I tried this code with a chest as handItem, the return was null
    Any idea how to get a user-friendly name of an item?
     
    com. BOY likes this.
  2. Offline

    fireblast709

    Just from the Material, you could use item.getType().name().replace("_", " ").toLowerCase();
     
    Kars and com. BOY like this.
  3. Offline

    TheTinySpider

    Okay but why didn't the getDisplayName() work?
     
  4. Offline

    Sagacious_Zed Bukkit Docs

    You should probably check that the item hasDisplayName first.
     
  5. Offline

    HappyPikachu

  6. Offline

    Jogy34

    The getDisplayName() method "Gets the display name that is set" so if you haven't set one then it won't have anything to return.
     
  7. Offline

    tommycake50

    i believe it gets it from the item's meta itself and if it cant find any meta it returns null.
    bukkit should check against the null-ness and if its null say the display name is the name of the item,
    it would make a lot more sense.
     
  8. Offline

    fireblast709

    That is why they give you the hadDisplayName() method. (Though, I agree they should just return the normal item name if it was null)
     
  9. Offline

    TheTinySpider

    Indeed, now i'm usign Vault to get me the item name.
    But it would be cool if bukkit can do this
     
  10. Offline

    fireblast709

    Code:java
    1. String name = stack.getItemMeta().hasDisplayName() ? stack.getItemMeta().getDisplayname() : stack.getType().replace("_", " ").toLowerCase();
     
  11. Offline

    TheTinySpider

    I know, but minecraft has odd names like Wooden Spade (WOODEN_SPADE) and Grilled Pork (GRILLED_PORK)
    Thats why I prefer Vault more, and I needed to add Vault eco anyway
     
  12. Offline

    fireblast709

    check the third part of the ternary operator. that would return "wooden spade" and "grilled pork"
     
  13. Offline

    TheTinySpider

    Lol i know i put the minecraft programmed name between these () and the outcome infront :p

    Thing is, it's not the name it gives you when holding the item
     
  14. Offline

    fireblast709

    wut?
     
  15. Perhaps he means that new tooltip thingy that pops up whenever you switch items in hand.
     
  16. Offline

    Jogy34

    In minecraft the default names for shovels are wooden shovel, iron shovel, etc... where your method would return wooden spade, iron spade, etc... same with cooked porkchop versus grilled pork and same with quite a few other items
     
  17. Offline

    fireblast709

    that is because with the Bukkit API you get the bukkit default names ;D
     
  18. Offline

    Jogy34

    He wants the in game names though
     
  19. Offline

    fireblast709

    Unless you have a file with the names stored, I cannot supply that with just the Bukkit API (afaik)
     
  20. Offline

    Jogy34

    Hence using vault which I am presuming that it get's the actual NBTTag of the item and then get's the name through that because that does store the original name in it by default.
     
  21. Offline

    fireblast709

    Just looked into Vault. They just have it hardcoded (but nonetheless the correct naming)
     
  22. Offline

    SoS_Dylan

    You could use this:
    Code:java
    1. String str = player.getKiller().getItemInHand().getType().name()
    2. .replace("_", " ").toLowerCase();
    3. StringBuilder b = new StringBuilder(str);
    4. int i = 0;
    5. do {
    6. b.replace(i, i + 1, b.substring(i, i + 1).toUpperCase());
    7. i = b.indexOf(" ", i) + 1;
    8. } while (i > 0 && i < b.length());

    'String str' is where you would put the variable and 'b.toString()' is the output.
    This will capitalise the first letter of each word and will remove the '_'.
     
    MCKimKunG likes this.
  23. Offline

    Jogy34

    If you actually read anything up there you would have see that he didn't want to do that as things like shovels and cooked pork it would return a different name than what there is in minecraft as the enum for that in bukkit is different from the actual name of the item.
     
  24. Offline

    LCastr0

    (I know it's solved, just wanted to give my opinion)
    You could make an enum with all the minecraft material types, with the "friendly" name :p
     
Thread Status:
Not open for further replies.

Share This Page