From a928ed94c821aa4b3d7d59fba227a953698da881 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Wed, 22 Jun 2016 14:32:52 +1000 Subject: [PATCH] Add token, allow using in bot names and skin names. --- GUI/Window.cs | 2 +- Games/ZombieSurvival/ZombieGame.Core.cs | 12 +++--- Games/ZombieSurvival/ZombieGame.cs | 4 +- Network/Player.Networking.cs | 8 ++-- Player/Chat.cs | 53 +++++++++++++++---------- Player/Entities.cs | 10 +++-- Player/Player.cs | 2 +- 7 files changed, 51 insertions(+), 40 deletions(-) diff --git a/GUI/Window.cs b/GUI/Window.cs index 5d001e90c..6e8ef2007 100644 --- a/GUI/Window.cs +++ b/GUI/Window.cs @@ -105,7 +105,7 @@ namespace MCGalaxy.Gui if (File.Exists("Changelog.txt")) { txtChangelog.Text = "Changelog for " + Server.Version + ":"; - foreach (string line in File.ReadAllLines(("Changelog.txt"))) + foreach (string line in File.ReadAllLines("Changelog.txt")) { txtChangelog.AppendText("\r\n " + line); } diff --git a/Games/ZombieSurvival/ZombieGame.Core.cs b/Games/ZombieSurvival/ZombieGame.Core.cs index cb2eacbda..abcc6f616 100644 --- a/Games/ZombieSurvival/ZombieGame.Core.cs +++ b/Games/ZombieSurvival/ZombieGame.Core.cs @@ -264,9 +264,9 @@ namespace MCGalaxy.Games { string msg = "&bInvisibility for &a" + left; if (p.HasCpeExt(CpeExt.MessageTypes)) - p.SendCpeMessage(CpeMessageType.BottomRight2, msg, true); + p.SendCpeMessage(CpeMessageType.BottomRight2, msg); else - p.SendMessage(msg, true); + p.SendMessage(msg); } } @@ -556,11 +556,11 @@ namespace MCGalaxy.Games { const string line1 = "&eLevel vote - type &a1&e, &c2&e or &93"; string line2 = "&a" + lvl1 + "&e, &c" + lvl2 + "&e, &9random"; if (p.HasCpeExt(CpeExt.MessageTypes)) { - p.SendCpeMessage(CpeMessageType.BottomRight2, line1, true); - p.SendCpeMessage(CpeMessageType.BottomRight1, line2, true); + p.SendCpeMessage(CpeMessageType.BottomRight2, line1); + p.SendCpeMessage(CpeMessageType.BottomRight1, line2); } else { - p.SendMessage(line1, true); - p.SendMessage(line2, true); + p.SendMessage(line1); + p.SendMessage(line2); } } } diff --git a/Games/ZombieSurvival/ZombieGame.cs b/Games/ZombieSurvival/ZombieGame.cs index a6b2b38cd..81eeb6f9a 100644 --- a/Games/ZombieSurvival/ZombieGame.cs +++ b/Games/ZombieSurvival/ZombieGame.cs @@ -165,7 +165,7 @@ namespace MCGalaxy.Games { void UpdatePlayerStatus(Player p) { int seconds = (int)(RoundEnd - DateTime.UtcNow).TotalSeconds; string status = GetStatusMessage(GetTimespan(seconds)); - p.SendCpeMessage(CpeMessageType.Status1, status, true); + p.SendCpeMessage(CpeMessageType.Status1, status); } internal void UpdateAllPlayerStatus() { @@ -178,7 +178,7 @@ namespace MCGalaxy.Games { Player[] players = PlayerInfo.Online.Items; foreach (Player p in players) { if (p.level != CurLevel) continue; - p.SendCpeMessage(CpeMessageType.Status1, message, true); + p.SendCpeMessage(CpeMessageType.Status1, message); } } diff --git a/Network/Player.Networking.cs b/Network/Player.Networking.cs index 0c7703ccb..834f53bc3 100644 --- a/Network/Player.Networking.cs +++ b/Network/Player.Networking.cs @@ -257,13 +257,11 @@ namespace MCGalaxy { } string ConvertMessage(string message, bool colorParse) { - if (colorParse) - message = Colors.EscapeColors(message); + if (colorParse) message = Colors.EscapeColors(message); StringBuilder sb = new StringBuilder(message); - if (colorParse) - ParseColors(sb); + if (colorParse) ParseColors(sb); - Chat.ApplyTokens(sb, this, colorParse); + Chat.ApplyTokens(sb, this); if ( Server.parseSmiley && parseSmiley ) { sb.Replace(":)", "(darksmile)"); sb.Replace(":D", "(smile)"); diff --git a/Player/Chat.cs b/Player/Chat.cs index fcbb8c350..ff9dd5b94 100644 --- a/Player/Chat.cs +++ b/Player/Chat.cs @@ -17,7 +17,7 @@ using System.Collections.Generic; using System.IO; using System.Text; using System.Text.RegularExpressions; -using TokenParser = System.Func; +using TokenParser = System.Func; namespace MCGalaxy { @@ -98,14 +98,21 @@ namespace MCGalaxy { Player.Message(p, Server.DefaultColor + message); } - public static void ApplyTokens(StringBuilder sb, Player p, bool colorParse) { + public static string ApplyTokens(string text, Player p) { + if (text.IndexOf('$') == -1) return text; + StringBuilder sb = new StringBuilder(text); + ApplyTokens(sb, p); + return sb.ToString(); + } + + public static void ApplyTokens(StringBuilder sb, Player p) { // only apply standard $tokens when necessary for (int i = 0; i < sb.Length; i++) { if (sb[i] != '$') continue; foreach (var token in standardTokens) { if (Server.disabledChatTokens.Contains(token.Key)) continue; - string value = token.Value(colorParse, p); + string value = token.Value(p); if (value == null) continue; sb.Replace(token.Key, value); } @@ -116,28 +123,30 @@ namespace MCGalaxy { } internal static Dictionary standardTokens = new Dictionary { - { "$name", (c, p) => p.DisplayName == null ? null : + { "$name", p => p.DisplayName == null ? null : (Server.dollarNames ? "$" : "") + Colors.StripColours(p.DisplayName) }, - { "$date", (c, p) => DateTime.Now.ToString("yyyy-MM-dd") }, - { "$time", (c, p) => DateTime.Now.ToString("HH:mm:ss") }, - { "$ip", (c, p) => p.ip }, - { "$serverip", (c, p) => Player.IsLocalIpAddress(p.ip) ? p.ip : Server.IP }, - { "$color", (c, p) => c ? p.color : null }, - { "$rank", (c, p) => p.group == null ? null : p.group.name }, - { "$level", (c, p) => p.level == null ? null : p.level.name }, + { "$truename", p => p.truename == null ? null : + (Server.dollarNames ? "$" : "") + p.truename }, + { "$date", p => DateTime.Now.ToString("yyyy-MM-dd") }, + { "$time", p => DateTime.Now.ToString("HH:mm:ss") }, + { "$ip", p => p.ip }, + { "$serverip", p => Player.IsLocalIpAddress(p.ip) ? p.ip : Server.IP }, + { "$color", p => p.color }, + { "$rank", p => p.group == null ? null : p.group.name }, + { "$level", p => p.level == null ? null : p.level.name }, - { "$deaths", (c, p) => p.overallDeath.ToString() }, - { "$money", (c, p) => p.money.ToString() }, - { "$blocks", (c, p) => p.overallBlocks.ToString() }, - { "$first", (c, p) => p.firstLogin.ToString() }, - { "$kicked", (c, p) => p.totalKicked.ToString() }, - { "$server", (c, p) => Server.name }, - { "$motd", (c, p) => Server.motd }, - { "$banned", (c, p) => Player.GetBannedCount().ToString() }, - { "$irc", (c, p) => Server.ircServer + " > " + Server.ircChannel }, + { "$deaths", p => p.overallDeath.ToString() }, + { "$money", p => p.money.ToString() }, + { "$blocks", p => p.overallBlocks.ToString() }, + { "$first", p => p.firstLogin.ToString() }, + { "$kicked", p => p.totalKicked.ToString() }, + { "$server", p => Server.name }, + { "$motd", p => Server.motd }, + { "$banned", p => Player.GetBannedCount().ToString() }, + { "$irc", p => Server.ircServer + " > " + Server.ircChannel }, - { "$infected", (c, p) => p.Game.TotalInfected.ToString() }, - { "$survived", (c, p) => p.Game.TotalRoundsSurvived.ToString() }, + { "$infected", p => p.Game.TotalInfected.ToString() }, + { "$survived", p => p.Game.TotalRoundsSurvived.ToString() }, }; public static Dictionary CustomTokens = new Dictionary(); diff --git a/Player/Entities.cs b/Player/Entities.cs index 7b19b6a4a..9973a2c7b 100644 --- a/Player/Entities.cs +++ b/Player/Entities.cs @@ -13,6 +13,7 @@ or implied. See the Licenses for the specific language governing permissions and limitations under the Licenses. */ using System; +using System.Text; using MCGalaxy.Games; namespace MCGalaxy { @@ -132,10 +133,13 @@ namespace MCGalaxy { } internal static void Spawn(Player dst, PlayerBot b) { + string name = Chat.ApplyTokens(b.name, dst); + string skin = Chat.ApplyTokens(b.skinName, dst); + if (dst.hasExtList) { - dst.SendExtAddEntity2(b.id, b.skinName, b.color + b.name, b.pos[0], b.pos[1], b.pos[2], b.rot[0], b.rot[1]); + dst.SendExtAddEntity2(b.id, skin, b.color + name, b.pos[0], b.pos[1], b.pos[2], b.rot[0], b.rot[1]); } else { - dst.SendSpawn(b.id, b.color + b.skinName, b.pos[0], b.pos[1], b.pos[2], b.rot[0], b.rot[1]); + dst.SendSpawn(b.id, b.color + skin, b.pos[0], b.pos[1], b.pos[2], b.rot[0], b.rot[1]); } if (Server.TablistBots) TabList.Add(dst, b); @@ -198,7 +202,7 @@ namespace MCGalaxy { #region Position updates public static byte[] GetPositionPacket(PlayerBot bot) { - // TODO: not sure why this bots only work with absolute packets + // TODO: not sure why bots only work with absolute packets byte[] buffer = new byte[10]; buffer[0] = Opcode.EntityTeleport; buffer[1] = bot.id; diff --git a/Player/Player.cs b/Player/Player.cs index 8e45e9a04..3654f6e1e 100644 --- a/Player/Player.cs +++ b/Player/Player.cs @@ -478,7 +478,7 @@ namespace MCGalaxy { if (p.ignoreAll || p.ignoreIRC) continue; if (p.level.worldChat && p.Chatroom == null) - p.SendMessage(message, true); + p.SendMessage(message); } }