1
0

AddAccentClothingSystem.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using Content.Server.Speech.Components;
  2. using Content.Shared.Clothing;
  3. namespace Content.Server.Speech.EntitySystems;
  4. public sealed class AddAccentClothingSystem : EntitySystem
  5. {
  6. [Dependency] private readonly IComponentFactory _componentFactory = default!;
  7. public override void Initialize()
  8. {
  9. base.Initialize();
  10. SubscribeLocalEvent<AddAccentClothingComponent, ClothingGotEquippedEvent>(OnGotEquipped);
  11. SubscribeLocalEvent<AddAccentClothingComponent, ClothingGotUnequippedEvent>(OnGotUnequipped);
  12. }
  13. // TODO: Turn this into a relay event.
  14. private void OnGotEquipped(EntityUid uid, AddAccentClothingComponent component, ref ClothingGotEquippedEvent args)
  15. {
  16. // does the user already has this accent?
  17. var componentType = _componentFactory.GetRegistration(component.Accent).Type;
  18. if (HasComp(args.Wearer, componentType))
  19. return;
  20. // add accent to the user
  21. var accentComponent = (Component) _componentFactory.GetComponent(componentType);
  22. AddComp(args.Wearer, accentComponent);
  23. // snowflake case for replacement accent
  24. if (accentComponent is ReplacementAccentComponent rep)
  25. rep.Accent = component.ReplacementPrototype!;
  26. component.IsActive = true;
  27. }
  28. private void OnGotUnequipped(EntityUid uid, AddAccentClothingComponent component, ref ClothingGotUnequippedEvent args)
  29. {
  30. if (!component.IsActive)
  31. return;
  32. // try to remove accent
  33. var componentType = _componentFactory.GetRegistration(component.Accent).Type;
  34. if (EntityManager.HasComponent(args.Wearer, componentType))
  35. {
  36. EntityManager.RemoveComponent(args.Wearer, componentType);
  37. }
  38. component.IsActive = false;
  39. }
  40. }