OnPlayerCommand: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
mNo edit summary
Line 15: Line 15:
The command will not be executed.
The command will not be executed.


==Example==
local CMD_INTERVAL        = 200 --// The interval that needs to pass before the player can execute another cmd
This example prevents players executing more than 5 commands a second, recommended for all servers.
local KICK_AFTER_INTERVAL = 50  --// Kick her if she executes more than 20 cmds/sec
<syntaxhighlight lang="lua">
local commandSpam = {}


function preventCommandSpam()
local executions = setmetatable({}, { --// Metatable for non-existing indexes
if (not commandSpam[source]) then
    __index = function(t, k)
commandSpam[source] = 1
        rawset(t, k, 0)
-- New person so add to table
        return 0 
elseif (commandSpam[source] == 5) then
    end
cancelEvent()
})
outputChatBox("Please refrain from command spamming!", source, 255, 0, 0)
-- This person is command spamming!
else
commandSpam[source] = commandSpam[source] + 1
-- Add one to the table
end
end
addEventHandler("onPlayerCommand", root, preventCommandSpam)
setTimer(function() commandSpam = {} end, 1000, 0) -- Clear the table every second
</syntaxhighlight>


{{See also/Server event|Player events}}
addEventHandler("onPlayerCommand", root,
    function(cmd)
        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
)

Revision as of 20:40, 18 August 2018

This event is triggered when a player issues a command.

[[{{{image}}}|link=|]] 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.

Parameters

string command

Source

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

Cancel effect

The command will not be executed.

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 executions = setmetatable({}, { --// Metatable for non-existing indexes

   __index = function(t, k)
       rawset(t, k, 0)
       return 0   
   end

})

addEventHandler("onPlayerCommand", root,

   function(cmd)
       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

)