Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
841644741b | ||
|
|
1aaea585be | ||
|
|
1fb1d41824 | ||
|
|
96306b4903 |
@@ -7,24 +7,31 @@ namespace CommandHook;
|
||||
|
||||
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 void StartServerSide(ICoreServerAPI api)
|
||||
{
|
||||
Mod.Logger.Notification("[CommandHook] Loaded");
|
||||
Mod.Logger.Notification("Loaded");
|
||||
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>();
|
||||
registrations[modId] = list;
|
||||
modReg = new ModRegistration(new Dictionary<string, CommandRegistration>());
|
||||
registrations[modId] = modReg;
|
||||
}
|
||||
list.Add(registration);
|
||||
|
||||
modReg.Commands[command] = 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,17 +2,11 @@ namespace CommandHook;
|
||||
|
||||
public struct CommandRegistration
|
||||
{
|
||||
public readonly string CommandFilter;
|
||||
public readonly CommandHookDelegate? Before;
|
||||
public readonly CommandHookDelegate? After;
|
||||
|
||||
public CommandRegistration(
|
||||
string commandFilter,
|
||||
CommandHookDelegate? before,
|
||||
CommandHookDelegate? after
|
||||
)
|
||||
public CommandRegistration(CommandHookDelegate? before, CommandHookDelegate? after)
|
||||
{
|
||||
CommandFilter = commandFilter;
|
||||
Before = before;
|
||||
After = after;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user