-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontrol.lua
520 lines (500 loc) · 14.6 KB
/
control.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
local Commands = {}
local function Split(String, Seperator)
local Return = {}
Seperator = Seperator or " "
for Text in string.gmatch(String, "[^%"..Seperator.."]+") do
table.insert(Return, Text)
end
return Return
end
local function toboolean(value)
value = tostring(value)
if value == "true" then
return true
elseif value == "0" or value == "false" then
return false
end
end
local function Reverse(Table)
local Return = {}
for i, v in ipairs(Table) do
Return[v] = i
end
return Return
end
local function Perms(Player, Permission)
if global.Groups[global.Players[Player.name].Group] then
return Player.admin or global.Groups[global.Players[Player.name].Group].Permissions[Permission]
else
return Player.admin
end
end
local Colors = {
["Orange"] = {
["r"] = 255,
["g"] = 128,
["b"] = 80
},
["Red"] = {
["r"] = 255,
["g"] = 0,
["b"] = 0
},
["Yellow"] = {
["r"] = 255,
["g"] = 255,
["b"] = 0
},
["Green"] = {
["r"] = 0,
["g"] = 255,
["b"] = 0
},
["Blue"] = {
["r"] = 0,
["g"] = 0,
["b"] = 255
},
["Delta"] = {
["r"] = 0,
["g"] = 200,
["b"] = 255
},
["AntiDelta"] = {
["r"] = 0,
["g"] = 255,
["b"] = 200
}
}
function PlayerCreated(event)
local PlayerIndex = event.player_index
local Player = game.players[PlayerIndex]
if Player then
if not global.Players[Player.name] then
global.Players[Player.name] = {}
global.Players[Player.name].ID = PlayerIndex
Player.print({"AdminUtils.General.welcome_text"})
if game.players[PlayerIndex].admin and global.Groups.Admins then
if not global.Groups.Admins.Members then
global.Groups.Admins.Members = {}
end
table.insert(global.Groups.Admins.Members, Player.name)
global.Players[Player.name].Group = "Admins"
Player.print({"AdminUtils.General.admin_text"})
else
local Group = "default"
for Name, G in pairs(global.Groups) do
if G.Settings.Default then
Group = Name
break
end
end
if not global.Groups[Group].Members then
global.Groups[Group].Members = {}
end
table.insert(global.Groups[Group].Members, Player.name)
global.Players[Player.name].Group = Group
end
end
end
end
local function Reset()
global.Groups = {}
global.Players = {}
global.Groups.default = {
["Permissions"] = {
["lua"] = false, -- Run Lua code
["help"] = true,
["help-list"] = true,
["mute"] = false,
["unmute"] = false,
["kick"] = false,
["ban"] = false,
["unban"] = false,
["groups"] = true, -- List groups
["groups-add"] = false, -- Create group
["groups-remove"] = false, -- Remove group
["groups-settings"] = false, -- View & modify group settings
["groups-perms"] = false, -- View & modify group permissions
["groups-join"] = true, -- Join other groups
["groups-join-others"] = false, -- Put others in groups
["groups-invite"] = false -- Invite others to groups
},
["Settings"] = {
["Default"] = true, --New players will be put in this group
["Admin"] = false, --People who join this group will be made into factorio admins. This is dangerous.
["Joinable"] = true --People can use group join default
},
["Members"] = {},
["Invites"] = {}
}
global.Groups.Admins = global.Groups.default
for i,_ in pairs(global.Groups.Admins.Permissions) do
global.Groups.Admins.Permissions[i] = true
end
global.Groups.Admins.Settings = {
["Default"] = false,
["Admin"] = true,
["Joinable"] = false
}
if game then
PlayerCreated({["player_index"] = 1})
end
end
if global.Groups == nil then
global.Groups = {}
global.Players = {}
end
if global.reset_flag ~= 1 then
Reset()
global.reset_flag = 1
end
Commands.Lua = {
["Name"] = "c",
["Help"] = "lua",
["Function"] = function(event)
local PlayerIndex = event.player_index
if game.players[PlayerIndex] then
Player = game.players[PlayerIndex]
if event.parameter then
if Perms(Player, "lua") then
local Command, Error = loadstring(tostring(event.parameter):gsub("game%.player%.", "game.players["..PlayerIndex.."]."):format("%q"))
if Command then
local Success, Error = pcall(Command)
if not Success then
Player.print({"AdminUtils.Commands.lua-error", Error}, Colors.Red)
end
else
Player.print({"AdminUtils.Commands.lua-command", Error}, Colors.Red)
end
else
Player.print({"AdminUtils.General.access-denied", "//c"}, Colors.Red)
end
else
Player.print({"AdminUtils.Commands.lua-syntax"}, Colors.Red)
end
else
game.print({"AdminUtils.General.console"}, Colors.Red)
end
end
}
Commands.Help = {
["Name"] = "help",
["Help"] = "help",
["Function"] = function(event)
local PlayerIndex = event.player_index
local Player = game.players[PlayerIndex]
if Player then
if event.parameter and Perms(Player, "help") then
local Selection = event.parameter:lower()
local Success = false
for Name, Command in pairs(Commands) do
if Command.Name:lower() == Selection then
Success = true
Selection = Name
break
end
end
if Success then
if Commands[Selection].Help then
Player.print({"AdminUtils.Help."..Commands[Selection].Help})
else
Player.print({"AdminUtils.Commands.help-no-help"}, Colors.Red)
end
else
Player.print({"AdminUtils.Commands.help-no-command"}, Colors.Red)
end
elseif Perms(Player, "help-list") then
local T = {}
for _, C in pairs(Commands) do
table.insert(T, C.Name)
end
Player.print({"", {"AdminUtils.Commands.help"}, table.concat(T, ", ")})
else
Player.print({"AdminUtils.General.access-denied", "//help"}, Colors.Red)
end
else
game.print({"AdminUtils.General.console"}, Colors.Red)
end
end
}
Commands.Groups = {
["Name"] = "groups",
["Help"] = "groups",
["Function"] = function(event)
local PlayerIndex = event.player_index
local Player = game.players[PlayerIndex]
if Player then
if event.parameter then
local Args = Split(event.parameter, " ") or {}
if Args[1] == "add" then
if Perms(Player, "groups-add") then
if Args[2] then
if not global.Groups[ Args[2] ] then
local Group = global.Groups[global.Players[Player.name].Group]
Group.Members = {Player.name}
local Members = global.Groups[global.Players[Player.name].Group].Members
table.remove(Members, Reverse(Members)[Player.name])
global.Groups[global.Players[Player.name].Group].Members = Members
global.Groups[ Args[2] ] = Group
global.Players[Player.name].Group = Group
Player.print({"AdminUtils.Groups.added", Args[2]})
else
Player.print({"AdminUtils.Groups.taken"}, Colors.Red)
end
else
Player.print({"AdminUtils.Groups.name"}, Colors.Red)
end
else
Player.print({"AdminUtils.General.access-denied", "//groups add"}, Colors.Red)
end
elseif Args[1] == "remove" then
if Perms(Player, "groups-remove") then
if Args[2] then
if global.Groups[ Args[2] ] then
local Group = global.Groups[ Args[2] ]
local Members = Group.Members
local Default = "default"
for Name, G in pairs(global.Groups) do
if G.Settings.Default then
Default = Name
if Default == Args[2] then
Player.print({"Adminutils.Groups.remove-default"}, Colors.Red)
return 1
end
end
end
for i, _ in pairs(Members) do
global.Players[i].Group = "default"
end
table.remove(Members, Reverse(Members)[Player.name])
global.Groups[global.Players[Player.name].Group].Members = Members
global.Groups[ Args[2] ] = Group
global.Players[Player.name].Group = Group
Player.print({"AdminUtils.Groups.removed", Args[2]})
else
Player.print({"AdminUtils.Groups.non-existing"}, Colors.Red)
end
else
Player.print({"AdminUtils.Groups.name"}, Colors.Red)
end
else
Player.print({"AdminUtils.General.access-denied", "//groups add"}, Colors.Red)
end
elseif Args[1] == "join" then
Player.print("WIP")
elseif Args[1] == "settings" then
if Perms(Player, "groups-settings") then
local Group = global.Groups[ Args[2] ]
if Group then
if Group.Settings[ Args[3] ] ~= nil then
if Args[4] then
local Setting = toboolean(Args[4]) or Args[4]
Group.Settings[ Args[3] ] = Setting
global.Groups[ Args[2] ] = Group
Player.print({"AdminUtils.Groups.setting_updated", Args[3], Setting})
else
local Setting = tostring(Group.Settings[ Args[3] ])
Player.print({"AdminUtils.Groups.setting_value", Args[3], Setting})
end
else
Player.print({"AdminUtils.Groups.setting_missing"}, Colors.Red)
end
else
Player.print({"AdminUtils.Groups.name"}, Colors.Red)
end
else
Player.print({"Adminutils.General.access-denied", "//groups settings"}, Colors.Red)
end
elseif Args[1] == "perms" then
if Perms(Player, "groups-perms") then
local Group = global.Groups[ Args[2] ]
if Group then
if Group.Permissions[ Args[3] ] ~= nil then
local Permission = toboolean(Args[4])
if Permission then
Group.Permissions[ Args[3] ] = Permission
global.Groups[ Args[2] ] = Group
Player.print({"AdminUtils.Groups.permission_updated", Args[3], Setting})
else
local Permission = Groups.Settings[ Args[3] ]
Player.print({"AdminUtils.Groups.permission_value", Permission})
end
else
Player.print({"AdminUtils.Groups.permission_missing", Args[3]}, Colors.Red)
end
else
Player.print({"AdminUtils.Groups.name"}, Colors.Red)
end
else
Player.print({"Adminutils.General.access-denied", "//groups settings"}, Colors.Red)
end
else
Player.print({"AdminUtils.Help.groups"}, Colors.Red)
end
elseif Perms(Player, "groups-list") then
local T = {}
for Name, _ in pairs(global.Groups) do
table.insert(T, Name)
end
Player.print({"", {"AdminUtils.Groups.list"}, table.concat(T, ", ")})
else
Player.print({"AdminUtils.General.access-denied", "//groups"}, Colors.Red)
end
else
game.print({"AdminUtils.General.console"}, Colors.Red)
end
end
}
Commands.Kick = {
["Name"] = "kick",
["Help"] = "kick",
["Function"] = function(event)
local PlayerIndex = event.player_index
local Player = game.players[PlayerIndex]
if Player then
if Perms(Player, "kick") then
if event.parameter then
Args = Split(event.parameter)
if game.players[ Args[1] ] then
local Target = game.players[ Args[1] ]
table.remove(Args, 1)
local Reason = table.concat(Args, " ") or "Unspecified"
game.kick_player(Target, Reason)
else
Player.print({"AdminUtils.Commands.kick-no-player"}, Colors.Red)
end
else
Player.print({"AdminUtils.Commands.kick-no-player"}, Colors.Red)
end
else
Player.print({"AdminUtils.General.access-denied", "//kick"}, Colors.Red)
end
else
game.print({"AdminUtils.General.console"}, Colors.Red)
end
end
}
Commands.Ban = {
["Name"] = "ban",
["Help"] = "ban",
["Function"] = function(event)
local PlayerIndex = event.player_index
local Player = game.players[PlayerIndex]
if Player then
if Perms(Player, "ban") then
if event.parameter then
Args = Split(event.parameter)
if game.players[ Args[1] ] then
local Target = game.players[ Args[1] ]
table.remove(Args, 1)
local Reason = table.concat(Args, " ")
if Reason ~= "" then
game.ban_player(Target, Reason)
else
Player.print({"AdminUtils.Commands.ban-no-player"}, Colors.Red)
end
else
Player.print({"AdminUtils.Commands.ban-no-player"}, Colors.Red)
end
else
Player.print({"AdminUtils.Commands.ban-no-player"}, Colors.Red)
end
else
Player.print({"AdminUtils.General.access-denied", "//ban"}, Colors.Red)
end
else
game.print({"AdminUtils.General.console"}, Colors.Red)
end
end
}
Commands.Unban = {
["Name"] = "unban",
["Help"] = "unban",
["Function"] = function(event)
local PlayerIndex = event.player_index
local Player = game.players[PlayerIndex]
if Player then
if Perms(Player, "unban") then
if event.parameter then
Target = event.parameter
if game.players[ Target ] then
local Target = game.players[ Target ]
game.unban_player(Target)
else
Player.print({"AdminUtils.Commands.unban-no-player"}, Colors.Red)
end
else
Player.print({"AdminUtils.Commands.unban-no-player"}, Colors.Red)
end
else
Player.print({"AdminUtils.General.access-denied", "//unban"}, Colors.Red)
end
else
game.print({"AdminUtils.General.console"}, Colors.Red)
end
end
}
Commands.Mute = {
["Name"] = "mute",
["Help"] = "mute",
["Function"] = function(event)
local PlayerIndex = event.player_index
local Player = game.players[PlayerIndex]
if Player then
if Perms(Player, "mute") then
if event.parameter then
Args = Split(event.parameter)
if game.players[ Args[1] ] then
local Target = game.players[ Args[1] ]
table.remove(Args, 1)
local Reason = table.concat(Args, " ") or "Unspecified"
game.mute_player(Target)
game.print(Target.name.." was muted for: "..Reason..".")
else
Player.print({"AdminUtils.Commands.mute-no-player"}, Colors.Red)
end
else
Player.print({"AdminUtils.Commands.mute-no-player"}, Colors.Red)
end
else
Player.print({"AdminUtils.General.access-denied", "//mute"}, Colors.Red)
end
else
game.print({"AdminUtils.General.console"}, Colors.Red)
end
end
}
Commands.Unmute = {
["Name"] = "unmute",
["Help"] = "unmute",
["Function"] = function(event)
local PlayerIndex = event.player_index
local Player = game.players[PlayerIndex]
if Player then
if Perms(Player, "unmute") then
if event.parameter then
local Target = event.parameter
Target = game.players[Target]
if Target then
game.unmute_player(Target)
else
Player.print({"AdminUtils.Commands.unmute-no-player"}, Colors.Red)
end
else
Player.print({"AdminUtils.Commands.unmute-no-player"}, Colors.Red)
end
else
Player.print({"AdminUtils.General.access-denied", "//unmute"}, Colors.Red)
end
else
game.print({"AdminUtils.General.console"}, Colors.Red)
end
end
}
for d, Command in pairs(Commands) do
commands.add_command("/"..Command.Name:lower(), {"AdminUtils.Help."..Command.Help}, Command.Function)
end
script.on_init(Reset)
script.on_event(defines.events.on_player_created, PlayerCreated)
remote.add_interface("adminutils", {["reset"] = Reset}) --Use remote.call() to reset