SiliconLawEui.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using Content.Server.Administration.Managers;
  2. using Content.Server.EUI;
  3. using Content.Shared.Administration;
  4. using Content.Shared.Eui;
  5. using Content.Shared.Silicons.Laws;
  6. using Content.Shared.Silicons.Laws.Components;
  7. namespace Content.Server.Silicons.Laws;
  8. public sealed class SiliconLawEui : BaseEui
  9. {
  10. private readonly SiliconLawSystem _siliconLawSystem;
  11. private readonly EntityManager _entityManager;
  12. private readonly IAdminManager _adminManager;
  13. private List<SiliconLaw> _laws = new();
  14. private ISawmill _sawmill = default!;
  15. private EntityUid _target;
  16. public SiliconLawEui(SiliconLawSystem siliconLawSystem, EntityManager entityManager, IAdminManager manager)
  17. {
  18. _siliconLawSystem = siliconLawSystem;
  19. _adminManager = manager;
  20. _entityManager = entityManager;
  21. _sawmill = Logger.GetSawmill("silicon-law-eui");
  22. }
  23. public override EuiStateBase GetNewState()
  24. {
  25. return new SiliconLawsEuiState(_laws, _entityManager.GetNetEntity(_target));
  26. }
  27. public void UpdateLaws(SiliconLawBoundComponent? lawBoundComponent, EntityUid player)
  28. {
  29. if (!IsAllowed())
  30. return;
  31. var laws = _siliconLawSystem.GetLaws(player, lawBoundComponent);
  32. _laws = laws.Laws;
  33. _target = player;
  34. StateDirty();
  35. }
  36. public override void HandleMessage(EuiMessageBase msg)
  37. {
  38. if (msg is not SiliconLawsSaveMessage message)
  39. {
  40. return;
  41. }
  42. if (!IsAllowed())
  43. return;
  44. var player = _entityManager.GetEntity(message.Target);
  45. if (_entityManager.TryGetComponent<SiliconLawProviderComponent>(player, out var playerProviderComp))
  46. _siliconLawSystem.SetLaws(message.Laws, player, playerProviderComp.LawUploadSound);
  47. }
  48. private bool IsAllowed()
  49. {
  50. var adminData = _adminManager.GetAdminData(Player);
  51. if (adminData == null || !adminData.HasFlag(AdminFlags.Moderator))
  52. {
  53. _sawmill.Warning("Player {0} tried to open / use silicon law UI without permission.", Player.UserId);
  54. return false;
  55. }
  56. return true;
  57. }
  58. }