Example #1
0
        /// <summary>
        /// Gets a list of all parts in the inventory with the given name.
        /// </summary>
        /// <param name="name">The name to search for</param>
        /// <returns>An IEnumerable of all InventoryParts with that name</returns>
        public IEnumerable <InventoryPart> FindPartsByName(string name)
        {
            if (!InventoryEnabled)
            {
                return(null);
            }

            PartInventory copy = Copy();

            return(copy.GetAllParts().Where(p => string.Equals(p.Name, name, StringComparison.OrdinalIgnoreCase)));
        }
Example #2
0
        /// <summary>
        /// Takes a List of part ConfigNodes and returns the ConfigNodes that are present in the inventory.
        /// Assumes the default strictness.
        /// </summary>
        /// <param name="sourceParts">Source list of parts</param>
        /// <returns>List of part ConfigNodes that are in the inventory</returns>
        public IList <ConfigNode> GetPartsInInventory_ConfigNodes(IEnumerable <ConfigNode> sourceParts, string strictness)
        {
            if (!ScrapYard.Instance.TheInventory.InventoryEnabled)
            {
                return(new List <ConfigNode>());
            }
            ComparisonStrength actualStrictness = parseStrictnessString(strictness);
            List <ConfigNode>  inInventory      = new List <ConfigNode>();
            PartInventory      InventoryCopy    = ScrapYard.Instance.TheInventory.Copy();

            foreach (ConfigNode part in sourceParts)
            {
                InventoryPart inputPart = new InventoryPart(part);
                if (InventoryCopy.RemovePart(inputPart, actualStrictness) != null)
                {
                    inInventory.Add(part);
                }
            }
            return(inInventory);
        }
Example #3
0
        public void SplitParts(List <InventoryPart> input, out List <InventoryPart> inInventory, out List <InventoryPart> notInInventory)
        {
            inInventory    = new List <InventoryPart>();
            notInInventory = new List <InventoryPart>();
            PartInventory InventoryCopy = new PartInventory(true);

            InventoryCopy.State = State; //TODO: Make a copy method
            foreach (InventoryPart inputPart in input)
            {
                if (InventoryCopy.GetPartQuantity(inputPart) > 0)
                {
                    inInventory.Add(inputPart);
                    InventoryCopy.AddPart(inputPart, -1);
                }
                else
                {
                    notInInventory.Add(inputPart);
                }
            }
        }
Example #4
0
        /// <summary>
        /// Finds all parts in the inventory for the given InventoryPart and the provided strictness
        /// </summary>
        /// <param name="part">The source part to find a match for</param>
        /// <param name="strength">The strictness of the comparison. Defaults to MODULES.</param>
        /// <returns>An IEnumerable of InventoryParts that match</returns>
        public IEnumerable <InventoryPart> FindParts(InventoryPart part, ComparisonStrength strength = ComparisonStrength.MODULES)
        {
            if (!InventoryEnabled)
            {
                return(null);
            }

            List <InventoryPart> foundParts = new List <InventoryPart>();
            PartInventory        copy       = Copy();
            InventoryPart        found      = null;

            do
            {
                found = copy.RemovePart(part, strength);
                if (found != null)
                {
                    foundParts.Add(found);
                }
            } while (found != null);

            return(foundParts);
        }
Example #5
0
        /// <summary>
        /// Copies the PartInventory to a new PartInventory
        /// </summary>
        /// <param name="disableEventsOnCopy">If true, the copy will not fire events</param>
        /// <returns>A copy of the PartInventory</returns>
        public PartInventory Copy(bool disableEventsOnCopy = true)
        {
            PartInventory ret             = null;
            bool          originalDisable = disableEvents;

            try
            {
                disableEvents = true;
                ret           = new PartInventory(disableEventsOnCopy)
                {
                    internalInventory = new HashSet <InventoryPart>(internalInventory.Select(p => p.Copy()))
                };
                //ret.State = State;
            }
            catch (Exception ex)
            {
                Logging.LogException(ex);
            }
            finally
            {
                disableEvents = originalDisable;
            }
            return(ret);
        }