feat: implement PlayerChat hook and dispatch logic
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Vintagestory.API.Common;
|
using Vintagestory.API.Common;
|
||||||
|
using Vintagestory.API.Datastructures;
|
||||||
using Vintagestory.API.Server;
|
using Vintagestory.API.Server;
|
||||||
|
|
||||||
namespace CommandHook;
|
namespace CommandHook;
|
||||||
@@ -13,6 +14,7 @@ public class CommandHookModSystem : ModSystem
|
|||||||
public override void StartServerSide(ICoreServerAPI api)
|
public override void StartServerSide(ICoreServerAPI api)
|
||||||
{
|
{
|
||||||
Mod.Logger.Notification("[CommandHook] Loaded");
|
Mod.Logger.Notification("[CommandHook] Loaded");
|
||||||
|
api.Event.PlayerChat += OnPlayerChat;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Register(string modId, CommandRegistration registration)
|
public void Register(string modId, CommandRegistration registration)
|
||||||
@@ -24,4 +26,31 @@ public class CommandHookModSystem : ModSystem
|
|||||||
}
|
}
|
||||||
list.Add(registration);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user