From af4ade0e8859352d0aa922252abf50d84cf360f1 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Tue, 25 Jul 2017 09:14:59 +1000 Subject: [PATCH] Modularise chat tokens, also add description for what each chat token does --- MCGalaxy/Chat/ChatTokens.cs | 104 ++++++++++++-------- MCGalaxy/Commands/building/CmdPortal.cs | 4 +- MCGalaxy/Games/ZombieSurvival/ZombieGame.cs | 14 ++- 3 files changed, 77 insertions(+), 45 deletions(-) diff --git a/MCGalaxy/Chat/ChatTokens.cs b/MCGalaxy/Chat/ChatTokens.cs index 624d36f47..3ab462c0b 100644 --- a/MCGalaxy/Chat/ChatTokens.cs +++ b/MCGalaxy/Chat/ChatTokens.cs @@ -19,6 +19,16 @@ using System.Text; using MCGalaxy.Util; namespace MCGalaxy { + public sealed class ChatToken { + public readonly string Trigger; + public readonly string Description; + public readonly StringFormatter Formatter; + + public ChatToken(string trigger, string desc, StringFormatter formatter) { + Trigger = trigger; Description = desc; Formatter = formatter; + } + } + public static class ChatTokens { public static string Apply(string text, Player p) { @@ -32,59 +42,71 @@ namespace MCGalaxy { // only apply standard $tokens when necessary for (int i = 0; i < sb.Length; i++) { if (sb[i] != '$') continue; - - foreach (var token in standardTokens) { - if (ServerConfig.DisabledChatTokens.Contains(token.Key)) continue; - string value = token.Value(p); - if (value == null) continue; - sb.Replace(token.Key, value); - } - break; + ApplyStandard(sb, p); break; } - foreach (var token in CustomTokens) - sb.Replace(token.Key, token.Value); + ApplyCustom(sb); } public static string ApplyCustom(string text) { - if (CustomTokens.Count == 0) return text; + if (Custom.Count == 0) return text; StringBuilder sb = new StringBuilder(text); - foreach (var token in CustomTokens) - sb.Replace(token.Key, token.Value); + ApplyCustom(sb); return sb.ToString(); } + static void ApplyStandard(StringBuilder sb, Player p) { + foreach (ChatToken token in Standard) { + if (ServerConfig.DisabledChatTokens.Contains(token.Trigger)) continue; + string value = token.Formatter(p); + if (value != null) sb.Replace(token.Trigger, value); + } + } - internal static Dictionary> standardTokens - = new Dictionary> { - { "$name", p => p.DisplayName == null ? null : - (ServerConfig.DollarBeforeNamesToken ? "$" : "") + Colors.StripColors(p.DisplayName) }, - { "$truename", p => p.truename == null ? null : - (ServerConfig.DollarBeforeNamesToken ? "$" : "") + 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 }, + static void ApplyCustom(StringBuilder sb) { + foreach (ChatToken token in Custom) { + sb.Replace(token.Trigger, token.Description); + } + } + + + public static List Standard = new List() { + new ChatToken("$name", "Nickname of the player", + p => (ServerConfig.DollarBeforeNamesToken ? "$" : "") + Colors.StripColors(p.DisplayName)), + new ChatToken("$truename", "Account name of the player", + p => (ServerConfig.DollarBeforeNamesToken ? "$" : "") + p.truename), + new ChatToken("$date", "Current date (year-month-day)", + p => DateTime.Now.ToString("yyyy-MM-dd")), + new ChatToken("$time", "Current time of day (hour:minute:second)", + p => DateTime.Now.ToString("HH:mm:ss")), + new ChatToken("$ip", "IP of the player", p => p.ip), + new ChatToken("$serverip", "IP player connected to the server via", + p => Player.IsLocalIpAddress(p.ip) ? p.ip : Server.IP), + new ChatToken("$color", "Color code of the player's nick", p => p.color), + new ChatToken("$rank", "Name of player's rank/group", p => p.group.Name), + new ChatToken("$level", "Name of level/map player is on", + p => p.level == null ? null : p.level.name), - { "$deaths", p => p.TimesDied.ToString() }, - { "$money", p => p.money.ToString() }, - { "$blocks", p => p.TotalModified.ToString() }, - { "$first", p => p.FirstLogin.ToString() }, - { "$kicked", p => p.TimesBeenKicked.ToString() }, - { "$server", p => ServerConfig.Name }, - { "$motd", p => ServerConfig.MOTD }, - { "$banned", p => Group.BannedRank.Players.Count.ToString() }, - { "$irc", p => ServerConfig.IRCServer + " > " + ServerConfig.IRCChannels }, - - { "$infected", p => p.Game.TotalInfected.ToString() }, - { "$survived", p => p.Game.TotalRoundsSurvived.ToString() }, + new ChatToken("$deaths", "Times the player died", + p => p.TimesDied.ToString()), + new ChatToken("$money", "Amount of server currency player has", + p => p.money.ToString()), + new ChatToken("$blocks", "Number of blocks modified by the player", + p => p.TotalModified.ToString()), + new ChatToken("$first", "Date player first logged in", + p => p.FirstLogin.ToString()), + new ChatToken("$kicked", "Times the player was kicked", + p => p.TimesBeenKicked.ToString()), + new ChatToken("$server", "Server's name", p => ServerConfig.Name), + new ChatToken("$motd", "Server's motd", p => ServerConfig.MOTD), + new ChatToken("$banned", "Number of banned players", + p => Group.BannedRank.Players.Count.ToString()), + new ChatToken("$irc", "IRC server and channels", + p => ServerConfig.IRCServer + " > " + ServerConfig.IRCChannels), }; - public static Dictionary CustomTokens = new Dictionary(); + public static List Custom = new List(); internal static void LoadCustom() { - CustomTokens.Clear(); + Custom.Clear(); TextFile tokensFile = TextFile.Files["Custom $s"]; tokensFile.EnsureExists(); @@ -99,7 +121,7 @@ namespace MCGalaxy { string key = parts[0].Trim(), value = parts[1].Trim(); if (key.Length == 0) continue; - CustomTokens.Add(key, value); + Custom.Add(new ChatToken(key, value, null)); } } } diff --git a/MCGalaxy/Commands/building/CmdPortal.cs b/MCGalaxy/Commands/building/CmdPortal.cs index 87ebd904a..35478beb9 100644 --- a/MCGalaxy/Commands/building/CmdPortal.cs +++ b/MCGalaxy/Commands/building/CmdPortal.cs @@ -46,10 +46,10 @@ namespace MCGalaxy.Commands.Building { data.Block = GetBlock(p, block); if (data.Block == ExtBlock.Invalid) return; if (!CommandParser.IsBlockAllowed(p, "place a portal of", data.Block)) return; - + data.Entries = new List(); + Player.Message(p, "Place an &aEntry block %Sfor the portal"); p.ClearBlockchange(); - data.Entries = new List(); p.blockchangeObject = data; p.Blockchange += EntryChange; } diff --git a/MCGalaxy/Games/ZombieSurvival/ZombieGame.cs b/MCGalaxy/Games/ZombieSurvival/ZombieGame.cs index 7cc9f3445..f839ddf4f 100644 --- a/MCGalaxy/Games/ZombieSurvival/ZombieGame.cs +++ b/MCGalaxy/Games/ZombieSurvival/ZombieGame.cs @@ -64,7 +64,7 @@ namespace MCGalaxy.Games { CurLevel = level; } - CurLevel.SaveChanges = false; + CurLevel.SaveChanges = false; Chat.MessageGlobal("A game of zombie survival is starting on: {0}", CurLevelName); Player[] players = PlayerInfo.Online.Items; foreach (Player p in players) { @@ -312,6 +312,7 @@ namespace MCGalaxy.Games { TopStat statMostInfected, statMaxInfected, statMostSurvived, statMaxSurvived; OfflineStatPrinter offlineZSStats; OnlineStatPrinter onlineZSStats; + ChatToken infectedToken, survivedToken; void HookStats() { if (TopStat.Stats.Contains(statMostInfected)) return; // don't duplicate @@ -324,11 +325,18 @@ namespace MCGalaxy.Games { statMaxSurvived = new TopStat("ConsecutiveSurvived", "ZombieStats", "MaxRounds", () => "Most consecutive rounds survived", TopStat.FormatInteger); + infectedToken = new ChatToken("$infected", "Total number of players infected", + p => p.Game.TotalInfected.ToString()); + survivedToken = new ChatToken("$survived", "Total number of rounds survived", + p => p.Game.TotalRoundsSurvived.ToString()); + offlineZSStats = PrintOfflineZSStats; onlineZSStats = PrintOnlineZSStats; OfflineStat.Stats.Add(offlineZSStats); OnlineStat.Stats.Add(onlineZSStats); - + ChatTokens.Standard.Add(infectedToken); + ChatTokens.Standard.Add(survivedToken); + TopStat.Stats.Add(statMostInfected); TopStat.Stats.Add(statMostSurvived); TopStat.Stats.Add(statMaxInfected); @@ -338,6 +346,8 @@ namespace MCGalaxy.Games { void UnhookStats() { OfflineStat.Stats.Remove(offlineZSStats); OnlineStat.Stats.Remove(onlineZSStats); + ChatTokens.Standard.Remove(infectedToken); + ChatTokens.Standard.Remove(survivedToken); TopStat.Stats.Remove(statMostInfected); TopStat.Stats.Remove(statMostSurvived);