package landmaster.plustic.traits;

import baubles.api.*;
import baubles.api.cap.*;
import java.util.*;
import net.minecraft.entity.*;
import net.minecraft.entity.player.*;
import net.minecraft.item.*;
import net.minecraft.world.*;
import slimeknights.tconstruct.library.traits.*;
import slimeknights.tconstruct.library.utils.*;
//import vazkii.botania.api.*;
import vazkii.botania.api.mana.*;

public class Mana extends AbstractTrait {
	public static final Mana mana = new Mana();
	
	public Mana() {
		super("mana", 0x54E5FF);
	}
	
	@Override
	public void onUpdate(ItemStack tool, World world, Entity entity, int itemSlot, boolean isSelected) {
		if (!world.isRemote && entity instanceof EntityPlayer && ToolHelper.getCurrentDurability(tool) < ToolHelper.getMaxDurability(tool)) {
			EntityPlayer ep = (EntityPlayer)entity;
			List<ItemStack[]> ivs = Arrays.asList(ep.inventory.mainInventory,ep.inventory.armorInventory,ep.inventory.offHandInventory);
			for (ItemStack[] iv: ivs) {
				for (int i=0; i<iv.length; ++i) {
					if (ManaItemHandler.requestManaExact(iv[i], ep, 100, true)) {
						ep.inventory.markDirty();
						ToolHelper.healTool(tool, 1, ep);
						return;
					}
				}
			}
			IBaublesItemHandler ib = BaublesApi.getBaublesHandler(ep);
			for (int i=0; i<ib.getSlots(); ++i) {
				ItemStack is = ib.getStackInSlot(i);
				if (is == null) continue;
				is = is.copy();
				if (ManaItemHandler.requestManaExact(is, ep, 100, true)) {
					ToolHelper.healTool(tool, 1, ep);
					ib.setStackInSlot(i, is);
					return;
				}
			}
		}
	}
	
	@Override
	public int onToolDamage(ItemStack tool, int damage, int newDamage, EntityLivingBase entity) {
		manadraw:
		if (entity instanceof EntityPlayer && newDamage > 0) {
			EntityPlayer ep = (EntityPlayer)entity;
			List<ItemStack[]> ivs = Arrays.asList(ep.inventory.mainInventory,ep.inventory.armorInventory,ep.inventory.offHandInventory);
			for (ItemStack[] iv: ivs) {
				for (int i=0; i<iv.length; ++i) {
					if (ManaItemHandler.requestManaExact(iv[i], ep, 100, true)) {
						ep.inventory.markDirty();
						if (--newDamage <= 0) break manadraw;
					}
				}
			}
			IBaublesItemHandler ib = BaublesApi.getBaublesHandler(ep);
			for (int i=0; i<ib.getSlots(); ++i) {
				ItemStack is = ib.getStackInSlot(i);
				if (is == null) continue;
				is = is.copy();
				if (ManaItemHandler.requestManaExact(is, ep, 100, true)) {
					ib.setStackInSlot(i, is);
					if (--newDamage <= 0) break manadraw;
				}
			}
		}
		return super.onToolDamage(tool, damage, newDamage, entity);
	}
}

@@ -1048,32 +1048,39 @@
 requestManaExact
+ForTool
 (iv%5Bi%5D, ep, 100,
@@ -1081,32 +1081,32 @@
 , 100, true)) %7B%0A
-
 %09%09%09%09%09%09ep.invento
@@ -1419,32 +1419,39 @@
 requestManaExact
+ForTool
 (is, ep, 100, tr
@@ -2016,16 +2016,23 @@
 anaExact
+ForTool
 (iv%5Bi%5D, 
@@ -2329,32 +2329,32 @@
 is = is.copy();%0A
-
 %09%09%09%09if (ManaItem
@@ -2377,16 +2377,23 @@
 anaExact
+ForTool
 (is, ep,

Fix #7