SpeciesRequirement.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using System.Diagnostics.CodeAnalysis;
  2. using System.Text;
  3. using Content.Shared.Humanoid.Prototypes;
  4. using Content.Shared.Preferences;
  5. using JetBrains.Annotations;
  6. using Robust.Shared.Prototypes;
  7. using Robust.Shared.Serialization;
  8. using Robust.Shared.Utility;
  9. namespace Content.Shared.Roles;
  10. /// <summary>
  11. /// Requires the character to be or not be on the list of specified species
  12. /// </summary>
  13. [UsedImplicitly]
  14. [Serializable, NetSerializable]
  15. public sealed partial class SpeciesRequirement : JobRequirement
  16. {
  17. [DataField(required: true)]
  18. public HashSet<ProtoId<SpeciesPrototype>> Species = new();
  19. public override bool Check(IEntityManager entManager,
  20. IPrototypeManager protoManager,
  21. HumanoidCharacterProfile? profile,
  22. IReadOnlyDictionary<string, TimeSpan> playTimes,
  23. [NotNullWhen(false)] out FormattedMessage? reason)
  24. {
  25. reason = new FormattedMessage();
  26. if (profile is null) //the profile could be null if the player is a ghost. In this case we don't need to block the role selection for ghostrole
  27. return true;
  28. var sb = new StringBuilder();
  29. sb.Append("[color=yellow]");
  30. foreach (var s in Species)
  31. {
  32. sb.Append(Loc.GetString(protoManager.Index(s).Name) + " ");
  33. }
  34. sb.Append("[/color]");
  35. if (!Inverted)
  36. {
  37. reason = FormattedMessage.FromMarkupPermissive($"{Loc.GetString("role-timer-whitelisted-species")}\n{sb}");
  38. if (!Species.Contains(profile.Species))
  39. return false;
  40. }
  41. else
  42. {
  43. reason = FormattedMessage.FromMarkupPermissive($"{Loc.GetString("role-timer-blacklisted-species")}\n{sb}");
  44. if (Species.Contains(profile.Species))
  45. return false;
  46. }
  47. return true;
  48. }
  49. }