@Override
  public ItemStack transferStackInSlot(EntityPlayer player, int slot) {
    ItemStack stack = null;
    Slot slotObject = (Slot) inventorySlots.get(slot);
    int numSlots = _entity.getSizeInventory();

    if (slotObject != null && slotObject.getHasStack()) {
      ItemStack stackInSlot = slotObject.getStack();
      stack = stackInSlot.copy();

      if (slot < numSlots) {
        if (!mergeItemStack(stackInSlot, numSlots, inventorySlots.size(), true)) {
          return null;
        }
      } else if (!mergeItemStack(stackInSlot, 0, numSlots, false)) {
        return null;
      }

      if (stackInSlot.stackSize == 0) {
        slotObject.putStack(null);
      } else {
        slotObject.onSlotChanged();
      }

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

      slotObject.onPickupFromSlot(player, stackInSlot);
    }

    return stack;
  }
 public ContainerCyaniteReprocessor(TileEntityCyaniteReprocessor entity, EntityPlayer player) {
   super();
   _entity = entity;
   addSlots();
   addPlayerInventory(player.inventory);
   _entity.beginUpdatingPlayer(player);
 }
  // Override this so we can check if stuff is valid for the slot
  // Stolen directly from powercrystals
  @Override
  protected boolean mergeItemStack(ItemStack stack, int slotStart, int slotRange, boolean reverse) {
    boolean successful = false;
    int slotIndex = slotStart;
    int maxStack = Math.min(stack.getMaxStackSize(), _entity.getInventoryStackLimit());

    if (reverse) {
      slotIndex = slotRange - 1;
    }

    Slot slot;
    ItemStack existingStack;

    if (stack.isStackable()) {
      while (stack.stackSize > 0
          && (!reverse && slotIndex < slotRange || reverse && slotIndex >= slotStart)) {
        slot = (Slot) this.inventorySlots.get(slotIndex);
        existingStack = slot.getStack();

        if (slot.isItemValid(stack)
            && existingStack != null
            && existingStack.itemID == stack.itemID
            && (!stack.getHasSubtypes() || stack.getItemDamage() == existingStack.getItemDamage())
            && ItemStack.areItemStackTagsEqual(stack, existingStack)) {
          int existingSize = existingStack.stackSize + stack.stackSize;

          if (existingSize <= maxStack) {
            stack.stackSize = 0;
            existingStack.stackSize = existingSize;
            slot.onSlotChanged();
            successful = true;
          } else if (existingStack.stackSize < maxStack) {
            stack.stackSize -= maxStack - existingStack.stackSize;
            existingStack.stackSize = maxStack;
            slot.onSlotChanged();
            successful = true;
          }
        }

        if (reverse) {
          --slotIndex;
        } else {
          ++slotIndex;
        }
      }
    }

    if (stack.stackSize > 0) {
      if (reverse) {
        slotIndex = slotRange - 1;
      } else {
        slotIndex = slotStart;
      }

      while (!reverse && slotIndex < slotRange || reverse && slotIndex >= slotStart) {
        slot = (Slot) this.inventorySlots.get(slotIndex);
        existingStack = slot.getStack();

        if (slot.isItemValid(stack) && existingStack == null) {
          slot.putStack(stack.copy());
          slot.onSlotChanged();
          stack.stackSize = 0;
          successful = true;
          break;
        }

        if (reverse) {
          --slotIndex;
        } else {
          ++slotIndex;
        }
      }
    }

    return successful;
  }
 @Override
 public boolean canInteractWith(EntityPlayer player) {
   return _entity.isUseableByPlayer(player);
 }
  @Override
  public void onContainerClosed(EntityPlayer player) {
    super.onContainerClosed(player);

    _entity.stopUpdatingPlayer(player);
  }