Make wielding precede cycling in event subs (#18915)

* Make wielding precede cycling in event subs

* Update API

* Remove unused using
This commit is contained in:
Vordenburg
2023-08-14 18:45:39 -04:00
committed by GitHub
parent 1edd6e53fb
commit 31f5f1caad

View File

@@ -1,4 +1,3 @@
using Content.Shared.Actions.Events;
using Content.Shared.DoAfter; using Content.Shared.DoAfter;
using Content.Shared.Hands; using Content.Shared.Hands;
using Content.Shared.Hands.Components; using Content.Shared.Hands.Components;
@@ -29,7 +28,7 @@ public sealed class WieldableSystem : EntitySystem
{ {
base.Initialize(); base.Initialize();
SubscribeLocalEvent<WieldableComponent, UseInHandEvent>(OnUseInHand); SubscribeLocalEvent<WieldableComponent, UseInHandEvent>(OnUseInHand, before: new [] { typeof(SharedGunSystem) });
SubscribeLocalEvent<WieldableComponent, WieldableDoAfterEvent>(OnDoAfter); SubscribeLocalEvent<WieldableComponent, WieldableDoAfterEvent>(OnDoAfter);
SubscribeLocalEvent<WieldableComponent, ItemUnwieldedEvent>(OnItemUnwielded); SubscribeLocalEvent<WieldableComponent, ItemUnwieldedEvent>(OnItemUnwielded);
SubscribeLocalEvent<WieldableComponent, GotUnequippedHandEvent>(OnItemLeaveHand); SubscribeLocalEvent<WieldableComponent, GotUnequippedHandEvent>(OnItemLeaveHand);
@@ -100,8 +99,8 @@ public sealed class WieldableSystem : EntitySystem
{ {
Text = component.Wielded ? Loc.GetString("wieldable-verb-text-unwield") : Loc.GetString("wieldable-verb-text-wield"), Text = component.Wielded ? Loc.GetString("wieldable-verb-text-unwield") : Loc.GetString("wieldable-verb-text-wield"),
Act = component.Wielded Act = component.Wielded
? () => AttemptUnwield(uid, component, args.User) ? () => TryUnwield(uid, component, args.User)
: () => AttemptWield(uid, component, args.User) : () => TryWield(uid, component, args.User)
}; };
args.Verbs.Add(verb); args.Verbs.Add(verb);
@@ -112,12 +111,10 @@ public sealed class WieldableSystem : EntitySystem
if (args.Handled) if (args.Handled)
return; return;
args.Handled = true;
if(!component.Wielded) if(!component.Wielded)
AttemptWield(uid, component, args.User); args.Handled = TryWield(uid, component, args.User);
else else
AttemptUnwield(uid, component, args.User); args.Handled = TryUnwield(uid, component, args.User);
} }
public bool CanWield(EntityUid uid, WieldableComponent component, EntityUid user, bool quiet=false) public bool CanWield(EntityUid uid, WieldableComponent component, EntityUid user, bool quiet=false)
@@ -156,15 +153,17 @@ public sealed class WieldableSystem : EntitySystem
/// <summary> /// <summary>
/// Attempts to wield an item, creating a DoAfter.. /// Attempts to wield an item, creating a DoAfter..
/// </summary> /// </summary>
public void AttemptWield(EntityUid used, WieldableComponent component, EntityUid user) /// <returns>True if the attempt wasn't blocked.</returns>
public bool TryWield(EntityUid used, WieldableComponent component, EntityUid user)
{ {
if (!CanWield(used, component, user)) if (!CanWield(used, component, user))
return; return false;
var ev = new BeforeWieldEvent(); var ev = new BeforeWieldEvent();
RaiseLocalEvent(used, ev); RaiseLocalEvent(used, ev);
if (ev.Cancelled) if (ev.Cancelled)
return; return false;
var doargs = new DoAfterArgs(user, component.WieldTime, new WieldableDoAfterEvent(), used, used: used) var doargs = new DoAfterArgs(user, component.WieldTime, new WieldableDoAfterEvent(), used, used: used)
{ {
@@ -173,22 +172,25 @@ public sealed class WieldableSystem : EntitySystem
}; };
_doAfter.TryStartDoAfter(doargs); _doAfter.TryStartDoAfter(doargs);
return true;
} }
/// <summary> /// <summary>
/// Attempts to unwield an item, with no DoAfter. /// Attempts to unwield an item, with no DoAfter.
/// </summary> /// </summary>
public void AttemptUnwield(EntityUid used, WieldableComponent component, EntityUid user) /// <returns>True if the attempt wasn't blocked.</returns>
public bool TryUnwield(EntityUid used, WieldableComponent component, EntityUid user)
{ {
var ev = new BeforeUnwieldEvent(); var ev = new BeforeUnwieldEvent();
RaiseLocalEvent(used, ev); RaiseLocalEvent(used, ev);
if (ev.Cancelled) if (ev.Cancelled)
return; return false;
var targEv = new ItemUnwieldedEvent(user); var targEv = new ItemUnwieldedEvent(user);
RaiseLocalEvent(used, targEv); RaiseLocalEvent(used, targEv);
return true;
} }
private void OnDoAfter(EntityUid uid, WieldableComponent component, DoAfterEvent args) private void OnDoAfter(EntityUid uid, WieldableComponent component, DoAfterEvent args)
@@ -261,7 +263,7 @@ public sealed class WieldableSystem : EntitySystem
private void OnVirtualItemDeleted(EntityUid uid, WieldableComponent component, VirtualItemDeletedEvent args) private void OnVirtualItemDeleted(EntityUid uid, WieldableComponent component, VirtualItemDeletedEvent args)
{ {
if (args.BlockingEntity == uid && component.Wielded) if (args.BlockingEntity == uid && component.Wielded)
AttemptUnwield(args.BlockingEntity, component, args.User); TryUnwield(args.BlockingEntity, component, args.User);
} }
private void OnGetMeleeDamage(EntityUid uid, IncreaseDamageOnWieldComponent component, ref GetMeleeDamageEvent args) private void OnGetMeleeDamage(EntityUid uid, IncreaseDamageOnWieldComponent component, ref GetMeleeDamageEvent args)