For OnChatEvent etc events, pass message by ref to allow plugins to alter the message if they really want to

This commit is contained in:
UnknownShadow200 2023-12-26 21:33:34 +11:00
parent ffab090303
commit be7765797c
6 changed files with 34 additions and 27 deletions

View File

@ -124,8 +124,9 @@ namespace MCGalaxy {
Player[] players = PlayerInfo.Online.Items;
ChatMessageFilter scopeFilter = scopeFilters[(int)scope];
OnChatSysEvent.Call(scope, msg, arg, ref filter, relay);
foreach (Player pl in players) {
OnChatSysEvent.Call(scope, ref msg, arg, ref filter, relay);
foreach (Player pl in players)
{
if (!scopeFilter(pl, arg)) continue;
if (filter != null && !filter(pl, arg)) continue;
pl.Message(msg);
@ -160,8 +161,9 @@ namespace MCGalaxy {
Player[] players = PlayerInfo.Online.Items;
ChatMessageFilter scopeFilter = scopeFilters[(int)scope];
OnChatFromEvent.Call(scope, source, msg, arg, ref filter, relay);
foreach (Player pl in players) {
OnChatFromEvent.Call(scope, source, ref msg, arg, ref filter, relay);
foreach (Player pl in players)
{
if (!scopeFilter(pl, arg)) continue;
if (filter != null && !filter(pl, arg)) continue;
@ -195,8 +197,9 @@ namespace MCGalaxy {
// Filter out bad words
if (Server.Config.ProfanityFiltering) msg = ProfanityFilter.Parse(msg);
OnChatEvent.Call(scope, source, msg, arg, ref filter, relay);
foreach (Player pl in players) {
OnChatEvent.Call(scope, source, ref msg, arg, ref filter, relay);
foreach (Player pl in players)
{
if (Ignoring(pl, source)) continue;
// Always show message to self too (unless ignoring self)

View File

@ -43,7 +43,6 @@ namespace MCGalaxy.Commands.Moderation
BlockID block;
if (!CommandParser.GetBlockIfAllowed(p, args[0], "change permissions of", out block)) return;
// TODO avoid showing message twice
if (canPlace) {
BlockPerms perms = BlockPerms.GetPlace(block);
placeMsg = SetPerms(p, args, data, perms, "block", "use", "usable");

View File

@ -22,7 +22,7 @@ namespace MCGalaxy.Core {
internal static class ChatHandler {
internal static void HandleOnChat(ChatScope scope, Player source, string msg,
internal static void HandleOnChat(ChatScope scope, Player source, ref string msg,
object arg, ref ChatMessageFilter filter, bool irc) {
msg = msg.Replace("λFULL", source.name).Replace("λNICK", source.name);
LogType logType = LogType.PlayerChat;

View File

@ -29,7 +29,8 @@ namespace MCGalaxy.Events.ServerEvents
public static void Call(Heartbeat service, ref string name) {
IEvent<OnSendingHeartbeat>[] items = handlers.Items;
// Can't use CallCommon because we need to pass arguments by ref
for (int i = 0; i < items.Length; i++) {
for (int i = 0; i < items.Length; i++)
{
try { items[i].method(service, ref name); }
catch (Exception ex) { LogHandlerException(ex, items[i]); }
}
@ -63,50 +64,54 @@ namespace MCGalaxy.Events.ServerEvents
public static void Call(Socket s, ref bool cancel, ref bool announce) {
IEvent<OnConnectionReceived>[] items = handlers.Items;
// Can't use CallCommon because we need to pass arguments by ref
for (int i = 0; i < items.Length; i++) {
for (int i = 0; i < items.Length; i++)
{
try { items[i].method(s, ref cancel, ref announce); }
catch (Exception ex) { LogHandlerException(ex, items[i]); }
}
}
}
public delegate void OnChatSys(ChatScope scope, string msg, object arg,
public delegate void OnChatSys(ChatScope scope, ref string msg, object arg,
ref ChatMessageFilter filter, bool relay);
public sealed class OnChatSysEvent : IEvent<OnChatSys>
{
public static void Call(ChatScope scope, string msg, object arg,
public static void Call(ChatScope scope, ref string msg, object arg,
ref ChatMessageFilter filter, bool relay) {
IEvent<OnChatSys>[] items = handlers.Items;
for (int i = 0; i < items.Length; i++) {
try { items[i].method(scope, msg, arg, ref filter, relay); }
for (int i = 0; i < items.Length; i++)
{
try { items[i].method(scope, ref msg, arg, ref filter, relay); }
catch (Exception ex) { LogHandlerException(ex, items[i]); }
}
}
}
public delegate void OnChatFrom(ChatScope scope, Player source, string msg,
public delegate void OnChatFrom(ChatScope scope, Player source, ref string msg,
object arg, ref ChatMessageFilter filter, bool relay);
public sealed class OnChatFromEvent : IEvent<OnChatFrom>
{
public static void Call(ChatScope scope,Player source, string msg,
public static void Call(ChatScope scope,Player source, ref string msg,
object arg, ref ChatMessageFilter filter, bool relay) {
IEvent<OnChatFrom>[] items = handlers.Items;
for (int i = 0; i < items.Length; i++) {
try { items[i].method(scope, source, msg, arg, ref filter, relay); }
for (int i = 0; i < items.Length; i++)
{
try { items[i].method(scope, source, ref msg, arg, ref filter, relay); }
catch (Exception ex) { LogHandlerException(ex, items[i]); }
}
}
}
public delegate void OnChat(ChatScope scope, Player source, string msg,
public delegate void OnChat(ChatScope scope, Player source, ref string msg,
object arg, ref ChatMessageFilter filter, bool relay);
public sealed class OnChatEvent : IEvent<OnChat>
{
public static void Call(ChatScope scope, Player source, string msg,
public static void Call(ChatScope scope, Player source, ref string msg,
object arg, ref ChatMessageFilter filter, bool relay) {
IEvent<OnChat>[] items = handlers.Items;
for (int i = 0; i < items.Length; i++) {
try { items[i].method(scope, source, msg, arg, ref filter, relay); }
for (int i = 0; i < items.Length; i++)
{
try { items[i].method(scope, source, ref msg, arg, ref filter, relay); }
catch (Exception ex) { LogHandlerException(ex, items[i]); }
}
}

View File

@ -321,7 +321,7 @@ namespace MCGalaxy.Modules.Relay
}
}
void OnChatSys(ChatScope scope, string msg, object arg,
void OnChatSys(ChatScope scope, ref string msg, object arg,
ref ChatMessageFilter filter, bool relay) {
if (!relay) return;
@ -329,7 +329,7 @@ namespace MCGalaxy.Modules.Relay
MessageToRelay(scope, msg, arg, filter);
}
void OnChatFrom(ChatScope scope, Player source, string msg,
void OnChatFrom(ChatScope scope, Player source, ref string msg,
object arg, ref ChatMessageFilter filter, bool relay) {
if (!relay) return;
@ -337,7 +337,7 @@ namespace MCGalaxy.Modules.Relay
MessageToRelay(scope, Unescape(source, msg), arg, filter);
}
void OnChat(ChatScope scope, Player source, string msg,
void OnChat(ChatScope scope, Player source, ref string msg,
object arg, ref ChatMessageFilter filter, bool relay) {
if (!relay) return;

View File

@ -37,8 +37,8 @@ namespace MCGalaxy
static SchedulerTask logTask;
public static void Init() {
if (!Directory.Exists("logs")) Directory.CreateDirectory("logs");
if (!Directory.Exists("logs/errors")) Directory.CreateDirectory("logs/errors");
Server.EnsureDirectoryExists("logs");
Server.EnsureDirectoryExists("logs/errors");
UpdatePaths();
Logger.LogHandler += LogMessage;