Refactors blocking system, fixes exception (#12947)

This commit is contained in:
keronshb
2022-12-09 22:42:38 -05:00
committed by GitHub
parent a8f9d1a79f
commit 153fcd2172
2 changed files with 67 additions and 35 deletions

View File

@@ -2,39 +2,33 @@
using Content.Shared.Damage.Prototypes; using Content.Shared.Damage.Prototypes;
using Robust.Shared.Audio; using Robust.Shared.Audio;
using Robust.Shared.Containers; using Robust.Shared.Containers;
using Robust.Shared.Prototypes;
namespace Content.Shared.Blocking; 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 DamageableSystem _damageable = default!;
[Dependency] private readonly SharedAudioSystem _audio = default!; [Dependency] private readonly SharedAudioSystem _audio = default!;
public override void Initialize() private void InitializeUser()
{ {
base.Initialize();
SubscribeLocalEvent<BlockingUserComponent, DamageChangedEvent>(OnDamageChanged); SubscribeLocalEvent<BlockingUserComponent, DamageChangedEvent>(OnDamageChanged);
SubscribeLocalEvent<BlockingUserComponent, DamageModifyEvent>(OnUserDamageModified); SubscribeLocalEvent<BlockingUserComponent, DamageModifyEvent>(OnUserDamageModified);
SubscribeLocalEvent<BlockingUserComponent, EntParentChangedMessage>(OnParentChanged); SubscribeLocalEvent<BlockingUserComponent, EntParentChangedMessage>(OnParentChanged);
SubscribeLocalEvent<BlockingUserComponent, ContainerGettingInsertedAttemptEvent>(OnInsertAttempt); SubscribeLocalEvent<BlockingUserComponent, ContainerGettingInsertedAttemptEvent>(OnInsertAttempt);
SubscribeLocalEvent<BlockingUserComponent, AnchorStateChangedEvent>(OnAnchorChanged); SubscribeLocalEvent<BlockingUserComponent, AnchorStateChangedEvent>(OnAnchorChanged);
SubscribeLocalEvent<BlockingUserComponent, EntityTerminatingEvent>(OnEntityTerminating);
} }
private void OnParentChanged(EntityUid uid, BlockingUserComponent component, ref EntParentChangedMessage args) private void OnParentChanged(EntityUid uid, BlockingUserComponent component, ref EntParentChangedMessage args)
{ {
if (TryComp<BlockingComponent>(component.BlockingItem, out var blockComp) && blockComp.IsBlocking) UserStopBlocking(uid, component);
_blockingSystem.StopBlocking(component.BlockingItem.Value, blockComp, uid);
} }
private void OnInsertAttempt(EntityUid uid, BlockingUserComponent component, ContainerGettingInsertedAttemptEvent args) private void OnInsertAttempt(EntityUid uid, BlockingUserComponent component, ContainerGettingInsertedAttemptEvent args)
{ {
if (TryComp<BlockingComponent>(component.BlockingItem, out var blockComp) && blockComp.IsBlocking) UserStopBlocking(uid, component);
_blockingSystem.StopBlocking(component.BlockingItem.Value, blockComp, uid);
} }
private void OnAnchorChanged(EntityUid uid, BlockingUserComponent component, ref AnchorStateChangedEvent args) private void OnAnchorChanged(EntityUid uid, BlockingUserComponent component, ref AnchorStateChangedEvent args)
@@ -42,8 +36,7 @@ public sealed class BlockingUserSystem : EntitySystem
if (args.Anchored) if (args.Anchored)
return; return;
if (TryComp<BlockingComponent>(component.BlockingItem, out var blockComp) && blockComp.IsBlocking) UserStopBlocking(uid, component);
_blockingSystem.StopBlocking(component.BlockingItem.Value, blockComp, uid);
} }
private void OnDamageChanged(EntityUid uid, BlockingUserComponent component, DamageChangedEvent args) 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<BlockingComponent>(component.BlockingItem, out var blockingComponent))
return;
StopBlockingHelper(component.BlockingItem.Value, blockingComponent, uid);
}
/// <summary>
/// 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 <see cref="BlockingUserComponent"/>
/// </summary>
/// <param name="uid">The user blocking</param>
/// <param name="component">The <see cref="BlockingUserComponent"/></param>
private void UserStopBlocking(EntityUid uid, BlockingUserComponent component)
{
if (TryComp<BlockingComponent>(component.BlockingItem, out var blockComp) && blockComp.IsBlocking)
StopBlocking(component.BlockingItem.Value, blockComp, uid);
}
} }

View File

@@ -1,17 +1,17 @@
using Content.Shared.Actions; using System.Linq;
using Content.Shared.Actions;
using Content.Shared.Actions.ActionTypes; using Content.Shared.Actions.ActionTypes;
using Content.Shared.Buckle;
using Content.Shared.Doors.Components; using Content.Shared.Doors.Components;
using Content.Shared.Hands; using Content.Shared.Hands;
using Content.Shared.Hands.Components;
using Content.Shared.Hands.EntitySystems; using Content.Shared.Hands.EntitySystems;
using Content.Shared.IdentityManagement; using Content.Shared.IdentityManagement;
using Content.Shared.Interaction.Events;
using Content.Shared.Maps; using Content.Shared.Maps;
using Content.Shared.MobState.Components; using Content.Shared.MobState.Components;
using Content.Shared.Physics; using Content.Shared.Physics;
using Content.Shared.Popups; using Content.Shared.Popups;
using Content.Shared.Toggleable; using Content.Shared.Toggleable;
using Robust.Shared.Containers;
using Robust.Shared.Map;
using Robust.Shared.Physics; using Robust.Shared.Physics;
using Robust.Shared.Physics.Components; using Robust.Shared.Physics.Components;
using Robust.Shared.Physics.Dynamics; using Robust.Shared.Physics.Dynamics;
@@ -21,26 +21,25 @@ using Robust.Shared.Prototypes;
namespace Content.Shared.Blocking; 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 IPrototypeManager _proto = default!;
[Dependency] private readonly SharedActionsSystem _actionsSystem = default!; [Dependency] private readonly SharedActionsSystem _actionsSystem = default!;
[Dependency] private readonly SharedTransformSystem _transformSystem = default!; [Dependency] private readonly SharedTransformSystem _transformSystem = default!;
[Dependency] private readonly FixtureSystem _fixtureSystem = default!; [Dependency] private readonly FixtureSystem _fixtureSystem = default!;
[Dependency] private readonly SharedHandsSystem _handsSystem = default!; [Dependency] private readonly SharedHandsSystem _handsSystem = default!;
[Dependency] private readonly SharedPopupSystem _popupSystem = default!; [Dependency] private readonly SharedPopupSystem _popupSystem = default!;
[Dependency] private readonly SharedContainerSystem _containerSystem = default!;
[Dependency] private readonly EntityLookupSystem _lookup = default!; [Dependency] private readonly EntityLookupSystem _lookup = default!;
[Dependency] private readonly SharedPhysicsSystem _physics = default!; [Dependency] private readonly SharedPhysicsSystem _physics = default!;
[Dependency] private readonly SharedBuckleSystem _buckleSystem = default!;
public override void Initialize() public override void Initialize()
{ {
base.Initialize(); base.Initialize();
InitializeUser();
SubscribeLocalEvent<BlockingComponent, GotEquippedHandEvent>(OnEquip); SubscribeLocalEvent<BlockingComponent, GotEquippedHandEvent>(OnEquip);
SubscribeLocalEvent<BlockingComponent, GotUnequippedHandEvent>(OnUnequip); SubscribeLocalEvent<BlockingComponent, GotUnequippedHandEvent>(OnUnequip);
SubscribeLocalEvent<BlockingComponent, DroppedEvent>(OnDrop);
SubscribeLocalEvent<BlockingComponent, GetItemActionsEvent>(OnGetActions); SubscribeLocalEvent<BlockingComponent, GetItemActionsEvent>(OnGetActions);
SubscribeLocalEvent<BlockingComponent, ToggleActionEvent>(OnToggleAction); SubscribeLocalEvent<BlockingComponent, ToggleActionEvent>(OnToggleAction);
@@ -53,8 +52,7 @@ public sealed class BlockingSystem : EntitySystem
component.User = args.User; component.User = args.User;
//To make sure that this bodytype doesn't get set as anything but the original //To make sure that this bodytype doesn't get set as anything but the original
if (TryComp<PhysicsComponent>(args.User, out var physicsComponent) && physicsComponent.BodyType != BodyType.Static if (TryComp<PhysicsComponent>(args.User, out var physicsComponent) && physicsComponent.BodyType != BodyType.Static && !HasComp<BlockingUserComponent>(args.User))
&& !TryComp<BlockingUserComponent>(args.User, out var blockingUserComponent))
{ {
var userComp = EnsureComp<BlockingUserComponent>(args.User); var userComp = EnsureComp<BlockingUserComponent>(args.User);
userComp.BlockingItem = uid; userComp.BlockingItem = uid;
@@ -64,16 +62,18 @@ public sealed class BlockingSystem : EntitySystem
private void OnUnequip(EntityUid uid, BlockingComponent component, GotUnequippedHandEvent args) 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) private void OnGetActions(EntityUid uid, BlockingComponent component, GetItemActionsEvent args)
{ {
if (component.BlockingToggleAction == null if (component.BlockingToggleAction == null && _proto.TryIndex(component.BlockingToggleActionId, out InstantActionPrototype? act))
&& _proto.TryIndex(component.BlockingToggleActionId, out InstantActionPrototype? act))
{
component.BlockingToggleAction = new(act); component.BlockingToggleAction = new(act);
}
if (component.BlockingToggleAction != null) if (component.BlockingToggleAction != null)
args.Actions.Add(component.BlockingToggleAction); args.Actions.Add(component.BlockingToggleAction);
@@ -84,11 +84,20 @@ public sealed class BlockingSystem : EntitySystem
if(args.Handled) if(args.Handled)
return; return;
foreach (var shield in _handsSystem.EnumerateHeld(args.Performer)) var blockQuery = GetEntityQuery<BlockingComponent>();
var handQuery = GetEntityQuery<SharedHandsComponent>();
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) if (shield == uid)
continue; continue;
if (TryComp<BlockingComponent>(shield, out var otherBlockComp) && otherBlockComp.IsBlocking)
if (blockQuery.TryGetComponent(shield, out var otherBlockComp) && otherBlockComp.IsBlocking)
{ {
CantBlockError(args.Performer); CantBlockError(args.Performer);
return; return;
@@ -109,7 +118,7 @@ public sealed class BlockingSystem : EntitySystem
if (component.User != null) if (component.User != null)
{ {
_actionsSystem.RemoveProvidedActions(component.User.Value, uid); _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
/// <returns></returns> /// <returns></returns>
public bool StopBlocking(EntityUid item, BlockingComponent component, EntityUid user) public bool StopBlocking(EntityUid item, BlockingComponent component, EntityUid user)
{ {
if (!component.IsBlocking) return false; if (!component.IsBlocking)
return false;
var xform = Transform(user); var xform = Transform(user);
@@ -251,14 +261,22 @@ public sealed class BlockingSystem : EntitySystem
/// <param name="uid"> The item the component is attached to</param> /// <param name="uid"> The item the component is attached to</param>
/// <param name="component"> The <see cref="BlockingComponent"/> </param> /// <param name="component"> The <see cref="BlockingComponent"/> </param>
/// <param name="user"> The person holding the blocking item </param> /// <param name="user"> The person holding the blocking item </param>
private void BlockingShutdownHelper(EntityUid uid, BlockingComponent component, EntityUid user) private void StopBlockingHelper(EntityUid uid, BlockingComponent component, EntityUid user)
{ {
if (component.IsBlocking) if (component.IsBlocking)
StopBlocking(uid, component, user); StopBlocking(uid, component, user);
foreach (var shield in _handsSystem.EnumerateHeld(user)) var userQuery = GetEntityQuery<BlockingUserComponent>();
var handQuery = GetEntityQuery<SharedHandsComponent>();
if (!handQuery.TryGetComponent(user, out var hands))
return;
var shields = _handsSystem.EnumerateHeld(user, hands).ToArray();
foreach (var shield in shields)
{ {
if (HasComp<BlockingComponent>(shield) && TryComp<BlockingUserComponent>(user, out var blockingUserComponent)) if (HasComp<BlockingComponent>(shield) && userQuery.TryGetComponent(user, out var blockingUserComponent))
{ {
blockingUserComponent.BlockingItem = shield; blockingUserComponent.BlockingItem = shield;
return; return;