ModernUO/Projects/UOContent/Context Menus/AddToSpellbookEntry.cs
Kamron Batman f58117a877
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)
> ```
2024-07-20 21:33:23 -07:00

59 lines
1.9 KiB
C#

using Server.Items;
using Server.Targeting;
namespace Server.ContextMenus
{
public class AddToSpellbookEntry : ContextMenuEntry
{
public AddToSpellbookEntry() : base(6144, 3)
{
}
public override void OnClick(Mobile from, IEntity target)
{
if (from.CheckAlive() && target is SpellScroll scroll)
{
from.Target = new InternalTarget(scroll);
}
}
private class InternalTarget : Target
{
private readonly SpellScroll m_Scroll;
public InternalTarget(SpellScroll scroll) : base(3, false, TargetFlags.None) => m_Scroll = scroll;
protected override void OnTarget(Mobile from, object targeted)
{
if (targeted is Spellbook book)
{
if (from.CheckAlive() && !m_Scroll.Deleted && m_Scroll.Movable && m_Scroll.Amount >= 1 &&
m_Scroll.CheckItemUse(from))
{
var type = Spellbook.GetTypeForSpell(m_Scroll.SpellID);
if (type != book.SpellbookType)
{
}
else if (book.HasSpell(m_Scroll.SpellID))
{
from.SendLocalizedMessage(500179); // That spell is already present in that spellbook.
}
else
{
var val = m_Scroll.SpellID - book.BookOffset;
if (val >= 0 && val < book.BookCount)
{
book.Content |= (ulong)1 << val;
m_Scroll.Consume();
from.SendSound(0x249, book.GetWorldLocation());
}
}
}
}
}
}
}
}