diff --git a/Content.Shared/Blocking/BlockingUserSystem.cs b/Content.Shared/Blocking/BlockingSystem.User.cs similarity index 68% rename from Content.Shared/Blocking/BlockingUserSystem.cs rename to Content.Shared/Blocking/BlockingSystem.User.cs index 4465674e71..e660e7f3b0 100644 --- a/Content.Shared/Blocking/BlockingUserSystem.cs +++ b/Content.Shared/Blocking/BlockingSystem.User.cs @@ -2,39 +2,33 @@ using Content.Shared.Damage.Prototypes; using Robust.Shared.Audio; using Robust.Shared.Containers; -using Robust.Shared.Prototypes; namespace Content.Shared.Blocking; -public sealed class BlockingUserSystem : EntitySystem +public sealed partial class BlockingSystem { - [Dependency] private readonly IPrototypeManager _proto = default!; - [Dependency] private readonly BlockingSystem _blockingSystem = default!; [Dependency] private readonly DamageableSystem _damageable = default!; [Dependency] private readonly SharedAudioSystem _audio = default!; - public override void Initialize() + private void InitializeUser() { - base.Initialize(); - SubscribeLocalEvent(OnDamageChanged); SubscribeLocalEvent(OnUserDamageModified); SubscribeLocalEvent(OnParentChanged); SubscribeLocalEvent(OnInsertAttempt); SubscribeLocalEvent(OnAnchorChanged); + SubscribeLocalEvent(OnEntityTerminating); } private void OnParentChanged(EntityUid uid, BlockingUserComponent component, ref EntParentChangedMessage args) { - if (TryComp(component.BlockingItem, out var blockComp) && blockComp.IsBlocking) - _blockingSystem.StopBlocking(component.BlockingItem.Value, blockComp, uid); + UserStopBlocking(uid, component); } private void OnInsertAttempt(EntityUid uid, BlockingUserComponent component, ContainerGettingInsertedAttemptEvent args) { - if (TryComp(component.BlockingItem, out var blockComp) && blockComp.IsBlocking) - _blockingSystem.StopBlocking(component.BlockingItem.Value, blockComp, uid); + UserStopBlocking(uid, component); } private void OnAnchorChanged(EntityUid uid, BlockingUserComponent component, ref AnchorStateChangedEvent args) @@ -42,8 +36,7 @@ public sealed class BlockingUserSystem : EntitySystem if (args.Anchored) return; - if (TryComp(component.BlockingItem, out var blockComp) && blockComp.IsBlocking) - _blockingSystem.StopBlocking(component.BlockingItem.Value, blockComp, uid); + UserStopBlocking(uid, component); } private void OnDamageChanged(EntityUid uid, BlockingUserComponent component, DamageChangedEvent args) @@ -66,4 +59,25 @@ public sealed class BlockingUserSystem : EntitySystem } } } + + private void OnEntityTerminating(EntityUid uid, BlockingUserComponent component, ref EntityTerminatingEvent args) + { + if (!TryComp(component.BlockingItem, out var blockingComponent)) + return; + + StopBlockingHelper(component.BlockingItem.Value, blockingComponent, uid); + + } + + /// + /// Check for the shield and has the user stop blocking + /// Used where you'd like the user to stop blocking, but also don't want to remove the + /// + /// The user blocking + /// The + private void UserStopBlocking(EntityUid uid, BlockingUserComponent component) + { + if (TryComp(component.BlockingItem, out var blockComp) && blockComp.IsBlocking) + StopBlocking(component.BlockingItem.Value, blockComp, uid); + } } diff --git a/Content.Shared/Blocking/BlockingSystem.cs b/Content.Shared/Blocking/BlockingSystem.cs index 0387b5e7ea..fb392ebc6e 100644 --- a/Content.Shared/Blocking/BlockingSystem.cs +++ b/Content.Shared/Blocking/BlockingSystem.cs @@ -1,17 +1,17 @@ -using Content.Shared.Actions; +using System.Linq; +using Content.Shared.Actions; using Content.Shared.Actions.ActionTypes; -using Content.Shared.Buckle; using Content.Shared.Doors.Components; using Content.Shared.Hands; +using Content.Shared.Hands.Components; using Content.Shared.Hands.EntitySystems; using Content.Shared.IdentityManagement; +using Content.Shared.Interaction.Events; using Content.Shared.Maps; using Content.Shared.MobState.Components; using Content.Shared.Physics; using Content.Shared.Popups; using Content.Shared.Toggleable; -using Robust.Shared.Containers; -using Robust.Shared.Map; using Robust.Shared.Physics; using Robust.Shared.Physics.Components; using Robust.Shared.Physics.Dynamics; @@ -21,26 +21,25 @@ using Robust.Shared.Prototypes; namespace Content.Shared.Blocking; -public sealed class BlockingSystem : EntitySystem +public sealed partial class BlockingSystem : EntitySystem { - [Dependency] private readonly IMapManager _mapManager = default!; [Dependency] private readonly IPrototypeManager _proto = default!; [Dependency] private readonly SharedActionsSystem _actionsSystem = default!; [Dependency] private readonly SharedTransformSystem _transformSystem = default!; [Dependency] private readonly FixtureSystem _fixtureSystem = default!; [Dependency] private readonly SharedHandsSystem _handsSystem = default!; [Dependency] private readonly SharedPopupSystem _popupSystem = default!; - [Dependency] private readonly SharedContainerSystem _containerSystem = default!; [Dependency] private readonly EntityLookupSystem _lookup = default!; [Dependency] private readonly SharedPhysicsSystem _physics = default!; - [Dependency] private readonly SharedBuckleSystem _buckleSystem = default!; public override void Initialize() { base.Initialize(); + InitializeUser(); SubscribeLocalEvent(OnEquip); SubscribeLocalEvent(OnUnequip); + SubscribeLocalEvent(OnDrop); SubscribeLocalEvent(OnGetActions); SubscribeLocalEvent(OnToggleAction); @@ -53,8 +52,7 @@ public sealed class BlockingSystem : EntitySystem component.User = args.User; //To make sure that this bodytype doesn't get set as anything but the original - if (TryComp(args.User, out var physicsComponent) && physicsComponent.BodyType != BodyType.Static - && !TryComp(args.User, out var blockingUserComponent)) + if (TryComp(args.User, out var physicsComponent) && physicsComponent.BodyType != BodyType.Static && !HasComp(args.User)) { var userComp = EnsureComp(args.User); userComp.BlockingItem = uid; @@ -64,16 +62,18 @@ public sealed class BlockingSystem : EntitySystem private void OnUnequip(EntityUid uid, BlockingComponent component, GotUnequippedHandEvent args) { - BlockingShutdownHelper(uid, component, args.User); + StopBlockingHelper(uid, component, args.User); + } + + private void OnDrop(EntityUid uid, BlockingComponent component, DroppedEvent args) + { + StopBlockingHelper(uid, component, args.User); } private void OnGetActions(EntityUid uid, BlockingComponent component, GetItemActionsEvent args) { - if (component.BlockingToggleAction == null - && _proto.TryIndex(component.BlockingToggleActionId, out InstantActionPrototype? act)) - { + if (component.BlockingToggleAction == null && _proto.TryIndex(component.BlockingToggleActionId, out InstantActionPrototype? act)) component.BlockingToggleAction = new(act); - } if (component.BlockingToggleAction != null) args.Actions.Add(component.BlockingToggleAction); @@ -84,11 +84,20 @@ public sealed class BlockingSystem : EntitySystem if(args.Handled) return; - foreach (var shield in _handsSystem.EnumerateHeld(args.Performer)) + var blockQuery = GetEntityQuery(); + var handQuery = GetEntityQuery(); + + if (!handQuery.TryGetComponent(args.Performer, out var hands)) + return; + + var shields = _handsSystem.EnumerateHeld(args.Performer, hands).ToArray(); + + foreach (var shield in shields) { if (shield == uid) continue; - if (TryComp(shield, out var otherBlockComp) && otherBlockComp.IsBlocking) + + if (blockQuery.TryGetComponent(shield, out var otherBlockComp) && otherBlockComp.IsBlocking) { CantBlockError(args.Performer); return; @@ -109,7 +118,7 @@ public sealed class BlockingSystem : EntitySystem if (component.User != null) { _actionsSystem.RemoveProvidedActions(component.User.Value, uid); - BlockingShutdownHelper(uid, component, component.User.Value); + StopBlockingHelper(uid, component, component.User.Value); } } @@ -213,7 +222,8 @@ public sealed class BlockingSystem : EntitySystem /// public bool StopBlocking(EntityUid item, BlockingComponent component, EntityUid user) { - if (!component.IsBlocking) return false; + if (!component.IsBlocking) + return false; var xform = Transform(user); @@ -251,14 +261,22 @@ public sealed class BlockingSystem : EntitySystem /// The item the component is attached to /// The /// The person holding the blocking item - private void BlockingShutdownHelper(EntityUid uid, BlockingComponent component, EntityUid user) + private void StopBlockingHelper(EntityUid uid, BlockingComponent component, EntityUid user) { if (component.IsBlocking) StopBlocking(uid, component, user); - foreach (var shield in _handsSystem.EnumerateHeld(user)) + var userQuery = GetEntityQuery(); + var handQuery = GetEntityQuery(); + + if (!handQuery.TryGetComponent(user, out var hands)) + return; + + var shields = _handsSystem.EnumerateHeld(user, hands).ToArray(); + + foreach (var shield in shields) { - if (HasComp(shield) && TryComp(user, out var blockingUserComponent)) + if (HasComp(shield) && userQuery.TryGetComponent(user, out var blockingUserComponent)) { blockingUserComponent.BlockingItem = shield; return;