Show Target Player Name

From GTA Connected
Revision as of 13:48, 12 October 2020 by Mex (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Example 1 - /id playerid

Description

This command shows the player name in the chat box for the player id text typed after the command name. E.g. /id 0 will search for a player with the player id 0.

Code

JavaScript, Server-Side:

addCommandHandler("id", function(cmd, args, client) {
	var client = getClientByClientId(client.name);
	if(!client)
		message("Player not found!", COLOUR_BLUE);
	else
		message("Player matching text '"+args+"' is: "+client.player.name, COLOUR_BLUE);
});

function getClientByClientId(text) {
	var clientIdInput = parseInt(text, 10);
    if(isNaN(clientIdInput))
		return null;
	var clients = getClients();
	for(var key in clients)
	{
		var client = clients[key];
		if(client.player.id == clientIdInput)
		{
			return client;
		}
	}
	return null;
}

Example 2 - /name playernameorid

Description

This command shows the player name in the chat box for the text typed after the command name. E.g. /name Player will search for a player with the name Player.

Code

JavaScript, Server-Side:

addCommandHandler("name", function(cmd, args, client) {
	var client = getClientByClientIdOrName(client.name);
	if(!client)
		message("Player not found!", COLOUR_BLUE);
	else
		message("Player matching text '"+args+"' is: "+client.player.name, COLOUR_BLUE);
});

function getClientByClientIdOrName(text) {
	var clientIdInput = parseInt(text, 10);
    if(isNaN(clientIdInput))
		return null;
	var clients = getClients();
	// check for client ID match first
	for(var key in clients)
	{
		var client = clients[key];
		if(client.player.id == clientIdInput)
		{
			return client;
		}
	}
	// check for client name match second
	var textLower = text.toLowerCase();
	for(var key in clients)
	{
		var client = clients[key];
		if(client.player.name.toLowerCase().indexOf(textLower) != -1)
		{
			return client;
		}
	}
	return null;
}