[Enum] User-friendly material names

Discussion in 'Resources' started by LCastr0, Mar 7, 2014.

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

    LCastr0

    Hello!
    After searching a little, I didn't find anything that fits my problem. I was doing a "give" system, and the messages needed the item's name, and I didn't find a better way than make an Enum with all the materials and it's name.
    So, here is the enum:

    Code:java
    1. package me.LCastr0.ButterCraft.SimpleCommands.Enums;
    2.  
    3. import java.util.HashMap;
    4. import java.util.Map;
    5.  
    6. import org.apache.commons.lang.WordUtils;
    7.  
    8. public enum ItemNames {
    9.  
    10. //Made by LCastr0
    11. //sbuttercraft.com
    12. //Please, do not remove this lines!
    13.  
    14. ACACIA_STAIRS("acacia wood stairs"),
    15. ACTIVATOR_RAIL("activator rail"),
    16. AIR("air"),
    17. ANVIL("anvil"),
    18. APPLE("apple"),
    19. ARROW("arrow"),
    20. BAKED_POTATO("baked potato"),
    21. BEACON("beacon"),
    22. BED("bed"),
    23. BED_BLOCK("bed block"),
    24. BEDROCK("bedrock"),
    25. BIRCH_WOOD_STAIRS("birch wood stairs"),
    26. BLAZE_POWDER("blaze powder"),
    27. BLAZE_ROD("blaze rod"),
    28. BOAT("boat"),
    29. BONE("bone"),
    30. BOOK("book"),
    31. BOOK_AND_QUILL("book and quill"),
    32. BOOKSHELF("bookshelf"),
    33. BOW("bow"),
    34. BOWL("bowl"),
    35. BREAD("bread"),
    36. BREWING_STAND("brewing stand"),
    37. BRICK("bricks"),
    38. BRICK_STAIRS("brick stairs"),
    39. BROWN_MUSHROOM("brown mushroom"),
    40. BUCKET("bucket"),
    41. BURNING_FURNACE("burning furnace"),
    42. CACTUS("cactus"),
    43. CAKE("cake"),
    44. CAKE_BLOCK("cake"),
    45. CARPET("carpet"),
    46. CARROT("carrot"),
    47. CARROT_ITEM("carrot"),
    48. CARROT_STICK("carrot on a stick"),
    49. CAULDRON("cauldron"),
    50. CAULDRON_ITEM("cauldron"),
    51. CHAINMAIL_BOOTS("chainmail boots"),
    52. CHAINMAIL_CHESTPLATE("chainmail chestplate"),
    53. CHAINMAIL_HELMET("chainmail helmet"),
    54. CHAINMAIL_LEGGINGS("chainmail leggings"),
    55. CHEST("chest"),
    56. CLAY("clay"),
    57. CLAY_BALL("clay"),
    58. CLAY_BRICK("brick"),
    59. COAL("coal"),
    60. COAL_BLOCK("block of coal"),
    61. COAL_ORE("coal ore"),
    62. COBBLE_WALL("cobblestone wall"),
    63. COBBLESTONE("cobblestone"),
    64. COBBLESTONE_STAIRS("cobblestone stairs"),
    65. COCOA("cocoa"),
    66. COMMAND("command block"),
    67. COMPASS("compass"),
    68. COOKED_BEEF("steak"),
    69. COOKED_CHICKEN("cooked chicken"),
    70. COOKED_FISH("cooked fish"),
    71. COOKIE("cookie"),
    72. CROPS("crops"),
    73. DARK_OAK_STAIRS("dark oak wood stairs"),
    74. DAYLIGHT_DETECTOR("daylight sensor"),
    75. DEAD_BUSH("dead bush"),
    76. DETECTOR_RAIL("detector rail"),
    77. DIAMOND("diamond"),
    78. DIAMOND_AXE("diamond axe"),
    79. DIAMOND_BARDING("diamond horse armor"),
    80. DIAMOND_BLOCK("diamond block"),
    81. DIAMOND_BOOTS("diamond boots"),
    82. DIAMOND_CHESTPLATE("diamond chestplate"),
    83. DIAMOND_HELMET("diamond helmet"),
    84. DIAMOND_HOE("diamond hoe"),
    85. DIAMOND_LEGGINGS("diamond leggings"),
    86. DIAMOND_ORE("diamond ore"),
    87. DIAMOND_PICKAXE("diamond pickaxe"),
    88. DIAMOND_SPADE("diamond shovel"),
    89. DIAMOND_SWORD("diamond sword"),
    90. DIODE("redstone repeater"),
    91. DIODE_BLOCK_OFF("redstone repeater"),
    92. DIODE_BLOCK_ON("redstone repeater"),
    93. DIRT("dirt"),
    94. DISPENSER("dispenser"),
    95. DOULBE_PLANT("double plant"),
    96. DOUBLE_STEP("double slab"),
    97. DRAGON_EGG("dragon egg"),
    98. DROPPER("dropper"),
    99. EGG("egg"),
    100. EMERALD("emerald"),
    101. EMERALD_BLOCK("emerald block"),
    102. EMERALD_ORE("emerald ore"),
    103. EMPTY_MAP("empty map"),
    104. ENCHANTED_BOOK("enchanted book"),
    105. ENCHANTMENT_TABLE("enchantment table"),
    106. ENDER_CHEST("ender chest"),
    107. ENDER_PEARL("ender pearl"),
    108. ENDER_PORTAL("ender portal"),
    109. ENDER_PORTAL_FRAME("ender protal frame"),
    110. ENDER_STONE("ender stone"),
    111. EXP_BOTTLE("bottle o' enchanting"),
    112. EXPLOSIVE_MINECART("minecart with tnt"),
    113. EYE_OF_ENDER("eye of ender"),
    114. FEATHER("feather"),
    115. FENCE("fence"),
    116. FENCE_GATE("fence gate"),
    117. FERMENTED_SPIDER_EYE("fermented spider eye"),
    118. FIRE("fire"),
    119. FIREBALL("fireball"),
    120. FIREWORK("firework"),
    121. FIREWORK_CHARGE("firework charge"),
    122. FISHING_ROD("fishing rod"),
    123. FLINT("flint"),
    124. FLINT_AND_STEEL("flint and steel"),
    125. FLOWER_POT("flower pot"),
    126. FLOWER_POT_ITEM("flower pot"),
    127. FURNACE("furnace"),
    128. GHAST_TEAR("ghast tear"),
    129. GLASS("glass"),
    130. GLASS_BOTTLE("glass bottle"),
    131. GLOWING_REDSTONE_ORE("redstone ore"),
    132. GLOWSTONE("glowstone"),
    133. GLOWSTONE_DUST("glowstone dust"),
    134. GOLD_AXE("gold axe"),
    135. GOLD_BARDING("gold horse armor"),
    136. GOLD_BLOCK("gold block"),
    137. GOLD_BOOTS("gold boots"),
    138. GOLD_CHESTPLATE("gold chestplate"),
    139. GOLD_HELMET("gold helmet"),
    140. GOLD_HOE("gold hoe"),
    141. GOLD_INGOT("gold ingot"),
    142. GOLD_LEGGINGS("gold leggings"),
    143. GOLD_NUGGET("gold nugget"),
    144. GOLD_ORE("gold ore"),
    145. GOLD_PICKAXE("gold pickaxe"),
    146. GOLD_PLATE("weighted pressure plate"),
    147. GOLD_RECORD("music disc - 13"),
    148. GOLD_SPADE("gold shovel"),
    149. GOLD_SWORD("gold sword"),
    150. GOLDEN_APPLE("golden apple"),
    151. GOLDEN_CARROT("golden carrot"),
    152. GRASS("grass"),
    153. GRAVEL("gravel"),
    154. GREEN_RECORD("music disc - cat"),
    155. GRILLED_PORK("cooked porkchop"),
    156. HARD_CLAY("hardened clay"),
    157. HAY_BLOCK("hay bale"),
    158. HOPPER("hopper"),
    159. HOPPER_MINECART("minecart with hopper"),
    160. HUGE_MUSHROOM_1("huge mushroom"),
    161. HUGE_MUSHROOM_2("huge mushroom"),
    162. ICE("ice"),
    163. INK_SACK("ink sack"),
    164. IRON_AXE("iron axe"),
    165. IRON_BARDING("iron horse armor"),
    166. IRON_BLOCK("iron block"),
    167. IRON_BOOTS("iron boots"),
    168. IRON_CHESTPLATE("iron chestplate"),
    169. IRON_DOOR("iron door"),
    170. IRON_DOOR_BLOCK("iron door block"),
    171. IRON_FENCE("iron fence"),
    172. IRON_HELMET("iron helmet"),
    173. IRON_HOE("iron hoe"),
    174. IRON_INGOT("iron ingot"),
    175. IRON_LEGGINGS("iron leggings"),
    176. IRON_ORE("iron ore"),
    177. IRON_PICKAXE("iron pickaxe"),
    178. IRON_PLATE("weighted pressure plate"),
    179. IRON_SPADE("iron shovel"),
    180. IRON_SWORD("iron sword"),
    181. ITEM_FRAME("item frame"),
    182. JACK_O_LANTERN("jack-o-lantern"),
    183. JUKEBOX("jukebox"),
    184. JUNGLE_WOOD_STAIRS("jungle wood stairs"),
    185. LADDER("ladder"),
    186. LAPIS_BLOCK("lapis lazuli block"),
    187. LAPIS_ORE("lapis lazuli ore"),
    188. LAVA("lava"),
    189. LAVA_BUCKET("lava bucket"),
    190. LEASH("lead"),
    191. LEATHER("leather"),
    192. LEATHER_BOOTS("leather boots"),
    193. LEATHER_CHESTPLATE("leather tunic"),
    194. LEATHER_HELMET("leather cap"),
    195. LEATHER_LEGGINGS("leather pants"),
    196. LEAVES("leaves"),
    197. LEAVES_2("leaves"),
    198. LEVER("lever"),
    199. LOCKED_CHEST("locked chest"),
    200. LOG("wood log"),
    201. LOG_1("wood log"),
    202. LONG_GRASS("shrub"),
    203. MAGMA_CREAM("magma cream"),
    204. MAP("map"),
    205. MELON("melon"),
    206. MELON_BLOCK("melon"),
    207. MELON_SEEDS("melon seeds"),
    208. MELON_STEM("melon stem"),
    209. MILK_BUCKET("milk bucket"),
    210. MINECART("minecart"),
    211. MOB_SPAWNER("mob spawner"),
    212. MONSTER_EGG("mob egg"),
    213. MONSTER_EGGS("silverfish block"),
    214. MOSSY_COBBLESTONE("mossy cobblestone"),
    215. MUSHROOM_SOUP("mushroom soup"),
    216. MYCEL("mycelium"),
    217. NAME_TAG("name tag"),
    218. NETHER_BRICK("nether brick"),
    219. NETHER_BRICK_ITEM("nether brick"),
    220. NETHER_BRICK_STAIRS("nether brick stairs"),
    221. NETHER_FENCE("nether brick fence"),
    222. NETHER_STALK("nether wart"),
    223. NETHER_STAR("nether star"),
    224. NETHER_WARTS("nether wart"),
    225. NETHERRACK("netherrack"),
    226. NOTE_BLOCK("note block"),
    227. OBSIDIAN("obsidian"),
    228. PAINTING("painting"),
    229. PAPER("paper"),
    230. PISTON_BASE("piston"),
    231. PISTON_EXTENSION("piston extension"),
    232. PISTON_MOVING_PIECE("moving piston"),
    233. PISTON_STICK_BASE("sticky piston"),
    234. POISONOUS_POTATO("poisonous potato"),
    235. PORK("raw porkchop"),
    236. PORTAL("portal"),
    237. POTATO("potato"),
    238. POTATO_ITEM("potato"),
    239. POTION("potion"),
    240. POWERED_MINECART("minecart with furnace"),
    241. POWERED_RAIL("powered rail"),
    242. PUMPKIN("pumpkin"),
    243. PUMPKIN_PIE("pumpkin pie"),
    244. PUMPKIN_SEEDS("pumpkin seeds"),
    245. PUMPKIN_STEM("pumpkin stem"),
    246. QUARTZ("quartz"),
    247. QUARTZ_BLOCK("block of quartz"),
    248. QUARTZ_ORE("quartz ore"),
    249. QUARTZ_STAIRS("quartz stairs"),
    250. RAILS("rail"),
    251. RAW_BEEF("raw beef"),
    252. RAW_CHICKEN("raw chicken"),
    253. RAW_FISH("raw fish"),
    254. RECORD_10("music disc - ward"),
    255. RECORD_11("music disc - 11"),
    256. RECORD_12("music disc - wait"),
    257. RECORD_3("music disc - blocks"),
    258. RECORD_4("music disc - chirp"),
    259. RECORD_5("music disc - far"),
    260. RECORD_6("music disc - mall"),
    261. RECORD_7("music disc - mellohi"),
    262. RECORD_8("mucis disc - stal"),
    263. RECORD_9("music disc - strad"),
    264. RED_MUSHROOM("red mushroom"),
    265. RED_ROSE("red rose"),
    266. REDSTONE("redstone"),
    267. REDSTONE_BLOCK("block of redstone"),
    268. REDSTONE_COMPARATOR("redstone comparator"),
    269. REDSTONE_COMPARATOR_OFF("redstone comparator"),
    270. REDSTONE_COMPARATOR_ON("redstone comparator"),
    271. REDSTONE_LAMP_OFF("redstone lamp"),
    272. REDSTONE_LAMP_ON("redstone lamp"),
    273. REDSTONE_ORE("redstone ore"),
    274. REDSTONE_TORCH_OFF("redstone torch"),
    275. REDSTONE_TORCH_ON("redstone torch"),
    276. REDSTONE_WIRE("redstone"),
    277. ROTTEN_FLESH("rotten flesh"),
    278. SADDLE("saddle"),
    279. SAND("sand"),
    280. SANDSTONE("sandstone"),
    281. SANDSTONE_STAIRS("sandstone stairs"),
    282. SAPLING("sapling"),
    283. SEEDS("seeds"),
    284. SHEARS("shears"),
    285. SIGN("sign"),
    286. SIGN_POST("sign"),
    287. SKULL("skull"),
    288. SKULL_ITEM("skull"),
    289. SLIME_BLOCK("slime block"),
    290. SMOOTH_BRICK("stone bricks"),
    291. SMOOTH_STAIRS("stone bricks stairs"),
    292. SNOW("snow"),
    293. SNOW_BALL("snow ball"),
    294. SNOW_BLOCK("snow block"),
    295. SOIL("farmland"),
    296. SOUL_SAND("soul sand"),
    297. SPECKLED_MELON("glistering melon"),
    298. SPIDER_EYE("spider eye"),
    299. SPONGE("sponge"),
    300. SPRUCE_WOOD_STAIRS("spruce wood stairs"),
    301. STAINED_CLAY("stained clay"),
    302. STAINED_GLASS("stained glass"),
    303. STAINED_GLASS_PANE("stained glass pane"),
    304. STATIONARY_LAVA("stationary lava"),
    305. STEP("slab"),
    306. STICK("stick"),
    307. STONE("stone"),
    308. STONE_AXE("stone axe"),
    309. STONE_BUTTON("button"),
    310. STONE_HOE("stone hoe"),
    311. STONE_PICKAXE("stone pickaxe"),
    312. STONE_PLATE("pressure plate"),
    313. STONE_SPADE("stone shovel"),
    314. STONE_SWORD("stone sword"),
    315. STORAGE_MINECART("minecart with chest"),
    316. STRING("string"),
    317. SUGAR("sugar"),
    318. SUGAR_CANE("sugar cane"),
    319. SUGAR_CANE_BLOCK("sugar cane"),
    320. SULPHUR("gunpowder"),
    321. THIN_GLASS("glass pane"),
    322. TNT("tnt"),
    323. TORCH("torch"),
    324. TRAP_DOOR("trapdoor"),
    325. TRAPPED_CHEST("trapped chest"),
    326. TRIPWIRE("tripwire"),
    327. TRIPWIRE_HOOK("tripwire hook"),
    328. VINE("vines"),
    329. WALL_SIGN("sign"),
    330. WATCH("clock"),
    331. WATER("water"),
    332. WATER_BUCKET("water bucket"),
    333. WATER_LILY("lily pad"),
    334. WEB("cobweb"),
    335. WHEAT("wheat"),
    336. WOOD("wood"),
    337. WOOD_AXE("wooden axe"),
    338. WOOD_BUTTON("button"),
    339. WOOD_DOOR("wooden door"),
    340. WOOD_DOUBLE_STEP("double wood slab"),
    341. WOOD_HOE("wooden hoe"),
    342. WOOD_PICKAXE("wooden pickaxe"),
    343. WOOD_PLATE("pressure plate"),
    344. WOOD_SPADE("wooden shovel"),
    345. WOOD_STAIRS("wood stairs"),
    346. WOOD_STEP("wood slab"),
    347. WOOD_SWORD("wooden sword"),
    348. WOODEN_DOOR("wooden door"),
    349. WOOL("wool"),
    350. WORKBENCH("crafting table"),
    351. WRITTEN_BOOK("written book"),
    352. YELLOW_FLOWER("dandelion")
    353. ;
    354.  
    355. private final String name;
    356.  
    357. private ItemNames(final String name){
    358. this.name = name;
    359. }
    360.  
    361. public String toString(){
    362. return name;
    363. }
    364.  
    365. private static final Map<String, ItemNames> lookup = new HashMap<String, ItemNames>();
    366. //Returns the Material name from the given block name
    367. public static String getMaterialName(String fromBlockName){
    368. for(ItemNames n : values()){
    369. lookup.put(n.toString(), n);
    370. }
    371. String result = lookup.get(fromBlockName).name();
    372. return result;
    373. }
    374.  
    375. //Returns the item name with the first letter uppercased (Example: pressure plate -> Pressure plate)
    376. public String firstUpperCased(){
    377. char first = Character.toUpperCase(name.charAt(0));
    378. return first + name.substring(1);
    379. }
    380.  
    381. //Returns the item name with all the words with the first letter uppercased (Example: pressure plate -> Pressure Plate)
    382. public String firstAllUpperCased(){
    383. return WordUtils.capitalizeFully(name);
    384. }
    385.  
    386. //Returns the item name with all the letters uppercased (Example: pressure plate -> PRESSURE PLATE)
    387. public String allUpperCased(){
    388. return name.toUpperCase();
    389. }
    390.  
    391. }
    392.  



    To use it, it's very simple:

    Returns the item name with no capital letters:
    Code:java
    1. private ItemNames names;
    2. public String itemName(Material type){
    3. return names.valueOf(type.toString()).toString();
    4. }


    Returns the item name with the first letter capitalized:
    Code:
    private ItemName names;
    public String itemName(Material type){
    return names.valueOf(type.toString()).firstUpperCased();
    }
    Returns the item name with the first letter of every word capitalized:
    Code:
    private ItemName names;
    public String itemName(Material type){
    return names.valueOf(type.toString()).firstAllUpperCased();
    }
    Returns the item name with all the letters capitalized:
    Code:
    private ItemName names;
    public String itemName(Material type){
    return names.valueOf(type.toString()).allUpperCased();
    }
    Returns the Material, from the given item name:
    Code:
    public Material fromItemName(String itemName){
    return Material.getMaterial(ItemNames.getMaterialName(itemName));
    }
    
    Please, don't remove the green lines from the Enum! I took more than 1 hour to write it!
    #Edit: If I missed any material, or changed any name, please, tell me!

    #Edit 1 - WALL_SIGN, LEATHER_LEGGINGS, LEATHER_HELMET and LEATHER_CHESTPLATE. Thanks hugokk

    #Edit 2 - Added option to return Material's name (or the Material itself) from the given item name
     
  2. Offline

    strubelcopter

    Just do this with the standart Material Enum:
    Code:java
    1. Material.GOLD_BLOCK.toString.toLowerCase.replace("_", " ");

    Most names should be fixed than.
     
    timtower likes this.
  3. Offline

    LCastr0

    It's not the same. Some items/blocks have a different name for the material:
    Code:
    GRILLED_PORK("cooked porkchop"),
    Code:
    HARD_CLAY("hardened clay"),
    And a lot more, that I won't put here.

    Edit #1: Workbench -> Crafting table

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  4. Offline

    hugokk

    Very nice list :D, it's really helpful!

    I found a few mistakes:
    WALL_SIGN: sing -> this is supposed to be sign ofcourse
    LEATHER_LEGGINGS: leather leggings -> they are called leather pants
    LEATHER_CHESTPLATE: leather chestplate -> this must be leather tunic
    LEATHER_HELMET: leather helmet -> leather cap
     
  5. Offline

    LCastr0

    Thank you :) Edited
     
  6. Offline

    xXSniperzzXx_SD

    LCastr0
    I'm lazy so is there anyway to make it work the other way too? So you feed in the friendly material name, and get the full MaterialType name?
     
  7. Offline

    LCastr0

    Not sure, because I'm not that good with Enums, but I'll try to find a way

    xXSniperzzXx_SD I know it's possible, but I'm not sure how...
    Maybe I could make a reverse enum, but it would take a lot of time :/

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  8. Offline

    Not2EXceL

    Why wouldn't you just tack on another param in the constructor and make a method that does that? srsly not that hard
     
  9. Offline

    LCastr0

    I was actually doing something wrong... (I changed the Map that would return the name D: )
    Edited the post with the new code, and how to use
     
Thread Status:
Not open for further replies.

Share This Page