From 55c89418c49e7a7e8410d7aa5bffdf16e1f40897 Mon Sep 17 00:00:00 2001 From: Resxt <55228336+Resxt@users.noreply.github.com> Date: Mon, 9 Oct 2023 07:39:01 +0200 Subject: [PATCH] chat_commands 1.1.2 Added the ability to find players by typing a part of their name (starting from the beginning of their names) instead of having to type their full names all the time. For example to kill Resxt you can now just type !kill res If there is a player named Resxt and another named Resxtola then typing !kill resxt will kill Resxt because we got an exact match. If you type !kill Resx it won't kill anyone because there are two potential players found so we can't be sure who the command should be run on. If you type !kill Resxto it will kill Resxtola --- chat_commands/chat_commands.gsc | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/chat_commands/chat_commands.gsc b/chat_commands/chat_commands.gsc index acb40f8..d5b3d13 100644 --- a/chat_commands/chat_commands.gsc +++ b/chat_commands/chat_commands.gsc @@ -520,6 +520,9 @@ FindPlayerByName(name) return self; } + potentialPlayersFound = 0; + foundPlayer = undefined; + foreach (player in level.players) { playerName = player.name; @@ -529,10 +532,25 @@ FindPlayerByName(name) playerName = StrTok(playerName, "]")[1]; // ignore the clantag } - if (ToLower(playerName) == ToLower(name)) + if (ToLower(playerName) == ToLower(name)) // if we get an exact match return the player { return player; } + + if (ToLower(GetSubStr(playerName, 0, name.size)) == ToLower(name)) // found a player who's name starts with the given text + { + potentialPlayersFound++; + } + + if (potentialPlayersFound == 1 && !IsDefined(foundPlayer)) // store the first player we find, since we only return one if and only if it only finds one + { + foundPlayer = player; + } + } + + if (potentialPlayersFound == 1) // we only found one player who's name starts with the given text so it's safe to return the player we found + { + return foundPlayer; } }