package com.cjm721.overloaded.block.tile;

import com.cjm721.overloaded.storage.LongEnergyStack;
import com.cjm721.overloaded.storage.energy.ForgeEnergyZero;
import com.cjm721.overloaded.storage.energy.IHyperHandlerEnergy;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ITickable;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.energy.IEnergyStorage;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import static com.cjm721.overloaded.util.CapabilityHyperEnergy.HYPER_ENERGY_HANDLER;
import static net.minecraftforge.energy.CapabilityEnergy.ENERGY;

public class TileEnergyExtractor extends AbstractTileEntityFaceable implements ITickable {

    public TileEnergyExtractor() {
    }


    /**
     * Like the old updateEntity(), except more generic.
     */
    @Override
    public void update() {
        BlockPos me = this.getPos();
        TileEntity frontTE = getWorld().getTileEntity(me.add(getFacing().getDirectionVec()));

        if (frontTE == null || !frontTE.hasCapability(HYPER_ENERGY_HANDLER, getFacing().getOpposite()))
            return;

        IHyperHandlerEnergy storage = frontTE.getCapability(HYPER_ENERGY_HANDLER, getFacing().getOpposite());
        LongEnergyStack energy = storage.take(new LongEnergyStack(Long.MAX_VALUE), false);
        for (EnumFacing facing : EnumFacing.values()) {
            if (energy.getAmount() == 0L)
                return;

            if (facing == getFacing())
                continue;

            TileEntity te = world.getTileEntity(me.add(facing.getDirectionVec()));
            if (te == null || !te.hasCapability(ENERGY, facing.getOpposite()))
                continue;

            IEnergyStorage receiver = te.getCapability(ENERGY, facing.getOpposite());
            if (!receiver.canReceive())
                continue;

            int acceptedAmount = receiver.receiveEnergy((int) Math.min(energy.getAmount(), Integer.MAX_VALUE), true);
            if (acceptedAmount != 0) {
                receiver.receiveEnergy(acceptedAmount, false);
                energy = storage.take(new LongEnergyStack(acceptedAmount), true);
            }
        }
    }

    @Override
    public boolean hasCapability(@Nonnull Capability<?> capability, @Nullable EnumFacing facing) {
        if (capability == ENERGY) {
            return true;
        }
        return super.hasCapability(capability, facing);
    }

    @Nullable
    @Override
    public <T> T getCapability(@Nonnull Capability<T> capability, @Nullable EnumFacing facing) {
        if (capability == ENERGY) {
            return (T) new ForgeEnergyZero();
        }
        return super.getCapability(capability, facing);
    }
}

@@ -1311,32 +1311,92 @@
 getOpposite());%0A
+        for (EnumFacing facing : EnumFacing.values()) %7B%0A    
         LongEner
@@ -1474,64 +1474,8 @@
 e);%0A
-        for (EnumFacing facing : EnumFacing.values()) %7B%0A
     
@@ -2118,38 +2118,78 @@
         
-receiver.receive
+LongEnergyStack actualTaken = storage.take(new Long
 Energy
+Stack
 (accepte
@@ -2195,22 +2195,22 @@
 edAmount
+)
 , 
-fals
+tru
 e);%0A    
@@ -2225,70 +2225,95 @@
     
-energy = storage.take(new LongEnergyStack(accepted
+receiver.receiveEnergy((int) Math.min(actualTaken.get
 Amount
+(
 ), 
-tru
+Integer.MAX_VALUE), fals
 e);%0A

Fix energy duplication bug with energy extractor.