ModernUO/Projects/UOContent/Items/Skill Items/Lumberjack/Log.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

156 lines
4.4 KiB
C#

using ModernUO.Serialization;
namespace Server.Items
{
[SerializationGenerator(2, false)]
[Flippable(0x1bdd, 0x1be0)]
public partial class Log : Item, ICommodity, IAxe
{
[InvalidateProperties]
[SerializableFieldAttr("[CommandProperty(AccessLevel.GameMaster)]")]
[SerializableField(0)]
private CraftResource _resource;
[Constructible]
public Log(int amount = 1) : this(CraftResource.RegularWood, amount)
{
}
[Constructible]
public Log(CraftResource resource) : this(resource, 1)
{
}
[Constructible]
public Log(CraftResource resource, int amount) : base(0x1BDD)
{
Stackable = true;
Weight = 2.0;
Amount = amount;
_resource = resource;
Hue = CraftResources.GetHue(resource);
}
public virtual bool Axe(Mobile from, BaseAxe axe) => TryCreateBoards(from, 0, new Board());
int ICommodity.DescriptionNumber => CraftResources.IsStandard(_resource)
? LabelNumber
: 1075062 + ((int)_resource - (int)CraftResource.RegularWood);
bool ICommodity.IsDeedable => true;
public override void GetProperties(IPropertyList list)
{
base.GetProperties(list);
if (!CraftResources.IsStandard(_resource))
{
var num = CraftResources.GetLocalizationNumber(_resource);
if (num > 0)
{
list.Add(num);
}
else
{
list.Add(CraftResources.GetName(_resource));
}
}
}
private void Deserialize(IGenericReader reader, int version)
{
_resource = version switch
{
1 => (CraftResource)reader.ReadInt(),
_ => CraftResource.RegularWood
};
}
public virtual bool TryCreateBoards(Mobile from, double skill, Item item)
{
if (Deleted || !from.CanSee(this))
{
return false;
}
if (from.Skills.Carpentry.Value < skill &&
from.Skills.Lumberjacking.Value < skill)
{
item.Delete();
from.SendLocalizedMessage(1072652); // You cannot work this strange and unusual wood.
return false;
}
ScissorHelper(from, item, 1, false);
return true;
}
}
[SerializationGenerator(0, false)]
public partial class HeartwoodLog : Log
{
[Constructible]
public HeartwoodLog(int amount = 1) : base(CraftResource.Heartwood, amount)
{
}
public override bool Axe(Mobile from, BaseAxe axe) => TryCreateBoards(from, 100, new HeartwoodBoard());
}
[SerializationGenerator(0, false)]
public partial class BloodwoodLog : Log
{
[Constructible]
public BloodwoodLog(int amount = 1) : base(CraftResource.Bloodwood, amount)
{
}
public override bool Axe(Mobile from, BaseAxe axe) => TryCreateBoards(from, 100, new BloodwoodBoard());
}
[SerializationGenerator(0, false)]
public partial class FrostwoodLog : Log
{
[Constructible]
public FrostwoodLog(int amount = 1) : base(CraftResource.Frostwood, amount)
{
}
public override bool Axe(Mobile from, BaseAxe axe) => TryCreateBoards(from, 100, new FrostwoodBoard());
}
[SerializationGenerator(0, false)]
public partial class OakLog : Log
{
[Constructible]
public OakLog(int amount = 1) : base(CraftResource.OakWood, amount)
{
}
public override bool Axe(Mobile from, BaseAxe axe) => TryCreateBoards(from, 65, new OakBoard());
}
[SerializationGenerator(0, false)]
public partial class AshLog : Log
{
[Constructible]
public AshLog(int amount = 1) : base(CraftResource.AshWood, amount)
{
}
public override bool Axe(Mobile from, BaseAxe axe) => TryCreateBoards(from, 80, new AshBoard());
}
[SerializationGenerator(0, false)]
public partial class YewLog : Log
{
[Constructible]
public YewLog(int amount = 1) : base(CraftResource.YewWood, amount)
{
}
public override bool Axe(Mobile from, BaseAxe axe) => TryCreateBoards(from, 95, new YewBoard());
}
}