refactor: use WildcardModRegistration and ModRegistration in dispatch
This commit is contained in:
@@ -7,7 +7,8 @@ namespace CommandHook;
|
|||||||
|
|
||||||
public class CommandHookModSystem : ModSystem
|
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;
|
public override bool ShouldLoad(EnumAppSide forSide) => forSide == EnumAppSide.Server;
|
||||||
|
|
||||||
@@ -17,14 +18,20 @@ public class CommandHookModSystem : ModSystem
|
|||||||
api.Event.PlayerChat += OnPlayerChat;
|
api.Event.PlayerChat += OnPlayerChat;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void RegisterWildcard(string modId, WildcardModRegistration registration)
|
||||||
|
{
|
||||||
|
wildcards[modId] = registration;
|
||||||
|
}
|
||||||
|
|
||||||
public void Register(string modId, CommandRegistration 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>();
|
modReg = new ModRegistration(new Dictionary<string, CommandRegistration>());
|
||||||
registrations[modId] = list;
|
registrations[modId] = modReg;
|
||||||
}
|
}
|
||||||
list.Add(registration);
|
|
||||||
|
modReg.Commands[registration.CommandFilter] = 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user