refactor: use WildcardModRegistration and ModRegistration in dispatch
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user