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

Commit

Permalink
redirect messages from outputChatBox
Browse files Browse the repository at this point in the history
  • Loading branch information
nrzull committed Jun 19, 2019
1 parent 03b3384 commit b282d56
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 15 deletions.
24 changes: 22 additions & 2 deletions chat2_client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ function show(bool)
if chatInstanceLoaded ~= true then
if chatInstanceLoading ~= true then
create()
setTimer(show, 300, 1, bool)
return setTimer(show, 300, 1, bool)
else
setTimer(show, 300, 1, bool)
return setTimer(show, 300, 1, bool)
end
end

Expand Down Expand Up @@ -83,6 +83,24 @@ function onChatSendMessage(message, messageType)
triggerServerEvent("onChat2SendMessage", resourceRoot, message, messageType)
end

function listenForOutputChatBox(_, _, _, _, _, message, r, g, b)
local hexColor = ""

if (r and g and b) then
hexColor = RGBToHex(r, g, b)
end

output(string.format("%s%s", hexColor, message))
end

function onClientResourceStart()
addDebugHook("postFunction", listenForOutputChatBox, {"outputChatBox"})
end

function onClientResourceStop()
removeDebugHook("postFunction", listenForOutputChatBox)
end

addEventHandler("onClientResourceStart", resourceRoot, onResourceStart)
addEventHandler("onClientResourceStop", resourceRoot, onResourceStop)
addEventHandler("onChat2Loaded", resourceRoot, onChatLoaded)
Expand All @@ -91,3 +109,5 @@ addEventHandler("onChat2SendMessage", resourceRoot, onChatSendMessage)
addEventHandler("onChat2Output", localPlayer, output)
addEventHandler("onChat2Clear", localPlayer, clear)
addEventHandler("onChat2Show", localPlayer, show)
addEventHandler("onClientResourceStart", resourceRoot, onClientResourceStart)
addEventHandler("onClientResourceStop", resourceRoot, onClientResourceStop)
31 changes: 18 additions & 13 deletions chat2_server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,6 @@ function useCustomEventHandlers(bool)
isDefaultOutput = not bool
end

function RGBToHex(red, green, blue)
if (red < 0 or red > 255 or green < 0 or green > 255 or blue < 0 or blue > 255) then
return nil
end

return string.format("#%.2X%.2X%.2X", red, green, blue)
end

function onChatSendMessage(message, messageType)
if type(message) ~= "string" or utf8.len(message) < minLength or utf8.len(message) > maxLength then
return
Expand Down Expand Up @@ -102,13 +94,26 @@ function onPlayerChat(message, messageType)
end
end

-- listen for messages that were sent from resources
function onChatMessage(message, elementOrResource)
if not isElement(elementOrResource) then
output(root, message)
function listenForOutputChatBox(_, _, _, _, _, message, receiver, r, g, b)
receiver = receiver or root
local hexColor = ""

if (r and g and b) then
hexColor = RGBToHex(r, g, b)
end

output(receiver, string.format("%s%s", hexColor, message))
end

function onResourceStart()
addDebugHook("postFunction", listenForOutputChatBox, {"outputChatBox"})
end

function onResourceStop()
removeDebugHook("postFunction", listenForOutputChatBox)
end

addEventHandler("onChat2SendMessage", resourceRoot, onChatSendMessage)
addEventHandler("onPlayerChat", root, onPlayerChat)
addEventHandler("onChatMessage", root, onChatMessage)
addEventHandler("onResourceStart", resourceRoot, onResourceStart)
addEventHandler("onResourceStop", resourceRoot, onResourceStop)
7 changes: 7 additions & 0 deletions chat2_shared.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function RGBToHex(red, green, blue)
if (red < 0 or red > 255 or green < 0 or green > 255 or blue < 0 or blue > 255) then
return nil
end

return string.format("#%.2X%.2X%.2X", red, green, blue)
end
1 change: 1 addition & 0 deletions meta.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<meta>
<info name="chat2" type="script" version="0.3.0" author="nrzull" />

<script src="chat2_shared.lua" type="shared" />
<script src="chat2_server.lua" type="server" />
<script src="chat2_client.lua" type="client" />
<file src="index.html" />
Expand Down

0 comments on commit b282d56

Please sign in to comment.