Compare commits
4
Commits
1514c50d4e
...
841644741b
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
841644741b | ||
|
|
1aaea585be | ||
|
|
1fb1d41824 | ||
|
|
96306b4903 |
@@ -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;
|
||||||
{
|
|
||||||
list = new List<CommandRegistration>();
|
|
||||||
registrations[modId] = list;
|
|
||||||
}
|
}
|
||||||
list.Add(registration);
|
|
||||||
|
public void Register(string modId, string command, CommandRegistration registration)
|
||||||
|
{
|
||||||
|
if (!registrations.TryGetValue(modId, out var modReg))
|
||||||
|
{
|
||||||
|
modReg = new ModRegistration(new Dictionary<string, CommandRegistration>());
|
||||||
|
registrations[modId] = modReg;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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