public override bool TransferItemsFrom(MyInventoryBase sourceInventory, MyInventoryItem item, int amount) { if (sourceInventory == null || item == null || amount > item.Amount) { return(false); } if (amount == 0) { return(true); } var limitedItem = amount < item.Amount ? item.Clone(amount) : item; if (!CanAddItems(item.DefinitionId, amount) && this != sourceInventory) { return(false); } if (this == sourceInventory) { return(sourceInventory.Remove(item, amount) && Add(limitedItem)); } if (!Add(limitedItem)) { return(false); } if (sourceInventory.Remove(item, amount)) { return(true); } Remove(item, amount); return(false); }
/// <summary> /// Removes all items from the inventory. Call on the server-side for best results. /// </summary> /// <param name="inventory"></param> public static void Clear(this MyInventoryBase inventory) { if (inventory == null) { return; } var items = inventory.Items; for (var i = items.Count - 1; i >= 0; i--) { inventory.Remove(items.ItemAt(i)); } }
public override bool Remove(IMyInventoryItem item, MyFixedPoint amount) { using (List <MyComponentBase> .Enumerator enumerator = this.m_children.Reader.GetEnumerator()) { while (true) { if (!enumerator.MoveNext()) { break; } MyInventoryBase current = (MyInventoryBase)enumerator.Current; if (current.ItemsCanBeRemoved(amount, item) && current.Remove(item, amount)) { return(true); } } } return(false); }
private static bool playerDrinkSomething(IMyEntity entity, PlayerData playerData) { MyInventoryBase inventory = ((MyEntity)entity).GetInventoryBase(); var items = inventory.GetItems(); foreach (IMyInventoryItem item in items) { float result; // Getting the item type string szItemContent = item.Content.ToString(); //MyAPIGateway.Utilities.ShowMessage("DEBUG", "szItemContent: " + item.Content.SubtypeName); string szTypeName = szItemContent.Substring(szItemContent.IndexOf(OBJECT_BUILDER_PREFIX) + OBJECT_BUILDER_PREFIX.Length); // Type verification if (!szTypeName.Equals("Ingot")) { continue; } if (mBeverageTypes.TryGetValue(item.Content.SubtypeName, out result)) { float canConsumeNum = Math.Min(((MAX_VALUE - playerData.thirst) / result), (float)item.Amount); //MyAPIGateway.Utilities.ShowMessage("DEBUG", "canDrink: " + canConsumeNum); if (canConsumeNum > 0) { inventory.Remove(item, (MyFixedPoint)canConsumeNum); playerData.thirst += result * (float)canConsumeNum; return(true); } } } return(false); }
private static bool playerEatSomething(IMyEntity entity, PlayerData playerData) { MyInventoryBase inventory = ((MyEntity)entity).GetInventoryBase(); var items = inventory.GetItems(); foreach (IMyInventoryItem item in items) { float result; if (mFoodTypes.TryGetValue(item.GetDefinitionId(), out result)) { float canConsumeNum = Math.Min(((MAX_VALUE - playerData.hunger) / result), (float)item.Amount); //MyAPIGateway.Utilities.ShowMessage("DEBUG", "canEat: " + canConsumeNum); if (canConsumeNum > 0) { inventory.Remove(item, (MyFixedPoint)canConsumeNum); playerData.hunger += result * (float)canConsumeNum; return(true); } } } return(false); }
/// <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; }
/// <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); } MyInventoryBase destinationInventory = this; 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); } bool transfered = false; 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)) { 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. destinationInventory.Remove(item, amount); } } } else { // same inventory transfer = splitting amount, need to remove first and add second if (sourceInventory.Remove(item, amount) && destinationInventory.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 = destinationInventory.Entity.EntityId; eventParams.DestinationInventoryId = destinationInventory.InventoryId; MyMultiplayer.RaiseStaticEvent(s => InventoryBaseTransferItem_Implementation, eventParams); } } return(transfered); }
private static bool playerDrinkSomething(IMyEntity entity, PlayerData playerData, float maxval_cap, float crapbonus) { MyInventoryBase inventory = ((MyEntity)entity).GetInventoryBase(); var items = inventory.GetItems(); foreach (IMyInventoryItem item in items) { float result; // Getting the item type string szItemContent = item.Content.ToString(); //MyAPIGateway.Utilities.ShowMessage("DEBUG", "szItemContent: " + item.Content.SubtypeName); string szTypeName = szItemContent.Substring(szItemContent.IndexOf(OBJECT_BUILDER_PREFIX) + OBJECT_BUILDER_PREFIX.Length); // Type verification if (!szTypeName.Equals("Ingot")) { continue; } if (mBeverageTypes.TryGetValue(item.Content.SubtypeName, out result)) { float canConsumeNum = Math.Min(((maxval_cap - playerData.thirst) / result), (float)item.Amount); //MyAPIGateway.Utilities.ShowMessage("DEBUG", "canDrink: " + canConsumeNum); if (canConsumeNum > 0) { inventory.Remove(item, (MyFixedPoint)canConsumeNum); playerData.thirst += result * (float)canConsumeNum; if (item.Content.SubtypeName.Contains("offee")) // TODO parametrize this { playerData.fatigue = Config.MaxValue; // TODO parametrize this } if (item.Content.SubtypeName.Contains("ouillon")) // TODO parametrize this { playerData.hunger += Math.Max(0f, Math.Min(result * (float)canConsumeNum, maxval_cap - playerData.hunger)); // TODO parametrize this } // waste management line if (CRAP_AMOUNT > 0.0) { inventory.AddItems((MyFixedPoint)(canConsumeNum * CRAP_AMOUNT * crapbonus), new MyObjectBuilder_Ingot() { SubtypeName = "GreyWater" }); if (CROSS_CRAP_AMOUNT > 0.0) { inventory.AddItems((MyFixedPoint)(canConsumeNum * (1 - CRAP_AMOUNT) * CROSS_CRAP_AMOUNT), new MyObjectBuilder_Ore() { SubtypeName = "Organic" }); } } return(true); } } } return(false); }
public static bool Apply <TInstance>(MyInventoryBase items, ListReader <ImmutableInventoryAction> actions, LuckyLoot.LootContext?luckContext = null, ActionWithArg <TInstance, ImmutableInventoryAction>?errorReporter = null) { var luck = luckContext ?? LuckyLoot.DefaultLoot; var success = true; foreach (var action in actions) { switch (action.Mode) { case ImmutableInventoryAction.InventoryActionMode.GiveTakeItem: { if (action.Amount > 0) { success = action.TargetId.TypeId == typeof(MyObjectBuilder_ItemTagDefinition) ? items.AddItemsWithTag(action.TargetId.SubtypeId, action.Amount) : items.AddItems(action.TargetId, action.Amount); } else { success = action.TargetId.TypeId == typeof(MyObjectBuilder_ItemTagDefinition) ? items.RemoveItemsWithTag(action.TargetId.SubtypeId, -action.Amount) : items.RemoveItems(action.TargetId, -action.Amount); } break; } case ImmutableInventoryAction.InventoryActionMode.RepairDamageItem: { var remaining = action.Amount; if (action.TargetId.TypeId == typeof(MyObjectBuilder_ItemTagDefinition)) { foreach (var item in items.Items) { if (item is MyDurableItem durable && item.HasTag(action.TargetId.SubtypeId)) { ApplyDurability(durable, ref remaining); if (durable.Durability == 0) { var broken = durable.GetDefinition().BrokenItem; var amount = item.Amount; if (!items.Remove(item)) { break; } if (broken.HasValue && !items.AddItems(broken.Value, amount)) { break; } } if (remaining == 0) { break; } } } } else { foreach (var item in items.Items) { if (item is MyDurableItem durable && item.DefinitionId == action.TargetId) { ApplyDurability(durable, ref remaining); if (durable.Durability == 0) { var broken = durable.GetDefinition().BrokenItem; var amount = item.Amount; if (!items.Remove(item)) { break; } if (broken.HasValue && !items.AddItems(broken.Value, amount)) { break; } } if (remaining == 0) { break; } } } } success = remaining == 0; break; } case ImmutableInventoryAction.InventoryActionMode.GiveTakeLootTable: { using (ItemCollection.Borrow(out var tmp)) { using (PoolManager.Get(out HashSet <MyStringHash> tmpSet)) { var table = MyDefinitionManager.Get <MyLootTableDefinition>(action.TargetId); for (var pass = 0; pass < Math.Abs(action.Amount); pass++) { tmpSet.Clear(); tmp.GenerateLuckyContent(table, luck, tmpSet); } } if (action.Amount > 0) { foreach (var item in tmp.Items) { success &= items.Add(item); } } else { foreach (var item in tmp.Items) { success &= items.RemoveItems(item.DefinitionId, item.Amount); } } } break; } default: throw new Exception("Bad mode"); } if (!success) { errorReporter?.Invoke(action); return(false); } } return(success); }