public ItemStack tryToFillSlots(ItemStack stack,int... slots){
  if (slots.length % 2 != 0) {
    throw new IllegalArgumentException("Slots must be pairs.");
  }
  ItemStack maxStack=stack.clone();
  maxStack.setAmount(stack.getMaxStackSize());
  for (int s=0; s < slots.length && stack.getAmount() > 0; s+=2) {
    int start=slots[s];
    int end=slots[s + 1];
    int delta=start < end ? 1 : -1;
    for (int i=start; i != end && stack.getAmount() > 0; i+=delta) {
      if (!itemShiftClickAllowed(i,stack)) {
        continue;
      }
      ItemStack currentStack=getItem(i);
      if (currentStack == null) {
        if (stack.getAmount() > stack.getMaxStackSize()) {
          setItem(i,maxStack);
          stack.setAmount(stack.getAmount() - stack.getMaxStackSize());
        }
 else {
          ItemStack finalStack=stack.clone();
          setItem(i,finalStack);
          stack.setAmount(0);
        }
      }
 else       if (currentStack.isSimilar(stack)) {
        int amount=currentStack.getAmount();
        int maxStackSize=Math.min(currentStack.getMaxStackSize(),getMaxStackSize());
        int transfer=Math.min(stack.getAmount(),maxStackSize - amount);
        if (transfer > 0) {
          currentStack.setAmount(amount + transfer);
          stack.setAmount(stack.getAmount() - transfer);
        }
        setItem(i,currentStack);
      }
    }
  }
  if (stack.getAmount() <= 0) {
    stack=null;
  }
  return stack;
}

public ItemStack tryToFillSlots ( ItemStack stack , int ... slots ) { if ( slots . length % 2 != 0 ) { throw new IllegalArgumentException ( "Slots must be pairs." ) ; } ItemStack maxStack = stack . clone ( ) ; maxStack . setAmount ( stack . getMaxStackSize ( ) ) ; for ( int s = 0 ; s < slots . length && stack . getAmount ( ) > 0 ; s += 2 ) { int start = slots [ s ] ; int end = slots [ s + 1 ] ; int delta = start < end ? 1 : - 1 ; for ( int i = start ; i != end && stack . getAmount ( ) > 0 ; i += delta ) { if ( ! itemShiftClickAllowed ( i , stack ) ) { continue ; } ItemStack currentStack = getItem ( i ) ; if ( currentStack == null ) { if ( stack . getAmount ( ) > stack . getMaxStackSize ( ) ) { setItem ( i , maxStack ) ; stack . setAmount ( stack . getAmount ( ) - stack . getMaxStackSize ( ) ) ; } else { ItemStack finalStack = stack . clone ( ) ; setItem ( i , finalStack ) ; stack . setAmount ( 0 ) ; } } else if ( currentStack . isSimilar ( stack ) ) { int amount = currentStack . getAmount ( ) ; int maxStackSize = Math . min ( currentStack . getMaxStackSize ( ) , getMaxStackSize ( ) ) ; int transfer = Math . min ( stack . getAmount ( ) , maxStackSize - amount ) ; if ( transfer > 0 ) { currentStack . setAmount ( amount + transfer ) ; stack . setAmount ( stack . getAmount ( ) - transfer ) ; } setItem ( i , currentStack ) ; } } } if ( stack . getAmount ( ) <= 0 ) { stack = null ; } return stack ; }
tries to put the given items into the specified slots of this inventory from the start slot ( inclusive ) to the end slot ( exclusive ) . the slots are supplied in pairs , first the start then the end slots . this will first try to fill up all partial slots and if items are still left after doing so , it places them into the first empty slot . if no empty slot was found and there are still items left , their returned from this method .
