diff --git a/CommandHook/CommandHookModSystem.cs b/CommandHook/CommandHookModSystem.cs index 949a40e..1f47a5d 100644 --- a/CommandHook/CommandHookModSystem.cs +++ b/CommandHook/CommandHookModSystem.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using Vintagestory.API.Common; +using Vintagestory.API.Datastructures; using Vintagestory.API.Server; namespace CommandHook; @@ -13,6 +14,7 @@ public class CommandHookModSystem : ModSystem public override void StartServerSide(ICoreServerAPI api) { Mod.Logger.Notification("[CommandHook] Loaded"); + api.Event.PlayerChat += OnPlayerChat; } public void Register(string modId, CommandRegistration registration) @@ -24,4 +26,31 @@ public class CommandHookModSystem : ModSystem } list.Add(registration); } + + private void OnPlayerChat( + IServerPlayer player, + int channelId, + ref string message, + ref string data, + BoolRef consumed + ) + { + if (!message.StartsWith('/')) + return; + + var cmdData = new CommandData(player, message); + + foreach (var (_, list) in registrations) + foreach (var reg in list) + if (reg.CommandFilter == "*" || reg.CommandFilter == cmdData.CommandName) + 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) + reg.After?.Invoke(ref cmdData); + } }