From b282d569b7bade4c07cc4442ef0fcae9ce2da18d Mon Sep 17 00:00:00 2001 From: nrzull Date: Wed, 19 Jun 2019 20:15:42 +0500 Subject: [PATCH] redirect messages from outputChatBox --- chat2_client.lua | 24 ++++++++++++++++++++++-- chat2_server.lua | 31 ++++++++++++++++++------------- chat2_shared.lua | 7 +++++++ meta.xml | 1 + 4 files changed, 48 insertions(+), 15 deletions(-) create mode 100644 chat2_shared.lua diff --git a/chat2_client.lua b/chat2_client.lua index 11fb8a8..df6f7bd 100644 --- a/chat2_client.lua +++ b/chat2_client.lua @@ -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 @@ -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) @@ -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) diff --git a/chat2_server.lua b/chat2_server.lua index f6a6fcd..8ddaf14 100644 --- a/chat2_server.lua +++ b/chat2_server.lua @@ -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 @@ -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) diff --git a/chat2_shared.lua b/chat2_shared.lua new file mode 100644 index 0000000..7e233f2 --- /dev/null +++ b/chat2_shared.lua @@ -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 diff --git a/meta.xml b/meta.xml index 23ab2c4..d074ad7 100644 --- a/meta.xml +++ b/meta.xml @@ -1,6 +1,7 @@ +