From b2e78f3dd088e6d835553131dfb972494295b009 Mon Sep 17 00:00:00 2001 From: nrzull Date: Mon, 17 Jun 2019 20:28:25 +0500 Subject: [PATCH] rename useDefaultOutput to useCustomEventHandlers listen for onChatMessage event --- README.md | 18 ++++++++++++------ chat2_server.lua | 33 ++++++++++++++------------------- meta.xml | 4 ++-- 3 files changed, 28 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index ae83b78..daa3de0 100644 --- a/README.md +++ b/README.md @@ -75,14 +75,20 @@ This chat is using CEF and it tries to simulate behavior of default chat ```lua addEventHandler("onPlayerJoin", root, function() exports.chat2:output(source, "#ccff00hello #ffcc00world") - exports.chat2:useDefaultOutput(false) -- disable built-in handler and use own handlers that listen for "onPlayerChat2" event + exports.chat2:useCustomEventHandlers(true) -- need to be executed if you want to disable default output handler and use your own output handlers end) -addEventHandler("onPlayerChat2", root, function(sender, message, messageType) - local text = string.format("%s wrote: %s", getPlayerName(sender), message) +-- listen for say/teamsay commands from console +-- may be created if useDefaultOutput was set to 'false' +addEventHandler("onPlayerChat", root, function(message, messageType) + -- some logic +end) - for _, player in ipairs(getElementsByType("player")) do - output(player, text) - end +-- listen for direct output from chat +-- may be created if useDefaultOutput was set to 'false' +addEventHandler("onPlayerChat2", root, function(sender, message, messageType) + if message == "ping" then + exports.chat2:output(sender, "pong") + end end) ``` diff --git a/chat2_server.lua b/chat2_server.lua index f74c5c8..f6a6fcd 100644 --- a/chat2_server.lua +++ b/chat2_server.lua @@ -5,17 +5,6 @@ local isDefaultOutput = true local minLength = 1 local maxLength = 96 -local rootPlayers = createElement("rootPlayers") -setElementParent(rootPlayers, root) - -function registerPlayer(p) - setElementParent(p or source, rootPlayers) -end - -for _, p in ipairs(getElementsByType("player")) do - registerPlayer(p) -end - function clear(player) triggerClientEvent(player, "onChat2Clear", player) end @@ -32,8 +21,8 @@ function output(player, message) triggerClientEvent(player, "onChat2Output", player, message) end -function useDefaultOutput(bool) - isDefaultOutput = bool +function useCustomEventHandlers(bool) + isDefaultOutput = not bool end function RGBToHex(red, green, blue) @@ -61,7 +50,7 @@ function onChatSendMessage(message, messageType) messageType = tonumber(messageType) if not isDefaultOutput then - triggerEvent("onPlayerChat2", rootPlayers, client, message, messageType) + triggerEvent("onPlayerChat2", root, client, message, messageType) return end @@ -106,14 +95,20 @@ function handleCommand(client, input) executeCommandHandler(cmd, client, unpack(splittedInput)) end -function onPlayerJoin() - registerPlayer(source) +-- listen for "say / teamsay" from player console +function onPlayerChat(message, messageType) + if isDefaultOutput then + defaultOutput(source, message, messageType) + end end -function onPlayerChat(message, messageType) - defaultOutput(source, message, messageType) +-- listen for messages that were sent from resources +function onChatMessage(message, elementOrResource) + if not isElement(elementOrResource) then + output(root, message) + end end addEventHandler("onChat2SendMessage", resourceRoot, onChatSendMessage) -addEventHandler("onPlayerJoin", root, onPlayerJoin) addEventHandler("onPlayerChat", root, onPlayerChat) +addEventHandler("onChatMessage", root, onChatMessage) diff --git a/meta.xml b/meta.xml index daf1ea1..23ab2c4 100644 --- a/meta.xml +++ b/meta.xml @@ -1,5 +1,5 @@ - +