public MyFixedPoint GetItemAmountCombined(MyInventoryBase inventory, MyDefinitionId contentId)
        {
            int amount = 0;
            var group = MyDefinitionManager.Static.GetGroupForComponent(contentId, out amount);
            if (group == null)
            {
                MyComponentSubstitutionDefinition substitutions;
                if (MyDefinitionManager.Static.TryGetComponentSubstitutionDefinition(contentId, out substitutions))
                {                    
                    foreach (var providingComponent in substitutions.ProvidingComponents)
                    {
                        amount += (int)inventory.GetItemAmount(providingComponent.Key) / providingComponent.Value;
                    }
                    return amount;
                }

                return inventory.GetItemAmount(contentId);
            }
            else
            {
                Clear();
                inventory.CountItems(m_componentCounts);
                AddItem(group.Id, amount, int.MaxValue);
                Solve(m_componentCounts);
                return GetSolvedItemCount();
            }
        }
Ejemplo n.º 2
0
        /*
        private void BeforeContentsChanged(MyInventoryBase inventory) {
            Log.Trace("BeforeContentsChanged called on inventory " + inventory.Entity.ToString(), "BeforeContentsChanged");
        }
        */
        private void OnContentsChanged(MyInventoryBase inventory)
        {
            Log.Trace("Updating inventory cache with inventory " + inventory.Entity.ToString(), "OnContentsChanged");

            ItemCountsAggregate cachedCount;
            if (!InventoryTotals.TryGetValue(inventory, out cachedCount)) {
                Log.Error("Received an update for inventory we're not tracking.", "UpdateInventory");
                return;
            }

            ItemCountsAggregate originalCounts = cachedCount.Copy();
            Totals -= originalCounts;

            if (WatchedItems != null) {
                foreach (var id in WatchedItems) {
                    cachedCount.Set(id, inventory.GetItemAmount(id));
                }
            }
            else {
                foreach (var item in inventory.GetItems()) {
                    cachedCount.Set(item.Content.GetObjectId(), item.Amount);
                }
            }

            Totals += cachedCount;

            if (SkipNextNotify) {
                SkipNextNotify = false;
            }
            else {
                NotifyContentsChanged(cachedCount - originalCounts);
            }

            //DebugPrint();
        }