private IItemStack GetStackForItem(IItem item) { IItemStack selectedStack = null; if (ItemStacks == null) { ItemStacks = new List <IItemStack>(); } foreach (var stack in ItemStacks.Reverse()) { if (stack.IsEmpty()) { selectedStack = stack; } if (!stack.ContainsItemOfType(item)) { continue; } if (!stack.HasSpaceFor(item)) { continue; } selectedStack = stack; break; } return(selectedStack ?? CreateNewStackIfPossible()); }