ModernUO/Projects/UOContent/Items/Special/Rares/Containers/BaseWaterContainer.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

138 lines
3.5 KiB
C#

namespace Server.Items
{
public abstract class BaseWaterContainer : Container, IHasQuantity
{
private int m_Quantity;
public BaseWaterContainer(int item_Id, bool filled)
: base(item_Id) =>
m_Quantity = filled ? MaxQuantity : 0;
public BaseWaterContainer(Serial serial)
: base(serial)
{
}
public abstract int voidItem_ID { get; }
public abstract int fullItem_ID { get; }
public abstract int MaxQuantity { get; }
public override int DefaultGumpID => 0x3e;
[CommandProperty(AccessLevel.GameMaster)]
public virtual bool IsEmpty => m_Quantity <= 0;
[CommandProperty(AccessLevel.GameMaster)]
public virtual bool IsFull => m_Quantity >= MaxQuantity;
[CommandProperty(AccessLevel.GameMaster)]
public virtual int Quantity
{
get => m_Quantity;
set
{
if (value != m_Quantity)
{
m_Quantity = value < 1 ? 0 :
value > MaxQuantity ? MaxQuantity : value;
Movable = !IsLockedDown ? IsEmpty : false;
ItemID = IsEmpty ? voidItem_ID : fullItem_ID;
if (!IsEmpty)
{
var rootParent = RootParent;
if (rootParent?.Map != null && rootParent.Map != Map.Internal)
{
MoveToWorld(rootParent.Location, rootParent.Map);
}
}
InvalidateProperties();
}
}
}
public override void OnDoubleClick(Mobile from)
{
if (IsEmpty)
{
base.OnDoubleClick(from);
}
}
public override void OnSingleClick(Mobile from)
{
if (IsEmpty)
{
base.OnSingleClick(from);
}
else
{
if (Name == null)
{
LabelTo(from, LabelNumber);
}
else
{
LabelTo(from, Name);
}
}
}
public override void OnAosSingleClick(Mobile from)
{
if (IsEmpty)
{
base.OnAosSingleClick(from);
}
else
{
if (Name == null)
{
LabelTo(from, LabelNumber);
}
else
{
LabelTo(from, Name);
}
}
}
public override void GetProperties(IPropertyList list)
{
if (IsEmpty)
{
base.GetProperties(list);
}
}
public override bool OnDragDropInto(Mobile from, Item item, Point3D p)
{
if (!IsEmpty)
{
return false;
}
return base.OnDragDropInto(from, item, p);
}
public override void Serialize(IGenericWriter writer)
{
base.Serialize(writer);
writer.Write(0); // version
writer.Write(m_Quantity);
}
public override void Deserialize(IGenericReader reader)
{
base.Deserialize(reader);
var version = reader.ReadInt();
m_Quantity = reader.ReadInt();
}
}
}