AntagLoadProfileRuleSystem.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using Content.Server.Antag;
  2. using Content.Server.GameTicking.Rules.Components;
  3. using Content.Server.Humanoid;
  4. using Content.Server.Preferences.Managers;
  5. using Content.Shared.Humanoid;
  6. using Content.Shared.Humanoid.Prototypes;
  7. using Content.Shared.Preferences;
  8. using Robust.Shared.Prototypes;
  9. namespace Content.Server.GameTicking.Rules;
  10. public sealed class AntagLoadProfileRuleSystem : GameRuleSystem<AntagLoadProfileRuleComponent>
  11. {
  12. [Dependency] private readonly HumanoidAppearanceSystem _humanoid = default!;
  13. [Dependency] private readonly IPrototypeManager _proto = default!;
  14. [Dependency] private readonly IServerPreferencesManager _prefs = default!;
  15. public override void Initialize()
  16. {
  17. base.Initialize();
  18. SubscribeLocalEvent<AntagLoadProfileRuleComponent, AntagSelectEntityEvent>(OnSelectEntity);
  19. }
  20. private void OnSelectEntity(Entity<AntagLoadProfileRuleComponent> ent, ref AntagSelectEntityEvent args)
  21. {
  22. if (args.Handled)
  23. return;
  24. var profile = args.Session != null
  25. ? _prefs.GetPreferences(args.Session.UserId).SelectedCharacter as HumanoidCharacterProfile
  26. : HumanoidCharacterProfile.RandomWithSpecies();
  27. if (profile?.Species is not { } speciesId || !_proto.TryIndex(speciesId, out var species))
  28. {
  29. species = _proto.Index<SpeciesPrototype>(SharedHumanoidAppearanceSystem.DefaultSpecies);
  30. }
  31. if (ent.Comp.SpeciesOverride != null
  32. && (ent.Comp.SpeciesOverrideBlacklist?.Contains(new ProtoId<SpeciesPrototype>(species.ID)) ?? false))
  33. {
  34. species = _proto.Index(ent.Comp.SpeciesOverride.Value);
  35. }
  36. args.Entity = Spawn(species.Prototype);
  37. _humanoid.LoadProfile(args.Entity.Value, profile?.WithSpecies(species.ID));
  38. }
  39. }