package party.lemons.anima.content.block.tileentity;

import net.minecraft.item.ItemStack;
import party.lemons.anima.content.item.ItemAnimaCharged;
import party.lemons.anima.energy.CapabilityAnima;

/**
 * Created by Sam on 29/06/2017.
 */
public class TileEntityCharger extends TileEntityLinkableWorker
{
	private int currentItem;

	public TileEntityCharger()
	{
		super(10, 10, 100, MachineLevel.BATTERY);
		currentItem = 0;
	}

	@Override
	public boolean canWork()
	{
		return !isEmpty();
	}

	@Override
	public boolean canSendEnergy()
	{
		return false;
	}

	@Override
	public boolean canSendItem(ItemStack stack)
	{
		if(stack.isEmpty() || !(stack.getItem() instanceof ItemAnimaCharged))
		{
			return true;
		}

		ItemAnimaCharged it = (ItemAnimaCharged) stack.getItem();
		return !(it.getCurrentCharge(stack) < it.getMaxCharge(stack));
	}

	private void increaseItem()
	{
		currentItem++;
		if(currentItem > items.getSlots() - 1)
		{
			currentItem = 0;
		}
	}

	@Override
	public void work()
	{
		extractWorkEnergy();
		ItemStack stack = items.getStackInSlot(currentItem);
		while(stack.isEmpty() || !(stack.getItem() instanceof ItemAnimaCharged))
		{
			increaseItem();
			stack = items.getStackInSlot(currentItem);
		}
		increaseItem();
		int chargeAmount = extractEnergy(100);
		int amountLeft = stack.getCapability(CapabilityAnima.ANIMA, null).receiveEnergy(chargeAmount, false);

		addEnergy(amountLeft);
	}
}

@@ -996,16 +996,60 @@
 rk()%0A%09%7B%0A
+%09%09if(!world.isRemote)%0A%09%09%7B%0A%09%09%09int safe = 0;%0A%09
 %09%09extrac
@@ -1065,16 +1065,17 @@
 gy();%0A%09%09
+%09
 ItemStac
@@ -1125,14 +1125,16 @@
 ;%0A%09%09
+%09
 while(
+(
 stac
@@ -1195,21 +1195,37 @@
 harged))
+ && safe %3C 10)
 %0A%09%09
+%09
 %7B%0A
+%09
 %09%09%09incre
@@ -1235,16 +1235,17 @@
 Item();%0A
+%09
 %09%09%09stack
@@ -1282,20 +1282,34 @@
 tItem);%0A
+%09%09%09%09safe++;%0A%09
 %09%09%7D%0A
+%09
 %09%09increa
@@ -1318,16 +1318,79 @@
 Item();%0A
+%09%09%09if(stack.hasCapability(CapabilityAnima.ANIMA, null))%0A%09%09%09%7B%0A%09%09
 %09%09int ch
@@ -1422,16 +1422,18 @@
 y(100);%0A
+%09%09
 %09%09int am
@@ -1528,17 +1528,18 @@
 false);%0A
-%0A
+%09%09
 %09%09addEne
@@ -1555,13 +1555,22 @@
 tLeft);%0A
+%09%09%09%7D%0A%09%09%7D%0A
 %09%7D%0A%7D%0A

Fix crash issue with charger on MP, items don't charge in MP