Compare commits

4 Commits
4 changed files with 52 additions and 7 deletions
+14 -4
View File
@@ -6,14 +6,24 @@ public struct CommandData
{ {
public readonly IServerPlayer? Sender; public readonly IServerPlayer? Sender;
public readonly string FullCommand; public readonly string FullCommand;
public readonly bool IsPlayerCommand; public readonly string CommandName;
public bool Cancel; private byte _flags;
private const byte FlagIsPlayerCommand = 1 << 0;
private const byte FlagCancel = 1 << 1;
public bool IsPlayerCommand => (_flags & FlagIsPlayerCommand) != 0;
public bool Cancel
{
get => (_flags & FlagCancel) != 0;
set => _flags = value ? (byte)(_flags | FlagCancel) : (byte)(_flags & ~FlagCancel);
}
public CommandData(IServerPlayer? sender, string fullCommand) public CommandData(IServerPlayer? sender, string fullCommand)
{ {
Sender = sender; Sender = sender;
FullCommand = fullCommand.Length > 0 ? fullCommand.Trim() : string.Empty; FullCommand = fullCommand.Length > 0 ? fullCommand.Trim() : string.Empty;
IsPlayerCommand = sender != null; CommandName = FullCommand.Length > 1 ? FullCommand[1..].Split(' ')[0] : string.Empty;
Cancel = false; _flags = sender != null ? FlagIsPlayerCommand : (byte)0;
} }
} }
+3
View File
@@ -0,0 +1,3 @@
namespace CommandHook;
public delegate void CommandHookDelegate(ref CommandData data);
+14 -1
View File
@@ -1,14 +1,27 @@
using Vintagestory.API.Server; using System.Collections.Generic;
using Vintagestory.API.Common; using Vintagestory.API.Common;
using Vintagestory.API.Server;
namespace CommandHook; namespace CommandHook;
public class CommandHookModSystem : ModSystem public class CommandHookModSystem : ModSystem
{ {
private readonly Dictionary<string, List<CommandRegistration>> _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("[CommandHook] Loaded");
} }
public void Register(string modId, CommandRegistration registration)
{
if (!_registrations.TryGetValue(modId, out var list))
{
list = new List<CommandRegistration>();
_registrations[modId] = list;
}
list.Add(registration);
}
} }
+19
View File
@@ -0,0 +1,19 @@
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
)
{
CommandFilter = commandFilter;
Before = before;
After = after;
}
}