Exemplo n.º 1
0
        private static void InventoryBaseTransferItem_Implementation(MyInventoryTransferEventContent eventParams)
        {
            if (!MyEntities.EntityExists(eventParams.DestinationOwnerId) || !MyEntities.EntityExists(eventParams.SourceOwnerId)) return;

            MyEntity sourceOwner = MyEntities.GetEntityById(eventParams.SourceOwnerId);
            MyInventoryBase source = sourceOwner.GetInventory(eventParams.SourceInventoryId);
            MyEntity destOwner = MyEntities.GetEntityById(eventParams.DestinationOwnerId);
            MyInventoryBase dst = destOwner.GetInventory(eventParams.DestinationInventoryId);
            var items = source.GetItems();
            MyPhysicalInventoryItem? foundItem = null;
            foreach (var item in items)
            {
                if (item.ItemId == eventParams.ItemId)
                {
                    foundItem = item;
                }
            }

            /*if (foundItem.HasValue)
                dstT.ransferItemsFrom(source, foundItem, eventParams.Amount, eventParams.DestinationItemIndex);*/
        }
Exemplo n.º 2
0
        /// <summary>
        /// Transfers safely given item from inventory given as parameter to this instance.
        /// </summary>
        /// <returns>true if items were succesfully transfered, otherwise, false</returns>
        public override bool TransferItemsFrom(MyInventoryBase sourceInventory, IMyInventoryItem item, MyFixedPoint amount)
        {
            if (sourceInventory == null)
            {
                System.Diagnostics.Debug.Fail("Source inventory is null!");
                return false;
            }
            if (item == null)
            {
                System.Diagnostics.Debug.Fail("Item is null!");
                return false;
            }
            if (amount == 0)
            {
                return true;
            }

            bool transfered = false;
            if ((ItemsCanBeAdded(amount, item) || this == sourceInventory) && sourceInventory.ItemsCanBeRemoved(amount, item))
            {
                if (Sync.IsServer)
                {
                    if (this != sourceInventory)
                    {
                        // try to add first and then remove to ensure this items don't disappear
                        if (Add(item, amount))
                        {
                            if (sourceInventory.Remove(item, amount))
                            {
                                // successfull transaction
                                return true;
                            }
                            else
                            {
                                // This can happend, that it can't be removed due to some lock, then we need to revert the add.
                                Remove(item, amount);
                            }
                        }
                    }
                    else
                    {
                        // same inventory transfer = splitting amount, need to remove first and add second
                        if (sourceInventory.Remove(item, amount) && Add(item, amount))
                        {
                            return true;
                        }
                        else
                        {
                            System.Diagnostics.Debug.Fail("Error! Unsuccesfull splitting!");
                        }
                    }
                }
                else
                {
                    Debug.Assert(sourceInventory != null);
                    MyInventoryTransferEventContent eventParams = new MyInventoryTransferEventContent();
                    eventParams.Amount = amount;
                    eventParams.ItemId = item.ItemId;
                    eventParams.SourceOwnerId = sourceInventory.Entity.EntityId;
                    eventParams.SourceInventoryId = sourceInventory.InventoryId;
                    eventParams.DestinationOwnerId = Entity.EntityId;
                    MyMultiplayer.RaiseStaticEvent(s => InventoryBaseTransferItem_Implementation, eventParams);
                }
            }

            return transfered;
        }