/// <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); }
/// <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); }
/// <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; }
/// <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; }