ModernUO/Projects/UOContent/Items/Aquarium/FishBowl.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

143 lines
4 KiB
C#

using System.Collections.Generic;
using ModernUO.Serialization;
using Server.ContextMenus;
using Server.Network;
namespace Server.Items
{
[SerializationGenerator(0, false)]
public partial class FishBowl : BaseContainer
{
[Constructible]
public FishBowl() : base(0x241C)
{
Hue = 0x47E;
MaxItems = 1;
}
public override int LabelNumber => 1074499; // A fish bowl
[CommandProperty(AccessLevel.GameMaster)]
public bool Empty => Items.Count == 0;
[CommandProperty(AccessLevel.GameMaster)]
public BaseFish Fish => Empty ? null : Items[0] as BaseFish;
public override double DefaultWeight => 2.0;
public override void OnDoubleClick(Mobile from)
{
}
public override bool TryDropItem(Mobile from, Item dropped, bool sendFullMessage)
{
if (!CheckHold(from, dropped, sendFullMessage, true))
{
return false;
}
DropItem(dropped);
return true;
}
public override bool OnDragDrop(Mobile from, Item dropped)
{
if (!IsAccessibleTo(from))
{
from.SendLocalizedMessage(502436); // That is not accessible.
return false;
}
if (!(dropped is BaseFish))
{
from.SendLocalizedMessage(1074836); // The container can not hold that type of object.
return false;
}
if (base.OnDragDrop(from, dropped))
{
((BaseFish)dropped).StopTimer();
InvalidateProperties();
return true;
}
return false;
}
public override bool CheckItemUse(Mobile from, Item item) => item == this && base.CheckItemUse(from, item);
public override bool CheckLift(Mobile from, Item item, ref LRReason reject)
{
if (item != this)
{
reject = LRReason.CannotLift;
return false;
}
return base.CheckLift(from, item, ref reject);
}
public override void AddNameProperties(IPropertyList list)
{
base.AddNameProperties(list);
if (!Empty)
{
var fish = Fish;
if (fish != null)
{
list.Add(1074494, $"#{fish.LabelNumber}"); // Contains: ~1_CREATURE~
}
}
}
public override void GetContextMenuEntries(Mobile from, List<ContextMenuEntry> list)
{
base.GetContextMenuEntries(from, list);
if (!Empty && IsAccessibleTo(from))
{
list.Add(new RemoveCreature(this));
}
}
private class RemoveCreature : ContextMenuEntry
{
private readonly FishBowl m_Bowl;
public RemoveCreature(FishBowl bowl) : base(6242, 3) => m_Bowl = bowl;
public override void OnClick()
{
if (m_Bowl?.Deleted != false || !m_Bowl.IsAccessibleTo(Owner.From))
{
return;
}
var fish = m_Bowl.Fish;
if (fish == null)
{
return;
}
if (fish.IsLockedDown) // for legacy fish bowls
{
Owner.From.SendLocalizedMessage(1010449); // You may not use this object while it is locked down.
}
else if (!Owner.From.PlaceInBackpack(fish))
{
Owner.From.SendLocalizedMessage(1074496); // There is no room in your pack for the creature.
}
else
{
Owner.From.SendLocalizedMessage(1074495); // The creature has been removed from the fish bowl.
fish.StartTimer();
m_Bowl.InvalidateProperties();
}
}
}
}
}