fix: Fixes adding items with ambiguous type lookup (#2307)

Fix [add command failing with ambiguous type names + refactor for performance

### Problem

[add blight would fail with "No type with that name was found" because multiple types contain "blight" (e.g., Server.Items.Blight, Server.Ethics.Evil.Blight, Server.Items.BlightGrippedLongbow, Server.Items.QuiverOfBlight). The old code only succeeded when exactly one type matched the search regardless of constructability and inheriting Mobi les/Items.

### Solution

Exact match takes priority: If a type's name exactly equals the search string (case-insensitive), use it directly. Otherwise, show the AddGump with all partial matches.

- [add blight → Creates Blight (exact name match)
- [add bligh → Shows gump with Blight, BlightGrippedLongbow, etc.

### Refactoring

- CommandEventArgs context: Added GetContext<T>/SetContext<T> to pass resolved type through the command chain without method signature changes
- Removed TrySetupTarget duplication: Validation now happens only in ValidateArgs, eliminating redundant code paths
- Split type matching:
  - ExactMatch(string) → Returns Type for exact name match (used by [add)
  - MatchEmptyCtor(string) → Returns ConstructorInfo[] for gump display (empty-callable constructors only)

### Memory & Performance Improvements

| Optimization                  | Benefit                                                                                      |
|-------------------------------|----------------------------------------------------------------------------------------------|
| _mobileItemTypes cache        | Filters Mobile/Item types once per assembly, reused on all subsequent searches               |
| ReadOnlySpan<string> for args | Avoids string[] heap allocations when slicing arguments                                      |
| ValueStringBuilder            | Stack-allocated string building, avoids StringBuilder heap allocation                        |
| Single type resolution        | Type resolved once in ValidateArgs, passed via context to Execute (was resolved 2-3x before) |
This commit is contained in:
Kamron Batman 2026-01-06 21:21:29 -08:00 committed by GitHub
parent c4c8b45b79
commit f4a87a8629
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 837 additions and 684 deletions

View file

@ -41,6 +41,8 @@ public class CommandEventArgs
public string[] Arguments { get; }
private Dictionary<string, object> _context;
public int Length => Arguments.Length;
public string GetString(int index) => index < 0 || index >= Arguments.Length ? "" : Arguments[index];
@ -57,6 +59,25 @@ public class CommandEventArgs
public TimeSpan GetTimeSpan(int index) =>
index < 0 || index >= Arguments.Length ? TimeSpan.Zero : Utility.ToTimeSpan(Arguments[index]);
public bool GetContext<T>(string key, out T t)
{
if (_context != null && _context.TryGetValue(key, out var value) && value is T tValue)
{
t = tValue;
return true;
}
t = default;
return false;
}
public T SetContext<T>(string key, T value)
{
_context ??= new Dictionary<string, object>();
_context[key] = value;
return value;
}
}
public static partial class EventSink