-
Notifications
You must be signed in to change notification settings - Fork 351
Expand file tree
/
Copy pathchat.lua
More file actions
167 lines (131 loc) · 4.55 KB
/
chat.lua
File metadata and controls
167 lines (131 loc) · 4.55 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
-- Original author: ZeikJT
-- Modified by Gwahir and TomyLobo
local IsValid = IsValid
local TextList = {
last = { "", 0, NULL }
}
local ChatAlert = {}
local chatAuthor
local chipHideChat
local chipChatReplacement
--[[************************************************************************]]--
registerCallback("destruct",function(self)
ChatAlert[self.entity] = nil
end)
hook.Add("PlayerSay","Exp2TextReceiving", function(ply, text, teamchat)
chipHideChat, chipChatReplacement = nil, nil
local entry = { text, CurTime(), ply, teamchat }
TextList[ply:EntIndex()] = entry
TextList.last = entry
chatAuthor = ply
E2Lib.triggerEvent("chat", { ply, text, teamchat and 1 or 0 })
for e, _ in pairs(ChatAlert) do
if IsValid(e) then
e.context.data.runByChat = entry
e:Execute()
e.context.data.runByChat = nil
else
ChatAlert[e] = nil
end
end
local hide, repl = chipHideChat, chipChatReplacement
chipHideChat, chipChatReplacement = nil, nil
if hide then return "" end
return repl
end)
hook.Add("EntityRemoved","Exp2ChatPlayerDisconnect", function(ply)
TextList[ply:EntIndex()] = nil
end)
--[[************************************************************************]]--
__e2setcost(3)
--- If <activate> == 0, the chip will no longer run on chat events, otherwise it makes this chip execute when someone chats. Only needs to be called once, not in every execution.
[deprecated = "Use the chat event instead"]
e2function void runOnChat(activate)
if activate ~= 0 then
ChatAlert[self.entity] = true
else
ChatAlert[self.entity] = nil
end
end
--- Returns 1 if the chip is being executed because of a chat event. Returns 0 otherwise.
[nodiscard, deprecated = "Use the chat event instead"]
e2function number chatClk()
return self.data.runByChat and 1 or 0
end
--- Returns 1 if the chip is being executed because of a chat event by player <ply>. Returns 0 otherwise.
[nodiscard, deprecated = "Use the chat event instead"]
e2function number chatClk(entity ply)
if not IsValid(ply) then return self:throw("Invalid player!", 0) end
local cause = self.data.runByChat
return cause and cause[3] == ply and 1 or 0
end
--- If <hide> != 0, hide the chat message that is currently being processed.
e2function void hideChat(hide)
if self.player == chatAuthor then
chipHideChat = hide ~= 0
end
end
--- Changes the chat message, if the chat message was written by the E2 owner.
e2function void modifyChat(string new)
if self.player == chatAuthor then
chipChatReplacement = new
end
end
--[[************************************************************************]]--
--- Returns the last player to speak.
[nodiscard, deprecated = "Use the chat event instead"]
e2function entity lastSpoke()
return TextList.last[3]
end
--- Returns the last message in the chat log.
[nodiscard, deprecated = "Use the chat event instead"]
e2function string lastSaid()
local entry = TextList.last
if not entry then return "" end
return entry[1]
end
--- Returns the time the last message was sent.
e2function number lastSaidWhen()
local entry = TextList.last
if not entry then return 0 end
return entry[2]
end
--- Returns 1 if the last message was sent in the team chat, 0 otherwise.
[nodiscard, deprecated = "Use the chat event instead"]
e2function number lastSaidTeam()
local entry = TextList.last
if not entry then return 0 end
return entry[4] and 1 or 0
end
--- Returns what the player <this> last said.
[nodiscard, deprecated = "Use the chat event instead"]
e2function string entity:lastSaid()
if not IsValid(this) then return self:throw("Invalid entity!", "") end
if not this:IsPlayer() then return self:throw("Not a player", "") end
local entry = TextList[this:EntIndex()]
if not entry then return "" end
return entry[1]
end
--- Returns when the given player last said something.
e2function number entity:lastSaidWhen()
if not IsValid(this) then return self:throw("Invalid entity!", 0) end
if not this:IsPlayer() then return self:throw("Not a player", 0) end
local entry = TextList[this:EntIndex()]
if not entry then return 0 end
return entry[2]
end
--- Returns 1 if the last message was sent in the team chat, 0 otherwise.
[nodiscard, deprecated = "Use the chat event instead"]
e2function number entity:lastSaidTeam()
if not IsValid(this) then return self:throw("Invalid entity!", 0) end
if not this:IsPlayer() then return self:throw("Not a player", 0) end
local entry = TextList[this:EntIndex()]
if not entry then return 0 end
return entry[4] and 1 or 0
end
-- Ply: entity, Msg: string, Team: number
E2Lib.registerEvent("chat", {
{ "Player", "e" },
{ "Message", "s" },
{ "Team", "n" }
})