From d1e9862c6b764ff1ab9196eff374b0458f7a286d Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Fri, 3 Mar 2017 19:24:24 +1100 Subject: [PATCH] Better Chat class --- MCGalaxy/Chat/Chat.cs | 76 ++++++++++++--------- MCGalaxy/Chat/ChatModes.cs | 4 +- MCGalaxy/Chat/ChatTokens.cs | 2 +- MCGalaxy/Commands/Chat/CmdChatRoom.cs | 12 ++-- MCGalaxy/Commands/Moderation/CmdTempRank.cs | 2 +- MCGalaxy/Commands/Moderation/CmdUnban.cs | 5 +- MCGalaxy/Player/Player.Handlers.cs | 2 +- MCGalaxy/Player/PlayerInfo.cs | 1 + 8 files changed, 56 insertions(+), 48 deletions(-) diff --git a/MCGalaxy/Chat/Chat.cs b/MCGalaxy/Chat/Chat.cs index ec86aeed8..8b88b1321 100644 --- a/MCGalaxy/Chat/Chat.cs +++ b/MCGalaxy/Chat/Chat.cs @@ -18,50 +18,54 @@ using System.Text; namespace MCGalaxy { public static class Chat { - public static void GlobalChatLevel(Player from, string message, bool showname) { + public static void GlobalChatLevel(Player source, string message, bool showname) { if (showname) - message = "" + from.FullName + ": &f" + message; + message = "" + source.FullName + ": &f" + message; + Player[] players = PlayerInfo.Online.Items; foreach (Player p in players) { - if (p.level == from.level && p.Chatroom == null) - SendMessage(p, from, message); + if (!NotIgnoring(source, p)) continue; + + if (p.level == source.level && p.Chatroom == null) + Player.Message(p, message); } } - - public static void GlobalChatRoom(Player from, string message, bool showname) { - string rawMessage = message; - if (showname) - message = " " + from.FullName + ": &f" + message; + + /// Sends a message to all players who are in any chatroom, + /// and are not ignoring source player. + /// Optionally prefixes message by <GlobalChatRoom> [source name]: + public static void MessageAllChatRooms(Player source, string message, bool showPrefix) { + Server.s.Log("" + source.name + ": " + message); + if (showPrefix) + message = " " + source.FullName + ": &f" + message; Player[] players = PlayerInfo.Online.Items; foreach (Player p in players) { + if (!NotIgnoring(source, p)) continue; + if (p.Chatroom != null) - SendMessage(p, from, message); + Player.Message(p, message); } - Server.s.Log("" + from.name + ": " + rawMessage); } - public static void ChatRoom(Player from, string message, bool showname, string chatroom) { - string rawMessage = message; - string messageforspy = " " + from.FullName + ": &f" + message; - if (showname) - message = " " + from.FullName + ": &f" + message; + /// Sends a message to all players who are either in or spying on the given chatroom, + /// and are not ignoring source player. + /// Optionally prefixes message by <ChatRoom: [chatRoom]> [source name]: + public static void MessageChatRoom(Player source, string message, bool showPrefix, string chatRoom) { + Server.s.Log("" + source.name + ": " + message); + string spyMessage = " " + source.FullName + ": &f" + message; + if (showPrefix) + message = " " + source.FullName + ": &f" + message; Player[] players = PlayerInfo.Online.Items; foreach (Player p in players) { - if (p.Chatroom == chatroom) - SendMessage(p, from, message); - if (p.spyChatRooms.Contains(chatroom) && p.Chatroom != chatroom) - SendMessage(p, from, messageforspy); + if (!NotIgnoring(source, p)) continue; + + if (p.Chatroom == chatRoom) + Player.Message(p, message); + if (p.spyChatRooms.Contains(chatRoom) && p.Chatroom != chatRoom) + Player.Message(p, spyMessage); } - Server.s.Log("" + from.name + ": " + rawMessage); - } - - static void SendMessage(Player p, Player from, string message) { - if (from != null && p.listignored.Contains(from.name)) return; - - if (!p.ignoreAll || (from != null && from == p)) - Player.Message(p, Server.DefaultColor + message); } @@ -90,7 +94,7 @@ namespace MCGalaxy { MessageWhere(message, pl => pl.Rank >= rank); } - /// Sends a message to all players, who do not have + /// Sends a message to all players, who do not have /// isolated level/level only chat, and are not in a chatroom. public static void MessageAll(string message) { message = Colors.EscapeColors(message); @@ -100,12 +104,13 @@ namespace MCGalaxy { p.SendMessage(message, true); } } - - public static string Format(string message, Player p, bool colors = true, + + + public static string Format(string message, Player p, bool colors = true, bool tokens = true, bool emotes = true) { if (colors) message = Colors.EscapeColors(message); StringBuilder sb = new StringBuilder(message); - if (colors) ParseColors(p, sb); + if (colors) ParseColors(p, sb); if (tokens) ChatTokens.Apply(sb, p); if (!emotes) return sb.ToString(); @@ -137,6 +142,13 @@ namespace MCGalaxy { } } } + } + + /// Returns true if the target player can see chat messags by source. + public static bool NotIgnoring(Player source, Player target) { + if (target.ignoreAll) return source == target; // don't ignore messages from self + + return source == null || !target.listignored.Contains(source.name); } diff --git a/MCGalaxy/Chat/ChatModes.cs b/MCGalaxy/Chat/ChatModes.cs index 5bc9c1395..0a722210b 100644 --- a/MCGalaxy/Chat/ChatModes.cs +++ b/MCGalaxy/Chat/ChatModes.cs @@ -76,10 +76,8 @@ namespace MCGalaxy { string format = "To " + group + " &f-{0}&f- {1}"; Chat.MessageWhere(format, - pl => pl.Rank >= perm && !pl.listignored.Contains(name), + pl => (p == pl || pl.Rank >= perm) && Chat.NotIgnoring(p, pl), displayName, message); - if (p != null && p.Rank < perm) - Player.Message(p, format, displayName, message); Server.s.Log("(" + group + "): " + name + ": " + message); Server.IRC.Say(displayName + "%S: " + message, true); diff --git a/MCGalaxy/Chat/ChatTokens.cs b/MCGalaxy/Chat/ChatTokens.cs index 357a071f6..e2be64bd0 100644 --- a/MCGalaxy/Chat/ChatTokens.cs +++ b/MCGalaxy/Chat/ChatTokens.cs @@ -89,7 +89,7 @@ namespace MCGalaxy { string line; while ((line = r.ReadLine()) != null) { if (line.StartsWith("//")) continue; - string[] split = line.Split(new[] char{ ':' }, 2); + string[] split = line.Split(new char[] { ':' }, 2); if (split.Length == 2 && !String.IsNullOrEmpty(split[0])) CustomTokens.Add(split[0], split[1]); } diff --git a/MCGalaxy/Commands/Chat/CmdChatRoom.cs b/MCGalaxy/Commands/Chat/CmdChatRoom.cs index 970f00ab2..99942179b 100644 --- a/MCGalaxy/Commands/Chat/CmdChatRoom.cs +++ b/MCGalaxy/Commands/Chat/CmdChatRoom.cs @@ -90,7 +90,7 @@ namespace MCGalaxy.Commands { } Player.Message(p, "You joined the chat room '{0}'", room); - Chat.ChatRoom(p, p.ColoredName + " %Shas joined your chat room", false, room); + Chat.MessageChatRoom(p, p.ColoredName + " %Shas joined your chat room", false, room); p.Chatroom = room; } else { Player.Message(p, "There is no chat room with that name"); @@ -99,7 +99,7 @@ namespace MCGalaxy.Commands { void HandleLeave(Player p) { Player.Message(p, "You left the chat room '{0}'", p.Chatroom); - Chat.ChatRoom(p, p.ColoredName + " %Shas left the chat room", false, p.Chatroom); + Chat.MessageChatRoom(p, p.ColoredName + " %Shas left the chat room", false, p.Chatroom); Chat.MessageAll("{0} %Shas left their chat room {1}", p.ColoredName, p.Chatroom); p.Chatroom = null; } @@ -210,7 +210,7 @@ namespace MCGalaxy.Commands { } Player.Message(pl, "You've been forced to join the chat room '{0}'", room); - Chat.ChatRoom(pl, pl.ColoredName + " %Shas force joined your chat room", false, room); + Chat.MessageChatRoom(pl, pl.ColoredName + " %Shas force joined your chat room", false, room); pl.Chatroom = room; Player.Message(p, pl.ColoredName + " %Swas forced to join the chatroom '{0}' by you", room); } @@ -231,7 +231,7 @@ namespace MCGalaxy.Commands { Player.Message(pl, "You were kicked from the chat room '" + pl.Chatroom + "'"); Player.Message(p, pl.ColoredName + " %Swas kicked from the chat room '" + pl.Chatroom + "'"); - Chat.ChatRoom(pl, pl.ColoredName + " %Swas kicked from your chat room", false, pl.Chatroom); + Chat.MessageChatRoom(pl, pl.ColoredName + " %Swas kicked from your chat room", false, pl.Chatroom); pl.Chatroom = null; } @@ -239,12 +239,12 @@ namespace MCGalaxy.Commands { int length = parts.Length > 1 ? parts[0].Length + 1 : parts[0].Length; message = message.Substring( length ); if (CheckExtraPerm(p, 7)) { - Chat.GlobalChatRoom(p, message, true); + Chat.MessageAllChatRooms(p, message, true); return; } if (p.lastchatroomglobal.AddSeconds(30) < DateTime.UtcNow) { - Chat.GlobalChatRoom(p, message, true); + Chat.MessageAllChatRooms(p, message, true); p.lastchatroomglobal = DateTime.UtcNow; } else { Player.Message(p, "Sorry, you must wait 30 seconds in between each global chatroom message!!"); diff --git a/MCGalaxy/Commands/Moderation/CmdTempRank.cs b/MCGalaxy/Commands/Moderation/CmdTempRank.cs index 3bcaa3f3d..1e484fecf 100644 --- a/MCGalaxy/Commands/Moderation/CmdTempRank.cs +++ b/MCGalaxy/Commands/Moderation/CmdTempRank.cs @@ -95,7 +95,7 @@ namespace MCGalaxy.Commands.Moderation { static void Delete(Player p, string name) { bool assigned = false; StringBuilder all = new StringBuilder(); - Player who = PlayerInfo.Find(name); + Player who = PlayerInfo.FindExact(name); foreach (string line in File.ReadAllLines(Paths.TempRanksFile)) { if (!line.CaselessStarts(name)) { all.AppendLine(line); continue; } diff --git a/MCGalaxy/Commands/Moderation/CmdUnban.cs b/MCGalaxy/Commands/Moderation/CmdUnban.cs index 207163977..c3b6bae56 100644 --- a/MCGalaxy/Commands/Moderation/CmdUnban.cs +++ b/MCGalaxy/Commands/Moderation/CmdUnban.cs @@ -27,11 +27,8 @@ namespace MCGalaxy.Commands.Moderation { public override void Use(Player p, string message) { if (message == "") { Help(p); return; } string[] args = message.SplitSpaces(2); - - Player who = PlayerInfo.Find(args[0]); - string name = who == null ? args[0] : who.name; string reason = args.Length > 1 ? args[1] : "(none given)"; - Unban(p, name, reason); + Unban(p, args[0], reason); } void Unban(Player p, string name, string reason) { diff --git a/MCGalaxy/Player/Player.Handlers.cs b/MCGalaxy/Player/Player.Handlers.cs index c6a2d0ff8..acb1fade3 100644 --- a/MCGalaxy/Player/Player.Handlers.cs +++ b/MCGalaxy/Player/Player.Handlers.cs @@ -565,7 +565,7 @@ namespace MCGalaxy { } text = HandleJoker(text); - if (Chatroom != null) { Chat.ChatRoom(this, text, true, Chatroom); return; } + if (Chatroom != null) { Chat.MessageChatRoom(this, text, true, Chatroom); return; } if (!level.worldChat) { Server.s.Log("<" + name + ">[level] " + text); diff --git a/MCGalaxy/Player/PlayerInfo.cs b/MCGalaxy/Player/PlayerInfo.cs index 89e73b36e..768302a76 100644 --- a/MCGalaxy/Player/PlayerInfo.cs +++ b/MCGalaxy/Player/PlayerInfo.cs @@ -38,6 +38,7 @@ namespace MCGalaxy { } + [Obsolete("Prefer FindMatches() or FindExact()")] public static Player Find(string name) { Player[] players = PlayerInfo.Online.Items; Player match = null; int matches = 0;