This repository was archived by the owner on Mar 11, 2024. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathchat2_server.lua
More file actions
143 lines (112 loc) · 3.5 KB
/
Copy pathchat2_server.lua
File metadata and controls
143 lines (112 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
addEvent("onChat2Message", true)
local isDefaultOutput = false
local minLength = 1
local maxLength = 96
function clear(player)
triggerClientEvent(player, "onChat2Clear", player)
end
function show(player, bool)
triggerClientEvent(player, "onChat2Show", player, bool)
end
function output(player, message)
triggerClientEvent(player, "onChat2Output", player, message)
end
function useDefaultOutput(bool)
isDefaultOutput = bool
end
function onPlayerChat(message, messageType)
if not isDefaultOutput then
return
end
if messageType ~= 0 and messageType ~= 2 then
return
end
local sender = source
local nickname = getPlayerName(sender)
local nicknameColor = ""
local r, g, b = getPlayerNametagColor(sender)
if (r and g and b) then
nicknameColor = RGBToHex(r, g, b)
nickname = string.format("%s%s", nicknameColor, nickname)
end
local team = getPlayerTeam(sender)
local text = string.format("%s: #ffffff%s", nickname, message)
local teamColor
if team then
local r, g, b = getTeamColor(team)
teamColor = RGBToHex(r, g, b)
end
if teamColor then
text = string.format("%s%s", teamColor, text)
end
if messageType == 0 then
for _, player in ipairs(getElementsByType("player")) do
output(player, text)
end
end
if messageType == 2 and team then
text = string.format("%s(team) %s", teamColor, text)
for _, player in ipairs(getPlayersInTeam(team)) do
output(player, text)
end
end
local serverLogMessage = pregReplace(text, "#[a-f0-9]{6}", "", "i")
if type(serverLogMessage) ~= "string" then
serverLogMessage = text
end
outputServerLog(serverLogMessage)
end
function handleCommand(client, input)
local splittedInput = split(input, " ")
local slashCmd = table.remove(splittedInput, 1)
local cmd = utf8.sub(slashCmd, 2, utf8.len(slashCmd))
local args = ""
for _, arg in ipairs(splittedInput) do
args = string.format("%s %s", args, arg)
end
args = utf8.sub(args, 2, utf8.len(args))
executeCommandHandler(cmd, client, args)
end
function onChatMessage(message, messageType)
if type(message) ~= "string" or utf8.len(message) < minLength or utf8.len(message) > maxLength then
return
end
if type(messageType) ~= "number" then
return
end
if utf8.sub(message, 0, 1) == "/" then
return handleCommand(client, message)
end
triggerEvent("onPlayerChat", client, message, messageType)
end
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))
return "skip"
end
function listenForShowChat(_, _, _, _, _, player, bool)
show(player, bool)
return "skip"
end
function listenForClearChatBox(_, _, _, _, _, player)
clear(player)
return "skip"
end
function onResourceStart()
addDebugHook("preFunction", listenForOutputChatBox, {"outputChatBox"})
addDebugHook("preFunction", listenForShowChat, {"showChat"})
addDebugHook("preFunction", listenForClearChatBox, {"clearChatBox"})
end
function onResourceStop()
removeDebugHook("preFunction", listenForOutputChatBox)
removeDebugHook("preFunction", listenForShowChat)
removeDebugHook("preFunction", listenForClearChatBox)
end
addEventHandler("onPlayerChat", root, onPlayerChat)
addEventHandler("onChat2Message", resourceRoot, onChatMessage)
addEventHandler("onResourceStart", resourceRoot, onResourceStart)
addEventHandler("onResourceStop", resourceRoot, onResourceStop)