Files

70 lines
2.4 KiB
C#
Raw Permalink Normal View History

using Content.Server.Actions;
2022-09-10 17:40:06 +02:00
using Content.Server.GameTicking;
using Content.Shared.Actions;
2024-11-19 21:16:49 -08:00
using Content.Shared.GameTicking;
2023-05-12 23:11:35 -04:00
using Content.Shared.Hands.Components;
using Content.Shared.Hands.EntitySystems;
using Content.Shared.Roles;
2022-09-10 17:40:06 +02:00
using Content.Shared.Traits;
using Content.Shared.Whitelist;
2022-09-10 17:40:06 +02:00
using Robust.Shared.Prototypes;
namespace Content.Server.Traits;
public sealed class TraitSystem : EntitySystem
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
2023-05-12 23:11:35 -04:00
[Dependency] private readonly SharedHandsSystem _sharedHandsSystem = default!;
[Dependency] private readonly EntityWhitelistSystem _whitelistSystem = default!;
2022-09-10 17:40:06 +02:00
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 ||
!_prototypeManager.Resolve<JobPrototype>(args.JobId, out var protoJob) ||
!protoJob.ApplyTraits)
{
return;
}
2022-09-10 17:40:06 +02:00
foreach (var traitId in args.Profile.TraitPreferences)
{
if (!_prototypeManager.TryIndex<TraitPrototype>(traitId, out var traitPrototype))
{
Log.Error($"No trait found with ID {traitId}!");
2022-09-10 17:40:06 +02:00
return;
}
if (_whitelistSystem.IsWhitelistFail(traitPrototype.Whitelist, args.Mob) ||
_whitelistSystem.IsBlacklistPass(traitPrototype.Blacklist, args.Mob))
continue;
2022-09-10 17:40:06 +02:00
// Add all components required by the prototype
if (traitPrototype.Components.Count > 0) //CP14 added check
EntityManager.AddComponents(args.Mob, traitPrototype.Components, false);
2023-05-12 23:11:35 -04:00
// Add item required by the trait
2024-06-03 21:47:06 +03:00
if (traitPrototype.TraitGear == null)
continue;
2023-05-12 23:11:35 -04:00
2024-06-03 21:47:06 +03:00
if (!TryComp(args.Mob, out HandsComponent? handsComponent))
continue;
var coords = Transform(args.Mob).Coordinates;
var inhandEntity = Spawn(traitPrototype.TraitGear, coords);
2024-06-03 21:47:06 +03:00
_sharedHandsSystem.TryPickup(args.Mob,
inhandEntity,
checkActionBlocker: false,
handsComp: handsComponent);
2022-09-10 17:40:06 +02:00
}
}
}