package reborncore.common.powerSystem.tesla;

import net.darkhax.tesla.capability.TeslaCapabilities;
import net.darkhax.tesla.lib.TeslaUtils;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.common.capabilities.Capability;
import reborncore.common.RebornCoreConfig;
import reborncore.common.powerSystem.TilePowerAcceptor;

/**
 * Created by modmuss50 on 06/05/2016.
 */
public class TeslaPowerManager implements ITeslaPowerManager {

	AdvancedTeslaContainer container;

	@Override
	public void readFromNBT(NBTTagCompound compound, TilePowerAcceptor powerAcceptor) {
		this.container = new AdvancedTeslaContainer(compound.getTag("TeslaContainer"), powerAcceptor);
	}

	@Override
	public void writeToNBT(NBTTagCompound compound, TilePowerAcceptor powerAcceptor) {
		compound.setTag("TeslaContainer", this.container.writeNBT());
	}

	@Override
	public <T> T getCapability(Capability<T> capability, EnumFacing facing, TilePowerAcceptor powerAcceptor) {
		if (capability == TeslaCapabilities.CAPABILITY_CONSUMER && powerAcceptor.canAcceptEnergy(facing)) {
			this.container.tile = powerAcceptor;
			return (T) this.container;
		} else if (capability == TeslaCapabilities.CAPABILITY_PRODUCER && powerAcceptor.canProvideEnergy(facing)) {
			this.container.tile = powerAcceptor;
			return (T) this.container;
		}
		if (capability == TeslaCapabilities.CAPABILITY_HOLDER) {
			this.container.tile = powerAcceptor;
			return (T) this.container;
		}
		return null;
	}

	@Override
	public boolean hasCapability(Capability<?> capability, EnumFacing facing, TilePowerAcceptor powerAcceptor) {
		if (capability == TeslaCapabilities.CAPABILITY_CONSUMER && powerAcceptor.canAcceptEnergy(facing)) {
			return true;
		} else if (capability == TeslaCapabilities.CAPABILITY_PRODUCER && powerAcceptor.canProvideEnergy(facing)) {
			return true;
		}
		if (capability == TeslaCapabilities.CAPABILITY_HOLDER)
			return true;
		return false;
	}

	@Override
	public void update(TilePowerAcceptor powerAcceptor) {
		if (powerAcceptor.canProvideEnergy(null)) {
			TeslaUtils.distributePowerToAllFaces(powerAcceptor.getWorld(), powerAcceptor.getPos(), (long) powerAcceptor.getMaxOutput(), false);
		}
	}

	@Override
	public void created(TilePowerAcceptor powerAcceptor) {
		this.container = new AdvancedTeslaContainer(powerAcceptor);
	}

	@Override
	public String getDisplayableTeslaCount(long tesla) {
		return TeslaUtils.getDisplayableTeslaCount(tesla * RebornCoreConfig.euPerFU);
	}

	@Override
	public boolean isTeslaCapability(Capability<?> capability) {
		return capability == TeslaCapabilities.CAPABILITY_CONSUMER || capability == TeslaCapabilities.CAPABILITY_PRODUCER || capability == TeslaCapabilities.CAPABILITY_HOLDER;
	}

	public static ITeslaPowerManager getPowerManager() {
		return new TeslaPowerManager();
	}
}

@@ -39,16 +39,61 @@
 tesla;%0A%0A
+import net.darkhax.tesla.api.ITeslaConsumer;%0A
 import n
@@ -417,16 +417,40 @@
 eptor;%0A%0A
+import java.util.List;%0A%0A
 /**%0A * C
@@ -2156,139 +2156,557 @@
 %0A%09%09%09
-TeslaUtils.distributePowerToAllFaces(powerAcceptor.getWorld(), powerAcceptor.getPos(), (long) powerAcceptor.getMaxOutput(), false);
+List%3CITeslaConsumer%3E connectedConsumers = TeslaUtils.getConnectedCapabilities(TeslaCapabilities.CAPABILITY_CONSUMER, powerAcceptor.getWorld(), powerAcceptor.getPos());%0A%0A%09%09%09for (ITeslaConsumer consumer : connectedConsumers) %7B%0A%09%09%09%09double euToTransfer = Math.min(powerAcceptor.getEnergy(), powerAcceptor.getMaxOutput());%0A%09%09%09%09long teslaTransferred = consumer.givePower((long) (euToTransfer / RebornCoreConfig.euPerFU), false);%0A%09%09%09%09powerAcceptor.useEnergy(teslaTransferred * RebornCoreConfig.euPerFU);%0A%0A%09%09%09%09if (powerAcceptor.getEnergy() %3C= 0) break;%0A%09%09%09%7D
 %0A%09%09%7D

Rewrite update in TeslaPowerManager in favour of a custom system