예제 #1
0
        /// <summary>
        /// Transfers safely given item from one to another inventory, uses ItemsCanBeAdded and ItemsCanBeRemoved checks. If sourceInventory == destionationInventory, it splits the amount.
        /// </summary>
        /// <returns>true if items were succesfully transfered, otherwise, false</returns>
        public static bool TransferItems(MyInventoryBase sourceInventory, MyInventoryBase destinationInventory, IMyInventoryItem item, MyFixedPoint amount, bool stack)
        {
            if (sourceInventory == null)
            {
                System.Diagnostics.Debug.Fail("Source inventory is null!");
                return(false);
            }
            if (destinationInventory == null)
            {
                System.Diagnostics.Debug.Fail("Destionation inventory is null!");
                return(false);
            }
            if (item == null)
            {
                System.Diagnostics.Debug.Fail("Item is null!");
                return(false);
            }
            if (amount == 0)
            {
                return(true);
            }

            if ((destinationInventory.ItemsCanBeAdded(amount, item) || destinationInventory == sourceInventory) && sourceInventory.ItemsCanBeRemoved(amount, item))
            {
                if (Sync.IsServer)
                {
                    if (destinationInventory != sourceInventory)
                    {
                        // try to add first and then remove to ensure this items don't disappear
                        if (destinationInventory.Add(item, amount, stack))
                        {
                            if (sourceInventory.Remove(item, amount))
                            {
                                // successfull transaction
                                return(true);
                            }
                            else
                            {
                                System.Diagnostics.Debug.Fail("Error! Items were added to inventory, but can't be removed!");
                            }
                        }
                    }
                    else
                    {
                        // same inventory transfer = splitting amount, need to remove first and add second
                        if (sourceInventory.Remove(item, amount) && destinationInventory.Add(item, amount, stack))
                        {
                            return(true);
                        }
                        else
                        {
                            System.Diagnostics.Debug.Fail("Error! Unsuccesfull splitting!");
                        }
                    }
                }
            }
            return(false);
        }
예제 #2
0
        /// <summary>
        /// Transfers safely given item from one to another inventory, uses ItemsCanBeAdded and ItemsCanBeRemoved checks
        /// </summary>
        /// <returns>true if items were succesfully transfered, otherwise, false</returns>
        public static bool TransferItems(MyInventoryBase sourceInventory, MyInventoryBase destinationInventory, IMyInventoryItem item, MyFixedPoint amount)
        {
            if (sourceInventory == null)
            {
                System.Diagnostics.Debug.Fail("Source inventory is null!");
                return(false);
            }
            if (destinationInventory == null)
            {
                System.Diagnostics.Debug.Fail("Destionation inventory is null!");
                return(false);
            }
            if (item == null)
            {
                System.Diagnostics.Debug.Fail("Item is null!");
                return(false);
            }
            if (amount == 0)
            {
                return(true);
            }


            if (destinationInventory.ItemsCanBeAdded(amount, item) && sourceInventory.ItemsCanBeRemoved(amount, item))
            {
                if (Sync.IsServer)
                {
                    if (destinationInventory.Add(item, amount))
                    {
                        if (sourceInventory.Remove(item, amount))
                        {
                            // successfull transaction
                            return(true);
                        }
                        else
                        {
                            System.Diagnostics.Debug.Fail("Error! Items were added to inventory, but can't be removed!");
                        }
                    }
                }
                else
                {
                    MySyncInventory.SendTransferItemsMessage(sourceInventory, destinationInventory, item, amount);
                }
            }

            return(false);
        }
예제 #3
0
        /// <summary>
        /// Transfers safely given item from one to another inventory, uses ItemsCanBeAdded and ItemsCanBeRemoved checks
        /// </summary>
        /// <returns>true if items were succesfully transfered, otherwise, false</returns>
        public static bool TransferItems(MyInventoryBase sourceInventory, MyInventoryBase destinationInventory, IMyInventoryItem item, MyFixedPoint amount)
        {
            if (sourceInventory == null)
            {
                System.Diagnostics.Debug.Fail("Source inventory is null!");
                return false;
            }
            if (destinationInventory == null)
            {
                System.Diagnostics.Debug.Fail("Destionation inventory is null!");
                return false;
            }
            if (item == null)
            {
                System.Diagnostics.Debug.Fail("Item is null!");
                return false;
            }
            if (amount == 0)
            {
                return true;
            }


            if (destinationInventory.ItemsCanBeAdded(amount, item) && sourceInventory.ItemsCanBeRemoved(amount, item))
            {
                if (Sync.IsServer)
                {
                    if (destinationInventory.Add(item, amount))
                    {
                        if (sourceInventory.Remove(item, amount))
                        {
                            // successfull transaction
                            return true;
                        }
                        else
                        {
                            System.Diagnostics.Debug.Fail("Error! Items were added to inventory, but can't be removed!");
                        }
                    }
                }
                else
                {
                    MySyncInventory.SendTransferItemsMessage(sourceInventory, destinationInventory, item, amount);
                }
            }
            
            return false;
        }
예제 #4
0
        public static MyInventoryBase GetInventory(this MyEntity entity, MyStringHash inventoryId)
        {
            MyInventoryBase inventory = null;

            if (entity is IMyInventoryOwner)
            {
                var inventoryOwner = entity as IMyInventoryOwner;
                for (int i = 0; i < inventoryOwner.InventoryCount; ++i)
                {
                    var iteratedInventory = inventoryOwner.GetInventory(i);
                    if (inventoryId.Equals(MyStringHash.GetOrCompute(iteratedInventory.InventoryId.ToString())))
                    {
                        return(iteratedInventory);
                    }
                }
            }

            inventory = entity.Components.Get <MyInventoryBase>();
            if (inventory != null)
            {
                if (inventoryId.Equals(MyStringHash.GetOrCompute(inventory.InventoryId.ToString())))
                {
                    return(inventory);
                }
            }

            if (inventory is MyInventoryAggregate)
            {
                var aggregate = inventory as MyInventoryAggregate;
                m_tmpList.Clear();
                aggregate.GetComponentsFlattened(m_tmpList);
                foreach (var component in m_tmpList)
                {
                    var componentInventory = component as MyInventoryBase;
                    if (inventoryId.Equals(MyStringHash.GetOrCompute(componentInventory.InventoryId.ToString())))
                    {
                        return(componentInventory);
                    }
                }
            }

            return(null);
        }
 private void inventory_OnContentsChanged(MyInventoryBase obj)
 {
     RefreshInventoryContents();
     if (InventoryContentsChanged != null)
         InventoryContentsChanged(this);
 }
예제 #6
0
        /// <summary>
        /// Transfers safely given item from one to another inventory, uses ItemsCanBeAdded and ItemsCanBeRemoved checks. If sourceInventory == destionationInventory, it splits the amount.
        /// </summary>
        /// <returns>true if items were succesfully transfered, otherwise, false</returns>
        public static bool TransferItems(MyInventoryBase sourceInventory, MyInventoryBase destinationInventory, IMyInventoryItem item, MyFixedPoint amount, bool stack)
        {
            if (sourceInventory == null)
            {
                System.Diagnostics.Debug.Fail("Source inventory is null!");
                return false;
            }
            if (destinationInventory == null)
            {
                System.Diagnostics.Debug.Fail("Destionation inventory is null!");
                return false;
            }
            if (item == null)
            {
                System.Diagnostics.Debug.Fail("Item is null!");
                return false;
            }
            if (amount == 0)
            {
                return true;
            }

            if ((destinationInventory.ItemsCanBeAdded(amount, item) || destinationInventory == sourceInventory ) && sourceInventory.ItemsCanBeRemoved(amount, item))
            {
                if (Sync.IsServer)
                {
                    if (destinationInventory != sourceInventory)
                    {
                        // try to add first and then remove to ensure this items don't disappear
                        if (destinationInventory.Add(item, amount, stack))
                        {
                            if (sourceInventory.Remove(item, amount))
                            {
                                // successfull transaction
                                return true;
                            }
                            else
                            {
                                System.Diagnostics.Debug.Fail("Error! Items were added to inventory, but can't be removed!");
                            }
                        }                        
                    }
                    else
                    {
                        // same inventory transfer = splitting amount, need to remove first and add second
                        if (sourceInventory.Remove(item, amount) && destinationInventory.Add(item, amount, stack))
                        {
                            return true;
                        }
                        else
                        {
                            System.Diagnostics.Debug.Fail("Error! Unsuccesfull splitting!");
                        }
                    }
                }
            }     
            return false;
        }