fix: Moves ContextMenu out of core, streamlines code, fixes bugs (#1873)

## Summary
- Removes allocation of a `List<ContextMenuEntry>` every time a context menu is created.
- Moves packet/context menu creation logic out of the core
- Fixes tame entry

## BREAKING CHANGE
> [!Important]
> **Developer Note**
> ```cs
> public virtual void GetContextMenuEntries(Mobile from, List<ContextMenuEntry> list)
> ```
> and similar functions changed to
> ```cs
> public virtual void GetContextMenuEntries(Mobile from, ref PooledRefList<ContextMenuEntry> list)
> ```
This commit is contained in:
Kamron Batman 2024-07-20 21:33:23 -07:00 • committed by GitHub
parent bb24b330e1
commit f58117a877
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
88 changed files with 1206 additions and 1483 deletions

View file

@ -1,5 +1,5 @@
using System.Collections.Generic;
using ModernUO.Serialization;
using Server.Collections;
using Server.ContextMenus;
using Server.Gumps;
using Server.Mobiles;
@ -12,18 +12,18 @@ namespace Server.Items
public const int TitheRange = 2;
public const int LockRange = 2;
public static void GetContextMenuEntries(Mobile from, Item item, List<ContextMenuEntry> list)
public static void GetContextMenuEntries(Mobile from, Item item, ref PooledRefList<ContextMenuEntry> list)
{
if (from is PlayerMobile mobile)
{
list.Add(new LockKarmaEntry(mobile));
list.Add(new LockKarmaEntry(mobile.KarmaLocked));
}
list.Add(new ResurrectEntry(from, item));
list.Add(new ResurrectEntry(from.Alive));
if (Core.AOS)
{
list.Add(new TitheEntry(from));
list.Add(new TitheEntry(from.Alive));
}
}
@ -51,62 +51,58 @@ namespace Server.Items
private class ResurrectEntry : ContextMenuEntry
{
private readonly Item _item;
private readonly Mobile _mobile;
public ResurrectEntry(bool enabled) : base(6195, ResurrectRange) => Enabled = enabled;
public ResurrectEntry(Mobile mobile, Item item) : base(6195, ResurrectRange)
public override void OnClick(Mobile from, IEntity target)
{
_mobile = mobile;
_item = item;
Enabled = !_mobile.Alive;
}
public override void OnClick()
{
Resurrect(_mobile, _item);
if (target is Item item)
{
Resurrect(from, item);
}
}
}
private class LockKarmaEntry : ContextMenuEntry
public class LockKarmaEntry : ContextMenuEntry
{
private readonly PlayerMobile _mobile;
public LockKarmaEntry(PlayerMobile mobile) : base(mobile.KarmaLocked ? 6197 : 6196, LockRange) =>
_mobile = mobile;
public override void OnClick()
public LockKarmaEntry(bool karmaLocked) : base(karmaLocked ? 6197 : 6196, LockRange)
{
_mobile.KarmaLocked = !_mobile.KarmaLocked;
}
if (_mobile.KarmaLocked)
public override void OnClick(Mobile from, IEntity target)
{
if (from is not PlayerMobile pm || target is not Item item)
{
return;
}
if (!from.InRange(item.GetWorldLocation(), 2))
{
from.SendLocalizedMessage(500446); // That is too far away.
}
pm.KarmaLocked = !pm.KarmaLocked;
if (pm.KarmaLocked)
{
// Your karma has been locked. Your karma can no longer be raised.
_mobile.SendLocalizedMessage(1060192);
pm.SendLocalizedMessage(1060192);
}
else
{
_mobile.SendLocalizedMessage(1060191); // Your karma has been unlocked. Your karma can be raised again.
pm.SendLocalizedMessage(1060191); // Your karma has been unlocked. Your karma can be raised again.
}
}
}
private class TitheEntry : ContextMenuEntry
{
private readonly Mobile _mobile;
public TitheEntry(bool enabled) : base(6198, TitheRange) => Enabled = enabled;
public TitheEntry(Mobile mobile) : base(6198, TitheRange)
public override void OnClick(Mobile from, IEntity target)
{
_mobile = mobile;
Enabled = _mobile.Alive;
}
public override void OnClick()
{
if (_mobile.CheckAlive())
if (from.CheckAlive())
{
_mobile.SendGump(new TithingGump(_mobile));
from.SendGump(new TithingGump(from));
}
}
}
@ -150,10 +146,10 @@ namespace Server.Items
}
}
public override void GetContextMenuEntries(Mobile from, List<ContextMenuEntry> list)
public override void GetContextMenuEntries(Mobile from, ref PooledRefList<ContextMenuEntry> list)
{
base.GetContextMenuEntries(from, list);
Ankhs.GetContextMenuEntries(from, this, list);
base.GetContextMenuEntries(from, ref list);
Ankhs.GetContextMenuEntries(from, this, ref list);
}
public override void OnDoubleClickDead(Mobile m)
@ -244,10 +240,10 @@ namespace Server.Items
}
}
public override void GetContextMenuEntries(Mobile from, List<ContextMenuEntry> list)
public override void GetContextMenuEntries(Mobile from, ref PooledRefList<ContextMenuEntry> list)
{
base.GetContextMenuEntries(from, list);
Ankhs.GetContextMenuEntries(from, this, list);
base.GetContextMenuEntries(from, ref list);
Ankhs.GetContextMenuEntries(from, this, ref list);
}
public override void OnDoubleClickDead(Mobile m)
@ -297,10 +293,10 @@ namespace Server.Items
}
}
public override void GetContextMenuEntries(Mobile from, List<ContextMenuEntry> list)
public override void GetContextMenuEntries(Mobile from, ref PooledRefList<ContextMenuEntry> list)
{
base.GetContextMenuEntries(from, list);
Ankhs.GetContextMenuEntries(from, this, list);
base.GetContextMenuEntries(from, ref list);
Ankhs.GetContextMenuEntries(from, this, ref list);
}
public override void OnDoubleClickDead(Mobile m)
@ -393,10 +389,10 @@ namespace Server.Items
}
}
public override void GetContextMenuEntries(Mobile from, List<ContextMenuEntry> list)
public override void GetContextMenuEntries(Mobile from, ref PooledRefList<ContextMenuEntry> list)
{
base.GetContextMenuEntries(from, list);
Ankhs.GetContextMenuEntries(from, this, list);
base.GetContextMenuEntries(from, ref list);
Ankhs.GetContextMenuEntries(from, this, ref list);
}
public override void OnDoubleClickDead(Mobile m)

View file

@ -1,5 +1,5 @@
using System.Collections.Generic;
using ModernUO.Serialization;
using Server.Collections;
using Server.ContextMenus;
using Server.Gumps;
using Server.Multis;
@ -74,10 +74,10 @@ public abstract partial class BaseHouseDoor : BaseDoor, ISecurable
_level = SecureLevel.Anyone;
}
public override void GetContextMenuEntries(Mobile from, List<ContextMenuEntry> list)
public override void GetContextMenuEntries(Mobile from, ref PooledRefList<ContextMenuEntry> list)
{
base.GetContextMenuEntries(from, list);
SetSecureLevelEntry.AddTo(from, this, list);
base.GetContextMenuEntries(from, ref list);
SetSecureLevelEntry.AddTo(from, this, ref list);
}
public BaseHouse FindHouse()