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,14 +1,13 @@
using Content.Server.Administration.Logs;
using Content.Server.DoAfter;
using Content.Server.Kitchen.Components;
using Content.Server.Nutrition.Components;
using Content.Server.Popups;
using Content.Shared.Database;
using Content.Shared.DoAfter;
using Content.Shared.DragDrop;
using Content.Shared.IdentityManagement;
using Content.Shared.Interaction;
using Content.Shared.Nutrition.Components;
using Robust.Shared.Audio;
using Robust.Shared.Player;
using Content.Shared.Storage;
using Robust.Shared.Random;
@@ -18,7 +17,6 @@ using Content.Shared.Kitchen;
using Content.Shared.Mobs.Components;
using Content.Shared.Mobs.Systems;
using Content.Shared.Popups;
using Robust.Server.GameObjects;
namespace Content.Server.Kitchen.EntitySystems
{
@@ -30,6 +28,7 @@ namespace Content.Server.Kitchen.EntitySystems
[Dependency] private readonly MobStateSystem _mobStateSystem = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;
[Dependency] private readonly SharedAudioSystem _audio = default!;
public override void Initialize()
{
@@ -40,8 +39,7 @@ namespace Content.Server.Kitchen.EntitySystems
SubscribeLocalEvent<KitchenSpikeComponent, DragDropTargetEvent>(OnDragDrop);
//DoAfter
SubscribeLocalEvent<KitchenSpikeComponent, SpikingFinishedEvent>(OnSpikingFinished);
SubscribeLocalEvent<KitchenSpikeComponent, SpikingFailEvent>(OnSpikingFail);
SubscribeLocalEvent<KitchenSpikeComponent, DoAfterEvent>(OnDoAfter);
SubscribeLocalEvent<KitchenSpikeComponent, SuicideEvent>(OnSuicide);
@@ -56,35 +54,32 @@ namespace Content.Server.Kitchen.EntitySystems
private void OnSuicide(EntityUid uid, KitchenSpikeComponent component, SuicideEvent args)
{
if (args.Handled) return;
if (args.Handled)
return;
args.SetHandled(SuicideKind.Piercing);
var victim = args.Victim;
var othersMessage = Loc.GetString("comp-kitchen-spike-suicide-other", ("victim", victim));
victim.PopupMessageOtherClients(othersMessage);
_popupSystem.PopupEntity(othersMessage, victim);
var selfMessage = Loc.GetString("comp-kitchen-spike-suicide-self");
victim.PopupMessage(selfMessage);
_popupSystem.PopupEntity(selfMessage, victim, victim);
}
private void OnSpikingFail(EntityUid uid, KitchenSpikeComponent component, SpikingFailEvent args)
private void OnDoAfter(EntityUid uid, KitchenSpikeComponent component, DoAfterEvent args)
{
component.InUse = false;
if (args.Args.Target == null)
return;
if (EntityManager.TryGetComponent<ButcherableComponent>(args.VictimUid, out var butcherable))
butcherable.BeingButchered = false;
}
private void OnSpikingFinished(EntityUid uid, KitchenSpikeComponent component, SpikingFinishedEvent args)
{
component.InUse = false;
if (EntityManager.TryGetComponent<ButcherableComponent>(args.VictimUid, out var butcherable))
if (TryComp<ButcherableComponent>(args.Args.Target.Value, out var butcherable))
butcherable.BeingButchered = false;
if (Spikeable(uid, args.UserUid, args.VictimUid, component, butcherable))
{
Spike(uid, args.UserUid, args.VictimUid, component);
}
if (args.Handled || args.Cancelled)
return;
if (Spikeable(uid, args.Args.User, args.Args.Target.Value, component, butcherable))
Spike(uid, args.Args.User, args.Args.Target.Value, component);
args.Handled = true;
}
private void OnDragDrop(EntityUid uid, KitchenSpikeComponent component, ref DragDropTargetEvent args)
@@ -96,8 +91,8 @@ namespace Content.Server.Kitchen.EntitySystems
if (Spikeable(uid, args.User, args.Dragged, component))
TrySpike(uid, args.User, args.Dragged, component);
}
private void OnInteractHand(EntityUid uid, KitchenSpikeComponent component, InteractHandEvent args)
{
if (args.Handled)
@@ -142,7 +137,7 @@ namespace Content.Server.Kitchen.EntitySystems
// TODO: Need to be able to leave them on the spike to do DoT, see ss13.
EntityManager.QueueDeleteEntity(victimUid);
SoundSystem.Play(component.SpikeSound.GetSound(), Filter.Pvs(uid), uid);
_audio.Play(component.SpikeSound, Filter.Pvs(uid), uid, true);
}
private bool TryGetPiece(EntityUid uid, EntityUid user, EntityUid used,
@@ -164,9 +159,7 @@ namespace Content.Server.Kitchen.EntitySystems
Loc.GetString("comp-kitchen-spike-meat-name", ("name", Name(ent)), ("victim", component.Victim));
if (component.PrototypesToSpawn.Count != 0)
{
_popupSystem.PopupEntity(component.MeatSource1p, uid, user, PopupType.MediumCaution);
}
else
{
UpdateAppearance(uid, null, component);
@@ -243,42 +236,18 @@ namespace Content.Server.Kitchen.EntitySystems
butcherable.BeingButchered = true;
component.InUse = true;
var doAfterArgs = new DoAfterEventArgs(userUid, component.SpikeDelay + butcherable.ButcherDelay, default, uid)
var doAfterArgs = new DoAfterEventArgs(userUid, component.SpikeDelay + butcherable.ButcherDelay, target:victimUid, used:uid)
{
BreakOnTargetMove = true,
BreakOnUserMove = true,
BreakOnDamage = true,
BreakOnStun = true,
NeedHand = true,
TargetFinishedEvent = new SpikingFinishedEvent(userUid, victimUid),
TargetCancelledEvent = new SpikingFailEvent(victimUid)
NeedHand = true
};
_doAfter.DoAfter(doAfterArgs);
return true;
}
private sealed class SpikingFinishedEvent : EntityEventArgs
{
public EntityUid VictimUid;
public EntityUid UserUid;
public SpikingFinishedEvent(EntityUid userUid, EntityUid victimUid)
{
UserUid = userUid;
VictimUid = victimUid;
}
}
private sealed class SpikingFailEvent : EntityEventArgs
{
public EntityUid VictimUid;
public SpikingFailEvent(EntityUid victimUid)
{
VictimUid = victimUid;
}
}
}
}

View File

@@ -1,4 +1,4 @@
using Content.Server.Body.Systems;
using Content.Server.Body.Systems;
using Content.Server.DoAfter;
using Content.Server.Kitchen.Components;
using Content.Shared.Body.Components;
@@ -8,10 +8,10 @@ using Content.Shared.Popups;
using Content.Shared.Storage;
using Content.Shared.Verbs;
using Content.Shared.Destructible;
using Content.Shared.DoAfter;
using Content.Shared.Mobs.Components;
using Content.Shared.Mobs.Systems;
using Robust.Server.Containers;
using Robust.Shared.Player;
using Robust.Shared.Random;
namespace Content.Server.Kitchen.EntitySystems;
@@ -31,8 +31,7 @@ public sealed class SharpSystem : EntitySystem
base.Initialize();
SubscribeLocalEvent<SharpComponent, AfterInteractEvent>(OnAfterInteract);
SubscribeLocalEvent<SharpButcherDoafterComplete>(OnDoafterComplete);
SubscribeLocalEvent<SharpButcherDoafterCancelled>(OnDoafterCancelled);
SubscribeLocalEvent<SharpComponent, DoAfterEvent>(OnDoAfter);
SubscribeLocalEvent<ButcherableComponent, GetVerbsEvent<InteractionVerb>>(OnGetInteractionVerbs);
}
@@ -63,64 +62,56 @@ public sealed class SharpSystem : EntitySystem
return;
var doAfter =
new DoAfterEventArgs(user, sharp.ButcherDelayModifier * butcher.ButcherDelay, default, target)
new DoAfterEventArgs(user, sharp.ButcherDelayModifier * butcher.ButcherDelay, target: target, used: knife)
{
BreakOnTargetMove = true,
BreakOnUserMove = true,
BreakOnDamage = true,
BreakOnStun = true,
NeedHand = true,
BroadcastFinishedEvent = new SharpButcherDoafterComplete { User = user, Entity = target, Sharp = knife },
BroadcastCancelledEvent = new SharpButcherDoafterCancelled { Entity = target, Sharp = knife }
NeedHand = true
};
_doAfterSystem.DoAfter(doAfter);
}
private void OnDoafterComplete(SharpButcherDoafterComplete ev)
private void OnDoAfter(EntityUid uid, SharpComponent component, DoAfterEvent args)
{
if (!TryComp<ButcherableComponent>(ev.Entity, out var butcher))
if (args.Handled || args.Cancelled || !TryComp<ButcherableComponent>(args.Args.Target, out var butcher))
return;
if (!TryComp<SharpComponent>(ev.Sharp, out var sharp))
return;
component.Butchering.Remove(args.Args.Target.Value);
sharp.Butchering.Remove(ev.Entity);
if (_containerSystem.IsEntityInContainer(ev.Entity))
if (_containerSystem.IsEntityInContainer(args.Args.Target.Value))
{
args.Handled = true;
return;
}
var spawnEntities = EntitySpawnCollection.GetSpawns(butcher.SpawnedEntities, _robustRandom);
var coords = Transform(ev.Entity).MapPosition;
EntityUid popupEnt = default;
var coords = Transform(args.Args.Target.Value).MapPosition;
EntityUid popupEnt = default!;
foreach (var proto in spawnEntities)
{
// distribute the spawned items randomly in a small radius around the origin
popupEnt = Spawn(proto, coords.Offset(_robustRandom.NextVector2(0.25f)));
}
var hasBody = TryComp<BodyComponent>(ev.Entity, out var body);
var hasBody = TryComp<BodyComponent>(args.Args.Target.Value, out var body);
// only show a big popup when butchering living things.
var popupType = PopupType.Small;
if (hasBody)
popupType = PopupType.LargeCaution;
_popupSystem.PopupEntity(Loc.GetString("butcherable-knife-butchered-success", ("target", ev.Entity), ("knife", ev.Sharp)),
popupEnt, ev.User, popupType);
_popupSystem.PopupEntity(Loc.GetString("butcherable-knife-butchered-success", ("target", args.Args.Target.Value), ("knife", uid)),
popupEnt, args.Args.User, popupType);
if (hasBody)
_bodySystem.GibBody(body!.Owner, body: body);
_bodySystem.GibBody(args.Args.Target.Value, body: body);
_destructibleSystem.DestroyEntity(ev.Entity);
}
_destructibleSystem.DestroyEntity(args.Args.Target.Value);
private void OnDoafterCancelled(SharpButcherDoafterCancelled ev)
{
if (!TryComp<SharpComponent>(ev.Sharp, out var sharp))
return;
sharp.Butchering.Remove(ev.Entity);
args.Handled = true;
}
private void OnGetInteractionVerbs(EntityUid uid, ButcherableComponent component, GetVerbsEvent<InteractionVerb> args)
@@ -165,16 +156,3 @@ public sealed class SharpSystem : EntitySystem
args.Verbs.Add(verb);
}
}
public sealed class SharpButcherDoafterComplete : EntityEventArgs
{
public EntityUid Entity;
public EntityUid Sharp;
public EntityUid User;
}
public sealed class SharpButcherDoafterCancelled : EntityEventArgs
{
public EntityUid Entity;
public EntityUid Sharp;
}