refactor: use WildcardModRegistration and ModRegistration in dispatch

This commit is contained in:
2026-06-03 18:54:25 +02:00
parent 96306b4903
commit 1fb1d41824
+22 -11
View File
@@ -7,7 +7,8 @@ namespace CommandHook;
public class CommandHookModSystem : ModSystem
{
private readonly Dictionary<string, List<CommandRegistration>> registrations = new();
private readonly Dictionary<string, WildcardModRegistration> wildcards = new();
private readonly Dictionary<string, ModRegistration> registrations = new();
public override bool ShouldLoad(EnumAppSide forSide) => forSide == EnumAppSide.Server;
@@ -17,14 +18,20 @@ public class CommandHookModSystem : ModSystem
api.Event.PlayerChat += OnPlayerChat;
}
public void RegisterWildcard(string modId, WildcardModRegistration registration)
{
wildcards[modId] = registration;
}
public void Register(string modId, CommandRegistration registration)
{
if (!registrations.TryGetValue(modId, out var list))
if (!registrations.TryGetValue(modId, out var modReg))
{
list = new List<CommandRegistration>();
registrations[modId] = list;
modReg = new ModRegistration(new Dictionary<string, CommandRegistration>());
registrations[modId] = modReg;
}
list.Add(registration);
modReg.Commands[registration.CommandFilter] = registration;
}
private void OnPlayerChat(
@@ -40,17 +47,21 @@ public class CommandHookModSystem : ModSystem
var cmdData = new CommandData(player, message);
foreach (var (_, list) in registrations)
foreach (var reg in list)
if (reg.CommandFilter == "*" || reg.CommandFilter == cmdData.CommandName)
foreach (var (_, reg) in wildcards)
reg.Before?.Invoke(ref cmdData);
foreach (var (_, modReg) in registrations)
if (modReg.Commands.TryGetValue(cmdData.CommandName, out var reg))
reg.Before?.Invoke(ref cmdData);
if (cmdData.Cancel)
consumed.value = true;
foreach (var (_, list) in registrations)
foreach (var reg in list)
if (reg.CommandFilter == "*" || reg.CommandFilter == cmdData.CommandName)
foreach (var (_, reg) in wildcards)
reg.After?.Invoke(ref cmdData);
foreach (var (_, modReg) in registrations)
if (modReg.Commands.TryGetValue(cmdData.CommandName, out var reg))
reg.After?.Invoke(ref cmdData);
}
}