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

Commit

Permalink
do not export show, clear and output functions
Browse files Browse the repository at this point in the history
remove serverside isVisible function
update README.md
update meta.xml
  • Loading branch information
nrzull committed Jun 19, 2019
1 parent 26abdd0 commit b8c4f8d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 54 deletions.
38 changes: 18 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
<!-- set this resource before all other resources -->
<resource src="chat2" startup="1" protected="0" />
```

Expand All @@ -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
Expand All @@ -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)
```
43 changes: 18 additions & 25 deletions chat2_client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ local chatInstance
local chatInstanceLoading
local chatInstanceLoaded

local state = {
show = false
}

addEvent("onChat2Loaded")
addEvent("onChat2Input")
addEvent("onChat2SendMessage")
Expand All @@ -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)
Expand All @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 0 additions & 4 deletions chat2_server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 3 additions & 5 deletions meta.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
<meta>
<info name="chat2" type="script" version="0.3.0" author="nrzull" />
<info name="chat2" type="script" version="0.4.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" />

<export function="clear" type="shared" />
<export function="show" type="shared" />
<export function="output" type="shared" />
<export function="isVisible" type="shared" />
<export function="isChatVisible" type="client" />
<export function="useCustomEventHandlers" type="server" />
</meta>

0 comments on commit b8c4f8d

Please sign in to comment.