@Override public ItemStack transferStackInSlot(EntityPlayer playerIn, int fromSlot) { ItemStack previous = null; Slot slot = (Slot) this.inventorySlots.get(fromSlot); if (slot != null && slot.getHasStack()) { ItemStack current = slot.getStack(); previous = current.copy(); if (fromSlot < 9) { if (!this.mergeItemStack(current, 9, 45, true)) return null; } else { if (!this.mergeItemStack(current, 0, 9, false)) return null; } if (current.getCount() == 0) slot.putStack((ItemStack) null); else slot.onSlotChanged(); if (current.getCount() == previous.getCount()) return null; } return previous; } @Override protected boolean mergeItemStack(ItemStack stack, int startIndex, int endIndex, boolean useEndIndex) { boolean success = false; int index = startIndex; if (useEndIndex) index = endIndex - 1; Slot slot; ItemStack stackinslot; if (stack.isStackable()) { while (stack.getCount() > 0 && (!useEndIndex && index < endIndex || useEndIndex && index >= startIndex)) { slot = (Slot) this.inventorySlots.get(index); stackinslot = slot.getStack(); if (stackinslot != null && stackinslot.getItem() == stack.getItem() && (!stack.getHasSubtypes() || stack.getMetadata() == stackinslot.getMetadata()) && ItemStack.areItemStackTagsEqual(stack, stackinslot)) { int l = stackinslot.getCount() + stack.getCount(); int maxsize = Math.min(stack.getMaxStackSize(), slot.getItemStackLimit(stack)); if (l <= maxsize) { stack.setCount(0); stackinslot.setCount(1); slot.onSlotChanged(); success = true; } else if (stackinslot.getCount() < maxsize) { stack.setCount(stack.getCount() - stack.getMaxStackSize() - stackinslot.getCount()); stackinslot.setCount(stack.getMaxStackSize()); slot.onSlotChanged(); success = true; } } if (useEndIndex) { --index; } else { ++index; } } } if (stack.getCount() > 0) { if (useEndIndex) { index = endIndex - 1; } else { index = startIndex; } while (!useEndIndex && index < endIndex || useEndIndex && index >= startIndex && stack.getCount() > 0) { slot = (Slot) this.inventorySlots.get(index); stackinslot = slot.getStack(); if (stackinslot == null && slot.isItemValid(stack)) { if (stack.getCount() < slot.getItemStackLimit(stack)) { slot.putStack(stack.copy()); stack.setCount(0); success = true; break; } else { ItemStack newstack = stack.copy(); newstack.setCount(slot.getItemStackLimit(stack)); slot.putStack(newstack); stack.setCount(stack.getCount() - slot.getItemStackLimit(stack)); success = true; } } if (useEndIndex) { --index; } else { ++index; } } } return success; }