LanguageUtils
LanguageUtils copied to clipboard
Name -> Item
So I have a situation where I need to take the name of an item, and convert it back to an item. I don't care for the item count or anything, just the data value and the material. Like a situation where I would type White Wool
and it would give me new ItemStack(Material.WOOL, 1, (byte)0)
. I was imagining I could just do a loop through Material.values()
, but that wouldn't give me the different colored wools, or types of logs, etc.
May have found a way of doing it. Let me know if this would be the correct way of doing it with your API:
private ItemStack getItem(String text) {
HashMap<ItemStack, String> names = new HashMap<>();
for(EnumItem ei : EnumItem.values()) {
String unlocal = ei.getUnlocalizedName();
String local = LanguageHelper.translateToLocal(unlocal, EnumLang.EN_US.getLocale());
names.put(new ItemStack(ei.getMaterial(), 1, (short)ei.getMetadata()), local);
}
for(ItemStack item : names.keySet()) {
String local = names.get(item);
if(local.equalsIgnoreCase(text))
return item;
}
return null;
}