Get the exact name of an item

Discussion in 'Spigot Plugin Development' started by yamerins, Jul 18, 2015.

  1. I am trying to find the exact name of the item in the player's hand and display it to the command sender when a command is run, which is more difficult than it sounds. Most ways that I try to accomplish this display the "catagory" the item falls under. For example, all types of wool come out as wool, not the specific color (which is what I want), and granite comes out as stone.


    This is my code for now as I'm trying to figure this out:
    Code (Text):
    if(commandLabel.equalsIgnoreCase("whatsinmyhand")){
                    ItemStack i = player.getItemInHand();
                    String name2 = i.getData().toString();
                    String name = i.toString();
                    name= name.replace("ItemStack{", "");
                    name= name.replace("}", "");
                    player.sendMessage(name);
                    player.sendMessage(name2);
                 }
    The command runs successfully. The string 'name' displays "catagory-name X item-number." The string 'name2' displays "catagory-name(id#)". Any suggestions?
     
  2. I don't think there's any built-in way to handle this. You can integrate with Vault, which has an ItemDB that will do most of it for you.

    Otherwise, you just have to special-case it all. Like for instance of you have wool, get the data value, get the DyeColor and then get the name of the color. But you'd have to do something special for every Material that has data.
     
  3. So
    So your saying that there is not really a way to check if a certain item has the same name as another item? I say name because the plugin I am developing requires names to match.

    I'm kinda new to plugin development, what does the method getData() get from an itemstack?
     
  4. Can't you just use reflection and the CraftItemStack class to get the item name?
     
  5. What do you mean?
     
  6. Oh, your trying to get the main category name? I thought you were just trying to get the default item name, my bad. As yamerins said, maybe integrate with Vault. :p
     
  7. All I'm trying to do is see if an item's name matches up with another name. For the sake of example, it can be in a player inventory. Is there really no way to do this? I feel like it's right in front of me. I want the current name of the item to be used in the comparison even if its renamed.

    Ex: renamed sword to "Hi"

    Difined string 'name' is equal to "Hi"

    method performed to see if the sword's current name and the string 'name' match, they do so it performs another method to the sword.
     
    #7 yamerins, Jul 18, 2015
    Last edited: Jul 18, 2015
  8. Get the itemmeta and then get the displayname
    Do some null checks on getting the meta & name, since it returns null if the item has a default name...
    If it returns null in one of ^ you know it doesnt have a special name, so you can just compare the material and durability
     
  9. Done that, most items don't have a display name from what I gather.
     
  10. (Edited my post just before you posted :3)
     
  11. So a display name is something that was renamed?

    EDIT: I tested some things and set the display name of an apple to "test" and then put the apply in my inventory. Didn't do anything to it, and when I checked for the display name of the apple, it still came up blank (null).
     
  12. Yep, displayname will always return null if it doesnt have a special name (can be renamed with an anvil or renamed by a plugin via editing the metadata)
     
  13. Editted my post
     
  14. I just committed on my plugin utilities repository, the class ItemNames will get the vanilla name of the item. If you want to get the current name of an item you will need to check if the display name is null, if so then use the vanilla item name else use the display name.
     
  15. This is becoming a weird "thing"

    If you change the displayname you have to set the meta of the item again, before putting it in an inv
    In pseudocode:
    Create itemstack
    Get itemmeta
    Set displayname of meta
    Itemstack.setitemmeta(meta)
    Give item
     
  16. Ignore this post, plugin must not have totally refreshed because it did most of the code but failed to rename the item. Works now.
     
  17. I'm gonna be honest I'm a bit lost in this. So to get the vanilla name I would call getName with the itemstack I want to find the name of as an argument?

    Wow, thats super useful. Thanks.
     
    #17 yamerins, Jul 18, 2015
    Last edited: Jul 18, 2015
  18. No problem.