Search in sources :

Example 1 with IEnergyConnection

use of cofh.api.energy.IEnergyConnection in project RFToolsDimensions by McJty.

the class EnergyExtractorTileEntity method checkStateServer.

private void checkStateServer() {
    int energyStored = getEnergyStored(EnumFacing.DOWN);
    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(getWorld());
        }
    }
    if (energyStored <= 0) {
        return;
    }
    int rf = MachineConfiguration.EXTRACTOR_SENDPERTICK;
    for (EnumFacing facing : EnumFacing.values()) {
        BlockPos pos = getPos().offset(facing);
        TileEntity te = getWorld().getTileEntity(pos);
        if (EnergyTools.isEnergyTE(te)) {
            EnumFacing opposite = facing.getOpposite();
            int rfToGive = rf <= energyStored ? rf : energyStored;
            int received;
            if (te instanceof IEnergyConnection) {
                IEnergyConnection connection = (IEnergyConnection) te;
                if (connection.canConnectEnergy(opposite)) {
                    received = EnergyTools.receiveEnergy(te, opposite, rfToGive);
                } else {
                    received = 0;
                }
            } else {
                // Forge unit
                received = EnergyTools.receiveEnergy(te, opposite, rfToGive);
            }
            energyStored -= storage.extractEnergy(received, false);
            if (energyStored <= 0) {
                break;
            }
        }
    }
}
Also used : GenericEnergyProviderTileEntity(mcjty.lib.entity.GenericEnergyProviderTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) DimensionStorage(mcjty.rftoolsdim.dimensions.DimensionStorage) IEnergyConnection(cofh.api.energy.IEnergyConnection) EnumFacing(net.minecraft.util.EnumFacing) BlockPos(net.minecraft.util.math.BlockPos)

Aggregations

IEnergyConnection (cofh.api.energy.IEnergyConnection)1 GenericEnergyProviderTileEntity (mcjty.lib.entity.GenericEnergyProviderTileEntity)1 DimensionStorage (mcjty.rftoolsdim.dimensions.DimensionStorage)1 TileEntity (net.minecraft.tileentity.TileEntity)1 EnumFacing (net.minecraft.util.EnumFacing)1 BlockPos (net.minecraft.util.math.BlockPos)1