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

Commit

Permalink
rename useDefaultOutput to useCustomEventHandlers
Browse files Browse the repository at this point in the history
listen for onChatMessage event
  • Loading branch information
nrzull committed Jun 17, 2019
1 parent d7bb5cd commit b2e78f3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 27 deletions.
18 changes: 12 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```
33 changes: 14 additions & 19 deletions chat2_server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
4 changes: 2 additions & 2 deletions meta.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<meta>
<info name="chat2" type="script" version="0.2.0" author="nrzull" />
<info name="chat2" type="script" version="0.3.0" author="nrzull" />

<script src="chat2_server.lua" type="server" />
<script src="chat2_client.lua" type="client" />
Expand All @@ -9,5 +9,5 @@
<export function="show" type="shared" />
<export function="output" type="shared" />
<export function="isVisible" type="shared" />
<export function="useDefaultOutput" type="server" />
<export function="useCustomEventHandlers" type="server" />
</meta>

0 comments on commit b2e78f3

Please sign in to comment.