1
0

TraitSystem.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using Content.Shared.GameTicking;
  2. using Content.Shared.Hands.Components;
  3. using Content.Shared.Hands.EntitySystems;
  4. using Content.Shared.Roles;
  5. using Content.Shared.Traits;
  6. using Content.Shared.Whitelist;
  7. using Robust.Shared.Prototypes;
  8. namespace Content.Server.Traits;
  9. public sealed class TraitSystem : EntitySystem
  10. {
  11. [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
  12. [Dependency] private readonly SharedHandsSystem _sharedHandsSystem = default!;
  13. [Dependency] private readonly EntityWhitelistSystem _whitelistSystem = default!;
  14. public override void Initialize()
  15. {
  16. base.Initialize();
  17. SubscribeLocalEvent<PlayerSpawnCompleteEvent>(OnPlayerSpawnComplete);
  18. }
  19. // When the player is spawned in, add all trait components selected during character creation
  20. private void OnPlayerSpawnComplete(PlayerSpawnCompleteEvent args)
  21. {
  22. // Check if player's job allows to apply traits
  23. if (args.JobId == null ||
  24. !_prototypeManager.TryIndex<JobPrototype>(args.JobId ?? string.Empty, out var protoJob) ||
  25. !protoJob.ApplyTraits)
  26. {
  27. return;
  28. }
  29. foreach (var traitId in args.Profile.TraitPreferences)
  30. {
  31. if (!_prototypeManager.TryIndex<TraitPrototype>(traitId, out var traitPrototype))
  32. {
  33. Log.Warning($"No trait found with ID {traitId}!");
  34. return;
  35. }
  36. if (_whitelistSystem.IsWhitelistFail(traitPrototype.Whitelist, args.Mob) ||
  37. _whitelistSystem.IsBlacklistPass(traitPrototype.Blacklist, args.Mob))
  38. continue;
  39. // Add all components required by the prototype
  40. EntityManager.AddComponents(args.Mob, traitPrototype.Components, false);
  41. // Add item required by the trait
  42. if (traitPrototype.TraitGear == null)
  43. continue;
  44. if (!TryComp(args.Mob, out HandsComponent? handsComponent))
  45. continue;
  46. var coords = Transform(args.Mob).Coordinates;
  47. var inhandEntity = EntityManager.SpawnEntity(traitPrototype.TraitGear, coords);
  48. _sharedHandsSystem.TryPickup(args.Mob,
  49. inhandEntity,
  50. checkActionBlocker: false,
  51. handsComp: handsComponent);
  52. }
  53. }
  54. }