Client LuaCsForBarotrauma
RemoveItemAction.cs
1 using System.Collections.Generic;
2 using System.Collections.Immutable;
3 
4 namespace Barotrauma
5 {
10  {
11  [Serialize("", IsPropertySaveable.Yes, description: "Tag of the item(s) to remove.")]
12  public Identifier TargetTag { get; set; }
13 
14  [Serialize("", IsPropertySaveable.Yes, description: "Optional list of identifiers the item(s) must have. You might for example want to go through all tagged items inside a cabinet, but only remove specific types of items.")]
15  public string ItemIdentifiers { get; set; }
16 
17  [Serialize(1, IsPropertySaveable.Yes, description: "Maximum number of items to remove.")]
18  public int Amount { get; set; }
19 
20  private readonly ImmutableHashSet<Identifier> itemIdentifierSplit;
21 
22  public RemoveItemAction(ScriptedEvent parentEvent, ContentXElement element) : base(parentEvent, element)
23  {
24  if (string.IsNullOrEmpty(ItemIdentifiers))
25  {
26  ItemIdentifiers = element.GetAttributeString("itemidentifier", element.GetAttributeString("identifier", string.Empty));
27  }
28  itemIdentifierSplit = ItemIdentifiers.Split(',').ToIdentifiers().ToImmutableHashSet();
29  }
30 
31  private bool isFinished = false;
32 
33  public override bool IsFinished(ref string goTo)
34  {
35  return isFinished;
36  }
37  public override void Reset()
38  {
39  isFinished = false;
40  }
41 
42  public override void Update(float deltaTime)
43  {
44  if (isFinished) { return; }
45 
46  var targets = ParentEvent.GetTargets(TargetTag);
47  bool hasValidTargets = false;
48  foreach (Entity target in targets)
49  {
50  if (target is Character character && character.Inventory != null || target is Item)
51  {
52  hasValidTargets = true;
53  break;
54  }
55  }
56  if (!hasValidTargets) { return; }
57 
58  HashSet<Item> removedItems = new HashSet<Item>();
59  foreach (Entity target in targets)
60  {
61  Inventory inventory = (target as Character)?.Inventory;
62  if (inventory != null)
63  {
64  while (removedItems.Count < Amount)
65  {
66  var item = inventory.FindItem(it =>
67  it != null &&
68  !removedItems.Contains(it) &&
69  (itemIdentifierSplit.Count == 0 || itemIdentifierSplit.Contains(it.Prefab.Identifier)), recursive: true);
70  if (item == null) { break; }
72  removedItems.Add(item);
73  }
74  }
75  else if (target is Item item)
76  {
77  if (itemIdentifierSplit.Count == 0 || itemIdentifierSplit.Contains(item.Prefab.Identifier))
78  {
80  removedItems.Add(item);
81  if (removedItems.Count >= Amount) { break; }
82  }
83  }
84  }
85  isFinished = true;
86  }
87  }
88 }
string? GetAttributeString(string key, string? def)
static EntitySpawner Spawner
Definition: Entity.cs:31
readonly ScriptedEvent ParentEvent
Definition: EventAction.cs:102
Item FindItem(Func< Item, bool > predicate, bool recursive)
Removes (deletes) a specific item or items.
override void Update(float deltaTime)
override bool IsFinished(ref string goTo)
Has the action finished.
RemoveItemAction(ScriptedEvent parentEvent, ContentXElement element)
IEnumerable< Entity > GetTargets(Identifier tag)