Client LuaCsForBarotrauma
WaitForItemFabricatedAction.cs
1 #nullable enable
2 
4 using System.Linq;
5 
6 namespace Barotrauma
7 {
12  {
13  [Serialize("", IsPropertySaveable.Yes, description: "Tag of the character who must fabricate the item. If empty, it doesn't matter who fabricates it.")]
14  public Identifier CharacterTag { get; set; }
15 
16  [Serialize("", IsPropertySaveable.Yes, description: "Identifier of the item that must be fabricated. Optional if ItemTag is set.")]
17  public Identifier ItemIdentifier { get; set; }
18 
19  [Serialize("", IsPropertySaveable.Yes, description: "Tag of the item that must be fabricated. Optional if ItemIdentifier is set.")]
20  public Identifier ItemTag { get; set; }
21 
22  [Serialize(1, IsPropertySaveable.Yes, description: "Number of items that need to be fabricated.")]
23  public int Amount { get; set; }
24 
25  [Serialize("", IsPropertySaveable.Yes, description: "Tag to apply to the fabricated item(s).")]
26  public Identifier ApplyTagToItem { get; set; }
27 
28  private int counter;
29 
30  public WaitForItemFabricatedAction(ScriptedEvent parentEvent, ContentXElement element) : base(parentEvent, element)
31  {
32  if (ItemTag.IsEmpty && ItemIdentifier.IsEmpty)
33  {
34  DebugConsole.ThrowError($"Error in event \"{ParentEvent.Prefab.Identifier}\". {nameof(WaitForItemFabricatedAction)} does't define either a tag or an identifier of the item to check.",
35  contentPackage: element.ContentPackage);
36  }
37  foreach (var item in Item.ItemList)
38  {
39  var fabricator = item.GetComponent<Fabricator>();
40  if (fabricator != null)
41  {
42  fabricator.OnItemFabricated += OnItemFabricated;
43  }
44  }
45  }
46 
47  public void OnItemFabricated(Item item, Character character)
48  {
49  if (item == null) { return; }
50  if (!CharacterTag.IsEmpty)
51  {
52  if (!ParentEvent.GetTargets(CharacterTag).Contains(character)) { return; }
53  }
54  if ((!ItemIdentifier.IsEmpty && item.Prefab.Identifier == ItemIdentifier) ||
55  (!ItemTag.IsEmpty && item.HasTag(ItemTag)))
56  {
57  if (!ApplyTagToItem.IsEmpty)
58  {
60  }
61  counter++;
62  }
63  }
64 
65  public override bool IsFinished(ref string goTo)
66  {
67  return counter >= Amount;
68  }
69 
70  public override void Reset()
71  {
72  counter = 0;
73  }
74 
75  public override string ToDebugString()
76  {
77  return $"{ToolBox.GetDebugSymbol(counter >= Amount)} {nameof(WaitForItemFabricatedAction)} -> ({ItemTag}, {counter}/{Amount})";
78  }
79  }
80 }
ContentPackage? ContentPackage
readonly ScriptedEvent ParentEvent
Definition: EventAction.cs:106
static readonly List< Item > ItemList
readonly Identifier Identifier
Definition: Prefab.cs:34
void AddTarget(Identifier tag, Entity target)
IEnumerable< Entity > GetTargets(Identifier tag)
Waits for some item(s) to be fabricated before continuing the execution of the event.
override string ToDebugString()
Rich test to display in debugdraw
WaitForItemFabricatedAction(ScriptedEvent parentEvent, ContentXElement element)
void OnItemFabricated(Item item, Character character)
override bool IsFinished(ref string goTo)
Has the action finished.