private ModuleKISInventory AddToContainer(WorkshopItem item) { var inventories = KISWrapper.GetInventories(vessel); if (inventories == null || inventories.Count == 0) { throw new Exception("No KIS Inventory found!"); } var freeInventories = inventories .Where(i => WorkshopUtils.HasFreeSpace(i, item)) .Where(WorkshopUtils.HasFreeSlot) .Where(WorkshopUtils.IsOccupied) .ToArray(); if (freeInventories.Any()) { // first pass with favored inventories var favoredInventories = freeInventories .Where(i => i.part.GetComponent <OseModuleInventoryPreference>() != null) .Where(i => i.part.GetComponent <OseModuleInventoryPreference>().IsFavored).ToArray(); foreach (var inventory in favoredInventories) { var kisItem = inventory.AddItem(item.Part.partPrefab); if (kisItem == null) { throw new Exception("Error adding item " + item.Part.name + " to inventory"); } foreach (var resourceInfo in kisItem.GetResources()) { if (WorkshopRecipeDatabase.HasResourceRecipe(resourceInfo.resourceName)) { kisItem.SetResource(resourceInfo.resourceName, (int)resourceInfo.maxAmount); } else { kisItem.SetResource(resourceInfo.resourceName, 0); } } return(inventory); } // second pass with the rest foreach (var inventory in freeInventories) { var kisItem = inventory.AddItem(item.Part.partPrefab); if (kisItem == null) { throw new Exception("Error adding item " + item.Part.name + " to inventory"); } foreach (var resourceInfo in kisItem.GetResources()) { if (WorkshopRecipeDatabase.HasResourceRecipe(resourceInfo.resourceName)) { kisItem.SetResource(resourceInfo.resourceName, (int)resourceInfo.maxAmount); } else { kisItem.SetResource(resourceInfo.resourceName, 0); } } return(inventory); } } return(null); }