Skip to content

Commit 0e82383

Browse files
committed
Command overloads can now specify multiple per-parameter parsers with selective property paths + overload names can now contain spaces too
1 parent 26fd595 commit 0e82383

50 files changed

Lines changed: 975 additions & 444 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

LabExtended/API/Custom/Items/CustomItem.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ public abstract class CustomItem : CustomObject<CustomItem>
6262
/// item-specific data.
6363
/// </summary>
6464
/// <param name="item">The item being processed. Represents the current item held by the player.</param>
65-
/// <param name="owner">The player who owns the item.</param>
6665
/// <param name="itemData">A reference to an object containing additional data associated with the item. Can be modified within the
6766
/// delegate to update item-specific information.</param>
6867
public delegate void ForEachPickupDelegate(ItemPickupBase item, ref object? itemData);
@@ -1119,7 +1118,7 @@ public virtual void OnTogglingLight(PlayerTogglingFlashlightEventArgs args, ref
11191118
/// </summary>
11201119
/// <param name="args">The event data associated with the player's flashlight toggle action, including information about the player
11211120
/// and the flashlight state.</param>
1122-
/// <param name="itemData">A reference to the item-specific data./param>
1121+
/// <param name="itemData">A reference to the item-specific data.</param>
11231122
public virtual void OnToggledLight(PlayerToggledFlashlightEventArgs args, ref object? itemData)
11241123
{
11251124

@@ -1194,7 +1193,7 @@ public virtual void OnUpgradedPickup(Scp914ProcessedPickupEventArgs args, ref ob
11941193
/// </summary>
11951194
/// <param name="args">The event data containing information about the player being disarmed and the context of the disarming
11961195
/// action.</param>
1197-
/// <param name="itemData">A reference to the item's data./param>
1196+
/// <param name="itemData">A reference to the item's data.</param>
11981197
public virtual void OnDisarming(PlayerCuffingEventArgs args, ref object? itemData)
11991198
{
12001199

LabExtended/API/PlayerStorage.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ public bool TryGet<T>(string name, out T value)
108108
/// <param name="name">The name the value was saved under.</param>
109109
/// <param name="defaultValue">The value to return if the specified name was not found.</param>
110110
/// <returns>The saved value if found, otherwise <paramref name="defaultValue"/>.</returns>
111-
public object Get(string name, object defaultValue = null)
112-
=> _storage.TryGetValue(name, out var value) ? value : defaultValue;
111+
public object Get(string name, object? defaultValue = null)
112+
=> _storage!.TryGetValue(name, out var value) ? value : defaultValue!;
113113

114114
/// <summary>
115115
/// Gets the value saved under the <paramref name="name"/>.
@@ -118,12 +118,12 @@ public object Get(string name, object defaultValue = null)
118118
/// <param name="name">The name the value was saved under.</param>
119119
/// <param name="defaultValue">The value to return if the specified name was not found.</param>
120120
/// <returns>The saved value if found, otherwise <paramref name="defaultValue"/>.</returns>
121-
public T Get<T>(string name, T defaultValue = default)
121+
public T Get<T>(string name, T? defaultValue = default)
122122
{
123-
if (_storage.TryGetValue(name, out var cachedValue) && cachedValue != null && cachedValue is T castValue)
123+
if (_storage!.TryGetValue(name, out var cachedValue) && cachedValue != null && cachedValue is T castValue)
124124
return castValue;
125125

126-
return defaultValue;
126+
return defaultValue!;
127127
}
128128

129129
/// <summary>

LabExtended/API/Toys/AdminToy.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ public byte MovementSmoothing
201201
}
202202

203203
/// <summary>
204-
/// Disables / enables position & rotation synchronization.
204+
/// Disables / enables position and rotation synchronization.
205205
/// </summary>
206206
public bool IsStatic
207207
{

LabExtended/Commands/Attributes/CommandAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class CommandAttribute : Attribute
2727
public bool IsStatic { get; set; } = true;
2828

2929
/// <summary>
30-
/// Whether or not this command should be hidden from the help command & Remote Admin's suggestions.
30+
/// Whether or not this command should be hidden from the help command and Remote Admin's suggestions.
3131
/// </summary>
3232
public bool IsHidden { get; set; } = false;
3333

LabExtended/Commands/Attributes/CommandParameterAttribute.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,49 @@ public CommandParameterAttribute()
7575
/// <param name="description">A description of the command parameter. If not specified, defaults to "No description".</param>
7676
public CommandParameterAttribute(string name, string description = "No description")
7777
{
78+
Name = name;
79+
Description = description;
80+
81+
InitRestriction();
82+
}
83+
84+
/// <summary>
85+
/// Initializes a new instance of the CommandParameterAttribute class with the specified restriction types. This
86+
/// constructor is obsolete.
87+
/// </summary>
88+
/// <remarks>This constructor is deprecated. Use the parameterless constructor and set the
89+
/// RestrictionType property separately to specify restrictions. Using this constructor will result in a
90+
/// compile-time error.</remarks>
91+
/// <param name="restrictionTypes">An array of types used to restrict the allowed values for the parameter. Only the first type in the array is
92+
/// used.</param>
93+
[Obsolete("This constructor is deprecated. Use the constructor without restrictionTypes and set RestrictionType property separately.", true)]
94+
public CommandParameterAttribute(params Type[] restrictionTypes)
95+
{
96+
if (restrictionTypes.Length > 0)
97+
RestrictionType = restrictionTypes[0];
98+
99+
InitRestriction();
100+
}
101+
102+
/// <summary>
103+
/// Initializes a new instance of the CommandParameterAttribute class with the specified name, description, and
104+
/// restriction types.
105+
/// </summary>
106+
/// <remarks>This constructor is obsolete. Use the constructor without restrictionTypes and set
107+
/// the RestrictionType property separately.</remarks>
108+
/// <param name="name">The name of the command parameter. Cannot be null.</param>
109+
/// <param name="description">A description of the command parameter. Cannot be null.</param>
110+
/// <param name="restrictionTypes">An array of types that restrict the allowed values for the parameter. If multiple types are provided, only
111+
/// the first is used.</param>
112+
[Obsolete("This constructor is deprecated. Use the constructor without restrictionTypes and set RestrictionType property separately.", true)]
113+
public CommandParameterAttribute(string name, string description, params Type[] restrictionTypes)
114+
{
115+
Name = name;
116+
Description = description;
117+
118+
if (restrictionTypes.Length > 0)
119+
RestrictionType = restrictionTypes[0];
120+
78121
InitRestriction();
79122
}
80123

LabExtended/Commands/CommandBase.cs

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,17 @@
22

33
using LabApi.Features.Enums;
44

5+
using LabExtended.Commands.Tokens;
6+
using LabExtended.Commands.Utilities;
57
using LabExtended.Commands.Interfaces;
68
using LabExtended.Commands.Parameters;
79

810
using LabExtended.API;
9-
using LabExtended.Commands.Tokens;
10-
using LabExtended.Commands.Utilities;
11-
using LabExtended.Extensions;
1211
using LabExtended.Utilities;
12+
using LabExtended.Extensions;
1313

1414
using NorthwoodLib.Pools;
1515

16-
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.
17-
1816
namespace LabExtended.Commands;
1917

2018
/// <summary>
@@ -62,66 +60,63 @@ public class CommandBase
6260
/// </summary>
6361
public string Line => Context.Line;
6462

63+
/// <summary>
64+
/// Gets the dictionary of the overload that is currently being initialized.
65+
/// </summary>
66+
public Dictionary<string, CommandParameterBuilder> Parameters { get; } = new();
67+
6568
/// <summary>
6669
/// Gets or sets the context's response.
6770
/// </summary>
6871
public CommandResponse? Response
6972
{
7073
get => Context.Response;
71-
set => Context.Response = value;
74+
set => Context.Response = value!;
7275
}
7376

74-
/// <summary>
75-
/// Called when an overload is called for the first time.
76-
/// <remarks><b><paramref name="overloadName"/> will be null if the default overload is being initialized!</b></remarks>
77-
/// </summary>
78-
/// <param name="overloadName">Name of the overload that is being initialized.</param>
79-
/// <param name="parameters">The overload's parameters.</param>
80-
public virtual void OnInitializeOverload(string? overloadName, Dictionary<string, CommandParameterBuilder> parameters) { }
81-
8277
/// <summary>
8378
/// Responds to the command.
8479
/// </summary>
8580
/// <param name="content">The message to respond with.</param>
8681
/// <param name="isSuccess">Whether or not the command was successfully executed.</param>
8782
public void Respond(object content, bool isSuccess = true)
88-
=> Response = new(isSuccess, false, false, null, content.ToString());
83+
=> Response = new(isSuccess, false, false, null!, content.ToString());
8984

9085
/// <summary>
9186
/// Responds to the command.
9287
/// </summary>
9388
/// <param name="contentBuilder">Delegate used to build the command's response.</param>
9489
/// <param name="isSuccess">Whether or not the command was successfully executed.</param>
9590
public void Respond(Action<StringBuilder> contentBuilder, bool isSuccess = true)
96-
=> Response = new(isSuccess, false, false, null, StringBuilderPool.Shared.BuildString(contentBuilder));
91+
=> Response = new(isSuccess, false, false, null!, StringBuilderPool.Shared.BuildString(contentBuilder));
9792

9893
/// <summary>
9994
/// Responds to the command with a success.
10095
/// </summary>
10196
/// <param name="content">The message to respond with.</param>
10297
public void Ok(object content)
103-
=> Response = new(true, false, false, null, content.ToString());
98+
=> Response = new(true, false, false, null!, content.ToString());
10499

105100
/// <summary>
106101
/// Responds to the command with a success.
107102
/// </summary>
108103
/// <param name="contentBuilder">The delegate used to build the command's response.</param>
109104
public void Ok(Action<StringBuilder> contentBuilder)
110-
=> Response = new(true, false, false, null, StringBuilderPool.Shared.BuildString(contentBuilder));
105+
=> Response = new(true, false, false, null!, StringBuilderPool.Shared.BuildString(contentBuilder));
111106

112107
/// <summary>
113108
/// Responds to the command with a failure.
114109
/// </summary>
115110
/// <param name="content">The message to respond with.</param>
116111
public void Fail(object content)
117-
=> Response = new(false, false, false, null, content.ToString());
112+
=> Response = new(false, false, false, null!, content.ToString());
118113

119114
/// <summary>
120115
/// Responds to the command with a failure.
121116
/// </summary>
122117
/// <param name="contentBuilder">The delegate used to build the command's response.</param>
123118
public void Fail(Action<StringBuilder> contentBuilder)
124-
=> Response = new(false, false, false, null, StringBuilderPool.Shared.BuildString(contentBuilder));
119+
=> Response = new(false, false, false, null!, StringBuilderPool.Shared.BuildString(contentBuilder));
125120

126121
/// <summary>
127122
/// Responds to the command with an input request.
@@ -155,7 +150,7 @@ void OnInput(string result)
155150
var argList = ListPool<string>.Shared.Rent();
156151

157152
var ctx = new CommandContext();
158-
var parameter = new CommandParameter(typeof(T), string.Empty, string.Empty, null, false);
153+
var parameter = new CommandParameter(typeof(T), string.Empty, string.Empty, null!, false);
159154

160155
ctx.Args = argList;
161156
ctx.Line = result;
@@ -181,7 +176,7 @@ void OnInput(string result)
181176
return;
182177
}
183178

184-
onInput?.InvokeSafe((T)parsed.Value);
179+
onInput?.InvokeSafe((T)parsed.Value!);
185180

186181
ListPool<string>.Shared.Return(argList);
187182
ListPool<ICommandToken>.Shared.Return(list);
@@ -219,17 +214,14 @@ public void Write(Action<StringBuilder> contentBuilder, bool success = true)
219214
/// <param name="content">The content of the message.</param>
220215
/// <param name="success">Whether or not to show the message as successful.</param>
221216
public void WriteThread(object content, bool success = true)
222-
{
223-
ThreadUtils.RunOnMainThread(() => Write(content, success));
224-
}
217+
=> ThreadUtils.RunOnMainThread(() => Write(content, success));
218+
225219

226220
/// <summary>
227221
/// Writes a message into the sender's console (you should use this method if you want to respond from another thread).
228222
/// </summary>
229223
/// <param name="contentBuilder">The delegate used to build the message.</param>
230224
/// <param name="success">Whether or not to show the message as successful.</param>
231225
public void WriteThread(Action<StringBuilder> contentBuilder, bool success = true)
232-
{
233-
ThreadUtils.RunOnMainThread(() => Write(contentBuilder, success));
234-
}
226+
=> ThreadUtils.RunOnMainThread(() => Write(contentBuilder, success));
235227
}

LabExtended/Commands/CommandData.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public class CommandData
5050
/// <summary>
5151
/// Gets the command's overloads.
5252
/// </summary>
53-
public Dictionary<string, CommandOverload> Overloads { get; } = new();
53+
public List<CommandOverload> Overloads { get; } = new();
5454

5555
/// <summary>
5656
/// Gets the constructor of the command's type.

0 commit comments

Comments
 (0)