DoAfter Refactor (#13225)

Co-authored-by: DrSmugleaf <drsmugleaf@gmail.com>
This commit is contained in:
keronshb
2023-02-24 19:01:25 -05:00
committed by GitHub
parent 7a9baa79c2
commit 9ebb452a3c
129 changed files with 2624 additions and 4132 deletions

View File

@@ -2,6 +2,7 @@ using Content.Server.DoAfter;
using Content.Server.Popups;
using Content.Server.Sticky.Components;
using Content.Server.Sticky.Events;
using Content.Shared.DoAfter;
using Content.Shared.Hands.EntitySystems;
using Content.Shared.Interaction;
using Content.Shared.Sticky.Components;
@@ -26,8 +27,7 @@ public sealed class StickySystem : EntitySystem
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<StickSuccessfulEvent>(OnStickSuccessful);
SubscribeLocalEvent<UnstickSuccessfulEvent>(OnUnstickSuccessful);
SubscribeLocalEvent<StickyComponent, DoAfterEvent>(OnStickSuccessful);
SubscribeLocalEvent<StickyComponent, AfterInteractEvent>(OnAfterInteract);
SubscribeLocalEvent<StickyComponent, GetVerbsEvent<Verb>>(AddUnstickVerb);
}
@@ -84,10 +84,11 @@ public sealed class StickySystem : EntitySystem
_popupSystem.PopupEntity(msg, user, user);
}
component.Stick = true;
// start sticking object to target
_doAfterSystem.DoAfter(new DoAfterEventArgs(user, delay, target: target)
_doAfterSystem.DoAfter(new DoAfterEventArgs(user, delay, target: target, used: uid)
{
BroadcastFinishedEvent = new StickSuccessfulEvent(uid, user, target),
BreakOnStun = true,
BreakOnTargetMove = true,
BreakOnUserMove = true,
@@ -103,13 +104,18 @@ public sealed class StickySystem : EntitySystem
return true;
}
private void OnStickSuccessful(StickSuccessfulEvent ev)
private void OnStickSuccessful(EntityUid uid, StickyComponent component, DoAfterEvent args)
{
// check if entity still has sticky component
if (!TryComp(ev.Uid, out StickyComponent? component))
if (args.Handled || args.Cancelled || args.Args.Target == null)
return;
StickToEntity(ev.Uid, ev.Target, ev.User, component);
if (component.Stick)
StickToEntity(uid, args.Args.Target.Value, args.Args.User, component);
else
UnstickFromEntity(uid, args.Args.User, component);
args.Handled = true;
}
private void StartUnsticking(EntityUid uid, EntityUid user, StickyComponent? component = null)
@@ -127,10 +133,11 @@ public sealed class StickySystem : EntitySystem
_popupSystem.PopupEntity(msg, user, user);
}
component.Stick = false;
// start unsticking object
_doAfterSystem.DoAfter(new DoAfterEventArgs(user, delay, target: uid)
{
BroadcastFinishedEvent = new UnstickSuccessfulEvent(uid, user),
BreakOnStun = true,
BreakOnTargetMove = true,
BreakOnUserMove = true,
@@ -144,15 +151,6 @@ public sealed class StickySystem : EntitySystem
}
}
private void OnUnstickSuccessful(UnstickSuccessfulEvent ev)
{
// check if entity still has sticky component
if (!TryComp(ev.Uid, out StickyComponent? component))
return;
UnstickFromEntity(ev.Uid, ev.User, component);
}
public void StickToEntity(EntityUid uid, EntityUid target, EntityUid user, StickyComponent? component = null)
{
if (!Resolve(uid, ref component))
@@ -215,30 +213,4 @@ public sealed class StickySystem : EntitySystem
component.StuckTo = null;
RaiseLocalEvent(uid, new EntityUnstuckEvent(target, user), true);
}
private sealed class StickSuccessfulEvent : EntityEventArgs
{
public readonly EntityUid Uid;
public readonly EntityUid User;
public readonly EntityUid Target;
public StickSuccessfulEvent(EntityUid uid, EntityUid user, EntityUid target)
{
Uid = uid;
User = user;
Target = target;
}
}
private sealed class UnstickSuccessfulEvent : EntityEventArgs
{
public readonly EntityUid Uid;
public readonly EntityUid User;
public UnstickSuccessfulEvent(EntityUid uid, EntityUid user)
{
Uid = uid;
User = user;
}
}
}