Getting inventory-style name of a material

Discussion in 'Plugin Development' started by Clavus, Jan 6, 2013.

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

    Clavus

    I'm trying to retrieve the 'inventory name' of a block when you a player interacts with it. What I tried so far:

    Code:
    ItemStack stack = new ItemStack(block.getType(), 1, (short)0);
    stack.setData(new MaterialData(block.getType(), block.getData()));
    String name = stack.getItemMeta().getDisplayName();
    but 'name' just comes out as 'null' in this case. Any help?
     
  2. Offline

    fireblast709

    shouldn't you get the inventory of the block (like casting the BlockState to org.bukkit.block.Chest for chests, get the inventory and get the name of that inventory)
     
  3. Offline

    Clavus

    Uhm maybe I wasn't too clear, I want to get the display name of the item, like how it is shown inside an inventory. Not the name of the actual inventory. Sorry for the confusion.
     
  4. Offline

    fireblast709

    Code:java
    1. String name = stack.getItemMeta().hasItemMeta() ? stack.getItemMeta().getDisplayName() : stack.getType().name().replace("_", " ").toLowerCase();
    Note that this gives the bukkit Material name, not the actual minecraft name. For that you should either have a list of the names yourself or somehow extract the name from the NMS classes
     
  5. Offline

    Clavus

    So there's no normal way to retrieve the display names of inventory items? That'd suck. The method you'd give wouldn't take metadata into account, so different colours of wool would just be displayed as "wool".
     
  6. Offline

    fireblast709

    Well most of the items would have the same name, but items like cooked porkchop would return the name "grilled porkchop"
     
  7. Offline

    Clavus

    Yes, since that has a different item ID.

    Meh, it's possible the inventory item names are not on the server at all since they're only shown on the client, and get their names from the language files.
     
  8. Offline

    fireblast709

    If you want to display the normal name, just set the display name to null
     
Thread Status:
Not open for further replies.

Share This Page