package landmaster.plustic.traits;

import baubles.api.*;
import baubles.api.cap.*;
import landmaster.plustic.api.*;
import mcjty.lib.tools.*;

import net.minecraft.entity.*;
import net.minecraft.entity.player.*;
import net.minecraft.item.*;
import net.minecraft.world.*;
import net.minecraftforge.items.*;
import slimeknights.tconstruct.library.traits.*;
import slimeknights.tconstruct.library.utils.*;
import vazkii.botania.api.mana.*;

public class Mana extends AbstractTrait {
	public static final int MANA_DRAW = 100;
	public static final Mana mana = new Mana();
	
	public Mana() {
		super("mana", 0x54E5FF);
		Toggle.toggleable.add(identifier);
	}
	
	@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)
				&& Toggle.getToggleState(tool, identifier)
				&& drawMana((EntityPlayer)entity)) {
			ToolHelper.healTool(tool, MANA_DRAW, (EntityPlayer)entity);
		}
	}
	
	@Override
	public int onToolDamage(ItemStack tool, int damage, int newDamage, EntityLivingBase entity) {
		if (entity instanceof EntityPlayer
				&& Toggle.getToggleState(tool, identifier)
				&& drawMana((EntityPlayer)entity)) {
			--newDamage;
		}
		return super.onToolDamage(tool, damage, newDamage, entity);
	}
	
	private static boolean drawMana(EntityPlayer ent) {
		IItemHandler handler = ent.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
		if (handler instanceof IItemHandlerModifiable) {
			for (int i=0; i<handler.getSlots(); ++i) {
				ItemStack is = ItemStackTools.safeCopy(handler.getStackInSlot(i));
				if (ManaItemHandler.requestManaExactForTool(is, ent, MANA_DRAW, true)) {
					((IItemHandlerModifiable) handler).setStackInSlot(i, is);
					return true;
				}
			}
		}
		
		IBaublesItemHandler ib = BaublesApi.getBaublesHandler(ent);
		for (int i=0; i<ib.getSlots(); ++i) {
			ItemStack is = ItemStackTools.safeCopy(ib.getStackInSlot(i));
			if (ManaItemHandler.requestManaExactForTool(is, ent, MANA_DRAW, true)) {
				ib.setStackInSlot(i, is);
				return true;
			}
		}
		
		return false;
 	}
}

@@ -113,34 +113,8 @@
 i.*;
-%0Aimport mcjty.lib.tools.*;
 %0A%0Aim
@@ -994,25 +994,17 @@
 l(tool, 
-MANA_DRAW
+1
 , (Entit
@@ -1141,16 +1141,57 @@
 %7B%0A%09%09if (
+!entity.getEntityWorld().isRemote%0A%09%09%09%09&& 
 entity i
@@ -1544,175 +1544,51 @@
 ;%0A%09%09
-if (handler instanceof IItemHandlerModifiable) %7B%0A%09%09%09for (int i=0; i%3Chandler.getSlots(); ++i) %7B%0A%09%09%09%09ItemStack is = ItemStackTools.safeCopy(handler.getStackInSlot(i));%0A%09
+for (int i=0; i%3Chandler.getSlots(); ++i) %7B%0A
 %09%09%09i
@@ -1622,34 +1622,57 @@
 anaExactForTool(
-is
+handler.getStackInSlot(i)
 , ent, MANA_DRAW
@@ -1686,72 +1686,8 @@
 ) %7B%0A
-%09%09%09%09%09((IItemHandlerModifiable) handler).setStackInSlot(i, is);%0A%09
 %09%09%09%09
@@ -1703,14 +1703,8 @@
 ue;%0A
-%09%09%09%09%7D%0A
 %09%09%09%7D
@@ -1817,73 +1817,8 @@
 ) %7B%0A
-%09%09%09ItemStack is = ItemStackTools.safeCopy(ib.getStackInSlot(i));%0A
 %09%09%09i
@@ -1861,17 +1861,35 @@
 orTool(i
-s
+b.getStackInSlot(i)
 , ent, M
@@ -1911,38 +1911,8 @@
 ) %7B%0A
-%09%09%09%09ib.setStackInSlot(i, is);%0A
 %09%09%09%09

Fix #65