GetCommandHandlers: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 15: Line 15:
===Returns===
===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 } )
Returns a ''table'' containing all the commands of the given resource or a table with subtables containing the command and theResource pointer ( { "command", theResource } )
==Example==
<section class="server" name="Server" show="true">
This simple example add a command to output list of all commands in the chat.
<syntaxhighlight lang="lua">
addCommandHandler("commands",
function()
local commandsList = {}
for _, subtable in pairs( getCommandHandlers() ) do
local commandName = subtable[1]
local resource = subtable[2]
if not commandsList[resource] then
commandsList[resource] = {}
end
table.insert(commandsList[resource], commandName)
end
for resource, commands in pairs(commandsList) do
local resourceName = getResourceInfo(resource, "name") or getResourceName(resource)
outputChatBox("== "..resourceName.. " ==")
for _, command in pairs(commands) do
outputChatBox("/"..command)
end
end
end
)
</syntaxhighlight>
</section>


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

Revision as of 13:26, 9 October 2016

This function is used to retrieve a list of all the registered command handlers of a given resource (or of all resources).

Syntax

Click to collapse [-]
Server
table getElementsByType ( [ resource theResource ] ) 

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 } )

Example

Click to collapse [-]
Server

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

addCommandHandler("commands", 
	function()
		local commandsList = {}
		
		for _, subtable in pairs( getCommandHandlers() ) do
			local commandName = subtable[1]
			local resource = subtable[2]
			
			if not commandsList[resource] then
				commandsList[resource] = {}
			end
			
			table.insert(commandsList[resource], commandName)
		end
		
		for resource, commands in pairs(commandsList) do
			local resourceName = getResourceInfo(resource, "name") or getResourceName(resource)
			
			outputChatBox("== "..resourceName.. " ==")
			
			for _, command in pairs(commands) do
				outputChatBox("/"..command)
			end
			
		end
	end
)

See Also