Skip to content
This repository has been archived by the owner on Mar 11, 2024. It is now read-only.

Commit

Permalink
redirect messages to onPlayerChat
Browse files Browse the repository at this point in the history
rename useCustomEventHandlers to useDefaultOutput
update README.md
  • Loading branch information
nrzull committed Jun 20, 2019
1 parent 8a5efb3 commit 7d53515
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 39 deletions.
32 changes: 11 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,36 +64,26 @@ After reviewing of all default mta resources code for just to be sure that there
Note that there is no last parameter `colorcoded`. Hex processing is enabled by default
- `showChat(element elem, bool show) -> void`
- `clearChatBox(element elem) -> void`
- `exports.chat2:useCustomEventHandlers(bool) -> void`
Enable/disable default output. If you disable it, then you need to write your own custom handlers for `onPlayerChat2` event

#### Events

- `onPlayerChat2`
handler params: (element player, string message, int messageType)
Will be emitted only after `exports.chat2:useCustomEventHandlers(false)`
- `exports.chat2:useDefaultOutput(bool) -> void`
Enable/disable default output. If you disable it, then you need to write your own custom handlers for `onPlayerChat` event

### Examples:

```lua
addEventHandler("onResourceStart", resourceRoot, function()
exports.chat2:useDefaultOutput(true) -- need to be executed if your gamemode doesn't output any messages to chat in onPlayerChat event handlers. As an example: play gamemode already uses its own output so you don't need to enable default output, but race gamemode doesn't have it, so you need to enable it.
end)

addEventHandler("onPlayerJoin", root, function()
outputChatBox("#ccff00hellow #ffcc00world", source)
-- clearChatBox(source)
outputChatBox("i'm red af", source, 255, 0, 0)
exports.chat2:useCustomEventHandlers(true) -- need to be executed if you want to disable default output handler and use your own output handlers
end)

-- listen for say/teamsay commands from console
-- may be created if useCustomEventHandlers was set to 'true'
addEventHandler("onPlayerChat", root, function(message, messageType)
-- some logic
end)

-- listen for direct output from chat
-- may be created if useCustomEventHandlers was set to 'true'
addEventHandler("onPlayerChat2", root, function(sender, message, messageType)
if message == "ping" then
outputChatBox("pong", sender)
end
-- should be created if useDefaultOutput wasn't set to 'true'
addEventHandler("onPlayerChat", root, function(message, messageType)
if message ~= "ping" then
outputChatBox("pong", source)
end
end)
```
10 changes: 6 additions & 4 deletions chat2_client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ local state = {
}

-- Define your local/global/team/etc input types here
-- First value of nested indexed table is a name of input type from where you want to send a message. It can be any name and it exists just for easy understanding for what messageType (second value of indexed table) is related.
-- second value is a messageType. You can define custom messageTypes. 0, 1, 2 are already used by MTA so I do not recommend to redefine them. They are useful when you want to add custom input type, like "global" or "police chat". Do not forget to execute exports.chat2:useDefaultOutput(false) and then handle this messageTypes in "onPlayerChat" event handlers
local inputKeyButtons = {
["t"] = "say",
["y"] = "teamsay"
["t"] = {"say", 0},
["y"] = {"teamsay", 2}
}

local scrollKeyButtons = {
Expand Down Expand Up @@ -101,7 +103,7 @@ function onChatInputButton(_, _, keyButton, definition)
return
end

execute(string.format("showInput(%s)", toJSON(definition)))
execute(string.format("showInput(%s)", toJSON(definition[1])))
focusBrowser(chatInstance)
guiSetInputEnabled(true)
state.activeInputKeyButton = keyButton
Expand All @@ -118,7 +120,7 @@ function onChatEnterButton(message)

execute("hideInput()")
guiSetInputEnabled(false)
triggerServerEvent("onChat2Message", resourceRoot, message, inputKeyButtons[state.activeInputKeyButton])
triggerServerEvent("onChat2Message", resourceRoot, message, inputKeyButtons[state.activeInputKeyButton][2])
state.activeInputKeyButton = nil
end

Expand Down
31 changes: 18 additions & 13 deletions chat2_server.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
addEvent("onChat2Message", true)
addEvent("onPlayerChat2")

local isDefaultOutput = true
local isDefaultOutput = false
local minLength = 1
local maxLength = 96

Expand All @@ -17,15 +16,20 @@ function output(player, message)
triggerClientEvent(player, "onChat2Output", player, message)
end

function useCustomEventHandlers(bool)
isDefaultOutput = not bool
function useDefaultOutput(bool)
isDefaultOutput = bool
end

function defaultOutput(sender, message, messageType)
if messageType ~= "say" and messageType ~= "teamsay" then
function defaultOutput(message, messageType)
if not isDefaultOutput then
return
end

if messageType ~= 0 and messageType ~= 2 then
return
end

local sender = source
local nickname = getPlayerName(sender)
local nicknameColor = ""
local r, g, b = getPlayerNametagColor(sender)
Expand All @@ -48,13 +52,13 @@ function defaultOutput(sender, message, messageType)
text = string.format("%s%s", teamColor, text)
end

if messageType == "say" then
if messageType == 0 then
for _, player in ipairs(getElementsByType("player")) do
output(player, text)
end
end

if messageType == "teamsay" and team then
if messageType == 2 and team then
text = string.format("%s(team) %s", teamColor, text)
for _, player in ipairs(getPlayersInTeam(team)) do
output(player, text)
Expand Down Expand Up @@ -88,15 +92,15 @@ function onChatMessage(message, messageType)
return
end

if utf8.sub(message, 0, 1) == "/" then
return handleCommand(client, message)
if type(messageType) ~= "number" then
return
end

if not isDefaultOutput then
return triggerEvent("onPlayerChat2", root, client, message, messageType)
if utf8.sub(message, 0, 1) == "/" then
return handleCommand(client, message)
end

defaultOutput(client, message, messageType)
triggerEvent("onPlayerChat", client, message, messageType)
end

function listenForOutputChatBox(_, _, _, _, _, message, receiver, r, g, b)
Expand Down Expand Up @@ -133,6 +137,7 @@ function onResourceStop()
removeDebugHook("preFunction", listenForClearChatBox)
end

addEventHandler("onPlayerChat", root, defaultOutput)
addEventHandler("onChat2Message", resourceRoot, onChatMessage)
addEventHandler("onResourceStart", resourceRoot, onResourceStart)
addEventHandler("onResourceStop", resourceRoot, onResourceStop)
2 changes: 1 addition & 1 deletion meta.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
<file src="scripts.js" />

<export function="isChatVisible" type="client" />
<export function="useCustomEventHandlers" type="server" />
<export function="useDefaultOutput" type="server" />
</meta>

0 comments on commit 7d53515

Please sign in to comment.