GetCommandHandlers: Difference between revisions
Jump to navigation
Jump to search
Line 18: | Line 18: | ||
==Example== | ==Example== | ||
<section class="server" name="Server" show="true"> | <section class="server" name="Server" show="true"> | ||
This | This example add a command to output list of all commands in the chat. | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
addCommandHandler( "commands", | addCommandHandler( "commands", | ||
Line 45: | Line 45: | ||
outputChatBox( "/"..command, player, 255, 255, 255 ) | outputChatBox( "/"..command, player, 255, 255, 255 ) | ||
end | end | ||
end | |||
end | |||
) | |||
</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] | |||
<syntaxhighlight lang="lua"> | |||
addCommandHandler( "commands", | |||
function( player, _, resourceName ) | |||
if not resourceName then | |||
resourceName = "" | |||
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 ) | |||
for _, command in pairs( commands ) do | |||
outputChatBox( "/"..command, player, 255, 255, 255 ) | |||
end | end | ||
end | end |
Revision as of 14:12, 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 [-]
Servertable 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 [-]
ServerThis 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 )
Click to collapse [-]
ServerThis 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 ) if not resourceName then resourceName = "" 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 ) for _, command in pairs( commands ) do outputChatBox( "/"..command, player, 255, 255, 255 ) end end )
See Also
- getMaxPlayers
- getServerConfigSetting
- getServerHttpPort
- getServerName
- getServerPassword
- getServerPort
- isGlitchEnabled
- setGlitchEnabled
- setMaxPlayers
- setServerConfigSetting
- setServerPassword
- shutdown