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.Engines.Harvest;
@ -29,11 +29,11 @@ public partial class FishingPole : Item
}
}
public override void GetContextMenuEntries(Mobile from, List<ContextMenuEntry> list)
public override void GetContextMenuEntries(Mobile from, ref PooledRefList<ContextMenuEntry> list)
{
base.GetContextMenuEntries(from, list);
base.GetContextMenuEntries(from, ref list);
BaseHarvestTool.AddContextMenuEntries(from, this, list, Fishing.System);
BaseHarvestTool.AddContextMenuEntries(from, this, ref list, Fishing.System);
}
public override bool CheckConflictingLayer(Mobile m, Item item, Layer layer)

View file

@ -1,6 +1,6 @@
using System;
using System.Collections.Generic;
using ModernUO.Serialization;
using Server.Collections;
using Server.ContextMenus;
using Server.Engines.Craft;
using Server.Engines.Harvest;
@ -125,14 +125,14 @@ public abstract partial class BaseHarvestTool : Item, IUsesRemaining, ICraftable
}
}
public override void GetContextMenuEntries(Mobile from, List<ContextMenuEntry> list)
public override void GetContextMenuEntries(Mobile from, ref PooledRefList<ContextMenuEntry> list)
{
base.GetContextMenuEntries(from, list);
base.GetContextMenuEntries(from, ref list);
AddContextMenuEntries(from, this, list, HarvestSystem);
AddContextMenuEntries(from, this, ref list, HarvestSystem);
}
public static void AddContextMenuEntries(Mobile from, Item item, List<ContextMenuEntry> list, HarvestSystem system)
public static void AddContextMenuEntries(Mobile from, Item item, ref PooledRefList<ContextMenuEntry> list, HarvestSystem system)
{
if (system != Mining.System)
{
@ -149,12 +149,14 @@ public abstract partial class BaseHarvestTool : Item, IUsesRemaining, ICraftable
return;
}
var miningEntry = new ContextMenuEntry(pm.ToggleMiningStone ? 6179 : 6178);
miningEntry.Color = 0x421F;
list.Add(miningEntry);
list.Add(new ContextMenuEntry(pm.ToggleMiningStone ? 6179 : 6178)
{
Color = 0x421F
});
list.Add(new ToggleMiningStoneEntry(pm, false, 6176));
list.Add(new ToggleMiningStoneEntry(pm, true, 6177));
var stoneMining = pm.StoneMining && pm.Skills.Mining.Base >= 100.0;
list.Add(new ToggleMiningStoneEntry(false, pm.ToggleMiningStone, 6176));
list.Add(new ToggleMiningStoneEntry(true, !pm.ToggleMiningStone && stoneMining, 6177));
}
private void Deserialize(IGenericReader reader, int version)
@ -178,51 +180,48 @@ public abstract partial class BaseHarvestTool : Item, IUsesRemaining, ICraftable
private class ToggleMiningStoneEntry : ContextMenuEntry
{
private PlayerMobile _mobile;
private bool _value;
private readonly bool _value;
public ToggleMiningStoneEntry(PlayerMobile mobile, bool value, int number) : base(number)
public ToggleMiningStoneEntry(bool value, bool enabled, int number) : base(number)
{
_mobile = mobile;
_value = value;
var stoneMining = mobile.StoneMining && mobile.Skills.Mining.Base >= 100.0;
if (mobile.ToggleMiningStone == value || value && !stoneMining)
{
Flags |= CMEFlags.Disabled;
}
Enabled = enabled;
}
public override void OnClick()
public override void OnClick(Mobile from, IEntity target)
{
var oldValue = _mobile.ToggleMiningStone;
if (from is not PlayerMobile pm)
{
return;
}
var oldValue = pm.ToggleMiningStone;
if (_value)
{
if (oldValue)
{
_mobile.SendLocalizedMessage(1054023); // You are already set to mine both ore and stone!
pm.SendLocalizedMessage(1054023); // You are already set to mine both ore and stone!
}
else if (!_mobile.StoneMining || _mobile.Skills.Mining.Base < 100.0)
else if (!pm.StoneMining || pm.Skills.Mining.Base < 100.0)
{
// You have not learned how to mine stone or you do not have enough skill!
_mobile.SendLocalizedMessage(1054024);
pm.SendLocalizedMessage(1054024);
}
else
{
_mobile.ToggleMiningStone = true;
_mobile.SendLocalizedMessage(1054022); // You are now set to mine both ore and stone.
pm.ToggleMiningStone = true;
pm.SendLocalizedMessage(1054022); // You are now set to mine both ore and stone.
}
}
else if (oldValue)
{
_mobile.ToggleMiningStone = false;
_mobile.SendLocalizedMessage(1054020); // You are now set to mine only ore.
pm.ToggleMiningStone = false;
pm.SendLocalizedMessage(1054020); // You are now set to mine only ore.
}
else
{
_mobile.SendLocalizedMessage(1054021); // You are already set to mine only ore!
pm.SendLocalizedMessage(1054021); // You are already set to mine only ore!
}
}
}

View file

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using ModernUO.Serialization;
using Server.Collections;
using Server.ContextMenus;
using Server.Engines.Craft;
using Server.Gumps;
@ -113,10 +114,10 @@ public partial class Runebook : Item, ISecurable, ICraftable
public override bool AllowEquippedCast(Mobile from) => true;
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);
}
private void Deserialize(IGenericReader reader, int version)

View file

@ -1,5 +1,5 @@
using System.Collections.Generic;
using ModernUO.Serialization;
using Server.Collections;
using Server.ContextMenus;
using Server.Multis;
using Server.Spells;
@ -25,9 +25,9 @@ public partial class SpellScroll : Item, ICommodity
int ICommodity.DescriptionNumber => LabelNumber;
bool ICommodity.IsDeedable => Core.ML;
public override void GetContextMenuEntries(Mobile from, List<ContextMenuEntry> list)
public override void GetContextMenuEntries(Mobile from, ref PooledRefList<ContextMenuEntry> list)
{
base.GetContextMenuEntries(from, list);
base.GetContextMenuEntries(from, ref list);
if (from.Alive && Movable)
{

View file

@ -1,6 +1,6 @@
using System;
using System.Collections.Generic;
using ModernUO.Serialization;
using Server.Collections;
using Server.ContextMenus;
using Server.Mobiles;
@ -79,14 +79,14 @@ public partial class Fukiya : Item, INinjaWeapon
NinjaWeapon.AttemptShoot((PlayerMobile)from, this);
}
public override void GetContextMenuEntries(Mobile from, List<ContextMenuEntry> list)
public override void GetContextMenuEntries(Mobile from, ref PooledRefList<ContextMenuEntry> list)
{
base.GetContextMenuEntries(from, list);
base.GetContextMenuEntries(from, ref list);
if (IsChildOf(from))
{
list.Add(new NinjaWeapon.LoadEntry(this, 6224));
list.Add(new NinjaWeapon.UnloadEntry(this, 6225));
list.Add(new NinjaWeapon.LoadEntry(6224));
list.Add(new NinjaWeapon.UnloadEntry(6225, UsesRemaining > 0));
}
}
}

View file

@ -1,6 +1,6 @@
using System;
using System.Collections.Generic;
using ModernUO.Serialization;
using Server.Collections;
using Server.ContextMenus;
using Server.Mobiles;
@ -92,14 +92,14 @@ public partial class LeatherNinjaBelt : BaseWaist, INinjaWeapon
NinjaWeapon.AttemptShoot((PlayerMobile)from, this);
}
public override void GetContextMenuEntries(Mobile from, List<ContextMenuEntry> list)
public override void GetContextMenuEntries(Mobile from, ref PooledRefList<ContextMenuEntry> list)
{
base.GetContextMenuEntries(from, list);
base.GetContextMenuEntries(from, ref list);
if (IsChildOf(from))
{
list.Add(new NinjaWeapon.LoadEntry(this, 6222));
list.Add(new NinjaWeapon.UnloadEntry(this, 6223));
list.Add(new NinjaWeapon.LoadEntry(6222));
list.Add(new NinjaWeapon.UnloadEntry(6223, UsesRemaining > 0));
}
}
}

View file

@ -329,39 +329,29 @@ public static class NinjaWeapon
public class LoadEntry : ContextMenuEntry
{
private readonly INinjaWeapon weapon;
public LoadEntry(INinjaWeapon wep, int entry)
: base(entry, 0) =>
weapon = wep;
public override void OnClick()
public LoadEntry(int entry) : base(entry, 0)
{
if (WeaponIsValid(weapon, Owner.From))
}
public override void OnClick(Mobile from, IEntity target)
{
if (target is INinjaWeapon weapon && WeaponIsValid(weapon, from))
{
Owner.From.BeginTarget(10, false, TargetFlags.Harmful, OnTarget, weapon);
from.BeginTarget(10, false, TargetFlags.Harmful, OnTarget, weapon);
}
}
}
public class UnloadEntry : ContextMenuEntry
{
private readonly INinjaWeapon weapon;
public UnloadEntry(int entry, bool enabled) : base(entry, 0) => Enabled = enabled;
public UnloadEntry(INinjaWeapon wep, int entry)
: base(entry, 0)
public override void OnClick(Mobile from, IEntity target)
{
weapon = wep;
Enabled = weapon.UsesRemaining > 0;
}
public override void OnClick()
{
if (WeaponIsValid(weapon, Owner.From))
if (target is INinjaWeapon weapon && WeaponIsValid(weapon, from))
{
Unload(Owner.From, weapon);
Unload(from, weapon);
}
}
}
}
}

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;
@ -86,10 +86,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);
SetSecureLevelEntry.AddTo(from, this, list);
base.GetContextMenuEntries(from, ref list);
SetSecureLevelEntry.AddTo(from, this, ref list);
}
public override void OnDoubleClick(Mobile from)