package org.cyclops.integrateddynamics.capability.energystorage;

import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import org.cyclops.cyclopscore.helper.ItemStackHelpers;
import org.cyclops.integrateddynamics.block.BlockEnergyBatteryConfig;
import org.cyclops.integrateddynamics.core.item.ItemBlockEnergyContainer;

/**
 * Energy Battery implementation for ItemBlock's.
 * @author rubensworks
 */
public class EnergyStorageItemBlockEnergyContainer implements IEnergyStorageCapacity {

    private final ItemBlockEnergyContainer itemBlockEnergyContainer;
    private final ItemStack itemStack;
    private final int rate;

    public EnergyStorageItemBlockEnergyContainer(ItemBlockEnergyContainer itemBlockEnergyContainer, ItemStack itemStack, int rate) {
        this.itemBlockEnergyContainer = itemBlockEnergyContainer;
        this.itemStack = itemStack;
        this.rate = rate;
    }

    public EnergyStorageItemBlockEnergyContainer(ItemBlockEnergyContainer itemBlockEnergyContainer, ItemStack itemStack) {
        this(itemBlockEnergyContainer, itemStack, Integer.MAX_VALUE);
    }

    public int getRate() {
        return rate;
    }

    @Override
    public int getEnergyStored() {
        NBTTagCompound tag = ItemStackHelpers.getSafeTagCompound(itemStack);
        return tag.getInteger(itemBlockEnergyContainer.get().getEneryContainerNBTName());
    }

    @Override
    public int getMaxEnergyStored() {
        NBTTagCompound tag = ItemStackHelpers.getSafeTagCompound(itemStack);
        if (!tag.hasKey(itemBlockEnergyContainer.get().getEneryContainerCapacityNBTName())) {
            return BlockEnergyBatteryConfig.capacity;
        }
        return tag.getInteger(itemBlockEnergyContainer.get().getEneryContainerCapacityNBTName());
    }

    @Override
    public boolean canExtract() {
        return true;
    }

    @Override
    public boolean canReceive() {
        return true;
    }

    @Override
    public int receiveEnergy(int energy, boolean simulate) {
        energy = Math.min(energy, getRate());
        int stored = getEnergyStored();
        int energyReceived = Math.min(getMaxEnergyStored() - stored, energy);
        if(!simulate) {
            setEnergy(itemStack, stored + energyReceived);
        }
        return energyReceived;
    }

    @Override
    public int extractEnergy(int energy, boolean simulate) {
        energy = Math.min(energy, getRate());
        int stored = getEnergyStored();
        int newEnergy = Math.max(stored - energy, 0);
        if(!simulate) {
            setEnergy(itemStack, newEnergy);
        }
        return stored - newEnergy;
    }

    protected void setEnergy(ItemStack itemStack, int energy) {
        NBTTagCompound tag = ItemStackHelpers.getSafeTagCompound(itemStack);
        tag.setInteger(itemBlockEnergyContainer.get().getEneryContainerNBTName(), energy);
    }

    @Override
    public void setCapacity(int capacity) {
        NBTTagCompound tag = ItemStackHelpers.getSafeTagCompound(itemStack);
        tag.setInteger(itemBlockEnergyContainer.get().getEneryContainerCapacityNBTName(), capacity);
    }
}

@@ -255,22 +255,157 @@
 yBattery
-Config
+Base;%0Aimport org.cyclops.integrateddynamics.block.BlockEnergyBatteryConfig;%0Aimport org.cyclops.integrateddynamics.block.IEnergyContainerBlock
 ;%0Aimport
@@ -1310,52 +1310,319 @@
     
-@Override%0A    public int getEnergyStored() %7B
+public boolean isCreative() %7B%0A        IEnergyContainerBlock block = itemBlockEnergyContainer.get();%0A        return block instanceof BlockEnergyBatteryBase && ((BlockEnergyBatteryBase) block).isCreative();%0A    %7D%0A%0A    @Override%0A    public int getEnergyStored() %7B%0A        if(isCreative()) return Integer.MAX_VALUE;
 %0A   
@@ -1836,32 +1836,83 @@
 nergyStored() %7B%0A
+        if(isCreative()) return Integer.MAX_VALUE;%0A
         NBTTagCo
@@ -2454,32 +2454,67 @@
 ean simulate) %7B%0A
+        if(isCreative()) return 0;%0A
         energy =
@@ -2859,32 +2859,72 @@
 ean simulate) %7B%0A
+        if(isCreative()) return energy;%0A
         energy =
@@ -3228,24 +3228,57 @@
 t energy) %7B%0A
+        if(isCreative()) return;%0A
         NBTT

Fix creative energy battery not being infinite in item form