OnPlayerChat: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Fixed incorrect argument provided to an example (nearbyPlayer))
(21 intermediate revisions by 15 users not shown)
Line 1: Line 1:
__NOTOC__
__NOTOC__
{{Server event}}
{{Server event}}
This event is triggered when a player chats inside the chat box.
This event is triggered when a player chats inside the chatbox.


==Parameters==
==Parameters==
Line 8: Line 8:
</syntaxhighlight>  
</syntaxhighlight>  


*'''message''': A string representing the message typed into the chat.
*'''message''': a [[string]] representing the message typed into the chat.
*'''messageType''': An integer value representing the message type:
*'''messageType''': an [[int]] value representing the message type:
**'''0''': normal message
**'''0''': normal message
**'''1''': action message (/me)
**'''1''': action message (/me)
Line 19: Line 19:
==Cancel effect==
==Cancel effect==
If this event is [[Event system#Canceling|canceled]], the game's chat system won't deliver the posts. You may use [[outputChatBox]] to send the messages then.
If this event is [[Event system#Canceling|canceled]], the game's chat system won't deliver the posts. You may use [[outputChatBox]] to send the messages then.
Cancelling this event also means the chat will not appear in the server console or logs. If you want chat logging, you will have to add a call to [[outputServerLog]] - See the second example.


==Example==  
==Examples==
 
<section name="Example 1" class="server" show="true">
This example limits receiving of chat messages to a spherical area around the player who sent the message, also blocking action and team text.
This example limits receiving of chat messages to a spherical area around the player who sent the message, also blocking action and team text.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
-- define our chat radius
-- define our chat radius
Line 27: Line 31:


-- define a handler that will distribute the message to all nearby players
-- define a handler that will distribute the message to all nearby players
function sendMessageToNearbyPlayers( message, messageType )
function sendMessageToNearbyPlayers(message, messageType)
     -- we will only send normal chat messages, action and team types will be ignored
     -- we will only send normal chat messages, action and team types will be ignored
     if messageType == 0 then
     if messageType == 0 then
         -- get the chatting player's position
         -- get the chatting player's position
         local posX, posY, posZ = getElementPosition( source )
         local posX1, posY1, posZ1 = getElementPosition(source)
       
 
         -- create a sphere of the specified radius in that position
         -- loop through all player and check distance
         local chatSphere = createColSphere( posX, posY, posZ, chatRadius )
         for id, player in ipairs(getElementsByType("player")) do
        -- get a table all player elements inside it
            local posX2, posY2, posZ2 = getElementPosition(player)
        local nearbyPlayers = getElementsWithinColShape( chatSphere, "player" )
            if getDistanceBetweenPoints3D(posX1, posY1, posZ1, posX2, posY2, posZ2) <= chatRadius then
        -- and destroy the sphere, since we're done with it
                outputChatBox(message, player)
        destroyElement( chatSphere )
            end
       
        -- deliver the message to each player in that table
        for index, nearbyPlayer in ipairs( nearbyPlayers ) do
            outputChatBox( message, nearbyPlayer )
         end
         end
     end
     end
    -- block the original message by cancelling this event
    cancelEvent()
end
end
-- attach our new chat handler to onPlayerChat
-- attach our new chat handler to onPlayerChat
addEventHandler( "onPlayerChat", getRootElement(), sendMessageToNearbyPlayers )
addEventHandler( "onPlayerChat", getRootElement(), sendMessageToNearbyPlayers )
</syntaxhighlight>
</section>
<section name="Example 2" class="server" show="true">
This example implements colored player names in chat.


-- define another handler function that cancels the event so that the message won't be delivered through the
function blockChatMessage()
    cancelEvent()
end
-- attach it as a handler to onPlayerChat
addEventHandler( "onPlayerChat", getRootElement(), blockChatMessage )
</syntaxhighlight>
This example sets random color to every player who joins.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
-- sets colors when player join
--This function is executed when a player joins, it sets the player's name-tag color to a random color.
function onJoin ()
local function playerJoin()
g_Red, g_Green, g_Blue = randInt (50, 255), randInt (50, 255), randInt (50, 255)
local red, green, blue = math.random (50, 255), math.random (50, 255), math.random (50, 255)
        setPlayerNametagColor(source, red, green, blue)
end
end
-- checks if player has sent a message
addEventHandler ("onPlayerJoin", root, playerJoin)
function onChat ( message, messageType )
 
if messageType == 0 then
--This function is executed when a player says something in chat, it outputs the player's message, with their nick colored to match their name tag color.
local root = getRootElement()
local function playerChat(message, messageType)
outputChatBox ( getClientName ( source ) .. ": #E0D0B0" .. message, getRootElement(), g_Red, g_Green, g_Blue, true )
if messageType == 0 then --Global (main) chat
cancelEvent()
                cancelEvent()
                local red, green, blue = getPlayerNametagColor(source)
outputChatBox(getPlayerName(source)..": #FFFFFF"..message, root, red, green, blue, true )
outputServerLog("CHAT: "..getPlayerName(source)..": "..message)--NOTE: Beacuse we cancelled the onPlayerChat event, we need to log chat manually.
end
end
end
end
addEventHandler ( "onPlayerJoin", getRootElement(), onJoin)
addEventHandler("onPlayerChat", root, playerChat)
addEventHandler ( "onPlayerChat", getRootElement(), onChat )
</syntaxhighlight>
</syntaxhighlight>
</section>
<section name="Example 3" class="server" show="true">
This is a script that kills any player that says 'kill'.
<syntaxhighlight lang="lua">
function onChat(message, messageType)
    if string.find(message, 'kill') then  -- Searches for the string 'kill' in the message sent
        killPed ( source, source ) -- Kills that player that typed the string 'kill'
    end
end --End of the function
addEventHandler("onPlayerChat", getRootElement(), onChat)
</syntaxhighlight>
</section>


{{See also/Server event|Player events}}
{{See also/Server event|Player events}}
[[ru:onPlayerChat]]

Revision as of 15:53, 22 January 2019

This event is triggered when a player chats inside the chatbox.

Parameters

string message, int messageType
  • message: a string representing the message typed into the chat.
  • messageType: an int value representing the message type:
    • 0: normal message
    • 1: action message (/me)
    • 2: team message

Source

The source of this event is the player who sent the chatbox message.

Cancel effect

If this event is canceled, the game's chat system won't deliver the posts. You may use outputChatBox to send the messages then. Cancelling this event also means the chat will not appear in the server console or logs. If you want chat logging, you will have to add a call to outputServerLog - See the second example.

Examples

Click to collapse [-]
Example 1

This example limits receiving of chat messages to a spherical area around the player who sent the message, also blocking action and team text.

-- define our chat radius
local chatRadius = 20 --units

-- define a handler that will distribute the message to all nearby players
function sendMessageToNearbyPlayers(message, messageType)
    -- we will only send normal chat messages, action and team types will be ignored
    if messageType == 0 then
        -- get the chatting player's position
        local posX1, posY1, posZ1 = getElementPosition(source)

        -- loop through all player and check distance
        for id, player in ipairs(getElementsByType("player")) do
            local posX2, posY2, posZ2 = getElementPosition(player)
            if getDistanceBetweenPoints3D(posX1, posY1, posZ1, posX2, posY2, posZ2) <= chatRadius then
                outputChatBox(message, player)
            end
        end
    end
    -- block the original message by cancelling this event
    cancelEvent()
end
-- attach our new chat handler to onPlayerChat
addEventHandler( "onPlayerChat", getRootElement(), sendMessageToNearbyPlayers )


Click to collapse [-]
Example 2

This example implements colored player names in chat.

--This function is executed when a player joins, it sets the player's name-tag color to a random color.
local function playerJoin()
	local red, green, blue = math.random (50, 255), math.random (50, 255), math.random (50, 255)
        setPlayerNametagColor(source, red, green, blue)
end
addEventHandler ("onPlayerJoin", root, playerJoin)

--This function is executed when a player says something in chat, it outputs the player's message, with their nick colored to match their name tag color.
local function playerChat(message, messageType)
	if messageType == 0 then --Global (main) chat
                cancelEvent()
                local red, green, blue = getPlayerNametagColor(source)
		outputChatBox(getPlayerName(source)..": #FFFFFF"..message, root, red, green, blue, true )
		outputServerLog("CHAT: "..getPlayerName(source)..": "..message)--NOTE: Beacuse we cancelled the onPlayerChat event, we need to log chat manually.
	end
end
addEventHandler("onPlayerChat", root, playerChat)


Click to collapse [-]
Example 3

This is a script that kills any player that says 'kill'.

function onChat(message, messageType)
    if string.find(message, 'kill') then  -- Searches for the string 'kill' in the message sent
        killPed ( source, source ) -- Kills that player that typed the string 'kill'
    end
end --End of the function
addEventHandler("onPlayerChat", getRootElement(), onChat)

See Also

Player events


Event functions