ActiveHandComponentPrecondition.cs 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. using Content.Shared.Hands.Components;
  2. using Robust.Shared.Prototypes;
  3. namespace Content.Server.NPC.HTN.Preconditions;
  4. /// <summary>
  5. /// Returns true if the active hand entity has the specified components.
  6. /// </summary>
  7. public sealed partial class ActiveHandComponentPrecondition : HTNPrecondition
  8. {
  9. [Dependency] private readonly IEntityManager _entManager = default!;
  10. [DataField("invert")]
  11. public bool Invert;
  12. [DataField("components", required: true)]
  13. public ComponentRegistry Components = new();
  14. public override bool IsMet(NPCBlackboard blackboard)
  15. {
  16. if (!blackboard.TryGetValue<Hand>(NPCBlackboard.ActiveHand, out var hand, _entManager) || hand.HeldEntity == null)
  17. {
  18. return Invert;
  19. }
  20. foreach (var comp in Components)
  21. {
  22. var hasComp = _entManager.HasComponent(hand.HeldEntity, comp.Value.Component.GetType());
  23. if (!hasComp ||
  24. Invert && hasComp)
  25. {
  26. return false;
  27. }
  28. }
  29. return true;
  30. }
  31. }