Example usage for net.minecraftforge.energy IEnergyStorage extractEnergy

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

Introduction

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

Prototype

int extractEnergy(int maxExtract, boolean simulate);

Source Link

Document

Removes energy from the storage.

Usage

From source file:com.buuz135.industrial.tile.CustomElectricMachine.java

License:Open Source License

@Override
public void protectedUpdate() {
    super.protectedUpdate();
    if (this.world.isRemote)
        return;/* www . ja  v a2s. com*/
    if (world.getTotalWorldTime() % 2 == 0 && hasAddon(EnergyFieldAddon.class)) {
        ItemStack addon = getAddonStack(EnergyFieldAddon.class);
        if (addon.hasCapability(CapabilityEnergy.ENERGY, null)) {
            IEnergyStorage storage = addon.getCapability(CapabilityEnergy.ENERGY, null);
            storage.extractEnergy((int) this.getEnergyStorage().givePower(storage.extractEnergy(512, true)),
                    false);
            BlockPos pos = ItemRegistry.energyFieldAddon.getLinkedBlockPos(addon);
            if (this.world.getBlockState(pos).getBlock() instanceof EnergyFieldProviderBlock
                    && this.world.isAreaLoaded(pos, pos)) {
                EnergyFieldProviderTile tile = (EnergyFieldProviderTile) this.world.getTileEntity(pos);
                if (tile.getWorkingArea().grow(1)
                        .contains(new Vec3d(this.pos.getX(), this.pos.getY(), this.pos.getZ()))) {
                    float pull = tile.consumeWorkEnergy(
                            Math.min(storage.getMaxEnergyStored() - storage.getEnergyStored(), 1000));
                    storage.receiveEnergy((int) pull, false);
                }
            }
            this.forceSync();
        }
    }
    if (world.getTotalWorldTime() % 10 == 0 && this.getAddonItems() != null) {
        workTransferAddon(this, this.getAddonItems());
    }
}

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

License:Open Source License

public static void diffuse(World world, BlockPos pos, IEnergyStorage host) {
    ArrayList<IEnergyStorage> consumers = getAdjacentStorage(world, pos);
    reorder(consumers); //We want to sample them in random order
    for (IEnergyStorage consumer : consumers) {
        //int totalEnergy = host.getEnergyStored() + consumer.getEnergyStored();
        //float energyRatio = host.getMaxEnergyStored() / (float)consumer.getMaxEnergyStored();
        float sourceLevel = host.getEnergyStored() / (float) host.getMaxEnergyStored();
        float destLevel = consumer.getEnergyStored() / (float) consumer.getMaxEnergyStored();
        if (sourceLevel > destLevel) {
            //Figure out how many quanta to transfer
            //int desiredSource = (int)(totalEnergy*energyRatio);
            //int desiredDest = (int)(totalEnergy/energyRatio);
            /*//ww w  .jav  a2s  .c o  m
             * Example: 2200 source, 1700 dest, capacity for each is 4000.
             * 
             *   totalEnergy: 3900
             *   energyRatio: 1.0f
             *   sourceLevel: 0.55f
             *   destLevel: 0.425f
             *   availableQuanta: 500 / 2 - 1 = 249
             * 
             */

            /*
             * Transmission data:
             * 0 blocks: 775.0
             * 1 block : 393.6
             * 2 blocks: 260.8
             * 3 blocks: 196.6
             * 4 blocks: 157.8
             * 5 blocks: 131.8
             */

            int availableQuanta = ((int) ((sourceLevel - destLevel) * host.getMaxEnergyStored()) / 2); //trunc leaves fractional power differences where they are
            if (availableQuanta <= 0)
                continue;
            //StringBuilder debugString = new StringBuilder("RFTransfer: ");
            //debugString.append(host.getEnergyStored());
            //debugString.append("("); debugString.append((int)(sourceLevel*100)); debugString.append("%) -> ");
            //debugString.append(consumer.getEnergyStored());
            //debugString.append("("); debugString.append((int)(destLevel*100)); debugString.append("%) :");

            //debugString.append(" req="); debugString.append(availableQuanta);
            int transferred = consumer.receiveEnergy(availableQuanta, true);
            if (transferred > 0)
                transferred = host.extractEnergy(transferred, false);
            //debugString.append(" rec="); debugString.append(transferred);
            //System.out.println(debugString.toString());
            consumer.receiveEnergy(transferred, false);
        }
    }
}

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  va2s. co m
    }
    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;//from w  w  w.  ja va 2s. 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 v a2  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:com.lothrazar.cyclicmagic.block.fishing.TileEntityFishing.java

License:Open Source License

private void damageTool() {
    ItemStack equip = this.getStackInSlot(SLOT_TOOL);
    if (equip.isEmpty()) {
        return;/*from ww  w .  ja  v a2s.  c o  m*/
    }
    if (equip.hasCapability(CapabilityEnergy.ENERGY, null)) {
        IEnergyStorage storage = equip.getCapability(CapabilityEnergy.ENERGY, null);
        if (storage != null) {
            storage.extractEnergy(ENERGY_PER_FISH, false);
            if (storage.getEnergyStored() <= 0) {
                this.sendOutputItem(equip);
                this.setInventorySlotContents(SLOT_TOOL, ItemStack.EMPTY);
            }
            return;
        }
    } else if (equip.getMaxDamage() > 0) { // -1 is unbreakable - EX Mystical Ag Supremium Fishing Rods
        equip.attemptDamageItem(1, getWorld().rand, null);//does respect unbreaking
        //IF enchanted and IF about to break, then spit it out
        int damageRem = equip.getMaxDamage() - equip.getItemDamage();
        if (damageRem == 1 && EnchantmentHelper.getEnchantments(equip).size() > 0) {
            sendOutputItem(equip);
            this.setInventorySlotContents(SLOT_TOOL, ItemStack.EMPTY);
        } //otherwise we also make sure if its fullly damanged
        if (equip.getItemDamage() >= equip.getMaxDamage()) {
            this.setInventorySlotContents(SLOT_TOOL, ItemStack.EMPTY);
        }
    }
}

From source file:com.lothrazar.cyclicmagic.item.cannon.ItemProjectileCannon.java

License:Open Source License

@Override
public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer player, EnumHand hand) {
    IEnergyStorage storage = player.getHeldItem(hand).getCapability(CapabilityEnergy.ENERGY, null);
    //HAS ENOUGH ENERGY? 
    if (storage.getEnergyStored() < ENERGY_COST && ENERGY_COST > storage.extractEnergy(ENERGY_COST, true)) {
        UtilChat.sendStatusMessage(player, "cyclic.item.empty");
        return new ActionResult<ItemStack>(EnumActionResult.PASS, player.getHeldItem(hand));
    }/*from ww  w .j av  a 2 s .  c  om*/
    this.createBullet(world, player);
    UtilSound.playSound(player, player.getPosition(), SoundRegistry.fireball_staff_launch,
            SoundCategory.PLAYERS, 0.1F);
    player.swingArm(hand);
    //DRAIN ENERGY 
    storage.extractEnergy(ENERGY_COST, false);
    return new ActionResult<ItemStack>(EnumActionResult.SUCCESS, player.getHeldItem(hand));
}

From source file:com.teambr.bookshelf.util.EnergyUtils.java

License:Creative Commons License

/**
 * Transfers power from one storage to another, either can be null if you are not sure if it is capable
 * @param source      The source energy storage
 * @param destination The destination energy storage
 * @param maxAmount   Max amount to transfer
 * @param simulate    True to only simulate, not actually transfer
 * @return The amount moved or would be moved
 *//*from  ww  w .  j  a v a 2s. c  om*/
public static int transferPower(@Nullable IEnergyStorage source, @Nullable IEnergyStorage destination,
        int maxAmount, boolean simulate) {
    if (source == null || destination == null)
        return 0;

    int amount = source.extractEnergy(destination.receiveEnergy(maxAmount, true), true);
    // Try move power
    return destination.receiveEnergy(source.extractEnergy(amount, simulate), simulate);
}

From source file:de.canitzp.rarmor.module.main.ActiveModuleMain.java

@Override
public void tick(World world, Entity entity, boolean isWearingHat, boolean isWearingChest,
        boolean isWearingPants, boolean isWearingShoes) {
    if (!world.isRemote) {
        if (this.data.getEnergyStored() < this.data.getMaxEnergyStored()) {
            ItemStack discharge = this.inventory.getStackInSlot(0);
            if (!discharge.isEmpty()) {
                if (discharge.hasCapability(CapabilityEnergy.ENERGY, EnumFacing.DOWN)) {
                    IEnergyStorage storage = discharge.getCapability(CapabilityEnergy.ENERGY, EnumFacing.DOWN);
                    if (storage != null) {
                        int canDischarge = storage.extractEnergy(Integer.MAX_VALUE, true);
                        if (canDischarge > 0) {
                            int discharged = this.data.receiveEnergy(canDischarge, false);
                            storage.extractEnergy(discharged, false);
                            this.data.setDirty();
                        }/*from www. java2  s.  c o m*/
                    }
                }
            }
        }

        if (this.data.getEnergyStored() > 0) {
            ItemStack charge = this.inventory.getStackInSlot(1);
            if (!charge.isEmpty()) {
                if (charge.hasCapability(CapabilityEnergy.ENERGY, EnumFacing.DOWN)) {
                    IEnergyStorage storage = charge.getCapability(CapabilityEnergy.ENERGY, EnumFacing.DOWN);
                    if (storage != null) {
                        int canDischarge = storage.receiveEnergy(Integer.MAX_VALUE, true);
                        if (canDischarge > 0) {
                            int discharged = this.data.extractEnergy(canDischarge, false);
                            storage.receiveEnergy(discharged, false);
                            this.data.setDirty();
                        }
                    }
                }
            }
        }

        int energy = this.data.getEnergyStored();
        if (energy != this.lastEnergy && this.data.getTotalTickedTicks() % 10 == 0) {
            this.data.queueUpdate();
            this.lastEnergy = energy;
        }
    }
}

From source file:de.ellpeck.actuallyadditions.mod.items.base.ItemEnergy.java

public int extractEnergy(ItemStack stack, int maxExtract, boolean simulate) {
    if (stack.hasCapability(CapabilityEnergy.ENERGY, null)) {
        IEnergyStorage storage = stack.getCapability(CapabilityEnergy.ENERGY, null);
        if (storage != null) {
            return storage.extractEnergy(maxExtract, simulate);
        }//from  w  ww . j a  v a 2 s  .  c o m
    }
    return 0;
}