LanguageUtils icon indicating copy to clipboard operation
LanguageUtils copied to clipboard

Name -> Item

Open Swedz opened this issue 6 years ago • 1 comments

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.

Swedz avatar Jul 05 '18 21:07 Swedz

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;
}

Swedz avatar Jul 06 '18 01:07 Swedz