GetCommandHandlers: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
mNo edit summary
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Server function}}
{{Shared function}}
__NOTOC__  
__NOTOC__  
{{New items|3.0153|1.5.3|
{{New items|3.0153|1.5.3|
This function is used to retrieve a list of all the registered command handlers of a given resource (or of all resources).
This function is used to retrieve a list of all the registered command handlers of a given resource (or of all resources).
}}
}}
{{New feature/item|3.0157|1.5.6|16256|Function also added client-side.}}


==Syntax==  
==Syntax==  
<section name="Server" class="server" show="true">
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
table getCommandHandlers ( [ resource theResource ] )  
table getCommandHandlers ( [ resource theResource ] )  
</syntaxhighlight>
</syntaxhighlight>


==Optional Arguments==
===Optional Arguments===
{{OptionalArg}}
*'''theResource:''' The resource from which you wish to retrieve all command handlers. Or leave it empty to retrieve command handlers of all resources.
*'''theResource:''' The resource from which you wish to retrieve all command handlers. Or leave it empty to retrieve command handlers of all resources.
</section>


===Returns===
===Returns===
Line 19: Line 19:


==Example==
==Example==
<section class="server" name="Server" show="true">
This example add a command to output list of all commands in the chat.
This example add a command to output list of all commands in the chat.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
addCommandHandler( "commands",  
addCommandHandler( "commands",  
function(player)
  function(player)
local commandsList = {} --table to store commands
      local commandsList = {} --table to store commands
--store/sort commands in the table where key is resource and value is table with commands
      --store/sort commands in the table where key is resource and value is table with commands
for _, subtable in pairs( getCommandHandlers() ) do
      for _, subtable in pairs( getCommandHandlers() ) do
local commandName = subtable[1]
local commandName = subtable[1]
local theResource = subtable[2]
local theResource = subtable[2]
if not commandsList[theResource] then
        if not commandsList[theResource] then
commandsList[theResource] = {}
    commandsList[theResource] = {}
end
end
table.insert( commandsList[theResource], commandName )
table.insert( commandsList[theResource], commandName )
end
      end
--output sorted information in the chat
      --output sorted information in the chat
for theResource, commands in pairs( commandsList ) do
      for theResource, commands in pairs( commandsList ) do
local resourceName = getResourceInfo( theResource, "name" ) or getResourceName( theResource ) --try to get full name, if no full name - use short name
  local resourceName = getResourceInfo( theResource, "name" ) or getResourceName( theResource ) --try to get full name, if no full name - use short name
outputChatBox( "== "..resourceName.. " ==", player, 0, 255, 0 )
  outputChatBox( "== "..resourceName.. " ==", player, 0, 255, 0 )
--output list of commands
  --output list of commands
for _, command in pairs( commands ) do
  for _, command in pairs( commands ) do
outputChatBox( "/"..command, player, 255, 255, 255 )
    outputChatBox( "/"..command, player, 255, 255, 255 )
end
  end
end
      end
end
  end
)
)
</syntaxhighlight>
</syntaxhighlight>
</section>


<section class="server" name="Server" show="true">
This example add a command to output list of all commands for the resource in the chat. Syntax: /commands [resource-name]
This example add a command to output list of all commands for the resource in the chat. Syntax: /commands [resource-name]
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
addCommandHandler( "commands",  
addCommandHandler("commands",  
function( player, _, resourceName )
  function( player, _, resourceName)
if not resourceName then
    local theResource = (resourceName and getResourceFromName(resourceName)) or resource  
resourceName = ""
    outputChatBox( "* Commands from "..getResourceName(theResource).." resource", player, 0, 255, 0 )
end
local theResource = getResourceFromName( resourceName )
if not theResource then
outputChatBox( "Can't find resource with name \"" .. resourceName .. "\".", player, 255, 0, 0 )
return false
end
outputChatBox( "== Commands from \""..resourceName.. "\" resource ==", player, 0, 255, 0 )
local commands = getCommandHandlers( theResource )
    local commands = getCommandHandlers( theResource )
for _, command in pairs( commands ) do
    for _, command in pairs( commands ) do
outputChatBox( "/"..command, player, 255, 255, 255 )
        outputChatBox( "/"..command, player, 255, 255, 255 )
end
    end
end
  end
)
)
</syntaxhighlight>
</syntaxhighlight>
</section>


==See Also==
==See Also==
{{Server functions}}
{{Server functions}}

Latest revision as of 09:53, 7 January 2019

This function is used to retrieve a list of all the registered command handlers of a given resource (or of all resources). Function also added client-side.

Syntax

table getCommandHandlers ( [ resource theResource ] ) 

Optional Arguments

NOTE: When using optional arguments, you might need to supply all arguments before the one you wish to use. For more information on optional arguments, see optional arguments.

  • theResource: The resource from which you wish to retrieve all command handlers. Or leave it empty to retrieve command handlers of all resources.

Returns

Returns a table containing all the commands of the given resource or a table with subtables containing the command and theResource pointer ( { "command", theResource } ). See examples below if you don't understand it.

Example

This example add a command to output list of all commands in the chat.

addCommandHandler( "commands", 
   function(player)
      local commandsList = {} --table to store commands
		
      --store/sort commands in the table where key is resource and value is table with commands
      for _, subtable in pairs( getCommandHandlers() ) do
	 local commandName = subtable[1]
	 local theResource = subtable[2]
			
         if not commandsList[theResource] then
	    commandsList[theResource] = {}
	 end
			
	 table.insert( commandsList[theResource], commandName )
      end
		
      --output sorted information in the chat
      for theResource, commands in pairs( commandsList ) do
	  local resourceName = getResourceInfo( theResource, "name" ) or getResourceName( theResource ) --try to get full name, if no full name - use short name
	  outputChatBox( "== "..resourceName.. " ==", player, 0, 255, 0 )
			
	  --output list of commands
	  for _, command in pairs( commands ) do
	     outputChatBox( "/"..command, player, 255, 255, 255 )
	  end
      end
  end
)

This example add a command to output list of all commands for the resource in the chat. Syntax: /commands [resource-name]

addCommandHandler("commands", 
  function( player, _, resourceName)
     local theResource = (resourceName and getResourceFromName(resourceName)) or resource 
     outputChatBox( "* Commands from "..getResourceName(theResource).." resource", player, 0, 255, 0 )
		
     local commands = getCommandHandlers( theResource )
     for _, command in pairs( commands ) do
        outputChatBox( "/"..command, player, 255, 255, 255 )
     end
  end
)

See Also