Merge pull request #3 from Chenterito/patch-1

Improve the "FindPlayerByName" function by letting players type the beginning of a name instead of always having to type the player's full name
This commit is contained in:
Resxt 2023-10-09 06:43:28 +02:00 committed by GitHub
commit 0ab00948ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 3 deletions

View File

@ -22,7 +22,7 @@ You must install at least one command script to be able to use commands. Otherwi
- A `commands` command that lists all available commands on the server you're on dynamically (only lists commands you have access to if the permission system is enabled) - A `commands` command that lists all available commands on the server you're on dynamically (only lists commands you have access to if the permission system is enabled)
- A `help` command that explains how to use a given command. For example `help map` (only works on commands you have access to if the permission system is enabled) - A `help` command that explains how to use a given command. For example `help map` (only works on commands you have access to if the permission system is enabled)
- `alias` and `aliases` commands that list the available aliases for a command. For example `alias godmode` (only works on commands you have access to if the permission system is enabled) - `alias` and `aliases` commands that list the available aliases for a command. For example `alias godmode` (only works on commands you have access to if the permission system is enabled)
- All commands that require a target work with `me`. Also it doesn't matter how you type the player's name as long as you type the full name. - All commands that require a target work with `me`. Also it doesn't matter how you type the player's name as long as you type his full name or type the beginning of his username (has to be unique, see [#3](https://github.com/Resxt/Plutonium-IW5-Scripts/pull/3/commits/2efa784709b5c42811510c67c3e6a2fc5eb3fc70)).
- Configurable command prefix. Set to `!` by default - Configurable command prefix. Set to `!` by default
- A plugin system to easily allow adding/removing commands. Each command has its own GSC file to easily add/remove/review/configure your commands. This also makes contributing by creating a PR to add a command a lot easier - A plugin system to easily allow adding/removing commands. Each command has its own GSC file to easily add/remove/review/configure your commands. This also makes contributing by creating a PR to add a command a lot easier

View File

@ -497,12 +497,30 @@ FindPlayerByName(name)
return self; return self;
} }
potentialPlayersFound = 0;
foundPlayer = undefined;
foreach (player in level.players) foreach (player in level.players)
{ {
if (ToLower(player.name) == ToLower(name)) if (ToLower(player.name) == ToLower(name)) // if we get an exact match return the player
{ {
return player; 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;
}
}
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;
} }
} }