|
3 | 3 | using LabApi.Events.Arguments.ServerEvents; |
4 | 4 | using LabApi.Events.Handlers; |
5 | 5 |
|
6 | | -using LabApi.Features.Enums; |
7 | 6 | using LabApi.Features.Permissions; |
8 | 7 |
|
| 8 | +using LabExtended.Commands.Runners; |
9 | 9 | using LabExtended.Commands.Utilities; |
10 | 10 | using LabExtended.Commands.Attributes; |
11 | 11 | using LabExtended.Commands.Interfaces; |
|
14 | 14 |
|
15 | 15 | using LabExtended.API; |
16 | 16 | using LabExtended.Core; |
17 | | -using LabExtended.Attributes; |
18 | | -using LabExtended.Commands.Runners; |
19 | 17 | using LabExtended.Extensions; |
20 | | -using LabExtended.Utilities.Unity; |
21 | | - |
22 | | -using MEC; |
23 | 18 |
|
24 | 19 | using NorthwoodLib.Pools; |
25 | | -#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type. |
26 | 20 |
|
| 21 | +#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type. |
27 | 22 | #pragma warning disable CS8604 // Possible null reference argument. |
28 | 23 | #pragma warning disable CS8601 // Possible null reference assignment. |
29 | 24 | #pragma warning disable CS8602 // Dereference of a possibly null reference. |
@@ -108,47 +103,50 @@ public static List<CommandData> RegisterCommands(this Assembly assembly) |
108 | 103 | { |
109 | 104 | if (method.IsStatic) |
110 | 105 | continue; |
111 | | - |
112 | | - if (!method.HasAttribute<CommandOverloadAttribute>(out var commandOverloadAttribute)) |
113 | | - continue; |
114 | | - |
115 | | - if (method.ReturnType != typeof(void) && method.ReturnType != typeof(IEnumerator<float>) && method.ReturnType != typeof(Task)) |
116 | | - { |
117 | | - ApiLog.Warn("Command Manager", $"Method &3{method.GetMemberName()}&r cannot be used as an overload " + |
118 | | - $"because it's return type is not supported (&1{method.ReturnType.FullName}&r). " + |
119 | | - $"Command method's should return only &1void&r, &1IEnumerator<float>&r coroutine or a &1Task&r."); |
120 | | - continue; |
121 | | - } |
122 | | - |
123 | | - var overload = new CommandOverload(method); |
124 | | - |
125 | | - overload.Name = commandOverloadAttribute.Name; |
126 | | - overload.Description = commandOverloadAttribute.Description; |
127 | 106 |
|
128 | | - if (commandOverloadAttribute.isDefaultOverload) |
| 107 | + foreach (var commandOverloadAttribute in method.GetCustomAttributes<CommandOverloadAttribute>(false)) |
129 | 108 | { |
130 | | - if (instance.DefaultOverload != null) |
| 109 | + if (method.ReturnType != typeof(void) |
| 110 | + && method.ReturnType != typeof(IEnumerator<float>) |
| 111 | + && method.ReturnType != typeof(Task)) |
131 | 112 | { |
132 | | - ApiLog.Error("Command Manager", $"Method &1{method.GetMemberName()}&r in command &1{instance.Name}&r " + |
133 | | - $"was specified as the default overload, but the command already has one " + |
134 | | - $"(&3{instance.DefaultOverload.Target.GetMemberName()}&r)"); |
135 | | - |
| 113 | + ApiLog.Warn("Command Manager", $"Method &3{method.GetMemberName()}&r cannot be used as an overload " + |
| 114 | + $"because it's return type is not supported (&1{method.ReturnType.FullName}&r). " + |
| 115 | + $"Command method's should return only &1void&r, &1IEnumerator<float>&r coroutine or a &1Task&r."); |
136 | 116 | continue; |
137 | 117 | } |
138 | | - |
139 | | - instance.DefaultOverload = overload; |
140 | | - } |
141 | | - else |
142 | | - { |
143 | | - if (instance.Overloads.ContainsKey(commandOverloadAttribute.Name)) |
| 118 | + |
| 119 | + var overload = new CommandOverload(method); |
| 120 | + |
| 121 | + overload.Name = commandOverloadAttribute.Name; |
| 122 | + overload.Permission = commandOverloadAttribute.Permission; |
| 123 | + overload.Description = commandOverloadAttribute.Description; |
| 124 | + |
| 125 | + if (commandOverloadAttribute.isDefaultOverload) |
144 | 126 | { |
145 | | - ApiLog.Error("Command Manager", $"Method &1{method.GetMemberName()}&r in command &1{instance.Name}&r" + |
146 | | - $"cannot be added as an overload because an overload with the same name already exists."); |
147 | | - |
148 | | - continue; |
| 127 | + if (instance.DefaultOverload != null) |
| 128 | + { |
| 129 | + ApiLog.Error("Command Manager", $"Method &1{method.GetMemberName()}&r in command &1{instance.Name}&r " + |
| 130 | + $"was specified as the default overload, but the command already has one " + |
| 131 | + $"(&3{instance.DefaultOverload.Target.GetMemberName()}&r)"); |
| 132 | + |
| 133 | + continue; |
| 134 | + } |
| 135 | + |
| 136 | + instance.DefaultOverload = overload; |
149 | 137 | } |
| 138 | + else |
| 139 | + { |
| 140 | + if (instance.Overloads.ContainsKey(commandOverloadAttribute.Name)) |
| 141 | + { |
| 142 | + ApiLog.Error("Command Manager", $"Method &1{method.GetMemberName()}&r in command &1{instance.Name}&r" + |
| 143 | + $"cannot be added as an overload because an overload with the same name already exists."); |
150 | 144 |
|
151 | | - instance.Overloads.Add(commandOverloadAttribute.Name, overload); |
| 145 | + continue; |
| 146 | + } |
| 147 | + |
| 148 | + instance.Overloads.Add(commandOverloadAttribute.Name, overload); |
| 149 | + } |
152 | 150 | } |
153 | 151 | } |
154 | 152 |
|
@@ -266,6 +264,20 @@ private static void OnCommand(CommandExecutingEventArgs ev) |
266 | 264 | overload = command.DefaultOverload; |
267 | 265 | } |
268 | 266 |
|
| 267 | + if (overload.Permission != null && !player.HasPermissions(overload.Permission)) |
| 268 | + { |
| 269 | + var response = |
| 270 | + CommandResponseFormatter.FormatMissingPermissionsFailure(overload.Permission, $"{command.Name} {overload.Name}", |
| 271 | + ev.CommandType); |
| 272 | + |
| 273 | + ev.WriteError(response, "red"); |
| 274 | + |
| 275 | + ServerEvents.OnCommandExecuted(new(ev.Sender, ev.CommandType, null, ev.Arguments, false, response)); |
| 276 | + |
| 277 | + ListPool<string>.Shared.Return(args); |
| 278 | + return; |
| 279 | + } |
| 280 | + |
269 | 281 | line = string.Join(" ", args); |
270 | 282 |
|
271 | 283 | var tokens = ListPool<ICommandToken>.Shared.Rent(); |
|
0 commit comments