From 2efa784709b5c42811510c67c3e6a2fc5eb3fc70 Mon Sep 17 00:00:00 2001 From: Resxt <55228336+Resxt@users.noreply.github.com> Date: Mon, 9 Oct 2023 06:33:17 +0200 Subject: [PATCH] chat_commands 1.4.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 | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/chat_commands/chat_commands.gsc b/chat_commands/chat_commands.gsc index da9e20c..f39c556 100644 --- a/chat_commands/chat_commands.gsc +++ b/chat_commands/chat_commands.gsc @@ -497,27 +497,30 @@ FindPlayerByName(name) return self; } - matchs = 0; + potentialPlayersFound = 0; + foundPlayer = undefined; foreach (player in level.players) { - if( isSubStr( tolower( player.name), tolower(name ) )) + if (ToLower(player.name) == ToLower(name)) // if we get an exact match return the player { - matchs++; + return player; + } + + if (ToLower(GetSubStr(player.name, 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; } } - wait 0.05; - - if( matchs == 1) + 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 { - foreach (player in level.players) - { - if( isSubStr( tolower( player.name), tolower(name ) )) - { - return player; - } - } + return foundPlayer; } }