Skip to content

Commit 8102082

Browse files
authored
Merge pull request #72 from binury/feat-command-aliases
feat: Command aliases
2 parents cc30b2f + 4eb0ae3 commit 8102082

File tree

3 files changed

+44
-23
lines changed

3 files changed

+44
-23
lines changed

Cove.ChatCommands/ChatCommands.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using Cove.Server.Actor;
33
using Cove.Server.Plugins;
44
using Steamworks;
5-
using System;
65

76
public class ChatCommands : CovePlugin
87
{
@@ -19,7 +18,7 @@ public override void onInit()
1918
{
2019
base.onInit();
2120

22-
RegisterCommand("users", (player, args) =>
21+
RegisterCommand(command: "users", aliases: ["players"], callback: (player, args) =>
2322
{
2423
if (!IsPlayerAdmin(player)) return;
2524
// Get the command arguments
@@ -187,7 +186,7 @@ public override void onInit()
187186
});
188187
SetCommandDescription("say", "Sends a message to all players");
189188

190-
RegisterCommand("chalkrecent", (player, args) =>
189+
RegisterCommand(command: "chalkrecent", aliases: ["recentchalk"], callback: (player, args) =>
191190
{
192191
if (!IsPlayerAdmin(player)) return;
193192

Cove/Server/Plugins/CovePlugin.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ public void SendPacketToAll(Dictionary<string, object> packet)
106106
ParentServer.sendPacketToPlayers(packet);
107107
}
108108

109+
public void RegisterCommand(string command, Action<WFPlayer, string[]> callback, string[]? aliases = null)
110+
{
111+
ParentServer.RegisterCommand(command, callback, aliases);
112+
}
113+
114+
/// Back-compat overload for existing plugins compiled against the older API
109115
public void RegisterCommand(string command, Action<WFPlayer, string[]> callback)
110116
{
111117
ParentServer.RegisterCommand(command, callback);
@@ -125,6 +131,5 @@ public bool DoesCommandExist(string command)
125131
{
126132
return ParentServer.DoseCommandExist(command);
127133
}
128-
129134
}
130135
}

Cove/Server/Server.Commands.cs

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@ public class RegisteredCommand
1616
public string Command;
1717
public string Description;
1818
public Action<WFPlayer, string[]> Callback;
19-
public RegisteredCommand(string command, string description, Action<WFPlayer, string[]> callback)
19+
public string[] Aliases;
20+
21+
public RegisteredCommand(string command, string description, Action<WFPlayer, string[]> callback, string[]? aliases = null)
2022
{
2123
Command = command.ToLower(); // make sure its lower case to not mess anything up
2224
Description = description;
2325
Callback = callback;
26+
Aliases = aliases ?? [];
27+
Aliases = [.. Aliases.Select(alias => alias.ToLower())];
2428
}
2529

2630
public void Invoke(WFPlayer player, string[] args)
@@ -40,10 +44,10 @@ public WFPlayer GetPlayer(string playerIdent)
4044
// if there is no player with the username try to find someone with that fisher ID
4145
if (selectedPlayer == null)
4246
selectedPlayer = AllPlayers.ToList().Find(p => p.FisherID.Equals(playerIdent, StringComparison.OrdinalIgnoreCase));
43-
47+
4448
return selectedPlayer;
4549
}
46-
50+
4751
public void RegisterDefaultCommands()
4852
{
4953
RegisterCommand("help", (player, args) =>
@@ -58,7 +62,7 @@ public void RegisterDefaultCommands()
5862
});
5963
SetCommandDescription("help", "Shows all commands");
6064

61-
RegisterCommand("exit", (player, args) =>
65+
RegisterCommand(command: "exit", aliases: ["shutdown"], cb: (player, args) =>
6266
{
6367
// make sure the player is the host
6468
if (player.SteamId != serverPlayer.SteamId)
@@ -79,7 +83,7 @@ public void RegisterDefaultCommands()
7983
string playerIdent = string.Join(" ", args);
8084
// try find a user with the username first
8185
var kickedplayer = GetPlayer(playerIdent);
82-
86+
8387
if (kickedplayer == null && System.Text.RegularExpressions.Regex.IsMatch(playerIdent, @"^7656119\d{10}$"))
8488
{
8589
// if it is a steam ID, try to find the player by steam ID
@@ -88,10 +92,10 @@ public void RegisterDefaultCommands()
8892
kickPlayer(steamId);
8993
return;
9094
}
91-
95+
9296
if (kickedplayer == null)
9397
{
94-
messagePlayer("That's not a player!" , player.SteamId);
98+
messagePlayer("That's not a player!", player.SteamId);
9599
}
96100
else
97101
{
@@ -109,14 +113,14 @@ public void RegisterDefaultCommands()
109113
string playerIdent = string.Join(" ", args);
110114
// try to find a user with the username first
111115
var playerToBan = GetPlayer(playerIdent);
112-
116+
113117
var previousPlayer = PreviousPlayers.ToList().Find(p => p.FisherID.Equals(playerIdent, StringComparison.OrdinalIgnoreCase));
114118
if (previousPlayer != null)
115119
{
116120
messagePlayer($"There is a previous player with that name, if you meant to ban them add a # before the ID: #{playerIdent}", player.SteamId);
117121
return;
118122
}
119-
123+
120124
previousPlayer = PreviousPlayers.ToList().Find(p => $"#{p.FisherID}".Equals(playerIdent, StringComparison.OrdinalIgnoreCase));
121125
if (previousPlayer != null)
122126
{
@@ -126,7 +130,7 @@ public void RegisterDefaultCommands()
126130
Username = previousPlayer.Username,
127131
};
128132
}
129-
133+
130134
// use regex to check if its a steam ID
131135
if (playerToBan == null && System.Text.RegularExpressions.Regex.IsMatch(playerIdent, @"^7656119\d{10}$"))
132136
{
@@ -136,11 +140,11 @@ public void RegisterDefaultCommands()
136140
banPlayer(steamId);
137141
else
138142
banPlayer(steamId, true);
139-
143+
140144
messagePlayer($"Banned player with Steam ID {playerIdent}", player.SteamId);
141145
return;
142146
}
143-
147+
144148
if (playerToBan == null)
145149
{
146150
messagePlayer("Player not found!", player.SteamId);
@@ -159,7 +163,7 @@ public void RegisterDefaultCommands()
159163
});
160164
SetCommandDescription("ban", "Bans a player from the server");
161165

162-
RegisterCommand("prev", (player, args) =>
166+
RegisterCommand(command:"prev", aliases: ["recent"], cb: (player, args) =>
163167
{
164168
if (!isPlayerAdmin(player.SteamId)) return;
165169
var sb = new StringBuilder();
@@ -174,7 +178,7 @@ public void RegisterDefaultCommands()
174178
{
175179
continue;
176180
}
177-
181+
178182
// get the time since the player left in a human readable format
179183
string timeLeft =
180184
$"{Math.Round((DateTime.UtcNow - DateTimeOffset.FromUnixTimeSeconds(prevPlayer.leftTimestamp).UtcDateTime).TotalMinutes)} minutes ago";
@@ -183,19 +187,25 @@ public void RegisterDefaultCommands()
183187
messagePlayer(sb.ToString(), player.SteamId);
184188
});
185189
SetCommandDescription("prev", "Shows a list of previous players that were connected to the server");
186-
190+
187191
}
188192

189-
public void RegisterCommand(string command, Action<WFPlayer, string[]> cb)
193+
public void RegisterCommand(string command, Action<WFPlayer, string[]> cb, string[]? aliases = null)
190194
{
195+
aliases ??= [];
191196

192197
if (Commands.Any(c => c.Command == command))
193198
{
194199
Log($"Command '{command}' is already registerd!");
195200
return;
196201
}
202+
else if (aliases.Any(alias => Commands.Find(c => c.Aliases.Contains(alias)) != null))
203+
{
204+
Log($"'{command}' has an alias that is already registerd elsewhere!");
205+
return;
206+
}
197207

198-
Commands.Add(new RegisteredCommand(command, "", cb));
208+
Commands.Add(new RegisteredCommand(command, "", cb, aliases));
199209

200210
}
201211

@@ -217,7 +227,7 @@ public void SetCommandDescription(string command, string description)
217227

218228
public void InvokeCommand(WFPlayer player, string command, string[] args)
219229
{
220-
var cmd = Commands.Find(c => c.Command == command);
230+
var cmd = FindCommand(command);
221231
if (cmd == null)
222232
{
223233
Log($"Command '{command}' not found!");
@@ -228,11 +238,18 @@ public void InvokeCommand(WFPlayer player, string command, string[] args)
228238

229239
public bool DoseCommandExist(string command)
230240
{
231-
var cmd = Commands.Find(c => c.Command == command);
241+
var cmd = FindCommand(command);
232242
if (cmd == null)
233243
return false;
234244

235245
return true;
236246
}
247+
248+
public RegisteredCommand? FindCommand(string name)
249+
{
250+
return Commands.Find(c =>
251+
c.Command == name.ToLower() || c.Aliases.Contains(name.ToLower())
252+
);
253+
}
237254
}
238255
}

0 commit comments

Comments
 (0)