Example usage for net.minecraftforge.energy IEnergyStorage canExtract

List of usage examples for net.minecraftforge.energy IEnergyStorage canExtract

Introduction

In this page you can find the example usage for net.minecraftforge.energy IEnergyStorage canExtract.

Prototype

boolean canExtract();

Source Link

Document

Returns if this storage can have energy extracted.

Usage

From source file:com.lothrazar.cyclicmagic.block.cable.TileEntityCableBase.java

License:Open Source License

private void moveEnergy(EnumFacing myFacingDir) {
    IEnergyStorage handlerHere = this.getCapability(CapabilityEnergy.ENERGY, myFacingDir);
    if (handlerHere.getEnergyStored() == 0) {
        return;//from  w ww  .  j a v a2 s  .  com
    }
    EnumFacing themFacingMe = myFacingDir.getOpposite();
    BlockPos posTarget = pos.offset(myFacingDir);
    TileEntity tileTarget = world.getTileEntity(posTarget);
    if (tileTarget == null || tileTarget.hasCapability(CapabilityEnergy.ENERGY, themFacingMe) == false) {
        return;
    }
    IEnergyStorage handlerOutput = tileTarget.getCapability(CapabilityEnergy.ENERGY, themFacingMe);
    if (handlerHere != null && handlerOutput != null && handlerHere.canExtract()
            && handlerOutput.canReceive()) {
        //first simulate
        int drain = handlerHere.extractEnergy(MENERGY, true);
        if (drain > 0) {
            //now push it into output, but find out what was ACTUALLY taken
            int filled = handlerOutput.receiveEnergy(drain, false);
            //now actually drain that much from here
            handlerHere.extractEnergy(filled, false);
            if (filled > 0 && tileTarget instanceof TileEntityCableBase) {
                //TODO: not so compatible with other fluid systems. itl do i guess
                TileEntityCableBase cable = (TileEntityCableBase) tileTarget;
                if (cable.isEnergyPipe()) {
                    cable.updateIncomingEnergyFace(themFacingMe);
                }
            }
        }
    }
}

From source file:com.lothrazar.cyclicmagic.block.cablepump.energy.TileEntityEnergyPump.java

License:Open Source License

@Override
public void update() {
    if (this.isRunning() == false) {
        return;/*  w ww.ja va2 s.c om*/
    }
    EnumFacing importFromSide = this.getCurrentFacing();
    IEnergyStorage myEnergy = this.getCapability(CapabilityEnergy.ENERGY, null);
    TileEntity importFromTile = world.getTileEntity(pos.offset(importFromSide));
    IEnergyStorage exportHandler = null;
    IEnergyStorage importHandlr = null;
    if (importFromTile != null
            && importFromTile.hasCapability(CapabilityEnergy.ENERGY, importFromSide.getOpposite())) {
        importHandlr = importFromTile.getCapability(CapabilityEnergy.ENERGY, importFromSide.getOpposite());
        // ModCyclic.logger.error("importFromTile  "+importFromTile.getBlockType().getLocalizedName());
    } else {
        return; //no tile or no capability
    }
    //ALL EXCEPT THIS SIDE
    //IMPORT
    if (importHandlr != null && importHandlr.canExtract()) {
        int drain = importHandlr.extractEnergy(transferRate, true);
        if (drain > 0) {
            //now push it into output, but find out what was ACTUALLY taken
            int filled = myEnergy.receiveEnergy(drain, false);
            //now actually drain that much  
            importHandlr.extractEnergy(filled, false);
            //    ModCyclic.logger.error("pump take IN  " + filled + "i am holding" + this.pumpEnergyStore.getEnergyStored());
        }
    }
    //EXPORT
    List<EnumFacing> sidesOut = getSidesNotFacing();
    for (EnumFacing exportToSide : sidesOut) {
        if (this.hasCapability(CapabilityEnergy.ENERGY, exportToSide) == false) {
            continue;
        }
        TileEntity exportToTile = world.getTileEntity(pos.offset(exportToSide));
        if (exportToTile != null
                && importFromTile.hasCapability(CapabilityEnergy.ENERGY, exportToSide.getOpposite())) {
            exportHandler = exportToTile.getCapability(CapabilityEnergy.ENERGY, exportToSide.getOpposite());
            //   ModCyclic.logger.error("exportToTile   "+exportToTile.getBlockType().getLocalizedName());
        }
        if (exportHandler != null && exportHandler.canReceive()) {
            int drain = myEnergy.extractEnergy(transferRate, true);
            if (drain > 0) {
                //now push it into output, but find out what was ACTUALLY taken
                int filled = exportHandler.receiveEnergy(drain, false);
                //now actually drain that much  
                myEnergy.extractEnergy(filled, false);
                if (world.getTileEntity(pos.offset(exportToSide)) instanceof TileEntityCableBase) {
                    //TODO: not so compatible with other fluid systems. itl do i guess
                    TileEntityCableBase cable = (TileEntityCableBase) world
                            .getTileEntity(pos.offset(exportToSide));
                    //  ModCyclic.logger.error("pump EXPORT  " + filled);
                    if (cable.isEnergyPipe()) {
                        cable.updateIncomingEnergyFace(importFromSide); // .getOpposite()
                    }
                }
                break;//found a side that works, all done
            }
        }
    }
}

From source file:com.lothrazar.cyclicmagic.block.core.TileEntityBaseMachineInvo.java

License:Open Source License

protected void tryOutputPower(int TRANSFER_ENERGY_PER_TICK) {
    // TODO share code in base class somehow? with CableBase maybe?
    List<EnumFacing> targetFaces = Arrays.asList(EnumFacing.values());
    Collections.shuffle(targetFaces);
    for (EnumFacing myFacingDir : targetFaces) {
        BlockPos posTarget = pos.offset(myFacingDir);
        if (this.hasCapability(CapabilityEnergy.ENERGY, myFacingDir) == false) {
            continue;
        }//from w w w .  ja  v a 2 s .c o m
        IEnergyStorage handlerHere = this.getCapability(CapabilityEnergy.ENERGY, myFacingDir);
        TileEntity tileTarget = world.getTileEntity(posTarget);
        if (tileTarget == null) {
            continue;
        }
        IEnergyStorage handlerOutput = tileTarget.getCapability(CapabilityEnergy.ENERGY,
                myFacingDir.getOpposite());
        if (handlerHere != null && handlerOutput != null && handlerHere.canExtract()
                && handlerOutput.canReceive()) {
            //first simulate
            int drain = handlerHere.extractEnergy(TRANSFER_ENERGY_PER_TICK, true);
            if (drain > 0) {
                //now push it into output, but find out what was ACTUALLY taken
                int filled = handlerOutput.receiveEnergy(drain, false);
                //now actually drain that much from here
                handlerHere.extractEnergy(filled, false);
                if (tileTarget instanceof TileEntityCableBase) {
                    //TODO: not so compatible with other fluid systems. itl do i guess
                    TileEntityCableBase cable = (TileEntityCableBase) tileTarget;
                    if (cable.isEnergyPipe())
                        cable.updateIncomingEnergyFace(myFacingDir.getOpposite());
                }
                //              return;// stop now because only pull from one side at a time
            }
        }
    }
}

From source file:de.sanandrew.mods.sanlib.lib.power.EnergyHelper.java

License:Creative Commons License

public static boolean canConnectEnergy(TileEntity te, EnumFacing facing) {
    //noinspection SimplifiableIfStatement
    if (COFH_EXISTS && te instanceof cofh.redstoneflux.api.IEnergyHandler) {
        return ((cofh.redstoneflux.api.IEnergyHandler) te).canConnectEnergy(facing);
    }/*from   www .  j a va  2  s  . c o  m*/
    if (TESLA_EXISTS) {
        if (te.getCapability(net.darkhax.tesla.capability.TeslaCapabilities.CAPABILITY_CONSUMER,
                facing) != null) {
            return true;
        }
        if (te.getCapability(net.darkhax.tesla.capability.TeslaCapabilities.CAPABILITY_PRODUCER,
                facing) != null) {
            return true;
        }
    }
    IEnergyStorage stg = te.getCapability(CapabilityEnergy.ENERGY, facing);
    return stg != null && (stg.canExtract() || stg.canReceive());
}