ComponentWireAction.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using Content.Shared.Wires;
  2. namespace Content.Server.Wires;
  3. /// <summary>
  4. /// convenience class for wires that depend on the existence of some component to function. Slightly reduces boilerplate.
  5. /// </summary>
  6. public abstract partial class ComponentWireAction<TComponent> : BaseWireAction where TComponent : Component
  7. {
  8. public abstract StatusLightState? GetLightState(Wire wire, TComponent component);
  9. public override StatusLightState? GetLightState(Wire wire)
  10. {
  11. return EntityManager.TryGetComponent(wire.Owner, out TComponent? component)
  12. ? GetLightState(wire, component)
  13. : StatusLightState.Off;
  14. }
  15. public abstract bool Cut(EntityUid user, Wire wire, TComponent component);
  16. public abstract bool Mend(EntityUid user, Wire wire, TComponent component);
  17. public abstract void Pulse(EntityUid user, Wire wire, TComponent component);
  18. public override bool Cut(EntityUid user, Wire wire)
  19. {
  20. base.Cut(user, wire);
  21. // if the entity doesn't exist, we need to return true otherwise the wire sprite is never updated
  22. return EntityManager.TryGetComponent(wire.Owner, out TComponent? component) ? Cut(user, wire, component) : true;
  23. }
  24. public override bool Mend(EntityUid user, Wire wire)
  25. {
  26. base.Mend(user, wire);
  27. return EntityManager.TryGetComponent(wire.Owner, out TComponent? component) ? Mend(user, wire, component) : true;
  28. }
  29. public override void Pulse(EntityUid user, Wire wire)
  30. {
  31. base.Pulse(user, wire);
  32. if (EntityManager.TryGetComponent(wire.Owner, out TComponent? component))
  33. Pulse(user, wire, component);
  34. }
  35. }