Get proper name from Material/MaterialID

Discussion in 'Plugin Development' started by Ne0nx3r0, Dec 6, 2012.

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

    Ne0nx3r0

    Hey I was just wondering if anyone has a nice helper class or if there is a simple way to get the proper name for an item given it's material + data value. This is an issue I've never properly solved.

    For example to take the material number and data value for a steve head or wood axe and return "Steve Head" or "Wood Axe", or whatever their proper name is.
     
  2. Offline

    gomeow

    I made a function that could take in 'bedblock' and return 'BedBlock',
    It could be easily modified instead of changing the underscore to "", but instead to " "
     
  3. Offline

    Gildan27

    The is the best I've come up with... can't help but think there's a better way:

    Code:
        public String getMaterialName(Material material) {
            StringBuilder materialName = new StringBuilder();
            for(String c : material.toString().toLowerCase().split("_")) {
                materialName.append(Character.toUpperCase(c.charAt(0))).append(c, 1, c.length()).append(" ");
            }
            return materialName.toString().trim();
        }
     
  4. Offline

    md_5

    There isn't really a way, I mean in Essentials we do it by having a huge CSV lookup table, but even then it only handles damage, not NBT data.
     
  5. Offline

    gomeow

    I can post my method if you want it, but it doesn't handle damage even
     
  6. Offline

    Ne0nx3r0

    Thanks for your replies :).

    If that's the case I suppose I can maintain my own. For my purpose I think the standard replace underscore+titlecase system will work, with the exception of skull items.

    Basically what Gildan27 uses with a prepended switch:
    Code:
    package com.gmail.ne0nx3r0.utils;
     
    import org.bukkit.Material;
     
    public class MaterialName
    {
        public static String getMaterialDisplayName(int matId,byte dv)
        {        
            Material m = Material.getMaterial(matId);
            
            if(m ==  Material.SKULL_ITEM)
            {
                switch(dv)
                {
                    case 0x0:
                        return "Skeleton Head";
                    case 0x1:
                        return "Wither Head";
                    case 0x2:
                        return "Zombie Head";
                    case 0x3:
                        return "Steve Head";
                    case 0x4:
                        return "Creeper Head";
                }
            }
     
            StringBuilder materialName = new StringBuilder();
            
            for(String c : m.toString().toLowerCase().split("_"))
            {
                materialName.append(Character.toUpperCase(c.charAt(0))).append(c, 1, c.length()).append(" ");
            }
            
            return materialName.toString().trim();
        }
    }
    
     
Thread Status:
Not open for further replies.

Share This Page