/// <summary> /// merges provided ItemStack with the first avaliable one in the container/player inventory /// </summary> protected virtual bool MergeItemStack(ItemStack par1ItemStack, int par2, int par3, bool par4) { bool flag = false; int i = par2; if (par4) { i = par3 - 1; } if (par1ItemStack.IsStackable()) { while (par1ItemStack.StackSize > 0 && (!par4 && i < par3 || par4 && i >= par2)) { Slot slot = InventorySlots[i]; ItemStack itemstack = slot.GetStack(); if (itemstack != null && itemstack.ItemID == par1ItemStack.ItemID && (!par1ItemStack.GetHasSubtypes() || par1ItemStack.GetItemDamage() == itemstack.GetItemDamage()) && ItemStack.Func_46154_a(par1ItemStack, itemstack)) { int k = itemstack.StackSize + par1ItemStack.StackSize; if (k <= par1ItemStack.GetMaxStackSize()) { par1ItemStack.StackSize = 0; itemstack.StackSize = k; slot.OnSlotChanged(); flag = true; } else if (itemstack.StackSize < par1ItemStack.GetMaxStackSize()) { par1ItemStack.StackSize -= par1ItemStack.GetMaxStackSize() - itemstack.StackSize; itemstack.StackSize = par1ItemStack.GetMaxStackSize(); slot.OnSlotChanged(); flag = true; } } if (par4) { i--; } else { i++; } } } if (par1ItemStack.StackSize > 0) { int j; if (par4) { j = par3 - 1; } else { j = par2; } do { if ((par4 || j >= par3) && (!par4 || j < par2)) { break; } Slot slot1 = InventorySlots[j]; ItemStack itemstack1 = slot1.GetStack(); if (itemstack1 == null) { slot1.PutStack(par1ItemStack.Copy()); slot1.OnSlotChanged(); par1ItemStack.StackSize = 0; flag = true; break; } if (par4) { j--; } else { j++; } }while (true); } return(flag); }