using Content.Server.GameTicking; using Content.Server.GameTicking.Rules.Components; using Content.Server._White.AspectsSystem.Aspects.Components; using Content.Server._White.AspectsSystem.Base; using Content.Server.GameTicking.Components; using Content.Server.Holiday.Christmas; using Content.Shared.Hands.Components; using Content.Shared.Hands.EntitySystems; using Content.Shared.Humanoid; namespace Content.Server._White.AspectsSystem.Aspects; public sealed class RandomItemAspect : AspectSystem { [Dependency] private readonly SharedHandsSystem _handsSystem = default!; [Dependency] private readonly RandomGiftSystem _giftSystem = default!; [Dependency] private readonly ChatHelper _chatHelper = default!; public override void Initialize() { base.Initialize(); SubscribeLocalEvent(HandleLateJoin); } protected override void Started(EntityUid uid, RandomItemAspectComponent component, GameRuleComponent gameRule, GameRuleStartedEvent args) { base.Started(uid, component, gameRule, args); var query = EntityQueryEnumerator(); while (query.MoveNext(out var ent, out _)) { GiveItem(ent, component); } } private void HandleLateJoin(PlayerSpawnCompleteEvent ev) { var query = EntityQueryEnumerator(); while (query.MoveNext(out var ruleEntity, out var component, out var gameRule)) { if (!GameTicker.IsGameRuleAdded(ruleEntity, gameRule)) continue; if (!ev.LateJoin) return; var mob = ev.Mob; GiveItem(mob, component); _chatHelper.SendAspectDescription(mob, Loc.GetString("random-item-aspect-desc")); } } #region Helpers private void GiveItem(EntityUid player, RandomItemAspectComponent component) { component.Item ??= _giftSystem.PickRandomItem(); if (component.Item == null) return; var transform = CompOrNull(player); if (transform == null) return; if (!HasComp(player)) return; var weaponEntity = EntityManager.SpawnEntity(component.Item, transform.Coordinates); _handsSystem.PickupOrDrop(player, weaponEntity); } #endregion }