Example usage for net.minecraftforge.energy IEnergyStorage canReceive

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

Introduction

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

Prototype

boolean canReceive();

Source Link

Document

Used to determine if this storage can receive energy.

Usage

From source file:com.elytradev.thermionics.tileentity.TileEntityBattery.java

License:Open Source License

@Override
public void update() {
    this.energyStorage.tick();
    IBlockState cur = world.getBlockState(pos);
    EnumFacing facing = cur.getValue(BlockBattery.FACING);
    IEnergyStorage target = RFTransport.getStorage(world, pos.offset(facing), facing.getOpposite());
    if (target.canReceive()) {
        int toPush = Math.min(energyStorage.getEnergyStored(), 800);
        int received = target.receiveEnergy(toPush, false);
        if (received > 0)
            energyStorage.extractEnergy(received, false);
    }//ww w.  j av a  2  s .  co  m
}

From source file:com.elytradev.thermionics.tileentity.TileEntityBatteryCreative.java

License:Open Source License

@Override
public void update() {
    this.energyStorage.tick();
    IBlockState cur = world.getBlockState(pos);
    EnumFacing facing = cur.getValue(BlockBattery.FACING);
    IEnergyStorage target = RFTransport.getStorage(world, pos.offset(facing), facing.getOpposite());
    if (target.canReceive()) {
        int toPush = Math.min(energyStorage.getEnergyStored(), target.getMaxEnergyStored());
        int received = target.receiveEnergy(toPush, false);
        if (received > 0)
            energyStorage.extractEnergy(received, false);
        energyStorage.receiveEnergy(energyStorage.getMaxEnergyStored(), false);
    }//w w w  . j a v  a  2  s.  c o m
}

From source file:com.elytradev.thermionics.transport.RFTransport.java

License:Open Source License

public static ArrayList<IEnergyStorage> getAdjacentStorage(World world, BlockPos pos) {
    ArrayList<IEnergyStorage> consumers = new ArrayList<>();

    if (pos.getY() < 255) {
        IEnergyStorage up = getStorage(world, pos.up(), EnumFacing.DOWN);
        if (up.canReceive())
            consumers.add(up);/* w w  w .  j av  a  2  s  .  c  o m*/
    }

    if (pos.getY() > 0) {
        IEnergyStorage down = getStorage(world, pos.down(), EnumFacing.UP);
        if (down.canReceive())
            consumers.add(down);
    }

    IEnergyStorage north = getStorage(world, pos.north(), EnumFacing.SOUTH);
    if (north.canReceive())
        consumers.add(north);
    IEnergyStorage south = getStorage(world, pos.south(), EnumFacing.NORTH);
    if (south.canReceive())
        consumers.add(south);
    IEnergyStorage east = getStorage(world, pos.east(), EnumFacing.WEST);
    if (east.canReceive())
        consumers.add(east);
    IEnergyStorage west = getStorage(world, pos.west(), EnumFacing.EAST);
    if (west.canReceive())
        consumers.add(west);

    return consumers;
}

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  ww w.j ava2s. c  om*/
    }
    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;/* ww  w  . j  a v  a2 s  .c o m*/
    }
    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 .j a  va  2s  . co 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:cpw.mods.simplepower.tiles.RelayTE.java

License:Open Source License

@Override
public void update() {
    if (getWorld().isRemote)
        return;/*w  w w  . j av  a 2 s  .  c o m*/
    List<IEnergyStorage> receivers = new ArrayList<>(associates.stream().map((pt) -> (ReceiverTE) pt)
            .filter(ReceiverTE::canReceive).collect(Collectors.toList()));

    for (EnumFacing facing : EnumFacing.VALUES) {
        TileEntity receiver = getWorld().getTileEntity(getPos().offset(facing));
        if (receiver != null && receiver.hasCapability(CapabilityEnergy.ENERGY, facing.getOpposite())) {
            final IEnergyStorage receiverCapability = receiver.getCapability(CapabilityEnergy.ENERGY,
                    facing.getOpposite());
            if (receiverCapability.canReceive())
                receivers.add(receiverCapability);
        }
    }

    if (receivers.isEmpty())
        return;
    if (storedEnergy < receivers.size())
        return;
    int amttosend = Math.min(100, storedEnergy / receivers.size());
    int sum = 0;
    for (IEnergyStorage e : receivers) {
        int amt = e.receiveEnergy(amttosend, true);
        amt = e.receiveEnergy(amt, false);
        sum += amt;
    }
    storedEnergy -= sum;
}

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 ww  w . j  a va2 s  .com*/
    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());
}

From source file:therogue.storehouse.energy.EnergyUtils.java

License:Open Source License

public static int sendEnergy(World world, BlockPos from, EnumFacing side, int maxRFToGive) {
    EnumFacing opposite = side.getOpposite();
    BlockPos pos = from.offset(side);/*from   w  w w.j a  v a  2 s .  c  o m*/
    TileEntity te = world.getTileEntity(pos);
    int received = 0;
    if (te != null && te.hasCapability(CapabilityEnergy.ENERGY, opposite)) {
        IEnergyStorage capability = te.getCapability(CapabilityEnergy.ENERGY, opposite);
        if (capability.canReceive()) {
            received = capability.receiveEnergy(maxRFToGive, false);
        }
    }
    return received;
}