From b8c4f8d74976d680f443e63ff544de3e0dfba9c6 Mon Sep 17 00:00:00 2001 From: nrzull Date: Thu, 20 Jun 2019 00:33:28 +0500 Subject: [PATCH] do not export show, clear and output functions remove serverside isVisible function update README.md update meta.xml --- README.md | 38 ++++++++++++++++++-------------------- chat2_client.lua | 43 ++++++++++++++++++------------------------- chat2_server.lua | 4 ---- meta.xml | 8 +++----- 4 files changed, 39 insertions(+), 54 deletions(-) diff --git a/README.md b/README.md index b491f1f..064627d 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ ## Description -This chat is using CEF and it tries to simulate behavior of default chat +This chat uses CEF and it tries to simulate behavior of default chat ## Pros @@ -21,6 +21,7 @@ This chat is using CEF and it tries to simulate behavior of default chat - add to `server/mods/deathmatch/mtaserver.conf`: ```xml + ``` @@ -39,43 +40,40 @@ This chat is using CEF and it tries to simulate behavior of default chat ## API -### Clientside +The resource intercepts `outputChatBox`, `clearChatBox` and `showChat` function calls and redirect their calls to internal `output`, `clear`, `show` so you can still use default MTA functions as before and all of your resources should probably work correct. -#### Functions +**WARNING!** THIS FUNCTIONS DO NOT RETURN ANY VALUES NO MORE AS BEFORE. IF YOU RELY ON RETURN VALUES, THEN YOU SHOULD REWRITE YOUR CODE OR DO NOT USE THIS RESOURCE AT ALL! + +After reviewing of all default mta resources code for just to be sure that there is no code that rely on return values, I made decision to change behavior of these functions. Also this decision was made when I realize that this resource is useless without easy integration in existing ecosystem. Noone will rewrite their codebase for just replacing one chat with another. And after all, some closed resources may use default chat API and you probably will not be able to change that. Sorry for this dirty hack. I apologize for it. -- **output(string message) -> void** - Writes a message to chat. Hex colors processing is enabled by default and this behavior can't be configured by end-user. +### Clientside -- **clear() -> void** - Clears all messages. +#### Functions -- **isVisible() -> bool** +- `exports.chat2:isChatVisible() -> bool` Returns true/false if chat is visible. -- **show(bool b) -> void** - Shows/hides a chat. - ### Serverside #### Functions -- **output(element player, string message) -> void** -- **clear(element player) -> void** -- **isVisible(element player) -> bool** -- **show(element player, bool b) -> 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** +- `onPlayerChat2` handler params: (element player, string message, int messageType) - Will be emitted only after useDefaultOutput(false) + Will be emitted only after `exports.chat2:useCustomEventHandlers(false)` ### Examples: ```lua addEventHandler("onPlayerJoin", root, function() - exports.chat2:output(source, "#ccff00hello #ffcc00world") - exports.chat2:useCustomEventHandlers(true) -- need to be executed if you want to disable default output handler and use your own output handlers + 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 @@ -88,7 +86,7 @@ end) -- may be created if useCustomEventHandlers was set to 'true' addEventHandler("onPlayerChat2", root, function(sender, message, messageType) if message == "ping" then - exports.chat2:output(sender, "pong") + outputChatBox("pong", sender) end end) ``` diff --git a/chat2_client.lua b/chat2_client.lua index 0f29d0e..8dccaa2 100644 --- a/chat2_client.lua +++ b/chat2_client.lua @@ -2,6 +2,10 @@ local chatInstance local chatInstanceLoading local chatInstanceLoaded +local state = { + show = false +} + addEvent("onChat2Loaded") addEvent("onChat2Input") addEvent("onChat2SendMessage") @@ -28,13 +32,15 @@ function output(message) return setTimer(output, 250, 1, message) end - local eval = string.format("addMessage(%s)", toJSON(message)) - execute(eval) + execute(string.format("addMessage(%s)", toJSON(message))) end function clear() - local eval = "clear()" - execute(eval) + execute("clear()") +end + +function isChatVisible() + return state.show end function show(bool) @@ -47,23 +53,8 @@ function show(bool) end end - local eval = "show(" .. tostring(bool) .. ");" - execute(eval) - setElementData(localPlayer, "chat2IsVisible", bool) -end - -function isVisible() - return getElementData(localPlayer, "chat2IsVisible", false) -end - -function onResourceStart() - showChat(false) - show(true) -end - -function onResourceStop() - show(false) - showChat(true) + execute(string.format("show(%s)", tostring(bool))) + state.show = bool end function onChatLoaded() @@ -105,19 +96,21 @@ function listenForClearChatBox() end function onClientResourceStart() - addDebugHook("preFunction", listenForOutputChatBox, {"outputChatBox"}) + showChat(false) addDebugHook("preFunction", listenForShowChat, {"showChat"}) + addDebugHook("preFunction", listenForOutputChatBox, {"outputChatBox"}) addDebugHook("preFunction", listenForClearChatBox, {"clearChatBox"}) + showChat(true) end function onClientResourceStop() - removeDebugHook("preFunction", listenForOutputChatBox) + showChat(false) removeDebugHook("preFunction", listenForShowChat) + removeDebugHook("preFunction", listenForOutputChatBox) removeDebugHook("preFunction", listenForClearChatBox) + showChat(true) end -addEventHandler("onClientResourceStart", resourceRoot, onResourceStart) -addEventHandler("onClientResourceStop", resourceRoot, onResourceStop) addEventHandler("onChat2Loaded", resourceRoot, onChatLoaded) addEventHandler("onChat2Input", resourceRoot, onChatInput) addEventHandler("onChat2SendMessage", resourceRoot, onChatSendMessage) diff --git a/chat2_server.lua b/chat2_server.lua index 7d4d29e..4c2b1a9 100644 --- a/chat2_server.lua +++ b/chat2_server.lua @@ -13,10 +13,6 @@ function show(player, bool) triggerClientEvent(player, "onChat2Show", player, bool) end -function isVisible(player) - return getElementData(player, "chat2IsVisible", false) -end - function output(player, message) triggerClientEvent(player, "onChat2Output", player, message) end diff --git a/meta.xml b/meta.xml index d074ad7..36a6287 100644 --- a/meta.xml +++ b/meta.xml @@ -1,14 +1,12 @@ - +