Move SendFileToUser to TSP.SendFileTextAsMessage.

This is not a great method, but it's actually the only method in TShock
that interpolates the %map% and %players% variables and it used in at
least three places in the codebase. Since it's already so specialized,
it's not worth changing it to take an actual File object, in my humble
opinion.

This also clarifies what the method does and what makes it special, as
opposed to being fairly generic.
This commit is contained in:
Lucas Nicodemus 2017-12-29 08:40:44 -07:00
parent 17d151b8f8
commit 3ac52091ea
5 changed files with 41 additions and 41 deletions

View file

@ -4979,12 +4979,12 @@ namespace TShockAPI
private static void Motd(CommandArgs args)
{
TShock.Utils.ShowFileToUser(args.Player, FileTools.MotdPath);
args.Player.SendFileTextAsMessage(FileTools.MotdPath);
}
private static void Rules(CommandArgs args)
{
TShock.Utils.ShowFileToUser(args.Player, FileTools.RulesPath);
args.Player.SendFileTextAsMessage(FileTools.RulesPath);
}
private static void Whisper(CommandArgs args)

View file

@ -1458,6 +1458,43 @@ namespace TShockAPI
SendDataFromPlayer(PacketTypes.SmartTextMessage, ply, msg, red, green, blue, -1);
}
/// <summary>
/// Sends the text of a given file to the player. Replacement of %map% and %players% if in the file.
/// </summary>
/// <param name="file">Filename relative to <see cref="TShock.SavePath"></see></param>
public void SendFileTextAsMessage(string file)
{
string foo = "";
bool containsOldFormat = false;
using (var tr = new StreamReader(file))
{
Color lineColor;
while ((foo = tr.ReadLine()) != null)
{
lineColor = Color.White;
if (string.IsNullOrWhiteSpace(foo))
{
continue;
}
var players = new List<string>();
foreach (TSPlayer ply in TShock.Players)
{
if (ply != null && ply.Active)
{
players.Add(ply.Name);
}
}
foo = foo.Replace("%map%", (TShock.Config.UseServerName ? TShock.Config.ServerName : Main.worldName));
foo = foo.Replace("%players%", String.Join(",", players));
SendMessage(foo, lineColor);
}
}
}
/// <summary>
/// Wounds the player with the given damage.
/// </summary>

View file

@ -1637,7 +1637,7 @@ namespace TShockAPI
if (Config.DisplayIPToAdmins)
Utils.SendLogs(string.Format("{0} has joined. IP: {1}", player.Name, player.IP), Color.Blue);
Utils.ShowFileToUser(player, FileTools.MotdPath);
player.SendFileTextAsMessage(FileTools.MotdPath);
string pvpMode = Config.PvPMode.ToLowerInvariant();
if (pvpMode == "always")

View file

@ -479,44 +479,6 @@ namespace TShockAPI
TShock.TileBans.UpdateBans();
}
/// <summary>
/// Shows a file to the user.
/// </summary>
/// <param name="player">Player the file contents will be sent to</param>
/// <param name="file">Filename relative to <see cref="TShock.SavePath"></see></param>
public void ShowFileToUser(TSPlayer player, string file)
{
string foo = "";
bool containsOldFormat = false;
using (var tr = new StreamReader(file))
{
Color lineColor;
while ((foo = tr.ReadLine()) != null)
{
lineColor = Color.White;
if (string.IsNullOrWhiteSpace(foo))
{
continue;
}
var players = new List<string>();
foreach (TSPlayer ply in TShock.Players)
{
if (ply != null && ply.Active)
{
players.Add(ply.Name);
}
}
foo = foo.Replace("%map%", (TShock.Config.UseServerName ? TShock.Config.ServerName : Main.worldName));
foo = foo.Replace("%players%", String.Join(",", players));
player.SendMessage(foo, lineColor);
}
}
}
/// <summary>
/// Returns a Group from the name of the group
/// </summary>