package mcjty.rftoolsdim.blocks.energyextractor;

import mcjty.lib.McJtyLib;
import mcjty.lib.compat.RedstoneFluxCompatibility;
import mcjty.lib.tileentity.GenericEnergyStorageTileEntity;
import mcjty.lib.varia.EnergyTools;
import mcjty.rftoolsdim.config.MachineConfiguration;
import mcjty.rftoolsdim.dimensions.DimensionStorage;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ITickable;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.energy.CapabilityEnergy;

public class EnergyExtractorTileEntity extends GenericEnergyStorageTileEntity implements ITickable {

    public EnergyExtractorTileEntity() {
        super(MachineConfiguration.EXTRACTOR_MAXENERGY, MachineConfiguration.EXTRACTOR_SENDPERTICK);
    }

    @Override
    public void update() {
        if (!getWorld().isRemote) {
            checkStateServer();
        }
    }

    private void checkStateServer() {
        int energyStored = getEnergyStored();

        if (energyStored < MachineConfiguration.EXTRACTOR_MAXENERGY) {
            // Get energy out of the dimension.
            DimensionStorage storage = DimensionStorage.getDimensionStorage(getWorld());
            int dimensionEnergy = storage.getEnergyLevel(getWorld().provider.getDimension());
            int needed = MachineConfiguration.EXTRACTOR_MAXENERGY - energyStored;
            if (needed > dimensionEnergy) {
                needed = dimensionEnergy;
            }

            if (needed > 0) {
                energyStored += needed;
                dimensionEnergy -= needed;
                modifyEnergyStored(needed);

                storage.setEnergyLevel(getWorld().provider.getDimension(), dimensionEnergy);
                storage.save();
            }
        }

        if (energyStored <= 0) {
            return;
        }

        int rf = MachineConfiguration.EXTRACTOR_SENDPERTICK;

        for (EnumFacing facing : EnumFacing.values()) {
            BlockPos pos = getPos().offset(facing);
            TileEntity te = getWorld().getTileEntity(pos);
            EnumFacing opposite = facing.getOpposite();
            if (EnergyTools.isEnergyTE(te, opposite) || (te != null && te.hasCapability(CapabilityEnergy.ENERGY, opposite))) {
                int rfToGive = rf <= energyStored ? rf : energyStored;
                int received;

                if (McJtyLib.redstoneflux && RedstoneFluxCompatibility.isEnergyConnection(te)) {
                    if (RedstoneFluxCompatibility.canConnectEnergy(te, opposite)) {
                        received = (int) EnergyTools.receiveEnergy(te, opposite, rfToGive);
                    } else {
                        received = 0;
                    }
                } else {
                    // Forge unit
                    received = (int) EnergyTools.receiveEnergy(te, opposite, rfToGive);
                }
                energyStored -= storage.extractEnergy(received, false);
                if (energyStored <= 0) {
                    break;
                }
            }
        }
    }
}

@@ -47,86 +47,8 @@
 r;%0A%0A
-import mcjty.lib.McJtyLib;%0Aimport mcjty.lib.compat.RedstoneFluxCompatibility;%0A
 impo
@@ -99,24 +99,24 @@
 TileEntity;%0A
+
 import mcjty
@@ -408,59 +408,8 @@
 Pos;
-%0Aimport net.minecraftforge.energy.CapabilityEnergy;
 %0A%0Apu
@@ -1195,16 +1195,25 @@
 eeded = 
+Math.min(
 MachineC
@@ -1263,109 +1263,27 @@
 ored
-;%0A            if (needed %3E dimensionEnergy) %7B%0A                needed = dimensionEnergy;%0A            %7D
+, dimensionEnergy);
 %0A%0A  
@@ -1656,70 +1656,8 @@
  %7D%0A%0A
-        int rf = MachineConfiguration.EXTRACTOR_SENDPERTICK;%0A%0A
     
@@ -1931,625 +1931,130 @@
 ite)
- %7C%7C (te != null && te.hasCapability(CapabilityEnergy.ENERGY, opposite))) %7B%0A                int rfToGive = rf %3C= energyStored ? rf : energyStored;%0A                int received;%0A%0A                if (McJtyLib.redstoneflux && RedstoneFluxCompatibility.isEnergyConnection(te)) %7B%0A                    if (RedstoneFluxCompatibility.canConnectEnergy(te, opposite)) %7B%0A                        received = (int) EnergyTools.receiveEnergy(te, opposite, rfToGive);%0A                    %7D else %7B%0A                        received = 0;%0A                    %7D%0A                %7D else %7B%0A                    // Forge unit%0A                   
+) %7B%0A                int rfToGive = Math.min(MachineConfiguration.EXTRACTOR_SENDPERTICK, energyStored);%0A                int
  rec
@@ -2118,34 +2118,16 @@
 oGive);%0A
-                %7D%0A
         

Simplify energy extractor