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

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

      if (slot < machInvSize) {
        if (!mergeItemStack(stackInSlot, machInvSize, inventorySlots.size(), true)) {
          return null;
        }
      } else if (!mergeItemStack(stackInSlot, 0, machInvSize, 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 ContainerBag(BagContainerWrapper ncw, InventoryPlayer inv) {
    _ncw = ncw;
    _nsi = inv.currentItem;

    for (int i = 0; i < _ncw.getSizeInventory(); ++i)
      this.addSlotToContainer(new SlotAcceptValid(_ncw, i, 44 + i * 18, 20));

    bindPlayerInventory(inv);
  }
  @Override
  protected boolean mergeItemStack(ItemStack stack, int slotStart, int slotRange, boolean reverse) {
    boolean successful = false;
    int slotIndex = slotStart;
    int maxStack = Math.min(stack.getMaxStackSize(), _ncw.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.getItem().equals(stack.getItem())
            && (!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 void onContainerClosed(EntityPlayer player) {
   if (UtilInventory.stacksEqual(player.inventory.mainInventory[_nsi], _ncw.getStack(), false))
     player.inventory.mainInventory[_nsi] = _ncw.getStack();
   super.onContainerClosed(player);
 }
 public String getName() {
   return _ncw.getInventoryName();
 }