Client LuaCsForBarotrauma
CharacterAbilityRemoveRandomIngredient.cs
1 #nullable enable
2 
5 using System.Collections.Generic;
6 using System.Linq;
7 
8 namespace Barotrauma.Abilities
9 {
10  internal sealed class CharacterAbilityRemoveRandomIngredient : CharacterAbility
11  {
12  private readonly AbilityConditionItem? condition;
13 
14  public CharacterAbilityRemoveRandomIngredient(CharacterAbilityGroup characterAbilityGroup, ContentXElement abilityElement) : base(characterAbilityGroup, abilityElement)
15  {
16  var conditionElement = abilityElement.GetChildElement(nameof(AbilityConditionItem));
17  if (conditionElement != null)
18  {
19  condition = new AbilityConditionItem(CharacterTalent, conditionElement);
20  }
21  }
22 
23  protected override void ApplyEffect(AbilityObject abilityObject)
24  {
25  if (abilityObject is not Fabricator.AbilityFabricationItemIngredients { Items.Count: > 0 } ingredients) { return; }
26 
27  List<Item> applicableIngredients = condition == null ?
28  ingredients.Items.ToList() :
29  ingredients.Items.Where(it => condition.MatchesItem(it.Prefab)).ToList();
30  if (applicableIngredients.None()) { return; }
31 
32  ingredients.Items.Remove(applicableIngredients.GetRandom(Rand.RandSync.Unsynced));
33  }
34  }
35 }