Compare commits

4 Commits
3 changed files with 38 additions and 20 deletions
+24 -13
View File
@@ -7,24 +7,31 @@ namespace CommandHook;
public class CommandHookModSystem : ModSystem public class CommandHookModSystem : ModSystem
{ {
private readonly Dictionary<string, List<CommandRegistration>> registrations = new(); private readonly Dictionary<string, CommandRegistration> wildcards = new();
private readonly Dictionary<string, ModRegistration> registrations = new();
public override bool ShouldLoad(EnumAppSide forSide) => forSide == EnumAppSide.Server; public override bool ShouldLoad(EnumAppSide forSide) => forSide == EnumAppSide.Server;
public override void StartServerSide(ICoreServerAPI api) public override void StartServerSide(ICoreServerAPI api)
{ {
Mod.Logger.Notification("[CommandHook] Loaded"); Mod.Logger.Notification("Loaded");
api.Event.PlayerChat += OnPlayerChat; api.Event.PlayerChat += OnPlayerChat;
} }
public void Register(string modId, CommandRegistration registration) public void RegisterWildcard(string modId, CommandRegistration registration)
{ {
if (!registrations.TryGetValue(modId, out var list)) wildcards[modId] = registration;
}
public void Register(string modId, string command, CommandRegistration registration)
{
if (!registrations.TryGetValue(modId, out var modReg))
{ {
list = new List<CommandRegistration>(); modReg = new ModRegistration(new Dictionary<string, CommandRegistration>());
registrations[modId] = list; registrations[modId] = modReg;
} }
list.Add(registration);
modReg.Commands[command] = registration;
} }
private void OnPlayerChat( private void OnPlayerChat(
@@ -40,17 +47,21 @@ public class CommandHookModSystem : ModSystem
var cmdData = new CommandData(player, message); var cmdData = new CommandData(player, message);
foreach (var (_, list) in registrations) foreach (var (_, reg) in wildcards)
foreach (var reg in list) reg.Before?.Invoke(ref cmdData);
if (reg.CommandFilter == "*" || reg.CommandFilter == cmdData.CommandName)
foreach (var (_, modReg) in registrations)
if (modReg.Commands.TryGetValue(cmdData.CommandName, out var reg))
reg.Before?.Invoke(ref cmdData); reg.Before?.Invoke(ref cmdData);
if (cmdData.Cancel) if (cmdData.Cancel)
consumed.value = true; consumed.value = true;
foreach (var (_, list) in registrations) foreach (var (_, reg) in wildcards)
foreach (var reg in list) reg.After?.Invoke(ref cmdData);
if (reg.CommandFilter == "*" || reg.CommandFilter == cmdData.CommandName)
foreach (var (_, modReg) in registrations)
if (modReg.Commands.TryGetValue(cmdData.CommandName, out var reg))
reg.After?.Invoke(ref cmdData); reg.After?.Invoke(ref cmdData);
} }
} }
+1 -7
View File
@@ -2,17 +2,11 @@ namespace CommandHook;
public struct CommandRegistration public struct CommandRegistration
{ {
public readonly string CommandFilter;
public readonly CommandHookDelegate? Before; public readonly CommandHookDelegate? Before;
public readonly CommandHookDelegate? After; public readonly CommandHookDelegate? After;
public CommandRegistration( public CommandRegistration(CommandHookDelegate? before, CommandHookDelegate? after)
string commandFilter,
CommandHookDelegate? before,
CommandHookDelegate? after
)
{ {
CommandFilter = commandFilter;
Before = before; Before = before;
After = after; After = after;
} }
+13
View File
@@ -0,0 +1,13 @@
using System.Collections.Generic;
namespace CommandHook;
public struct ModRegistration
{
public readonly Dictionary<string, CommandRegistration> Commands;
public ModRegistration(Dictionary<string, CommandRegistration> commands)
{
Commands = commands;
}
}