feat: Adds PlayerDeathEvent and CreatureDeathEvent using code generated events (#1927)

### Summary
- Adds [Code Generated Events](https://github.com/modernuo/CodeGeneratedEvents)
- Removes EventSink.PlayerDeath
- Adds `PlayerDeathEvent` and `CreatureDeathEvent` using code generated events

> [!Important]
> **Developer Note**
> Use `[OnEvent(nameof(PlayerMobile.PlayerDeathEvent))]` instead of `EventSink.PlayerDeath` delegate
> Check this commit for examples of how to use this.
This commit is contained in:
Guyute 2024-09-06 00:23:03 -04:00 • committed by GitHub
parent d5984ce058
commit 95cd8749a3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
26 changed files with 191 additions and 135 deletions

View file

@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using ModernUO.CodeGeneratedEvents;
using Server.Engines.PartySystem;
using Server.Factions;
using Server.Gumps;
@ -596,6 +597,9 @@ namespace Server.Engines.ConPVP
}
}
[OnEvent(nameof(PlayerMobile.PlayerDeathEvent))]
public static void OnPlayerDeath(PlayerMobile m) => m.DuelContext?.OnDeath(m, m.Corpse);
public void OnDeath(Mobile mob, Container corpse)
{
if (!Registered || !Started)
@ -1843,7 +1847,7 @@ namespace Server.Engines.ConPVP
}
TransformationSpellHelper.RemoveContext(mob, true);
AnimalForm.RemoveContext(mob, true);
AnimalForm.RemoveContext(mob);
DisguisePersistence.StopTimer(mob);
if (!mob.CanBeginAction<PolymorphSpell>())

View file

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using ModernUO.CodeGeneratedEvents;
using Server.Commands;
using Server.Commands.Generic;
using Server.Engines.MLQuests.Gumps;
@ -611,6 +612,7 @@ namespace Server.Engines.MLQuests
return context;
}
[OnEvent(nameof(PlayerMobile.PlayerDeathEvent))]
public static void HandleDeath(PlayerMobile pm)
{
var context = GetContext(pm);

View file

@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using ModernUO.CodeGeneratedEvents;
using Server.Factions;
using Server.Mobiles;
using Server.Network;
using Server.Targeting;
@ -99,7 +101,6 @@ namespace Server.Engines.PartySystem
public static void Configure()
{
EventSink.Logout += EventSink_Logout;
EventSink.PlayerDeath += EventSink_PlayerDeath;
CommandSystem.Register("ListenToParty", AccessLevel.GameMaster, ListenToParty_OnCommand);
}
@ -135,7 +136,8 @@ namespace Server.Engines.PartySystem
}
}
public static void EventSink_PlayerDeath(Mobile from)
[OnEvent(nameof(PlayerMobile.PlayerDeathEvent))]
public static void OnPlayerDeathEvent(Mobile from)
{
var p = Get(from);

View file

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using ModernUO.CodeGeneratedEvents;
using Server.Gumps;
using Server.Misc;
using Server.Mobiles;
@ -26,16 +27,16 @@ public class ReportMurdererGump : StaticGump<ReportMurdererGump>
public static void Initialize()
{
_recentlyReportedDelay = ServerConfiguration.GetOrUpdateSetting("murderSystem.recentlyReportedDelay", TimeSpan.FromMinutes(10));
EventSink.PlayerDeath += OnPlayerDeath;
}
public static void OnPlayerDeath(Mobile m)
[OnEvent(nameof(PlayerMobile.PlayerDeathEvent))]
public static void OnPlayerDeathEvent(PlayerMobile m)
{
List<Mobile> killers = null;
HashSet<Mobile> toGive = null;
// Guards won't take reports of the death of a thief!
bool notInThievesGuild = m is not PlayerMobile { NpcGuild: NpcGuild.ThievesGuild };
bool notInThievesGuild = m.NpcGuild != NpcGuild.ThievesGuild;
foreach (var ai in m.Aggressors)
{

View file

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using ModernUO.CodeGeneratedEvents;
using ModernUO.Serialization;
using Server.Events.Halloween;
using Server.Items;
@ -60,15 +61,13 @@ namespace Server.Engines.Events
{
_timer = Timer.DelayCall(tick, 0, Timer_Callback);
_clearTimer = Timer.DelayCall(clear, 0, Clear_Callback);
EventSink.PlayerDeath += EventSink_PlayerDeath;
}
}
public static void EventSink_PlayerDeath(Mobile m)
[OnEvent(nameof(PlayerMobile.PlayerDeathEvent))]
public static void OnPlayerDeathEvent(PlayerMobile pm)
{
if (m is PlayerMobile { Deleted: false } pm &&
_timer.Running && !_deathQueue.Contains(pm) && _deathQueue.Count < m_DeathQueueLimit)
if (_timer.Running && !_deathQueue.Contains(pm) && _deathQueue.Count < m_DeathQueueLimit)
{
_deathQueue.Add(pm);
}

View file

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using ModernUO.CodeGeneratedEvents;
using Server.Mobiles;
namespace Server.Items;
@ -69,6 +70,7 @@ public class DisguisePersistence : GenericPersistence
t.Stop();
}
[OnEvent(nameof(PlayerMobile.PlayerDeathEvent))]
public static void RemoveTimer(Mobile m)
{
if (Timers.Remove(m, out var t))

View file

@ -36,7 +36,7 @@ public partial class BaseFormTalisman : Item
if (parent is Mobile m)
{
AnimalForm.RemoveContext(m, true);
AnimalForm.RemoveContext(m);
}
}

View file

@ -1248,7 +1248,7 @@ namespace Server
if (m.Skills.Ninjitsu.Value < AnimalForm.Entries[i].ReqSkill)
{
AnimalForm.RemoveContext(m, true);
AnimalForm.RemoveContext(m);
}
}

View file

@ -1050,7 +1050,7 @@ namespace Server.Guilds
}
}
public static void HandleDeath(Mobile victim, Mobile killer = null)
public static void HandleDeath(Mobile victim, Mobile killer)
{
if (!NewGuildSystem)
{

View file

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using ModernUO.CodeGeneratedEvents;
using Server.Collections;
using Server.ContextMenus;
using Server.Engines.ConPVP;
@ -155,7 +156,7 @@ namespace Server.Mobiles
}
}
public abstract class BaseCreature : Mobile, IHonorTarget, IQuestGiver
public abstract partial class BaseCreature : Mobile, IHonorTarget, IQuestGiver
{
public enum Allegiance
{
@ -3248,10 +3249,11 @@ namespace Server.Mobiles
}
}
[GeneratedEvent(nameof(CreatureDeathEvent))]
public static partial void CreatureDeathEvent(Mobile m);
public override void OnDeath(Container c)
{
MeerMage.StopEffect(this, false);
if (IsBonded)
{
Effects.PlaySound(this, GetDeathSound());
@ -3312,7 +3314,7 @@ namespace Server.Mobiles
OwnerAbandonTime = DateTime.MinValue;
}
GiftOfLifeSpell.HandleDeath(this);
CreatureDeathEvent(this);
CheckStatTimers();
return;
@ -3435,6 +3437,8 @@ namespace Server.Mobiles
{
c.Delete();
}
CreatureDeathEvent(this);
}
public override void OnDelete()

View file

@ -1,5 +1,4 @@
using ModernUO.Serialization;
using Server.Gumps;
namespace Server.Mobiles;

View file

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using ModernUO.CodeGeneratedEvents;
using ModernUO.Serialization;
using Server.Items;
@ -65,12 +66,8 @@ namespace Server.Mobiles
public override bool BardImmune => true;
public override Poison PoisonImmune => Poison.Lethal;
public new static void Initialize()
{
EventSink.PlayerDeath += EventSink_PlayerDeath;
}
public static void EventSink_PlayerDeath(Mobile m)
[OnEvent(nameof(PlayerMobile.PlayerDeathEvent))]
public static void OnPlayerDeathEvent(Mobile m)
{
var lastKiller = m.LastKiller;

View file

@ -1,6 +1,7 @@
using ModernUO.Serialization;
using System;
using System.Collections.Generic;
using ModernUO.CodeGeneratedEvents;
using Server.Items;
namespace Server.Mobiles
@ -152,7 +153,9 @@ namespace Server.Mobiles
public static bool UnderEffect(Mobile m) => m_Table.ContainsKey(m);
public static void StopEffect(Mobile m, bool message)
[OnEvent(nameof(PlayerMobile.PlayerDeathEvent))]
[OnEvent(nameof(CreatureDeathEvent))]
public static void StopEffect(Mobile m, bool message = false)
{
if (m_Table.Remove(m, out var timer))
{
@ -170,7 +173,7 @@ namespace Server.Mobiles
{
if (!m.Alive)
{
StopEffect(m, false);
StopEffect(m);
return;
}
@ -197,7 +200,7 @@ namespace Server.Mobiles
if (!m.Alive)
{
StopEffect(m, false);
StopEffect(m);
}
}
}

View file

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using ModernUO.CodeGeneratedEvents;
using Server.Accounting;
using Server.Collections;
using Server.ContextMenus;
@ -34,7 +35,6 @@ using Server.Spells.Mysticism;
using Server.Spells.Necromancy;
using Server.Spells.Ninjitsu;
using Server.Spells.Second;
using Server.Spells.Seventh;
using Server.Spells.Sixth;
using Server.Spells.Spellweaving;
using Server.Targeting;
@ -97,7 +97,7 @@ namespace Server.Mobiles
DismountRecovery = 1070859 // You cannot mount while recovering from a dismount special maneuver.
}
public class PlayerMobile : Mobile, IHonorTarget, IHasSteps
public partial class PlayerMobile : Mobile, IHonorTarget, IHasSteps
{
private static bool m_NoRecursion;
@ -1089,7 +1089,7 @@ namespace Server.Mobiles
}
else if (AnimalForm.UnderTransformation(this))
{
AnimalForm.RemoveContext(this, true);
AnimalForm.RemoveContext(this);
}
}
@ -2549,6 +2549,9 @@ namespace Server.Mobiles
return res;
}
[GeneratedEvent(nameof(PlayerDeathEvent))]
public static partial void PlayerDeathEvent(PlayerMobile m);
public override void OnDeath(Container c)
{
if (m_NonAutoreinsuredItems > 0)
@ -2563,27 +2566,14 @@ namespace Server.Mobiles
HueMod = -1;
NameMod = null;
SavagePaintExpiration = TimeSpan.Zero;
SetHairMods(-1, -1);
PolymorphSpell.StopTimer(this);
IncognitoSpell.StopTimer(this);
DisguisePersistence.RemoveTimer(this);
AnimalForm.RemoveContext(this, true);
EndAction<PolymorphSpell>();
EndAction<IncognitoSpell>();
MeerMage.StopEffect(this, false);
if (Flying)
{
Flying = false;
BuffInfo.RemoveBuff(this, BuffIcon.Fly);
}
StolenItem.ReturnOnDeath(this, c);
if (PermaFlags.Count > 0)
{
PermaFlags.Clear();
@ -2669,10 +2659,6 @@ namespace Server.Mobiles
Guilds.Guild.HandleDeath(this, killer);
MLQuestSystem.HandleDeath(this);
DuelContext?.OnDeath(this, c);
if (m_BuffTable != null)
{
using var queue = PooledRefQueue<BuffInfo>.Create();
@ -2690,6 +2676,8 @@ namespace Server.Mobiles
RemoveBuff(queue.Dequeue());
}
}
PlayerDeathEvent(this);
}
public override bool MutateSpeech(List<Mobile> hears, ref string text, ref object context)

View file

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using ModernUO.CodeGeneratedEvents;
using Server.Engines.ConPVP;
using Server.Engines.Stealables;
using Server.Factions;
@ -465,10 +466,13 @@ namespace Server.SkillHandlers
return false;
}
public static void ReturnOnDeath(Mobile killed, Container corpse)
[OnEvent(nameof(PlayerMobile.PlayerDeathEvent))]
public static void ReturnOnDeath(Mobile killed)
{
Clean();
var corpse = killed.Corpse;
foreach (var si in m_Queue)
{
if (si.Stolen.RootParent == corpse && si.Victim != null && !si.IsExpired)

View file

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using ModernUO.CodeGeneratedEvents;
using Server.Factions;
using Server.Items;
using Server.Mobiles;
@ -124,6 +125,13 @@ namespace Server.Spells.Fifth
FinishSequence();
}
[OnEvent(nameof(PlayerMobile.PlayerDeathEvent))]
public static void OnPlayerDeathEvent(Mobile m)
{
StopTimer(m);
m.EndAction<IncognitoSpell>();
}
public static void StopTimer(Mobile m)
{
if (_table.Remove(m, out var timerToken))

View file

@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using ModernUO.CodeGeneratedEvents;
using Server.Mobiles;
using Server.Targeting;
namespace Server.Spells.Mysticism;
@ -25,11 +27,6 @@ public class SpellPlagueSpell : MysticSpell, ITargetingSpell<Mobile>
public override SpellCircle Circle => SpellCircle.Seventh;
public static void Initialize()
{
EventSink.PlayerDeath += OnPlayerDeath;
}
public override void OnCast()
{
Caster.Target = new SpellTarget<Mobile>(this, TargetFlags.Harmful);
@ -75,6 +72,7 @@ public class SpellPlagueSpell : MysticSpell, ITargetingSpell<Mobile>
public static bool UnderEffect(Mobile m) => _table.ContainsKey(m);
[OnEvent(nameof(PlayerMobile.PlayerDeathEvent))]
public static bool RemoveEffect(Mobile m)
{
if (_table.Remove(m, out var timer))
@ -95,8 +93,6 @@ public class SpellPlagueSpell : MysticSpell, ITargetingSpell<Mobile>
}
}
private static void OnPlayerDeath(Mobile m) => RemoveEffect(m);
private static void VisualEffect(Mobile to)
{
to.PlaySound(0x658);

View file

@ -1,5 +1,7 @@
using System.Collections.Generic;
using ModernUO.CodeGeneratedEvents;
using Server.Factions;
using Server.Mobiles;
using Server.Spells.Fifth;
using Server.Spells.Ninjitsu;
using Server.Spells.Seventh;
@ -26,11 +28,6 @@ namespace Server.Spells.Mysticism
public override SpellCircle Circle => SpellCircle.Fourth;
public static void Initialize()
{
EventSink.PlayerDeath += OnPlayerDeath;
}
public static bool UnderEffect(Mobile m) => _table.ContainsKey(m);
public override bool CheckCast()
@ -137,6 +134,7 @@ namespace Server.Spells.Mysticism
FinishSequence();
}
[OnEvent(nameof(PlayerMobile.PlayerDeathEvent))]
public static void RemoveEffects(Mobile m)
{
if (!_table.Remove(m, out var mods))
@ -154,10 +152,5 @@ namespace Server.Spells.Mysticism
BuffInfo.RemoveBuff(m, BuffIcon.StoneForm);
}
private static void OnPlayerDeath(Mobile m)
{
RemoveEffects(m);
}
}
}

View file

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using ModernUO.CodeGeneratedEvents;
using Server.Gumps;
using Server.Items;
using Server.Mobiles;
@ -135,7 +136,7 @@ public class AnimalForm : NinjaSpell
}
else if (context != null)
{
RemoveContext(Caster, context, true);
RemoveContext(Caster, context);
Caster.Mana -= mana;
}
else
@ -254,17 +255,18 @@ public class AnimalForm : NinjaSpell
}
}
public static void RemoveContext(Mobile m, bool resetGraphics)
[OnEvent(nameof(PlayerMobile.PlayerDeathEvent))]
public static void RemoveContext(Mobile m)
{
var context = GetContext(m);
if (context != null)
{
RemoveContext(m, context, resetGraphics);
RemoveContext(m, context);
}
}
public static void RemoveContext(Mobile m, AnimalFormContext context, bool resetGraphics)
public static void RemoveContext(Mobile m, AnimalFormContext context)
{
_table.Remove(m);
@ -287,11 +289,8 @@ public class AnimalForm : NinjaSpell
m.RemoveSkillMod(mod);
}
if (resetGraphics)
{
m.HueMod = -1;
m.BodyMod = 0;
}
m.HueMod = -1;
m.BodyMod = 0;
m.FixedParticles(0x3728, 10, 13, 2023, EffectLayer.Waist);
@ -517,7 +516,7 @@ public class AnimalFormTimer : Timer
{
if (_mobile.Deleted || !_mobile.Alive || _mobile.Body != _body || _mobile.Hue != _hue)
{
AnimalForm.RemoveContext(_mobile, true);
AnimalForm.RemoveContext(_mobile);
Stop();
return;
}

View file

@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using ModernUO.CodeGeneratedEvents;
using Server.Factions;
using Server.Gumps;
using Server.Items;
using Server.Mobiles;
using Server.Spells.Fifth;
namespace Server.Spells.Seventh
@ -184,6 +186,13 @@ namespace Server.Spells.Seventh
FinishSequence();
}
[OnEvent(nameof(PlayerMobile.PlayerDeathEvent))]
public static void OnPlayerDeathEvent(Mobile m)
{
StopTimer(m);
m.EndAction<PolymorphSpell>();
}
public static void StopTimer(Mobile m)
{
if (_table.Remove(m, out var timer))

View file

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using ModernUO.CodeGeneratedEvents;
using Server.Gumps;
using Server.Mobiles;
using Server.Targeting;
@ -70,17 +71,14 @@ namespace Server.Spells.Spellweaving
}
}
public static void Initialize()
{
EventSink.PlayerDeath += HandleDeath;
}
public override void OnCast()
{
Caster.Target = new SpellTarget<Mobile>(this, TargetFlags.Beneficial);
}
public static void HandleDeath(Mobile m)
[OnEvent(nameof(BaseCreature.CreatureDeathEvent))]
[OnEvent(nameof(PlayerMobile.PlayerDeathEvent))]
public static void OnDeathEvent(Mobile m)
{
if (_table.ContainsKey(m))
{
@ -130,16 +128,6 @@ namespace Server.Spells.Spellweaving
timer.DoExpire();
}
public static void OnLogin(Mobile m)
{
if (m?.Alive != false || _table[m] == null)
{
return;
}
HandleDeath_OnCallback(m);
}
private class ExpireTimer : Timer
{
private Mobile _mobile;

View file

@ -42,6 +42,8 @@
<PackageReference Include="Microsoft.Extensions.FileSystemGlobbing" Version="8.0.0" />
<PackageReference Include="CommunityToolkit.HighPerformance" Version="8.2.2" />
<PackageReference Include="Argon2.Bindings" Version="1.16.1" />
<PackageReference Include="ModernUO.CodeGeneratedEvents.Annotations" Version="1.0.0" />
<PackageReference Include="ModernUO.CodeGeneratedEvents.Generator" Version="1.0.3.2" PrivateAssets="all" />
<PackageReference Include="Zstd.Binaries" Version="1.6.0" />
<PackageReference Include="ModernUO.Serialization.Annotations" Version="2.9.1" />