Add fallback for finding players using tsi & tsn

This commit adds a fallback to address problems with FindByNameOrID
potentially returning ambiguous results. Now, in response to a multiple
match error, a player can specify tsi:[number] or tsn:[exact name] to
match a user ID or name exactly. This behaves analogous to the old
behavior of the search method.
This commit is contained in:
Lucas Nicodemus 2021-05-25 19:19:01 -07:00
parent 09fe254f17
commit e73ce17130
3 changed files with 38 additions and 10 deletions

View file

@ -2282,6 +2282,14 @@ namespace TShockAPI
args.Player.Kick("You have been Bounced.", true, true);
return true;
}
if (name.Trim().StartsWith("tsi:") || name.Trim().StartsWith("tsn:"))
{
TShock.Log.ConsoleDebug("GetDataHandlers / rejecting player for name prefix starting with tsi: or tsn:.");
args.Player.Kick("Illegal name: prefixes tsi: and tsn: are forbidden.", true, true);
return true;
}
if (args.Player.ReceivedInfo)
{
// Since Terraria 1.2.3 these character properties can change ingame.

View file

@ -75,33 +75,52 @@ namespace TShockAPI
public static readonly TSPlayer All = new TSPlayer("All");
/// <summary>
/// Finds a TSPlayer based on name or ID
/// Finds a TSPlayer based on name or ID.
/// If the string comes with tsi: or tsn:, we'll only return a list with one element,
/// either the player with the matching ID or name, respectively.
/// </summary>
/// <param name="plr">Player name or ID</param>
/// <returns>A list of matching players</returns>
public static List<TSPlayer> FindByNameOrID(string plr)
public static List<TSPlayer> FindByNameOrID(string search)
{
var found = new List<TSPlayer>();
search = search.Trim();
// tsi: and tsn: are used to disambiguate between usernames and not
// and are also both 3 characters to remove them from the search
// (the goal was to pick prefixes unlikely to be used by names)
// (and not to collide with other prefixes used by other commands)
var exactIndexOnly = search.StartsWith("tsi:");
var exactNameOnly = search.StartsWith("tsn:");
if (exactNameOnly || exactIndexOnly)
search = search.Remove(0, 4);
// Avoid errors caused by null search
if (plr == null)
if (search == null || search == "")
return found;
byte plrID;
if (byte.TryParse(plr, out plrID) && plrID < Main.maxPlayers)
byte searchID;
if (byte.TryParse(search, out searchID) && searchID < Main.maxPlayers)
{
TSPlayer player = TShock.Players[plrID];
TSPlayer player = TShock.Players[searchID];
if (player != null && player.Active)
{
if (exactIndexOnly)
return new List<TSPlayer> { player };
found.Add(player);
}
}
string plrLower = plr.ToLower();
string searchLower = search.ToLower();
foreach (TSPlayer player in TShock.Players)
{
if (player != null)
{
if (player.Name.ToLower().StartsWith(plrLower))
if ((search == player.Name) && exactNameOnly)
return new List<TSPlayer> { player };
if (player.Name.ToLower().StartsWith(searchLower))
found.Add(player);
}
}
@ -1664,12 +1683,13 @@ namespace TShockAPI
/// <param name="matches">An enumerable list with the matches</param>
public void SendMultipleMatchError(IEnumerable<object> matches)
{
SendErrorMessage("More than one match found: ");
SendErrorMessage("More than one match found -- unable to decide which is correct: ");
var lines = PaginationTools.BuildLinesFromTerms(matches.ToArray());
lines.ForEach(SendInfoMessage);
SendErrorMessage("Use \"my query\" for items with spaces.");
SendErrorMessage("Use tsi:[number] or tsn:[username] to distinguish between user IDs and usernames.");
}
[Conditional("DEBUG")]