package org.squiddev.plethora.integration.vanilla.meta;

import com.google.common.collect.Maps;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.item.ItemEnchantedBook;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import org.squiddev.plethora.api.meta.BasicMetaProvider;
import org.squiddev.plethora.api.meta.IMetaProvider;

import javax.annotation.Nonnull;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * Lists enchantments on items
 */
@IMetaProvider.Inject(value = ItemStack.class, namespace = "enchantments")
public class MetaItemEnchantment extends BasicMetaProvider<ItemStack> {
	@Nonnull
	@Override
	public Map<Object, Object> getMeta(@Nonnull ItemStack stack) {
		NBTTagList enchants = null;
		if (stack.isItemEnchanted()) {
			enchants = stack.getEnchantmentTagList();
		} else if (stack.getItem() instanceof ItemEnchantedBook) {
			enchants = ((ItemEnchantedBook) stack.getItem()).getEnchantments(stack);
		}

		if (enchants != null && enchants.tagCount() > 0) {
			HashMap<Object, Object> items = Maps.newHashMap();

			for (int i = 0; i < enchants.tagCount(); i++) {
				NBTTagCompound tag = enchants.getCompoundTagAt(i);
				Enchantment enchantment = Enchantment.getEnchantmentByID(tag.getShort("id"));
				if (enchantment != null) {
					HashMap<String, Object> enchant = Maps.newHashMap();
					enchant.put("name", enchantment.getName());

					short level = tag.getShort("lvl");
					enchant.put("level", level);
					enchant.put("fullName", enchantment.getTranslatedName(level));

					items.put(i + 1, enchant);
				}
			}

			return items;
		} else {
			return Collections.emptyMap();
		}
	}
}

@@ -160,30 +160,37 @@
 aft.
-item.ItemEnchantedBook
+enchantment.EnchantmentHelper
 ;%0Aim
@@ -228,86 +228,8 @@
 ck;%0A
-import net.minecraft.nbt.NBTTagCompound;%0Aimport net.minecraft.nbt.NBTTagList;%0A
 impo
@@ -724,178 +724,34 @@
 %7B%0A%09%09
-NBTTagList enchants = null;%0A%09%09if (stack.isItemEnchanted()) %7B%0A%09%09%09enchants = stack.getEnchantmentTagList();%0A%09%09%7D else if (stack.getItem() instanceof ItemEnchantedBook) %7B%0A%09%09%09
+Map%3CEnchantment, Integer%3E 
 ench
@@ -761,45 +761,25 @@
 s = 
-((Item
 Enchant
-edBook) stack.getItem())
+mentHelper
 .get
@@ -802,20 +802,16 @@
 ck);
-%0A%09%09%7D
 %0A%0A%09%09if (
 ench
@@ -810,51 +810,27 @@
 if (
-enchants != null && enchants.tagCount() %3E 0
+!enchants.isEmpty()
 ) %7B%0A
@@ -891,13 +891,8 @@
 %0A%09%09%09
-for (
 int 
@@ -901,95 +901,79 @@
 = 0;
- i %3C enchants.tagCount(); i++) %7B%0A%09%09%09%09NBTTagCompound tag = enchants.getCompoundTagAt(i);
+%0A%09%09%09for (Map.Entry%3CEnchantment, Integer%3E entry : enchants.entrySet()) %7B
 %0A%09%09%09
@@ -1003,92 +1003,24 @@
 t = 
-Enchantm
 ent
+ry
 .get
-EnchantmentByID(tag.getShort(%22id%22));%0A%09%09%09%09if (enchantment != null) %7B%0A%09
+Key();%0A
 %09%09%09%09
@@ -1072,17 +1072,16 @@
 hMap();%0A
-%09
 %09%09%09%09ench
@@ -1129,13 +1129,10 @@
 %09%09%09%09
-%09shor
+in
 t le
@@ -1141,30 +1141,26 @@
 l = 
-tag.getShort(%22lvl%22
+entry.getValue(
 );%0A
-%09
 %09%09%09%09
@@ -1184,17 +1184,27 @@
 l%22, 
-level
+entry.getValue()
 );%0A
-%09
 %09%09%09%09
@@ -1271,17 +1271,16 @@
 );%0A%0A%09%09%09%09
-%09
 items.pu
@@ -1285,13 +1285,11 @@
 put(
-i + 1
+++i
 , en
@@ -1300,14 +1300,8 @@
 t);%0A
-%09%09%09%09%7D%0A
 %09%09%09%7D

Update MetaItemEnchantment to use Minecraft functions