CatStation/Content.Server/Traits/TraitSystem.cs
Whatstone 07f97fffa6
Cleanup: Resolve/suppress many obsolete warnings, delete EntityPrototypeHelpers. (#46141)
* Resolve warnings, mostly obsolete

* resolve via the entitymanager directly, not wref

* Ent<T>, why not

* minimize test diff

* StorageTest: consistent print order

* ChameleonProj: use Disguised value consistently

* GasArrayTest: TryComp

* OrganPickingMarker: cache ILocalizationManager

avoids warning from Loc.TryGetString, and avoids multiple IoCManager hits

* FormattedMessage.FromMarkup to FromMarkupOrThrow

* rerun tests 🙏
2026-10-05 01:58:03 +00:00

72 lines
2.5 KiB
C#

using Content.Shared.GameTicking;
using Content.Shared.Hands.Components;
using Content.Shared.Hands.EntitySystems;
using Content.Shared.Roles;
using Content.Shared.Traits;
using Content.Shared.Whitelist;
namespace Content.Server.Traits;
public sealed partial class TraitSystem : EntitySystem
{
[Dependency] private SharedHandsSystem _sharedHandsSystem = default!;
[Dependency] private EntityWhitelistSystem _whitelistSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<PlayerSpawnCompleteEvent>(OnPlayerSpawnComplete);
}
// When the player is spawned in, add all trait components selected during character creation
private void OnPlayerSpawnComplete(PlayerSpawnCompleteEvent args)
{
// Check if player's job allows to apply traits
if (args.JobId == null ||
!ProtoMan.Resolve<JobPrototype>(args.JobId, out var protoJob) ||
!protoJob.ApplyTraits)
{
return;
}
foreach (var traitId in args.Profile.TraitPreferences)
{
if (!ProtoMan.TryIndex<TraitPrototype>(traitId, out var traitPrototype))
{
Log.Error($"No trait found with ID {traitId}!");
return;
}
if (_whitelistSystem.IsWhitelistFail(traitPrototype.Whitelist, args.Mob) ||
_whitelistSystem.IsWhitelistPass(traitPrototype.Blacklist, args.Mob))
continue;
// Add all components required by the prototype
#pragma warning disable CS0618 // Enabling compatibility behaviour for TraitPrototype.Components
if (traitPrototype.Components.Count > 0)
EntityManager.AddComponents(args.Mob, traitPrototype.Components, false);
#pragma warning restore CS0618
// Add all JobSpecials required by the prototype
foreach (var special in traitPrototype.Specials)
{
special.AfterEquip(args.Mob);
}
// Add item required by the trait
if (traitPrototype.TraitGear == null)
continue;
if (!TryComp(args.Mob, out HandsComponent? handsComponent))
continue;
var coords = Transform(args.Mob).Coordinates;
var inhandEntity = Spawn(traitPrototype.TraitGear, coords);
_sharedHandsSystem.TryPickup(args.Mob,
inhandEntity,
checkActionBlocker: false,
handsComp: handsComponent);
}
}
}