JobCondition.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using System.Linq;
  2. using Content.Shared.EntityEffects;
  3. using Content.Shared.Localizations;
  4. using Content.Shared.Mind;
  5. using Content.Shared.Mind.Components;
  6. using Content.Shared.Roles;
  7. using Content.Shared.Roles.Jobs;
  8. using Robust.Shared.Prototypes;
  9. namespace Content.Server.EntityEffects.EffectConditions;
  10. public sealed partial class JobCondition : EntityEffectCondition
  11. {
  12. [DataField(required: true)] public List<ProtoId<JobPrototype>> Job;
  13. public override bool Condition(EntityEffectBaseArgs args)
  14. {
  15. args.EntityManager.TryGetComponent<MindContainerComponent>(args.TargetEntity, out var mindContainer);
  16. if ( mindContainer is null
  17. || !args.EntityManager.TryGetComponent<MindComponent>(mindContainer.Mind, out var mind))
  18. return false;
  19. foreach (var roleId in mind.MindRoles)
  20. {
  21. if(!args.EntityManager.HasComponent<JobRoleComponent>(roleId))
  22. continue;
  23. if (!args.EntityManager.TryGetComponent<MindRoleComponent>(roleId, out var mindRole))
  24. {
  25. Logger.Error($"Encountered job mind role entity {roleId} without a {nameof(MindRoleComponent)}");
  26. continue;
  27. }
  28. if (mindRole.JobPrototype == null)
  29. {
  30. Logger.Error($"Encountered job mind role entity {roleId} without a {nameof(JobPrototype)}");
  31. continue;
  32. }
  33. if (Job.Contains(mindRole.JobPrototype.Value))
  34. return true;
  35. }
  36. return false;
  37. }
  38. public override string GuidebookExplanation(IPrototypeManager prototype)
  39. {
  40. var localizedNames = Job.Select(jobId => prototype.Index(jobId).LocalizedName).ToList();
  41. return Loc.GetString("reagent-effect-condition-guidebook-job-condition", ("job", ContentLocalizationManager.FormatListToOr(localizedNames)));
  42. }
  43. }