public void updateEnergyValueFromInventory() {
   float newEnergyValue = storedEnergyValue.getValue();
   for (int i = 0; i <= STONE_INDEX; i++) {
     if (inventory[i] != null && EnergyValueRegistryProxy.hasEnergyValue(inventory[i])) {
       newEnergyValue += EnergyValueRegistryProxy.getEnergyValueForStack(inventory[i]).getValue();
     }
   }
   this.availableEnergyValue = new EnergyValue(newEnergyValue);
 }
  public void consumeInventoryForEnergyValue(ItemStack outputItemStack) {
    EnergyValue outputEnergyValue = EnergyValueRegistryProxy.getEnergyValue(outputItemStack);

    /**
     * Algorithm:
     *
     * <p>1) Check the Stone slot, and attempt to take EMC out of the stone there (til 0) 2) Search
     * the inventory for items that will most make up the difference, decrement them and consume
     * their EMC 3) Repeat 2 until Stored EMC > outputItemStack EMC 4) Profit
     */
    if (this.storedEnergyValue.compareTo(outputEnergyValue) >= 0) {
      this.storedEnergyValue =
          new EnergyValue(this.storedEnergyValue.getValue() - outputEnergyValue.getValue());
    } else {
      while (this.storedEnergyValue.compareTo(outputEnergyValue) < 0
          && this.availableEnergyValue.compareTo(outputEnergyValue) >= 0) {
        for (int i = 0; i < STONE_INDEX; i++) {
          ItemStack stackInSlot = getStackInSlot(i);
          if (stackInSlot != null && EnergyValueRegistryProxy.hasEnergyValue(stackInSlot)) {
            this.storedEnergyValue =
                new EnergyValue(
                    this.storedEnergyValue.getValue()
                        + EnergyValueRegistryProxy.getEnergyValue(stackInSlot).getValue());
            decrStackSize(i, 1);
          }
        }
      }

      if (this.storedEnergyValue.getValue() >= outputEnergyValue.getValue()) {
        this.storedEnergyValue =
            new EnergyValue(this.storedEnergyValue.getValue() - outputEnergyValue.getValue());
      }
    }

    updateEnergyValueFromInventory();
  }
  @Override
  public void writeToNBT(NBTTagCompound nbtTagCompound) {
    super.writeToNBT(nbtTagCompound);
    nbtTagCompound.setInteger("rotation", rotation.ordinal());

    // Write the ItemStacks in the inventory to NBT
    NBTTagList tagList = new NBTTagList();
    for (int currentIndex = 0; currentIndex < inventory.length; ++currentIndex) {
      if (inventory[currentIndex] != null) {
        NBTTagCompound tagCompound = new NBTTagCompound();
        tagCompound.setByte("Slot", (byte) currentIndex);
        inventory[currentIndex].writeToNBT(tagCompound);
        tagList.appendTag(tagCompound);
      }
    }
    nbtTagCompound.setTag(Names.NBT.ITEMS, tagList);

    NBTTagCompound energyValueTagCompound = new NBTTagCompound();
    if (storedEnergyValue != null) {
      storedEnergyValue.writeToNBT(energyValueTagCompound);
    }
    nbtTagCompound.setTag("storedEnergyValue", energyValueTagCompound);
  }
  @Override
  public void readFromNBT(NBTTagCompound nbtTagCompound) {
    super.readFromNBT(nbtTagCompound);
    rotation = ForgeDirection.getOrientation(nbtTagCompound.getInteger("rotation"));

    // Read in the ItemStacks in the inventory from NBT
    NBTTagList tagList = nbtTagCompound.getTagList(Names.NBT.ITEMS, 10);
    inventory = new ItemStack[this.getSizeInventory()];
    for (int i = 0; i < tagList.tagCount(); ++i) {
      NBTTagCompound tagCompound = tagList.getCompoundTagAt(i);
      byte slotIndex = tagCompound.getByte("Slot");
      if (slotIndex >= 0 && slotIndex < inventory.length) {
        inventory[slotIndex] = ItemStack.loadItemStackFromNBT(tagCompound);
      }
    }

    NBTTagCompound energyValueTagCompound = nbtTagCompound.getCompoundTag("storedEnergyValue");
    if (!energyValueTagCompound.hasNoTags()) {
      storedEnergyValue = EnergyValue.loadEnergyValueFromNBT(energyValueTagCompound);
    } else {
      storedEnergyValue = new EnergyValue(0);
    }
  }