using Content.Shared.Clothing.Components; using Content.Shared.Inventory.Events; using Content.Shared.NPC.Components; using Content.Shared.NPC.Systems; using Robust.Shared.Player; // Frontier - Dont edit AI factions using Content.Shared.Inventory; // Frontier using Content.Shared.NPC.Prototypes; // Frontier using Robust.Shared.Prototypes; // Frontier using Content.Shared.Mind.Components; // Frontier namespace Content.Shared.Clothing.EntitySystems; /// /// Handles faction adding and removal. /// public sealed class FactionClothingSystem : EntitySystem { [Dependency] private readonly NpcFactionSystem _faction = default!; [Dependency] private readonly InventorySystem _inventory = default!; // Frontier public override void Initialize() { base.Initialize(); SubscribeLocalEvent(OnEquipped); SubscribeLocalEvent(OnUnequipped); SubscribeLocalEvent(OnPlayerAttached); // Frontier SubscribeLocalEvent(OnPlayerDetached); // Frontier } // Frontier: rewritten from scratch private void OnEquipped(Entity ent, ref GotEquippedEvent args) { var alreadyMember = CheckEntityEquipmentForFaction(args.Equipee, ent.Comp.Faction, args.Equipment); if (alreadyMember is null) { TryComp(args.Equipee, out var factionComp); var faction = (args.Equipee, factionComp); ent.Comp.AlreadyMember = _faction.IsMember(faction, ent.Comp.Faction); // Do not edit factions on AI controlled mobs if (!HasComp(args.Equipee)) return; if (!ent.Comp.AlreadyMember) _faction.AddFaction(faction, ent.Comp.Faction); } else { ent.Comp.AlreadyMember = alreadyMember.Value; } } private void OnUnequipped(Entity ent, ref GotUnequippedEvent args) { // Reset the component, should be false when unworn. if (ent.Comp.AlreadyMember) { ent.Comp.AlreadyMember = false; return; } // Do not edit factions on AI controlled mobs if (!HasComp(args.Equipee)) return; var alreadyMember = CheckEntityEquipmentForFaction(args.Equipee, ent.Comp.Faction, args.Equipment); if (alreadyMember is null) { _faction.RemoveFaction(args.Equipee, ent.Comp.Faction); } } public bool? CheckEntityEquipmentForFaction(EntityUid ent, ProtoId prototype, EntityUid? skipEnt = null) { var enumerator = _inventory.GetSlotEnumerator(ent); while (enumerator.NextItem(out var item)) { if (!TryComp(item, out var faction)) continue; if (faction.Faction == prototype && item != skipEnt) return faction.AlreadyMember; } return null; } private void OnPlayerAttached(Entity ent, ref PlayerAttachedEvent args) { // Iterate through all items, add factions for any items found where AlreadyMember is false List> factions = new(); var enumerator = _inventory.GetSlotEnumerator(ent.Owner); while (enumerator.NextItem(out var item)) { if (!TryComp(item, out var faction)) continue; if (!faction.AlreadyMember && !factions.Contains(faction.Faction)) { _faction.AddFaction((ent.Owner, ent.Comp), faction.Faction); factions.Add(faction.Faction); } } } private void OnPlayerDetached(Entity ent, ref PlayerDetachedEvent args) { // Iterate through all items, remove factions for any items found where AlreadyMember is true List> factions = new(); var enumerator = _inventory.GetSlotEnumerator(ent.Owner); while (enumerator.NextItem(out var item)) { if (!TryComp(item, out var faction)) continue; if (!faction.AlreadyMember && !factions.Contains(faction.Faction)) { _faction.RemoveFaction((ent.Owner, ent.Comp), faction.Faction); factions.Add(faction.Faction); } } } // End Frontier }