ItemNames: get the friendly printable name for an ItemStack

Discussion in 'Resources' started by desht, Jun 27, 2014.

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

    desht

    After a little discussion in this thread, in which a few bugs were fixed, I decided to add a post here with a class that will hopefully be useful to some:

    https://github.com/desht/dhutils/blob/master/Lib/src/main/java/me/desht/dhutils/ItemNames.java

    It provides a two static methods:
    • String lookup(ItemStack stack) - given an ItemStack, it returns the name of the item, as the (English-language) client displays it.
    • String lookupWithAmount(ItemStack stack) - given an ItemStack it returns "<n> x <stack-name>" where <n> is the number of items in the stack, and <stack-name> is the result of lookup(stack).
    This can be nice if you want to get a prettier name for items than the simple stack.getType().toString() Material representation.

    Example:

    PHP:
    @EventHandler
    public void onInteract(PlayerInteractEvent event) {
      if (
    event.getItem() != null) {
        
    event.getPlayer().sendMessage("You are holding " ItemNames.lookupWithAmount(event.getItem()));
      }
    }
    ItemNames is aware of colour data (e.g. on dyes, wool, stained glass etc.), and of item metadata. It does its best to handle bad material/data combinations. It works with material ID's, so it does use some deprecated Bukkit API - when it becomes clear how Bukkit will handle the complete retirement of fixed material ID's, I'll update ItemNames accordingly.

    While ItemNames is part of dhutils, it has no extra dependencies, so you are free to use the class as a standalone class in your plugins. Alternatively, if you use Maven, you can shade dhutils into your plugin - see https://github.com/desht/dhutils/blob/master/README.md.
     
  2. Offline

    MCForger

  3. Offline

    shohouku

    So it get's any? including custom?
     
  4. Offline

    MCForger

    shohouku
    It returns the material name of an itemstack in a nice readable format that you could use in a plugin when sending a message about what items your giving to a player.
     
    desht and shohouku like this.
  5. Offline

    BungeeTheCookie

    Depends what server you run. If you run vanilla minecraft or plain minecraft forge server, this probably will not work. I'd you want to give a player a custom item via cauldron, you have to make a custom craft item stack wrapper with the mod you are using. That is probably too advanced for you if you are asking if this work on nah item stack. If you want to get an item stack, yea this will work. You will need to add the material values yourself with the id name.
     
  6. Offline

    desht

    BungeeTheCookie Yeah, it's not going to work for any custom (non-vanilla) items, e.g. as added by Cauldron/MCPC+. But if I remember right, Cauldron injects fake names into the Material enum for all the modded items that it adds, so ItemNames should at least return a raw material name like "EXTRAUTILITIES_COMPRESSED_COBBLESTONE".

    In any case, this is very much intended for Bukkit plugins with vanilla clients. I don't plan to go out of my way to support anything other than plain CraftBukkit, so let's not discuss other Bukkit implementations too deeply please, to avoid a thread lock :)
     
  7. Offline

    BungeeTheCookie

    • Trolling while impersonating mods doesn't make it less of a problem ;)
    Locked. We do not support CraftBukkit. :)
     
    Plo124 and desht like this.
  8. Offline

    xTrollxDudex

  9. Offline

    desht

  10. Offline

    xTrollxDudex

    desht
    Meh, you can add data values :p
     
  11. Offline

    GriffinPvP

    I know this thread is old, but I was using your library for some time, today I discovered this:

    public static String getName(ItemStack stack) {
    return CraftItemStack.asNMSCopy(stack).getName();
    }

    It's pretty amazing, I know.
     
    JanTuck and Bammerbom like this.
Thread Status:
Not open for further replies.

Share This Page