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

@@ -1,5 +1,3 @@
using System.Threading;
namespace Content.Server.Fluids.Components;
[RegisterComponent]
@@ -17,6 +15,4 @@ public sealed class SpillableComponent : Component
[DataField("spillDelay")]
public float? SpillDelay;
public CancellationTokenSource? CancelToken;
}

View File

@@ -5,6 +5,7 @@ using Content.Server.DoAfter;
using Content.Server.Fluids.Components;
using Content.Server.Popups;
using Content.Shared.Chemistry.Components;
using Content.Shared.DoAfter;
using Content.Shared.FixedPoint;
using Content.Shared.Fluids;
using Content.Shared.Interaction;
@@ -34,9 +35,7 @@ public sealed class MoppingSystem : SharedMoppingSystem
base.Initialize();
SubscribeLocalEvent<AbsorbentComponent, ComponentInit>(OnAbsorbentInit);
SubscribeLocalEvent<AbsorbentComponent, AfterInteractEvent>(OnAfterInteract);
SubscribeLocalEvent<AbsorbentComponent, SolutionChangedEvent>(OnAbsorbentSolutionChange);
SubscribeLocalEvent<TransferCancelledEvent>(OnTransferCancelled);
SubscribeLocalEvent<TransferCompleteEvent>(OnTransferComplete);
SubscribeLocalEvent<AbsorbentComponent, DoAfterEvent<AbsorbantData>>(OnDoAfter);
}
private void OnAbsorbentInit(EntityUid uid, AbsorbentComponent component, ComponentInit args)
@@ -256,63 +255,38 @@ public sealed class MoppingSystem : SharedMoppingSystem
if (!component.InteractingEntities.Add(target))
return;
var doAfterArgs = new DoAfterEventArgs(user, delay, target: target)
var aborbantData = new AbsorbantData(targetSolution, msg, sfx, transferAmount);
var doAfterArgs = new DoAfterEventArgs(user, delay, target: target, used:used)
{
BreakOnUserMove = true,
BreakOnStun = true,
BreakOnDamage = true,
MovementThreshold = 0.2f,
BroadcastCancelledEvent = new TransferCancelledEvent(target, component),
BroadcastFinishedEvent = new TransferCompleteEvent(used, target, component, targetSolution, msg, sfx, transferAmount)
MovementThreshold = 0.2f
};
_doAfterSystem.DoAfter(doAfterArgs);
_doAfterSystem.DoAfter(doAfterArgs, aborbantData);
}
private void OnTransferComplete(TransferCompleteEvent ev)
private void OnDoAfter(EntityUid uid, AbsorbentComponent component, DoAfterEvent<AbsorbantData> args)
{
_audio.PlayPvs(ev.Sound, ev.Tool);
_popups.PopupEntity(ev.Message, ev.Tool);
_solutionSystem.TryTransferSolution(ev.Target, ev.Tool, ev.TargetSolution, AbsorbentComponent.SolutionName, ev.TransferAmount);
ev.Component.InteractingEntities.Remove(ev.Target);
if (args.Handled || args.Cancelled || args.Args.Target == null)
return;
_audio.PlayPvs(args.AdditionalData.Sound, uid);
_popups.PopupEntity(Loc.GetString(args.AdditionalData.Message, ("target", args.Args.Target.Value), ("used", uid)), uid);
_solutionSystem.TryTransferSolution(args.Args.Target.Value, uid, args.AdditionalData.TargetSolution,
AbsorbentComponent.SolutionName, args.AdditionalData.TransferAmount);
component.InteractingEntities.Remove(args.Args.Target.Value);
args.Handled = true;
}
private void OnTransferCancelled(TransferCancelledEvent ev)
private record struct AbsorbantData(string TargetSolution, string Message, SoundSpecifier Sound, FixedPoint2 TransferAmount)
{
ev.Component.InteractingEntities.Remove(ev.Target);
}
}
public sealed class TransferCompleteEvent : EntityEventArgs
{
public readonly EntityUid Tool;
public readonly EntityUid Target;
public readonly AbsorbentComponent Component;
public readonly string TargetSolution;
public readonly string Message;
public readonly SoundSpecifier Sound;
public readonly FixedPoint2 TransferAmount;
public TransferCompleteEvent(EntityUid tool, EntityUid target, AbsorbentComponent component, string targetSolution, string message, SoundSpecifier sound, FixedPoint2 transferAmount)
{
Tool = tool;
Target = target;
Component = component;
TargetSolution = targetSolution;
Message = Loc.GetString(message, ("target", target), ("used", tool));
Sound = sound;
TransferAmount = transferAmount;
}
}
public sealed class TransferCancelledEvent : EntityEventArgs
{
public readonly EntityUid Target;
public readonly AbsorbentComponent Component;
public TransferCancelledEvent(EntityUid target, AbsorbentComponent component)
{
Target = target;
Component = component;
public readonly string TargetSolution = TargetSolution;
public readonly string Message = Message;
public readonly SoundSpecifier Sound = Sound;
public readonly FixedPoint2 TransferAmount = TransferAmount;
}
}

View File

@@ -16,7 +16,7 @@ using Robust.Shared.Map;
using Robust.Shared.Prototypes;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
using Content.Shared.DoAfter;
namespace Content.Server.Fluids.EntitySystems;
@@ -38,8 +38,7 @@ public sealed class SpillableSystem : EntitySystem
SubscribeLocalEvent<SpillableComponent, GetVerbsEvent<Verb>>(AddSpillVerb);
SubscribeLocalEvent<SpillableComponent, GotEquippedEvent>(OnGotEquipped);
SubscribeLocalEvent<SpillableComponent, SolutionSpikeOverflowEvent>(OnSpikeOverflow);
SubscribeLocalEvent<SpillableComponent, SpillFinishedEvent>(OnSpillFinished);
SubscribeLocalEvent<SpillableComponent, SpillCancelledEvent>(OnSpillCancelled);
SubscribeLocalEvent<SpillableComponent, DoAfterEvent>(OnDoAfter);
}
private void OnSpikeOverflow(EntityUid uid, SpillableComponent component, SolutionSpikeOverflowEvent args)
@@ -142,20 +141,14 @@ public sealed class SpillableSystem : EntitySystem
{
verb.Act = () =>
{
if (component.CancelToken == null)
_doAfterSystem.DoAfter(new DoAfterEventArgs(args.User, component.SpillDelay.Value, target:uid)
{
component.CancelToken = new CancellationTokenSource();
_doAfterSystem.DoAfter(new DoAfterEventArgs(args.User, component.SpillDelay.Value, component.CancelToken.Token, component.Owner)
{
BreakOnTargetMove = true,
BreakOnUserMove = true,
BreakOnDamage = true,
BreakOnStun = true,
NeedHand = true,
TargetFinishedEvent = new SpillFinishedEvent(args.User, component.Owner, solution),
TargetCancelledEvent = new SpillCancelledEvent(component.Owner)
});
}
BreakOnTargetMove = true,
BreakOnUserMove = true,
BreakOnDamage = true,
BreakOnStun = true,
NeedHand = true
});
};
}
verb.Impact = LogImpact.Medium; // dangerous reagent reaction are logged separately.
@@ -263,46 +256,19 @@ public sealed class SpillableSystem : EntitySystem
return puddleComponent;
}
private void OnSpillFinished(EntityUid uid, SpillableComponent component, SpillFinishedEvent ev)
private void OnDoAfter(EntityUid uid, SpillableComponent component, DoAfterEvent args)
{
component.CancelToken = null;
//solution gone by other means before doafter completes
if (ev.Solution == null || ev.Solution.Volume == 0)
if (args.Handled || args.Cancelled || args.Args.Target == null)
return;
var puddleSolution = _solutionContainerSystem.SplitSolution(uid,
ev.Solution, ev.Solution.Volume);
//solution gone by other means before doafter completes
if (!_solutionContainerSystem.TryGetDrainableSolution(uid, out var solution) || solution.Volume == 0)
return;
SpillAt(puddleSolution, Transform(component.Owner).Coordinates, "PuddleSmear");
}
var puddleSolution = _solutionContainerSystem.SplitSolution(uid, solution, solution.Volume);
private void OnSpillCancelled(EntityUid uid, SpillableComponent component, SpillCancelledEvent ev)
{
component.CancelToken = null;
}
SpillAt(puddleSolution, Transform(uid).Coordinates, "PuddleSmear");
internal sealed class SpillFinishedEvent : EntityEventArgs
{
public SpillFinishedEvent(EntityUid user, EntityUid spillable, Solution solution)
{
User = user;
Spillable = spillable;
Solution = solution;
}
public EntityUid User { get; }
public EntityUid Spillable { get; }
public Solution Solution { get; }
}
private sealed class SpillCancelledEvent : EntityEventArgs
{
public EntityUid Spillable;
public SpillCancelledEvent(EntityUid spillable)
{
Spillable = spillable;
}
args.Handled = true;
}
}