-
Notifications
You must be signed in to change notification settings - Fork 2
/
chatcommands.lua
763 lines (694 loc) · 24.7 KB
/
chatcommands.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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
-------------------------------------------------------------------------------
-- factions Mod by Sapier
--
-- License WTFPL
--
--! @file chatcommnd.lua
--! @brief factions chat interface
--! @copyright Sapier, agrecascino, shamoanjac
--! @author Sapier, agrecascino, shamoanjac
--! @date 2016-08-12
--
-- Contact sapier a t gmx net
-------------------------------------------------------------------------------
local send_error = function(player, message)
minetest.chat_send_player(player, message)
end
factions_chat = {}
factions.commands = {}
factions.register_command = function(cmd_name, cmd)
factions.commands[cmd_name] = { -- default command
name = cmd_name,
faction_permissions = {},
global_privileges = {},
format = {},
infaction = true,
description = "This command has no description.",
run = function(self, player, argv)
if self.global_privileges then
local tmp = {}
for i in ipairs(self.global_privileges) do
tmp[self.global_privileges[i]] = true
end
local bool, missing_privs = minetest.check_player_privs(player, tmp)
if not bool then
send_error(player, "Unauthorized.")
return false
end
end
-- checks argument formats
local args = {
factions = {},
players = {},
strings = {},
other = {}
}
if #argv < #(self.format) then
send_error(player, "Not enough parameters.")
return false
end
for i in ipairs(self.format) do
local argtype = self.format[i]
local arg = argv[i]
if argtype == "faction" then
local fac = factions.get_faction(arg)
if not fac then
send_error(player, "Specified faction "..arg.." does not exist")
return false
else
table.insert(args.factions, fac)
end
elseif argtype == "player" then
local pl = minetest.get_player_by_name(arg)
if not pl and not factions.players[arg] then
send_error(player, "Player is not online.")
return false
else
table.insert(args.players, pl)
end
elseif argtype == "string" then
table.insert(args.strings, arg)
else
minetest.log("error", "Bad format definition for function "..self.name)
send_error(player, "Internal server error")
return false
end
end
for i=#self.format, #argv, 1 do
table.insert(args.other, argv[i])
end
-- checks permissions
local player_faction = factions.get_player_faction(player)
if self.infaction and not player_faction then
minetest.chat_send_player(player, "This command is only available within a faction.")
return false
end
if self.faction_permissions then
for i in ipairs(self.faction_permissions) do
if not player_faction:has_permission(player, self.faction_permissions[i]) then
send_error(player, "You don't have permissions to do that.")
return false
end
end
end
-- get some more data
local pos = minetest.get_player_by_name(player):getpos()
local parcelpos = factions.get_parcel_pos(pos)
return self.on_success(player, player_faction, pos, parcelpos, args)
end,
on_success = function(player, faction, pos, parcelpos, args)
minetest.chat_send_player(player, "Not implemented yet!")
end
}
-- override defaults
for k, v in pairs(cmd) do
factions.commands[cmd_name][k] = v
end
end
local init_commands
init_commands = function()
minetest.register_privilege("faction_user",
{
description = "this user is allowed to interact with faction mod",
give_to_singleplayer = true,
}
)
minetest.register_privilege("faction_admin",
{
description = "this user is allowed to create or delete factions",
give_to_singleplayer = true,
}
)
minetest.register_chatcommand("factions",
{
params = "<cmd> <parameter 1> .. <parameter n>",
description = "faction administration functions",
privs = { interact=true },
func = factions_chat.cmdhandler,
}
)
minetest.register_chatcommand("f",
{
params = "<command> parameters",
description = "Factions commands. Type /f help for available commands.",
privs = { interact=true},
func = factions_chat.cmdhandler,
}
)
end
-------------------------------------------
-- R E G I S T E R E D C O M M A N D S |
-------------------------------------------
factions.register_command ("claim", {
faction_permissions = {"claim"},
description = "Claim the plot of land you're on.",
on_success = function(player, faction, pos, parcelpos, args)
local can_claim = faction:can_claim_parcel(parcelpos)
if can_claim then
minetest.chat_send_player(player, "Claming parcel "..parcelpos)
faction:claim_parcel(parcelpos)
return true
else
local parcel_faction = factions.get_parcel_faction(parcelpos)
if not parcel_faction then
send_error(player, "You faction cannot claim any (more) parcel(s).")
return false
elseif parcel_faction.name == faction.name then
send_error(player, "This parcel already belongs to your faction.")
return false
else
send_error(player, "This parcel belongs to another faction.")
return false
end
end
end
})
factions.register_command("unclaim", {
faction_permissions = {"claim"},
description = "Unclaim the plot of land you're on.",
on_success = function(player, faction, pos, parcelpos, args)
local parcel_faction = factions.get_parcel_faction(parcelpos)
if parcel_faction.name ~= faction.name then
send_error(player, "This parcel does not belong to you.")
return false
else
faction:unclaim_parcel(parcelpos)
return true
end
end
})
--list all known factions
factions.register_command("list", {
description = "List all registered factions.",
infaction = false,
on_success = function(player, faction, pos, parcelpos, args)
local list = factions.get_faction_list()
local tosend = "Existing factions:"
for i,v in ipairs(list) do
if i ~= #list then
tosend = tosend .. " " .. v .. ","
else
tosend = tosend .. " " .. v
end
end
minetest.chat_send_player(player, tosend, false)
return true
end
})
--show factions mod version
factions.register_command("version", {
description = "Displays mod version.",
on_success = function(player, faction, pos, parcelpos, args)
minetest.chat_send_player(player, "factions: version " .. factions_version , false)
end
})
--show description of faction
factions.register_command("info", {
format = {"faction"},
description = "Shows a faction's description.",
on_success = function(player, faction, pos, parcelpos, args)
minetest.chat_send_player(player,
"factions: " .. args.factions[1].name .. ": " ..
args.factions[1].description, false)
return true
end
})
factions.register_command("leave", {
description = "Leave your faction.",
on_success = function(player, faction, pos, parcelpos, args)
faction:remove_player(player)
return true
end
})
factions.register_command("kick", {
faction_permissions = {"playerslist"},
format = {"player"},
description = "Kick a player from your faction.",
on_success = function(player, faction, pos, parcelpos, args)
local victim = args.players[1]
local victim_faction = factions.get_player_faction(victim:get_player_name())
if victim_faction and victim:get_player_name() ~= faction.leader then -- can't kick da king
faction:remove_player(victim:get_player_name())
return true
elseif not victim_faction then
send_error(player, victim:get_player_name().." is not in your faction.")
return false
else
send_error(player, victim:get_player_name().." cannot be kicked from your faction.")
return false
end
end
})
--create new faction
factions.register_command("create", {
format = {"string"},
infaction = false,
description = "Create a new faction.",
on_success = function(player, faction, pos, parcelpos, args)
if faction then
send_error(player, "You are already in a faction.")
return false
end
local factionname = args.strings[1]
if factions.can_create_faction(factionname) then
new_faction = factions.new_faction(factionname, nil)
new_faction:add_player(player, new_faction.default_leader_rank)
return true
else
send_error(player, "Faction cannot be created.")
return false
end
end
})
factions.register_command("join", {
format = {"faction"},
description = "Join a faction.",
infaction = false,
on_success = function(player, faction, pos, parcelpos, args)
local new_faction = args.factions[1]
if new_faction:can_join(player) then
if faction then -- leave old faction
faction:remove_player(player)
end
new_faction:add_player(player)
else
send_error(player, "You cannot join this faction.")
return false
end
return true
end
})
factions.register_command("disband", {
faction_permissions = {"disband"},
description = "Disband your faction.",
on_success = function(player, faction, pos, parcelpos, args)
faction:disband()
return true
end
})
factions.register_command("close", {
faction_permissions = {"playerslist"},
description = "Make your faction invite-only.",
on_success = function(player, faction, pos, parcelpos, args)
faction:toggle_join_free(false)
return true
end
})
factions.register_command("open", {
faction_permissions = {"playerslist"},
description = "Allow any player to join your faction.",
on_success = function(player, faction, pos, parcelpos, args)
faction:toggle_join_free(true)
return true
end
})
factions.register_command("description", {
faction_permissions = {"description"},
description = "Set your faction's description",
on_success = function(player, faction, pos, parcelpos, args)
faction:set_description(table.concat(args.other," "))
return true
end
})
factions.register_command("invite", {
format = {"player"},
faction_permissions = {"playerslist"},
description = "Invite a player to your faction.",
on_success = function(player, faction, pos, parcelpos, args)
faction:invite_player(args.players[1]:get_player_name())
return true
end
})
factions.register_command("uninvite", {
format = {"player"},
faction_permissions = {"playerslist"},
description = "Revoke a player's invite.",
on_success = function(player, faction, pos, parcelpos, args)
faction:revoke_invite(args.players[1]:get_player_name())
return true
end
})
factions.register_command("delete", {
global_privileges = {"faction_admin"},
format = {"faction"},
infaction = false,
description = "Delete a faction.",
on_success = function(player, faction, pos, parcelpos, args)
args.factions[1]:disband()
return true
end
})
factions.register_command("ranks", {
description = "List ranks within your faction",
on_success = function(player, faction, pos, parcelpos, args)
for rank, permissions in pairs(faction.ranks) do
minetest.chat_send_player(player, rank..": "..table.concat(permissions, " "))
end
return true
end
})
factions.register_command("who", {
description = "List players in your faction, and their ranks.",
on_success = function(player, faction, pos, parcelpos, args)
if not faction.players then
minetest.chat_send_player(player, "There is nobody in this faction ("..faction.name..")")
return true
end
minetest.chat_send_player(player, "Players in faction "..faction.name..": ")
for p, rank in pairs(faction.players) do
minetest.chat_send_player(player, p.." ("..rank..")")
end
return true
end
})
factions.register_command("newrank", {
description = "Add a new rank.",
format = {"string"},
faction_permissions = {"ranks"},
on_success = function(player, faction, pos, parcelpos, args)
local rank = args.strings[1]
if #rank > factions.rank then
send_error(player, "Go away Todd")
return false
end
if faction.ranks[rank] then
send_error(player, "Rank already exists")
return false
end
faction:add_rank(rank, args.other)
return true
end
})
factions.register_command("delrank", {
description = "Replace and delete a rank.",
format = {"string", "string"},
faction_permissions = {"ranks"},
on_success = function(player, faction, pos, parcelpos, args)
local rank = args.strings[1]
local newrank = args.strings[2]
if not faction.ranks[rank] or not faction.ranks[newrank] then
send_error(player, "One of the specified ranks do not exist.")
return false
end
faction:delete_rank(rank, newrank)
return true
end
})
factions.register_command("setspawn", {
description = "Set the faction's spawn",
faction_permissions = {"spawn"},
on_success = function(player, faction, pos, parcelpos, args)
faction:set_spawn(pos)
return true
end
})
factions.register_command("where", {
description = "See whose parcel you stand on.",
infaction = false,
on_success = function(player, faction, pos, parcelpos, args)
local parcel_faction = factions.get_parcel_faction(parcelpos)
local place_name = (parcel_faction and parcel_faction.name) or "Wilderness"
minetest.chat_send_player(player, "You are standing on parcel "..parcelpos..", part of "..place_name)
return true
end
})
factions.register_command("help", {
description = "Shows help for commands.",
infaction = false,
on_success = function(player, faction, pos, parcelpos, args)
factions_chat.show_help(player)
return true
end
})
factions.register_command("spawn", {
description = "Shows your faction's spawn",
on_success = function(player, faction, pos, parcelpos, args)
local spawn = faction.spawn
if spawn then
local spawn = {spawn.x, spawn.y, spawn.z}
minetest.chat_send_player(player, "Spawn is at ("..table.concat(spawn, ", ")..")")
return true
else
minetest.chat_send_player(player, "Your faction has no spawn set.")
return false
end
end
})
factions.register_command("promote", {
description = "Promotes a player to a rank",
format = {"player", "string"},
faction_permissions = {"promote"},
on_success = function(player, faction, pos, parcelpos, args)
local rank = args.strings[1]
if faction.ranks[rank] then
faction:promote(args.players[1]:get_player_name(), rank)
return true
else
send_error(player, "The specified rank does not exist.")
return false
end
end
})
factions.register_command("power", {
description = "Display your faction's power",
on_success = function(player, faction, pos, parcelpos, args)
minetest.chat_send_player(player, "Power: "..faction.power.."/"..faction.maxpower - faction.usedpower.."/"..faction.maxpower)
return true
end
})
factions.register_command("setbanner", {
description = "Sets the banner you're on as the faction's banner.",
faction_permissions = {"banner"},
on_success = function(player, faction, pos, parcelpos, args)
local meta = minetest.get_meta({x = pos.x, y = pos.y - 1, z = pos.z})
local banner = meta:get_string("banner")
if not banner then
minetest.chat_send_player(player, "No banner found.")
return false
end
faction:set_banner(banner)
end
})
factions.register_command("convert", {
description = "Load factions in the old format",
infaction = false,
global_privileges = {"faction_admin"},
format = {"string"},
on_success = function(player, faction, pos, parcelpos, args)
if factions.convert(args.strings[1]) then
minetest.chat_send_player(player, "Factions successfully converted.")
else
minetest.chat_send_player(player, "Error.")
end
return true
end
})
factions.register_command("free", {
description = "Forcefully frees a parcel",
infaction = false,
global_privileges = {"faction_admin"},
on_success = function(player, faction, pos, parcelpos, args)
local parcel_faction = factions.get_parcel_faction(parcelpos)
if not parcel_faction then
send_error(player, "No claim at this position")
return false
else
parcel_faction:unclaim_parcel(parcelpos)
return true
end
end
})
factions.register_command("chat", {
description = "Send a message to your faction's members",
on_success = function(player, faction, pos, parcelpos, args)
local msg = table.concat(args.other, " ")
faction:broadcast(msg, player)
end
})
factions.register_command("forceupdate", {
description = "Forces an update tick.",
global_privileges = {"faction_admin"},
on_success = function(player, faction, pos, parcelpos, args)
factions.faction_tick()
end
})
factions.register_command("which", {
description = "Gets a player's faction",
infaction = false,
format = {"string"},
on_success = function(player, faction, pos, parcelpos, args)
local playername = args.strings[1]
local faction = factions.get_player_faction(playername)
if not faction then
send_error(player, "Player "..playername.." does not belong to any faction")
return false
else
minetest.chat_send_player(player, "player "..playername.." belongs to faction "..faction.name)
return true
end
end
})
factions.register_command("setleader", {
description = "Set a player as a faction's leader",
infaction = false,
global_privileges = {"faction_admin"},
format = {"faction", "player"},
on_success = function(player, faction, pos, parcelpos, args)
local playername = args.players[1]:get_player_name()
local playerfaction = factions.get_player_faction(playername)
local targetfaction = args.factions[1]
if playerfaction.name ~= targetfaction.name then
send_error(player, "Player "..playername.." is not in faction "..targetfaction.name..".")
return false
end
targetfaction:set_leader(playername)
return true
end
})
factions.register_command("setadmin", {
description = "Make a faction an admin faction",
infaction = false,
global_privileges = {"faction_admin"},
format = {"faction"},
on_success = function(player, faction, pos, parcelpos, args)
args.factions[1].is_admin = false
return true
end
})
factions.register_command("resetpower", {
description = "Reset a faction's power",
infaction = false,
global_privileges = {"faction_admin"},
format = {"faction"},
on_success = function(player, faction, pos, parcelpos, args)
args.factions[1].power = 0
return true
end
})
factions.register_command("obliterate", {
description = "Remove all factions",
infaction = false,
global_privileges = {"faction_admin"},
on_success = function(player, faction, pos, parcelpos, args)
for _, f in pairs(factions.factions) do
f:disband("obliterated")
end
return true
end
})
factions.register_command("getspawn", {
description = "Get a faction's spawn",
infaction = false,
global_privileges = {"faction_admin"},
format = {"faction"},
on_success = function(player, faction, pos, parcelpos, args)
local spawn = args.factions[1].spawn
if spawn then
minetest.chat_send_player(player, spawn.x..","..spawn.y..","..spawn.z)
return true
else
send_error(player, "Faction has no spawn set.")
return false
end
end
})
factions.register_command("whoin", {
description = "Get all members of a faction.",
infaction = false,
global_privileges = {"faction_admin"},
format = {"faction"},
on_success = function(player, faction, pos, parcelpos, args)
local msg = {}
for player, _ in pairs(args.factions[1].players) do
table.insert(msg, player)
end
minetest.chat_send_player(player, table.concat(msg, ", "))
return true
end
})
factions.register_command("stats", {
description = "Get stats of a faction.",
infaction = false,
global_privileges = {"faction_admin"},
format = {"faction"},
on_success = function(player, faction, pos, parcelpos, args)
local f = args.factions[1]
minetest.chat_send_player(player, "Power: "..f.power.."/"..f.maxpower - f.usedpower.."/"..f.maxpower)
return true
end
})
factions.register_command("seen", {
description = "Check the last time a faction had a member logged in",
infaction = false,
global_privileges = {"faction_admin"},
format = {"faction"},
on_success = function(player, faction, pos, parcelpos, args)
local lastseen = args.factions[1].last_logon
local now = os.time()
local time = now - lastseen
local minutes = math.floor(time / 60)
local hours = math.floor(minutes / 60)
local days = math.floor(hours / 24)
minetest.chat_send_player(player, "Last seen "..days.." day(s), "..
hours % 24 .." hour(s), "..minutes % 60 .." minutes, "..time % 60 .." second(s) ago.")
return true
end
})
-------------------------------------------------------------------------------
-- name: cmdhandler(playername,parameter)
--
--! @brief chat command handler
--! @memberof factions_chat
--! @private
--
--! @param playername name
--! @param parameter data supplied to command
-------------------------------------------------------------------------------
factions_chat.cmdhandler = function (playername,parameter)
local player = minetest.env:get_player_by_name(playername)
local params = parameter:split(" ")
local player_faction = factions.get_player_faction(playername)
if parameter == nil or
parameter == "" then
if player_faction then
minetest.chat_send_player(playername, "You are in faction "..player_faction.name..". Type /f help for a list of commands.")
else
minetest.chat_send_player(playername, "You are part of no faction")
end
return
end
local cmd = factions.commands[params[1]]
if not cmd then
send_error(playername, "Unknown command.")
return false
end
local argv = {}
for i=2, #params, 1 do
table.insert(argv, params[i])
end
cmd:run(playername, argv)
end
-------------------------------------------------------------------------------
-- name: show_help(playername,parameter)
--
--! @brief send help message to player
--! @memberof factions_chat
--! @private
--
--! @param playername name
-------------------------------------------------------------------------------
function factions_chat.show_help(playername)
local MSG = function(text)
minetest.chat_send_player(playername,text,false)
end
MSG("factions mod")
MSG("Usage:")
for k, v in pairs(factions.commands) do
local args = {}
for i in ipairs(v.format) do
table.insert(args, v.format[i])
end
MSG("\t/factions "..k.." <"..table.concat(args, "> <").."> : "..v.description)
end
end
init_commands()