Skip to content

Commit 5576f52

Browse files
add "neededRank" argument for CustomCommand flag
1 parent a2fdd96 commit 5576f52

File tree

4 files changed

+43
-4
lines changed

4 files changed

+43
-4
lines changed

Code/FlagSystem/Flags/CustomCommandFlag.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using CommandSystem;
22
using JetBrains.Annotations;
3+
using LabApi.Features.Permissions;
34
using RemoteAdmin;
45
using SER.Code.Helpers.Exceptions;
56
using SER.Code.Helpers.Extensions;
@@ -73,6 +74,13 @@ public class CustomCommandFlag : Flag
7374
"The description of the command.",
7475
AddDescription,
7576
false
77+
),
78+
new(
79+
"neededRank",
80+
"The required remote admin rank in order to have access to this command. " +
81+
"You can provide multiple ranks, and if the player has any of the listed ranks, they will be able to use the command.",
82+
AddNeededRank,
83+
false
7684
)
7785
];
7886

@@ -160,6 +168,7 @@ public bool Execute(ArraySegment<string> arguments, ICommandSender sender, out s
160168
public string Description { get; set; } = "";
161169
public ConsoleType ConsoleTypes { get; set; } = ConsoleType.Server;
162170
public string[] Usage { get; set; } = [];
171+
public string[] NeededRanks { get; set; } = [];
163172
public string GetHelp(ArraySegment<string> arguments)
164173
{
165174
return $"Description: {Description}\n" +
@@ -173,6 +182,15 @@ public string GetHelp(ArraySegment<string> arguments)
173182

174183
public static Result RunAttachedScript(CustomCommand requestingCommand, ScriptExecutor sender, string[] args)
175184
{
185+
if (requestingCommand.NeededRanks.Any() && sender is IPlayerExecutor { Player: { } player })
186+
{
187+
if (player.UserGroup is not { } group || requestingCommand.NeededRanks.All(rank => group.Name != rank))
188+
{
189+
return "This command is reserved for players with a rank: " +
190+
$"{requestingCommand.NeededRanks.JoinStrings(", ")}";
191+
}
192+
}
193+
176194
if (!ScriptCommands.TryGetValue(requestingCommand, out var flag))
177195
{
178196
return "The script that was supposed to handle this command was not found.";
@@ -264,4 +282,10 @@ private Result AddDescription(string[] args)
264282
Command.Description = args.JoinStrings(" ");
265283
return true;
266284
}
285+
286+
private Result AddNeededRank(string[] args)
287+
{
288+
Command.NeededRanks = args;
289+
return true;
290+
}
267291
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using LabApi.Features.Wrappers;
2+
3+
namespace SER.Code.ScriptSystem.Structures;
4+
5+
public interface IPlayerExecutor
6+
{
7+
public Player? Player { get; }
8+
}

Code/ScriptSystem/Structures/PlayerConsoleExecutor.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
namespace SER.Code.ScriptSystem.Structures;
1+
using LabApi.Features.Wrappers;
22

3-
public class PlayerConsoleExecutor : ScriptExecutor
3+
namespace SER.Code.ScriptSystem.Structures;
4+
5+
public class PlayerConsoleExecutor : ScriptExecutor, IPlayerExecutor
46
{
57
public required ReferenceHub Sender { get; init; }
68

9+
public Player Player => Player.Get(Sender);
10+
711
public override void Reply(string content, Script scr)
812
{
913
Sender.gameConsoleTransmission.SendToClient(content, "green");

Code/ScriptSystem/Structures/RemoteAdminExecutor.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
using RemoteAdmin;
1+
using LabApi.Features.Wrappers;
2+
using RemoteAdmin;
23

34
namespace SER.Code.ScriptSystem.Structures;
45

5-
public class RemoteAdminExecutor : ScriptExecutor
6+
public class RemoteAdminExecutor : ScriptExecutor, IPlayerExecutor
67
{
78
public required PlayerCommandSender Sender { get; init; }
89

10+
public Player? Player => Player.Get(Sender);
11+
912
public override void Reply(string content, Script scr)
1013
{
1114
Sender.Print(content);

0 commit comments

Comments
 (0)