forked from cuberite/Core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole.lua
388 lines (279 loc) · 9.15 KB
/
console.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
-- console.lua
-- Implements things related to console commands
function HandleConsoleClear(Split)
if (#Split == 1) then
return true, "Usage: clear <PlayerName>"
end
local InventoryCleared = false;
local ClearInventory = function(Player)
if (Player:GetName() == Split[2]) then
Player:GetInventory():Clear()
InventoryCleared = true
end
end
cRoot:Get():FindAndDoWithPlayer(Split[2], ClearInventory);
if (InventoryCleared) then
return true, "You cleared the inventory of " .. Split[2]
else
return true, "Player not found"
end
end
function HandleConsoleKick(Split)
if (#Split < 2) then
return true, "Usage: kick <PlayerName> [<Reason>]"
end
local Reason = cChatColor.Red .. "You have been kicked."
if (#Split > 2) then
Reason = table.concat(Split, " ", 3)
end
if (KickPlayer(Split[2], Reason)) then
return true
end
return true, "Cannot find player " .. Split[2]
end
function HandleConsoleKill(Split)
-- Check the params:
if (#Split == 1) then
return true, "Usage: kill <PlayerName>"
end
-- Kill the player:
local HasKilled = false;
cRoot:Get():FindAndDoWithPlayer(Split[2],
function(Player)
if (Player:GetName() == Split[2]) then
Player:TakeDamage(dtAdmin, nil, 1000, 1000, 0)
HasKilled = true
end
end
);
-- Report success or failure:
if (HasKilled) then
return true, "Player " .. Split[2] .. " is killed"
else
return true, "Player not found"
end
end
function HandleConsoleList(Split)
local PlayerTable = {}
cRoot:Get():ForEachPlayer(
function(a_Player)
table.insert(PlayerTable, a_Player:GetName())
end
)
table.sort(PlayerTable)
local Out = "Players (" .. #PlayerTable .. "): " .. table.concat(PlayerTable, ", ")
return true, Out
end
function HandleConsoleListGroups(a_Split)
if (a_Split[3] ~= nil) then
-- Too many params:
return true, "Too many parameters. Usage: listgroups [<RankName>]"
end
-- If no params are given, list all groups that the manager knows:
local RankName = a_Split[2]
if (RankName == nil) then
-- Get all the groups:
local Groups = cRankManager:GetAllGroups()
-- Output the groups, concatenated to a string:
local Out = "Available groups:\n"
Out = Out .. table.concat(Groups, ", ")
return true, Out
end
-- A rank name is given, list the groups in that rank:
local Groups = cRankManager:GetRankGroups(RankName)
local Out = "Groups in rank " .. RankName .. ":\n" .. table.concat(Groups, ", ")
return true, Out
end
function HandleConsoleListRanks(Split)
-- Get all the groups:
local Groups = cRankManager:GetAllRanks()
-- Output the groups, concatenated to a string:
local Out = "Available ranks:\n"
Out = Out .. table.concat(Groups, ", ")
return true, Out
end
function HandleConsoleNumChunks(Split)
-- List each world's chunk count into a table, sum the total chunk count:
local Output = {}
local Total = 0
cRoot:Get():ForEachWorld(
function(a_World)
table.insert(Output, a_World:GetName() .. ": " .. a_World:GetNumChunks() .. " chunks")
Total = Total + a_World:GetNumChunks()
end
)
table.sort(Output)
-- Return the complete report:
return true, table.concat(Output, "\n") .. "\nTotal: " .. Total .. " chunks\n"
end
function HandleConsolePlayers(Split)
local PlayersInWorlds = {} -- "WorldName" => [players array]
local AddToTable = function(Player)
local WorldName = Player:GetWorld():GetName()
if (PlayersInWorlds[WorldName] == nil) then
PlayersInWorlds[WorldName] = {}
end
table.insert(PlayersInWorlds[WorldName], Player:GetName() .. " @ " .. Player:GetIP())
end
cRoot:Get():ForEachPlayer(AddToTable)
local Out = ""
for WorldName, Players in pairs(PlayersInWorlds) do
Out = Out .. "World " .. WorldName .. ":\n"
for i, PlayerName in ipairs(Players) do
Out = Out .. " " .. PlayerName .. "\n"
end
end
return true, Out
end
function HandleConsolePlugins(Split)
local PluginManager = cRoot:Get():GetPluginManager()
local PluginList = PluginManager:GetAllPlugins()
local PluginTable = {}
for k, Plugin in pairs(PluginList) do
if Plugin then
table.insert(PluginTable, Plugin:GetName())
end
end
table.sort(PluginTable)
local Out = "There are " .. #PluginTable .. " loaded plugins: " .. table.concat(PluginTable, ", ")
return true, Out
end
function HandleConsoleRank(a_Split)
-- Check parameters:
if ((a_Split[2] == nil) or (a_Split[4] ~= nil)) then
-- Not enough or too many parameters
return true, "Usage: rank <Player> [<Rank>]"
end
-- Translate the PlayerName to a UUID:
local PlayerName = a_Split[2]
local PlayerUUID
if (cRoot:Get():GetServer():ShouldAuthenticate()) then
-- The server is in online-mode, get the UUID from Mojang servers and check for validity:
PlayerUUID = cMojangAPI:GetUUIDFromPlayerName(PlayerName)
if ((PlayerUUID == nil) or (string.len(PlayerUUID) ~= 32)) then
return true, "There is no such player: " .. PlayerName
end
else
-- The server is in offline mode, generate an offline-mode UUID, no validity check is possible:
PlayerUUID = cClientHandle:GenerateOfflineUUID(PlayerName)
end
-- View the player's rank, if requested:
if (a_Split[3] == nil) then
-- "/rank <PlayerName>" usage, display the rank:
local CurrRank = cRankManager:GetPlayerRankName(PlayerUUID)
if (CurrRank == "") then
return true, "The player has no rank assigned to them."
else
return true, "The player's rank is " .. CurrRank
end
end
-- Change the player's rank:
local NewRank = a_Split[3]
if not(cRankManager:RankExists(NewRank)) then
return true, "The specified rank does not exist!"
end
cRankManager:SetPlayerRank(PlayerUUID, PlayerName, NewRank)
-- Update all players in the game of the given name and let them know:
cRoot:Get():ForEachPlayer(
function(a_CBPlayer)
if (a_CBPlayer:GetName() == PlayerName) then
a_CBPlayer:SendMessageInfo("You were assigned the rank " .. NewRank .. " by the server console")
a_CBPlayer:LoadRank()
end
end
)
return true, "Player " .. PlayerName .. " is now in rank " .. NewRank
end
function HandleConsoleSaveAll(Split)
cRoot:Get():SaveAllChunks()
return true
end
function HandleConsoleSay(a_Split)
cRoot:Get():BroadcastChat(cChatColor.Gold .. "[SERVER] " .. cChatColor.Yellow .. table.concat(a_Split, " ", 2))
return true
end
function HandleConsoleTeleport(Split)
local TeleportToCoords = function(Player)
if (Player:GetName() == Split[2]) then
IsPlayerOnline = true
Player:TeleportToCoords(Split[3], Split[4], Split[5])
end
end
local IsPlayerOnline = false;
local FirstPlayerOnline = false;
local GetPlayerCoords = function(Player)
if (Player:GetName() == Split[3]) then
PosX = Player:GetPosX()
PosY = Player:GetPosY()
PosZ = Player:GetPosZ()
FirstPlayerOnline = true
end
end
local TeleportToPlayer = function(Player)
if (Player:GetName() == Split[2]) then
Player:TeleportToCoords(PosX, PosY, PosZ)
IsPlayerOnline = true
end
end
if (#Split == 3) then
cRoot:Get():FindAndDoWithPlayer(Split[3], GetPlayerCoords);
if (FirstPlayerOnline) then
cRoot:Get():FindAndDoWithPlayer(Split[2], TeleportToPlayer);
if (IsPlayerOnline) then
return true, "Teleported " .. Split[2] .." to " .. Split[3]
end
else
return true, "Player " .. Split[3] .." not found"
end
elseif (#Split == 5) then
cRoot:Get():FindAndDoWithPlayer(Split[2], TeleportToCoords);
if (IsPlayerOnline) then
return true, "You teleported " .. Split[2] .. " to [X:" .. Split[3] .. " Y:" .. Split[4] .. " Z:" .. Split[5] .. "]"
else
return true, "Player not found"
end
else
return true, "Usage: tp <PlayerName> <ToPlayerName> or tp <PlayerName> <X> <Y> <Z>"
end
end
function HandleConsoleUnload(Split)
local UnloadChunks = function(World)
World:QueueUnloadUnusedChunks()
end
local Out = "Num loaded chunks before: " .. cRoot:Get():GetTotalChunkCount() .. "\n"
cRoot:Get():ForEachWorld(UnloadChunks)
Out = Out .. "Num loaded chunks after: " .. cRoot:Get():GetTotalChunkCount()
return true, Out
end
function HandleConsoleUnrank(a_Split)
-- Check params:
if ((a_Split[2] == nil) or (a_Split[3] ~= nil)) then
-- Too few or too many parameters:
return true, "Usage: unrank <PlayerName>"
end
-- Translate the PlayerName to a UUID:
local PlayerName = a_Split[2]
local PlayerUUID
if (cRoot:Get():GetServer():ShouldAuthenticate()) then
-- The server is in online-mode, get the UUID from Mojang servers and check for validity:
PlayerUUID = cMojangAPI:GetUUIDFromPlayerName(PlayerName)
if ((PlayerUUID == nil) or (string.len(PlayerUUID) ~= 32)) then
return true, "There is no such player: " .. PlayerName
end
else
-- The server is in offline mode, generate an offline-mode UUID, no validity check is possible:
PlayerUUID = cClientHandle:GenerateOfflineUUID(PlayerName)
end
-- Unrank the player:
cRankManager:RemovePlayerRank(PlayerUUID)
-- Update all players in the game of the given name and let them know:
cRoot:Get():ForEachPlayer(
function(a_CBPlayer)
if (a_CBPlayer:GetName() == PlayerName) then
a_CBPlayer:SendMessageInfo("You were unranked by the server console")
a_CBPlayer:LoadRank()
end
end
)
return true, "Player " .. PlayerName .. " is now in the default rank."
end