OnPlayerCommand: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(overall page improvements)
Line 2: Line 2:
{{Server event}}
{{Server event}}
This event is triggered when a player issues a command.
This event is triggered when a player issues a command.
{{Note|This event triggers regardless of whether the command exists or not. Also, typing anything in chat will execute command "say", so this event will be triggered on every chat message as well.}}
{{Note|This event triggers regardless of whether the command exists in a script or is hardcoded. Also, typing anything in chat will execute the internal command "say", so this event will be triggered on every chat message as well. Therefore you should avoid excessive use of this function on busy servers, out of performance considerations.}}


==Parameters==
==Parameters==
Line 12: Line 12:
The [[event system#Event source|source]] of this event is the [[player]] who tried to execute a command.
The [[event system#Event source|source]] of this event is the [[player]] who tried to execute a command.


==Cancel effect==
==Result of cancelling this event==
The command will not be executed.
The command will not be executed.


==Example==
<section name="Server" class="server" show="true">
This example implements an anti-flood protection timer for commands
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
local CMD_INTERVAL        = 200 --// The interval that needs to pass before the player can execute another cmd
local CMD_INTERVAL        = 200 --// The interval that needs to pass before the player can execute another cmd
local KICK_AFTER_INTERVAL = 50  --// Kick her if she executes more than 20 cmds/sec
local KICK_AFTER_INTERVAL = 50  --// Kick the player if they execute more than 20 cmds/sec


local executions = setmetatable({}, { --// Metatable for non-existing indexes
local executions = setmetatable({}, { --// Metatable for non-existing indexes
Line 38: Line 41:
)
)
</syntaxhighlight>
</syntaxhighlight>
<section name="Server" class="server" show="true">
This example disables the hardcoded command 'whois'
<syntaxhighlight lang="lua">
addEventHandler("onPlayerCommand",root,
    function(command)
if (command == "whois") then
    cancelEvent()
end
end)
</syntaxhighlight>
</section>

Revision as of 03:37, 21 September 2018

This event is triggered when a player issues a command.

[[{{{image}}}|link=|]] Note: This event triggers regardless of whether the command exists in a script or is hardcoded. Also, typing anything in chat will execute the internal command "say", so this event will be triggered on every chat message as well. Therefore you should avoid excessive use of this function on busy servers, out of performance considerations.

Parameters

string command

Source

The source of this event is the player who tried to execute a command.

Result of cancelling this event

The command will not be executed.

Example

Click to collapse [-]
Server

This example implements an anti-flood protection timer for commands

local CMD_INTERVAL        = 200 --// The interval that needs to pass before the player can execute another cmd
local KICK_AFTER_INTERVAL = 50  --// Kick the player if they execute more than 20 cmds/sec

local executions = setmetatable({}, { --// Metatable for non-existing indexes
    __index = function(t, k)
        rawset(t, k, 0)
        return 0   
    end
})

addEventHandler("onPlayerCommand", root,
    function()
        if (executions[source]-getTickCount()<=CMD_INTERVAL) then
            if (executions[source]-getTickCount()<=KICK_AFTER_INTERVAL) then 
                kickPlayer(source, "Anti-Flood", "Don't flood")
            end 
            cancelEvent()
        end
        executions[source] = getTickCount()
    end
)

<section name="Server" class="server" show="true"> This example disables the hardcoded command 'whois'

addEventHandler("onPlayerCommand",root,
    function(command)
	if (command == "whois") then
	    cancelEvent()
	end
end)