@Override
  public boolean isItemValidForSlot(int slotID, ItemStack itemstack) {
    if (slotID == 1) {
      return RecipeHandler.getItemToGasOutput(
              itemstack, false, Recipe.CHEMICAL_DISSOLUTION_CHAMBER.get())
          != null;
    } else if (slotID == 3) {
      return ChargeUtils.canBeDischarged(itemstack);
    }

    return false;
  }
  public boolean canOperate() {
    if (inventory[1] == null) {
      return false;
    }

    GasStack stack =
        RecipeHandler.getItemToGasOutput(
            inventory[1], false, Recipe.CHEMICAL_DISSOLUTION_CHAMBER.get());

    if (stack == null
        || (outputTank.getGas() != null
            && (outputTank.getGas().getGas() != stack.getGas()
                || outputTank.getNeeded() < stack.amount))) {
      return false;
    }

    return true;
  }
  @Override
  public void onUpdate() {
    if (worldObj.isRemote) {
      Mekanism.proxy.registerSound(this);

      if (updateDelay > 0) {
        updateDelay--;

        if (updateDelay == 0 && clientActive != isActive) {
          isActive = clientActive;
          MekanismUtils.updateBlock(worldObj, xCoord, yCoord, zCoord);
        }
      }
    }

    if (!worldObj.isRemote) {
      if (updateDelay > 0) {
        updateDelay--;

        if (updateDelay == 0 && clientActive != isActive) {
          Mekanism.packetHandler.sendToAll(
              new TileEntityMessage(Coord4D.get(this), getNetworkedData(new ArrayList())));
        }
      }

      ChargeUtils.discharge(3, this);

      if (inventory[0] != null
          && (injectTank.getGas() == null || injectTank.getStored() < injectTank.getMaxGas())) {
        injectTank.receive(
            GasTransmission.removeGas(
                inventory[0], GasRegistry.getGas("sulfuricAcid"), injectTank.getNeeded()),
            true);
      }

      if (inventory[2] != null && outputTank.getGas() != null) {
        outputTank.draw(GasTransmission.addGas(inventory[2], outputTank.getGas()), true);
      }

      boolean changed = false;

      if (canOperate()
          && getEnergy() >= MekanismUtils.getEnergyPerTick(this, ENERGY_USAGE)
          && injectTank.getStored() >= INJECT_USAGE
          && MekanismUtils.canFunction(this)) {
        setActive(true);
        setEnergy(getEnergy() - MekanismUtils.getEnergyPerTick(this, ENERGY_USAGE));

        if (operatingTicks < MekanismUtils.getTicks(this, TICKS_REQUIRED)) {
          operatingTicks++;
          injectTank.draw(INJECT_USAGE, true);
        } else {
          GasStack stack =
              RecipeHandler.getItemToGasOutput(
                  inventory[1], true, Recipe.CHEMICAL_DISSOLUTION_CHAMBER.get());

          outputTank.receive(stack, true);
          injectTank.draw(INJECT_USAGE, true);

          operatingTicks = 0;

          if (inventory[1].stackSize <= 0) {
            inventory[1] = null;
          }

          markDirty();
        }
      } else {
        if (prevEnergy >= getEnergy()) {
          changed = true;
          setActive(false);
        }
      }

      if (changed && !canOperate()) {
        operatingTicks = 0;
      }

      prevEnergy = getEnergy();

      if (outputTank.getGas() != null) {
        GasStack toSend =
            new GasStack(outputTank.getGas().getGas(), Math.min(outputTank.getStored(), gasOutput));

        TileEntity tileEntity =
            Coord4D.get(this).getFromSide(MekanismUtils.getRight(facing)).getTileEntity(worldObj);

        if (tileEntity instanceof IGasHandler) {
          if (((IGasHandler) tileEntity)
              .canReceiveGas(
                  MekanismUtils.getRight(facing).getOpposite(), outputTank.getGas().getGas())) {
            outputTank.draw(
                ((IGasHandler) tileEntity)
                    .receiveGas(MekanismUtils.getRight(facing).getOpposite(), toSend),
                true);
          }
        }
      }
    }
  }
  @Override
  public ItemStack transferStackInSlot(EntityPlayer player, int slotID) {
    ItemStack stack = null;
    Slot currentSlot = (Slot) inventorySlots.get(slotID);

    if (currentSlot != null && currentSlot.getHasStack()) {
      ItemStack slotStack = currentSlot.getStack();
      stack = slotStack.copy();

      if (RecipeHandler.getItemToGasOutput(
              slotStack, false, Recipe.CHEMICAL_DISSOLUTION_CHAMBER.get())
          != null) {
        if (slotID != 1) {
          if (!mergeItemStack(slotStack, 1, 2, true)) {
            return null;
          }
        } else {
          if (!mergeItemStack(slotStack, 5, inventorySlots.size(), true)) {
            return null;
          }
        }
      } else if (ChargeUtils.canBeDischarged(slotStack)) {
        if (slotID != 3) {
          if (!mergeItemStack(slotStack, 3, 4, false)) {
            return null;
          }
        } else if (slotID == 3) {
          if (!mergeItemStack(slotStack, 5, inventorySlots.size(), true)) {
            return null;
          }
        }
      } else if (slotStack.getItem() instanceof IGasItem) {
        if (slotID != 0 && slotID != 2) {
          if (!mergeItemStack(slotStack, 0, 1, false)) {
            if (!mergeItemStack(slotStack, 2, 3, false)) {
              return null;
            }
          }
        } else {
          if (!mergeItemStack(slotStack, 5, inventorySlots.size(), true)) {
            return null;
          }
        }
      } else if (slotStack.getItem() instanceof ItemMachineUpgrade) {
        if (slotID != 0 && slotID != 1 && slotID != 2 && slotID != 3 && slotID != 4) {
          if (!mergeItemStack(slotStack, 4, 5, false)) {
            return null;
          }
        } else {
          if (!mergeItemStack(slotStack, 5, inventorySlots.size(), true)) {
            return null;
          }
        }
      } else {
        if (slotID >= 5 && slotID <= 31) {
          if (!mergeItemStack(slotStack, 32, inventorySlots.size(), false)) {
            return null;
          }
        } else if (slotID > 31) {
          if (!mergeItemStack(slotStack, 5, 31, false)) {
            return null;
          }
        } else {
          if (!mergeItemStack(slotStack, 5, inventorySlots.size(), true)) {
            return null;
          }
        }
      }

      if (slotStack.stackSize == 0) {
        currentSlot.putStack((ItemStack) null);
      } else {
        currentSlot.onSlotChanged();
      }

      if (slotStack.stackSize == stack.stackSize) {
        return null;
      }

      currentSlot.onPickupFromSlot(player, slotStack);
    }

    return stack;
  }