1
0

MeleeSpeechSystem.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using Content.Server.Administration.Logs;
  2. using Content.Shared.Actions;
  3. using Content.Shared.Database;
  4. using Content.Shared.Speech.Components;
  5. using Content.Shared.Speech.EntitySystems;
  6. using Robust.Server.GameObjects;
  7. using Robust.Shared.Player;
  8. namespace Content.Server.Speech.EntitySystems;
  9. public sealed class MeleeSpeechSystem : SharedMeleeSpeechSystem
  10. {
  11. [Dependency] private readonly IAdminLogManager _adminLogger = default!;
  12. [Dependency] private readonly SharedActionsSystem _actionSystem = default!;
  13. [Dependency] private readonly UserInterfaceSystem _uiSystem = default!;
  14. public override void Initialize()
  15. {
  16. base.Initialize();
  17. SubscribeLocalEvent<MeleeSpeechComponent, MeleeSpeechBattlecryChangedMessage>(OnBattlecryChanged);
  18. SubscribeLocalEvent<MeleeSpeechComponent, MeleeSpeechConfigureActionEvent>(OnConfigureAction);
  19. SubscribeLocalEvent<MeleeSpeechComponent, GetItemActionsEvent>(OnGetActions);
  20. SubscribeLocalEvent<MeleeSpeechComponent, MapInitEvent>(OnComponentMapInit);
  21. }
  22. private void OnComponentMapInit(EntityUid uid, MeleeSpeechComponent component, MapInitEvent args)
  23. {
  24. _actionSystem.AddAction(uid, ref component.ConfigureActionEntity, component.ConfigureAction, uid);
  25. }
  26. private void OnGetActions(EntityUid uid, MeleeSpeechComponent component, GetItemActionsEvent args)
  27. {
  28. args.AddAction(ref component.ConfigureActionEntity, component.ConfigureAction);
  29. }
  30. private void OnBattlecryChanged(EntityUid uid, MeleeSpeechComponent comp, MeleeSpeechBattlecryChangedMessage args)
  31. {
  32. if (!TryComp<MeleeSpeechComponent>(uid, out var meleeSpeechUser))
  33. return;
  34. var battlecry = args.Battlecry;
  35. if (battlecry.Length > comp.MaxBattlecryLength)
  36. battlecry = battlecry[..comp.MaxBattlecryLength];
  37. TryChangeBattlecry(uid, battlecry, meleeSpeechUser);
  38. }
  39. /// <summary>
  40. /// Attempts to open the Battlecry UI.
  41. /// </summary>
  42. private void OnConfigureAction(EntityUid uid, MeleeSpeechComponent comp, MeleeSpeechConfigureActionEvent args)
  43. {
  44. TryOpenUi(args.Performer, uid, comp);
  45. }
  46. public void TryOpenUi(EntityUid user, EntityUid source, MeleeSpeechComponent? component = null)
  47. {
  48. if (!Resolve(source, ref component))
  49. return;
  50. if (!TryComp<ActorComponent>(user, out var actor))
  51. return;
  52. _uiSystem.TryToggleUi(source, MeleeSpeechUiKey.Key, actor.PlayerSession);
  53. }
  54. /// <summary>
  55. /// Attempts to change the battlecry of an entity.
  56. /// Returns true/false.
  57. /// </summary>
  58. /// <remarks>
  59. /// Logs changes to an entity's battlecry
  60. /// </remarks>
  61. public bool TryChangeBattlecry(EntityUid uid, string? battlecry, MeleeSpeechComponent? meleeSpeechComp = null)
  62. {
  63. if (!Resolve(uid, ref meleeSpeechComp))
  64. return false;
  65. if (!string.IsNullOrWhiteSpace(battlecry))
  66. {
  67. battlecry = battlecry.Trim();
  68. }
  69. else
  70. {
  71. battlecry = null;
  72. }
  73. if (meleeSpeechComp.Battlecry == battlecry)
  74. return true;
  75. meleeSpeechComp.Battlecry = battlecry;
  76. Dirty(uid, meleeSpeechComp);
  77. _adminLogger.Add(LogType.ItemConfigure, LogImpact.Medium, $" {ToPrettyString(uid):entity}'s battlecry has been changed to {battlecry}");
  78. return true;
  79. }
  80. }