Search in sources :

Example 6 with IEnergyStorage

use of net.minecraftforge.energy.IEnergyStorage in project Overloaded by CJ-MC-Mods.

the class TileEnergyExtractor method update.

/**
     * Like the old updateEntity(), except more generic.
     */
@Override
public void update() {
    BlockPos me = this.getPos();
    TileEntity frontTE = getWorld().getTileEntity(me.add(front.getDirectionVec()));
    if (frontTE == null || !frontTE.hasCapability(HYPER_ENERGY_HANDLER, front.getOpposite()))
        return;
    IHyperHandlerEnergy storage = frontTE.getCapability(HYPER_ENERGY_HANDLER, front.getOpposite());
    LongEnergyStack energy = storage.take(new LongEnergyStack(Long.MAX_VALUE), false);
    for (EnumFacing facing : EnumFacing.values()) {
        if (energy.getAmount() == 0L)
            return;
        if (facing == front)
            continue;
        TileEntity te = world.getTileEntity(me.add(facing.getDirectionVec()));
        if (te == null || !te.hasCapability(ENERGY, facing.getOpposite()))
            continue;
        IEnergyStorage receiver = te.getCapability(ENERGY, facing.getOpposite());
        if (!receiver.canReceive())
            continue;
        int acceptedAmount = receiver.receiveEnergy((int) Math.min(energy.getAmount(), Integer.MAX_VALUE), true);
        if (acceptedAmount != 0) {
            receiver.receiveEnergy(acceptedAmount, false);
            energy = storage.take(new LongEnergyStack(acceptedAmount), true);
        }
    }
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) LongEnergyStack(com.cjm721.overloaded.storage.LongEnergyStack) EnumFacing(net.minecraft.util.EnumFacing) IEnergyStorage(net.minecraftforge.energy.IEnergyStorage) BlockPos(net.minecraft.util.math.BlockPos) IHyperHandlerEnergy(com.cjm721.overloaded.storage.energy.IHyperHandlerEnergy)

Aggregations

IEnergyStorage (net.minecraftforge.energy.IEnergyStorage)6 BlockPos (net.minecraft.util.math.BlockPos)3 ItemStack (net.minecraft.item.ItemStack)2 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)2 TileEntity (net.minecraft.tileentity.TileEntity)2 EnumFacing (net.minecraft.util.EnumFacing)2 TextComponentString (net.minecraft.util.text.TextComponentString)2 ModItem (com.cjm721.overloaded.item.ModItem)1 LongEnergyStack (com.cjm721.overloaded.storage.LongEnergyStack)1 IHyperHandlerEnergy (com.cjm721.overloaded.storage.energy.IHyperHandlerEnergy)1 IBlockState (net.minecraft.block.state.IBlockState)1 EntityItem (net.minecraft.entity.item.EntityItem)1 Item (net.minecraft.item.Item)1 ItemBlock (net.minecraft.item.ItemBlock)1 Vec3i (net.minecraft.util.math.Vec3i)1 ITextComponent (net.minecraft.util.text.ITextComponent)1 SideOnly (net.minecraftforge.fml.relauncher.SideOnly)1