-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathchat.lua
187 lines (172 loc) · 6.47 KB
/
chat.lua
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
Channel = {};
function OnChat(Player, Message)
if (config.handle_chat == true) then --Only handle chat if set so in the config
local UUID = Player:GetUUID();
local senderName = Player:GetName();
--If the player is in the specified channel, send it to the players having that channel active
if (Channel[UUID] == "nation") then --Nation channel
local sql = "SELECT DISTINCT town_residents.player_uuid FROM town_residents INNER JOIN towns ON town_residents.town_id = towns.town_id WHERE towns.nation_id = (SELECT nations.nation_id FROM nations INNER JOIN towns ON nations.nation_id = towns.nation_id INNER JOIN town_residents ON towns.town_id = town_residents.town_id WHERE town_residents.player_uuid = ?)";
local parameter = {UUID};
local players = ExecuteStatement(sql, parameter);
for key, value in pairs(players) do
cRoot:Get():DoWithPlayerByUUID(value[1],
function (Player)
Player:SendMessage("@b[Nation] @f" .. senderName .. ": " .. Message);
end
);
end
elseif (Channel[UUID] == "town") then --Town channel
local sql = "SELECT DISTINCT player_uuid FROM town_residents WHERE town_id = (SELECT town_id FROM town_residents WHERE player_uuid = ? LIMIT 1)";
local parameter = {UUID};
local players = ExecuteStatement(sql, parameter);
for key, value in pairs(players) do
player = cRoot:Get():DoWithPlayerByUUID(value[1],
function (Player)
Player:SendMessage("@b[Town] @f" .. senderName .. ": " .. Message);
end
);
end
elseif (Channel[UUID] == "local") then
local world = Player:GetWorld():ForEachPlayer(
function (a_OtherPlayer)
if ((math.abs(Player:GetPosX() - a_OtherPlayer:GetPosX()) <= config.range_localChat) and (math.abs(Player:GetPosZ() - a_OtherPlayer:GetPosZ()) <= config.range_localChat)) then
a_OtherPlayer:SendMessage("@f[Local] " .. senderName .. ": " .. Message);
end
end
);
elseif (Channel[UUID] == "moderator") then
cRoot:Get():ForEachPlayer(
function (a_OtherPlayer)
if (a_OtherPlayer:HasPermission("townvalds.chat.moderator")) or (a_OtherPlayer:HasPermission("townvalds.chat.admin")) then
a_OtherPlayer:SendMessage("@b[Moderator] @f" .. senderName .. ": " .. Message);
end
end
);
elseif (Channel[UUID] == "admin") then
cRoot:Get():ForEachPlayer(
function (a_OtherPlayer)
if (a_OtherPlayer:HasPermission("townvalds.chat.admin")) then
a_OtherPlayer:SendMessage("@b[Admin] @f" .. senderName .. ": " .. Message);
end
end
);
else
cRoot:Get():BroadcastChat("@a[Global] @f" .. Player:GetName() .. ": " .. Message);
end
return true;
else
return false;
end
end
function SwitchChat(Split, Player)
if (config.handle_chat == true) then --Only handle chat if set so in the config
local UUID = Player:GetUUID();
if (Split[1] == "/switchchat" or Split[1] == "/sc") then
if not(Split[2]) then
Player:SendMessageFailure("This command requires another argument!");
Player:SendMessageFailure("Usage: /switchchat (channel)");
else
if (Split[2] == "global") then
switchGlobal(Player, UUID);
elseif (Split[2] == "nation") then
switchNation(Player, UUID);
elseif (Split[2] == "town") then
switchTown(Player, UUID);
elseif (Split[2] == "local") then
switchLocal(Player, UUID);
elseif (Split[2] == "moderator") then
switchModerator(Player, UUID);
elseif (Split[2] == "admin") then
switchAdmin(Player, UUID);
else
Player:SendMessageFailure("That channel doesn't exist.");
end
end
elseif (Split[1] == "/gc") then
switchGlobal(Player, UUID);
elseif (Split[1] == "/nc") then
switchNation(Player, UUID);
elseif (Split[1] == "/tc") then
switchTown(Player, UUID);
elseif (Split[1] == "/lc") then
switchLocal(Player, UUID);
elseif (Split[1] == "/mc") then
switchModerator(Player, UUID);
elseif (Split[1] == "/ac") then
switchAdmin(Player, UUID);
else
Player:SendMessageFailure("That channel doesn't exist.");
end
end
return true;
end
function switchGlobal(Player, UUID)
if (Channel[UUID] == "global") then
Player:SendMessageFailure("You are already in this channel");
else
Channel[UUID] = "global";
Player:SendMessageInfo("Switched to the global channel");
end
end
function switchNation(Player, UUID)
if (Channel[UUID] == "nation") then
Player:SendMessageFailure("You are already in this channel");
else
local sql = "SELECT DISTINCT nations.nation_id FROM nations INNER JOIN towns ON nations.nation_id = towns.nation_id INNER JOIN town_residents ON towns.town_id = town_residents.town_id WHERE town_residents.player_uuid = ?";
local parameter = {UUID};
local nation = ExecuteStatement(sql, parameter)[1];
if not (nation) then
Player:SendMessageFailure("You can not switch to nation chat if you're not part of a nation");
else
Channel[UUID] = "nation";
Player:SendMessageSuccess("Switched to the nation channel");
end
end
end
function switchTown(Player, UUID)
if (Channel[UUID] == "town") then
Player:SendMessageFailure("You are already in this channel");
else
sql = "SELECT DISTINCT town_id FROM town_residents WHERE player_uuid = ?";
parameter = {UUID};
local town_id = ExecuteStatement(sql, parameter)[1][1];
if not (town_id) then
Player:SendMessageFailure("You can not switch to town chat if you're not part of a town");
else
Channel[UUID] = "town";
Player:SendMessageInfo("Switched to the town channel");
end
end
end
function switchLocal(Player, UUID)
if (Channel[UUID] == "local") then
Player:SendMessageFailure("You are already in this channel");
else
Channel[UUID] = "local";
Player:SendMessageInfo("Switched to the local channel. Your messages now have a reach of " .. config.range_localChat .. " blocks");
end
end
function switchModerator(Player, UUID)
if (Channel[UUID] == "moderator") then
Player:SendMessageFailure("You are already in this channel");
else
if not (Player:HasPermission("townvalds.chat.moderator")) then
Player:SendMessageFailure("You don't have the right permissions for this chat channel");
else
Channel[UUID] = "moderator";
Player:SendMessageSuccess("Switched to the moderator channel");
end
end
end
function switchAdmin(Player, UUID)
if (Channel[UUID] == "admin") then
Player:SendMessageFailure("You are already in this channel");
else
if not (Player:HasPermission("townvalds.chat.admin")) then
Player:SendMessageFailure("You don't have the right permissions for this chat channel");
else
Channel[UUID] = "admin";
Player:SendMessageSuccess("Switched to the admin channel");
end
end
end