Files
crystall-punk-14/Content.Server/Traits/TraitSystem.cs

58 lines
2.1 KiB
C#
Raw Normal View History

2022-09-10 17:40:06 +02:00
using Content.Server.GameTicking;
2023-05-12 23:11:35 -04:00
using Content.Shared.Hands.Components;
using Content.Shared.Hands.EntitySystems;
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;
using Robust.Shared.Serialization.Manager;
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)
{
foreach (var traitId in args.Profile.TraitPreferences)
{
if (!_prototypeManager.TryIndex<TraitPrototype>(traitId, out var traitPrototype))
{
Log.Warning($"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
2024-06-03 21:47:06 +03:00
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 = EntityManager.SpawnEntity(traitPrototype.TraitGear, coords);
_sharedHandsSystem.TryPickup(args.Mob,
inhandEntity,
checkActionBlocker: false,
handsComp: handsComponent);
2022-09-10 17:40:06 +02:00
}
}
}