ModernUO/Projects/UOContent/Holiday Stuff/Valentine/2012/Items/CupidsArrow.cs
Kamron Batman ecbee17690
fix: Optimizes OPL using string interpolation (#1041)
## Breaking Changes (New API)
ObjectPropertyList supports the following API:
```cs
list.Add(500000);
list.Add(500001, stringArgument);
list.Add("Some text");
list.Add($"Some text with {argument}");
list.Add(500002, $"{arg1}\t{arg2}");
```

## Notes
1. All API uses that require a formatter like this:
    ```cs
    list.Add(500002, "{0}\t{1}", arg1, arg2);
    ```
    Should be changed to use string interpolation, for example:
    ```cs
    list.Add(500002, $"{arg1}\t{arg2}");
    ```
2. The following paradigm should no longer be used:
    ```cs
    list.Add(1061170, prop.ToString()); // strength requirement ~1_val~
    ```
    The new string interpolation API will avoid having to convert the argument to a string before writing it to the packet. Instead use the following:
    ```cs
    list.Add(1061170, $"{prop}"); // strength requirement ~1_val~
    ```

### Benchmarks
```cs
|                         Method |     Mean |   Error |  StdDev |  Gen 0 | Allocated |
|------------------------------- |---------:|--------:|--------:|-------:|----------:|
|                BenchmarkOldOPL | 241.0 ns | 0.56 ns | 0.47 ns | 0.0105 |      88 B |
| BenchmarkStringInterpolatedOPL | 199.9 ns | 2.44 ns | 2.39 ns |      - |         - |
```

### Changes
- [X] Removes crash in STArray.Return when array is null.
- [X] Fixes NPE in OPL when entity is null. Serial in packet will be 0 when entity is null.
- [X] Fixes NPE in AosAttributes when Parent is null.
- [X] Changes OPL to use string interpolation.
- [X] Introduces `IPropertyList` to allow extending PropertyList for other uses.
2022-06-02 10:09:53 -07:00

108 lines
2.9 KiB
C#

using ModernUO.Serialization;
using Server.Targeting;
namespace Server.Items
{
[SerializationGenerator(0, false)]
public partial class CupidsArrow : Item
{
[InternString]
[InvalidateProperties]
[SerializableField(0)]
[SerializableFieldAttr("[CommandProperty(AccessLevel.GameMaster)]")]
private string _from;
[InternString]
[InvalidateProperties]
[SerializableField(1)]
[SerializableFieldAttr("[CommandProperty(AccessLevel.GameMaster)]")]
private string _to;
[Constructible]
public CupidsArrow() : base(0x4F7F) => LootType = LootType.Blessed;
// TODO: Check messages
public override int LabelNumber => 1152270; // Cupid's Arrow 2012
public bool IsSigned => _from != null && _to != null;
public override void AddNameProperty(IPropertyList list)
{
base.AddNameProperty(list);
if (IsSigned)
{
list.Add(1152273, $"{_from}\t{_to}"); // ~1_val~ is madly in love with ~2_val~
}
}
public static bool CheckSeason(Mobile from)
{
if (Core.Now.Month == 2)
{
return true;
}
from.SendLocalizedMessage(1152318); // You may not use this item out of season.
return false;
}
public override void OnSingleClick(Mobile from)
{
base.OnSingleClick(from);
if (IsSigned)
{
LabelTo(from, 1152273, $"{_from}\t{_to}"); // ~1_val~ is madly in love with ~2_val~
}
}
public override void OnDoubleClick(Mobile from)
{
if (IsSigned || !CheckSeason(from))
{
return;
}
if (!IsChildOf(from.Backpack))
{
from.SendLocalizedMessage(1080063); // This must be in your backpack to use it.
return;
}
from.BeginTarget(10, false, TargetFlags.None, OnTarget);
from.SendMessage("Who do you wish to use this on?");
}
private void OnTarget(Mobile from, object targeted)
{
if (IsSigned || !IsChildOf(from.Backpack))
{
return;
}
if (targeted is Mobile m)
{
if (!m.Alive)
{
from.SendLocalizedMessage(
1152269
); // That target is dead and even Cupid's arrow won't make them love you.
return;
}
From = from.Name;
To = m.Name;
InvalidateProperties();
from.SendMessage("You inscribe the arrow.");
}
else
{
from.SendMessage("That is not a person.");
}
}
}
}