Skip to content

Commit

Permalink
For OnChatEvent etc events, pass message by ref to allow plugins to a…
Browse files Browse the repository at this point in the history
…lter the message if they really want to
  • Loading branch information
UnknownShadow200 committed Dec 26, 2023
1 parent ffab090 commit be77657
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 27 deletions.
15 changes: 9 additions & 6 deletions MCGalaxy/Chat/Chat.cs
Expand Up @@ -124,8 +124,9 @@ public static class Chat {
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);
Expand Down Expand Up @@ -160,8 +161,9 @@ public static class Chat {
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;

Expand Down Expand Up @@ -195,8 +197,9 @@ public static class Chat {
// 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)

Expand Down
1 change: 0 additions & 1 deletion MCGalaxy/Commands/Moderation/CmdBlockSet.cs
Expand Up @@ -43,7 +43,6 @@ public sealed class CmdBlockSet : ItemPermsCmd
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");
Expand Down
2 changes: 1 addition & 1 deletion MCGalaxy/CorePlugin/ChatHandler.cs
Expand Up @@ -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;
Expand Down
33 changes: 19 additions & 14 deletions MCGalaxy/Events/ServerEvents.cs
Expand Up @@ -29,7 +29,8 @@ public sealed class OnSendingHeartbeatEvent : IEvent<OnSendingHeartbeat>
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]); }
}
Expand Down Expand Up @@ -63,50 +64,54 @@ public sealed class OnConnectionReceivedEvent : IEvent<OnConnectionReceived>
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]); }
}
}
Expand Down
6 changes: 3 additions & 3 deletions MCGalaxy/Modules/Relay/RelayBot.cs
Expand Up @@ -321,23 +321,23 @@ public abstract class RelayBot
}
}

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;

msg = PrepareMessage(msg);
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;

msg = PrepareMessage(msg);
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;

Expand Down
4 changes: 2 additions & 2 deletions MCGalaxy/Server/FileLogger.cs
Expand Up @@ -37,8 +37,8 @@ public static class FileLogger
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;

Expand Down

0 comments on commit be77657

Please sign in to comment.