-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.lua
344 lines (290 loc) · 8.17 KB
/
init.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
-- badges/init.lua
badges = {}
local registered = {}
local default
local chat_send_all = minetest.chat_send_all
if rawget(_G, "discord") then
chat_send_all = discord.chat_send_all
end
---
--- API
---
-- [local function] Get colour
local function get_colour(colour)
if type(colour) == "table" and minetest.rgba then
return minetest.rgba(colour.r, colour.g, colour.b, colour.a)
elseif type(colour) == "string" then
return colour
else
return "#ffffff"
end
end
-- [function] Register badge
function badges.register(name, def)
assert(name ~= "clear", "Invalid name \"clear\" for badge")
registered[name] = def
if def.default then
default = name
end
end
-- [function] Unregister badge
function badges.unregister(name)
registered[name] = nil
end
-- [function] List badges in plain text
function badges.list_plaintext()
local list = ""
for badge, i in pairs(registered) do
if list == "" then
list = badge
else
list = list..", "..badge
end
end
return list
end
-- [function] Get player badge
function badges.get_badge(player)
if type(player) == "string" then
player = minetest.get_player_by_name(player)
end
local badge = player:get_attribute("badges:badge")
if badge and registered[badge] then
return badge
end
end
-- [function] Get badge definition
function badges.get_def(badge)
if not badge then
return
end
return registered[badge]
end
-- [function] Update player privileges
function badges.update_privs(player, trigger)
if type(player) == "string" then
player = minetest.get_player_by_name(player)
end
if not player then
return
end
local name = player:get_player_name()
local badge = badges.get_badge(player)
if badge then
-- [local function] Warn
local function warn(msg)
if msg and trigger and minetest.get_player_by_name(trigger) then
minetest.chat_send_player(trigger, minetest.colorize("red", "Warning: ")..msg)
end
end
local def = registered[badge]
if not def.privs then
return
end
if def.strict_privs == true then
minetest.set_player_privs(name, def.privs)
warn(name.."'s privileges have been reset to that of their badge (strict privileges)")
return true
end
local privs = minetest.get_player_privs(name)
if def.grant_missing == true then
local changed = false
for name, priv in pairs(def.privs) do
if not privs[name] and priv == true then
privs[name] = priv
changed = true
end
end
if changed then
warn("Missing badge privileges have been granted to "..name)
end
end
if def.revoke_extra == true then
local changed = false
for name, priv in pairs(privs) do
if not def.privs[name] then
privs[name] = nil
changed = true
end
end
if changed then
warn("Extra non-badge privileges have been revoked from "..name)
end
end
local admin = player:get_player_name() == minetest.settings:get("name")
-- If owner, grant `badge` privilege
if admin then
local name = player:get_player_name()
local privs = minetest.get_player_privs(name)
privs["badge"] = true
minetest.set_player_privs(name, privs)
end
minetest.set_player_privs(name, privs)
return true
end
end
-- [function] Set player badge
function badges.set_badge(player, badge)
if type(player) == "string" then
player = minetest.get_player_by_name(player)
end
if registered[badge] then
-- Set attribute
player:set_attribute("badges:badge", badge)
-- Update privileges
badges.update_privs(player)
return true
end
end
-- [function] Remove badge from player
function badges.remove_badge(player)
if type(player) == "string" then
player = minetest.get_player_by_name(player)
end
local badge = badges.get_badge(player)
if badge then
local name = player:get_player_name()
-- Clear attribute
player:set_attribute("badges:badge", nil)
-- Update privileges
local basic_privs =
minetest.string_to_privs(minetest.settings:get("basic_privs") or "interact,shout")
minetest.set_player_privs(name, basic_privs)
end
end
-- [function] Send prefixed message (if enabled)
function badges.chat_send(name, message)
if minetest.settings:get("badges.prefix_chat") ~= "false" then
local badge = badges.get_badge(name)
if badge then
local def = badges.get_def(badge)
if def.prefix then
local colour = get_colour(def.colour)
local prefix = minetest.colorize(colour, def.prefix)
chat_send_all(prefix.." <"..name..">: "..message)
return true
end
end
chat_send_all("<"..name..">: "..message)
return true
end
end
---
--- Registrations
---
-- [privilege] badge
minetest.register_privilege("badge", {
description = "Permission to use /badge chatcommand",
give_to_singleplayer = false,
})
-- Assign/update badge on join player
minetest.register_on_joinplayer(function(player)
if badges.get_badge(player) then
-- Update privileges
badges.update_privs(player)
else
if badges.default then
badges.set_badge(player, badges.default)
end
end
end)
-- Prefix messages if enabled
--minetest.register_on_chat_message(function(name, message)
-- return badges.chat_send(name, message)
--end)
-- [chatcommand] /badge
minetest.register_chatcommand("badge", {
description = "Set a player's badge",
params = "<player> <new badge> / \"list\" | username, badgename / list badges",
privs = {badge = true},
func = function(name, param)
local param = param:split(" ")
if #param == 0 then
return false, "Invalid usage (see /help badge)"
end
if #param == 1 and param[1] == "list" then
return true, "Available badges: "..badges.list_plaintext()
elseif #param == 2 then
if minetest.get_player_by_name(param[1]) then
if badges.get_def(param[2]) then
if badges.set_badge(param[1], param[2]) then
if name ~= param[1] then
minetest.chat_send_player(param[1], name.." set your badge to "..param[2])
end
return true, "Set "..param[1].."'s badge to "..param[2]
else
return false, "Unknown error while setting "..param[1].."'s badge to "..param[2]
end
elseif param[2] == "clear" then
badges.remove_badge(param[1])
return true, "Removed badge from "..param[1]
else
return false, "Invalid badge (see /badge list)"
end
else
return false, "Invalid player \""..param[1].."\""
end
else
return false, "Invalid usage (see /help badge)"
end
end,
})
-- [chatcommand] /getbadge
minetest.register_chatcommand("getbadge", {
description = "Get a player's badge. If no player is specified, your own badge is returned.",
params = "<name> | name of player",
func = function(name, param)
if param and param ~= "" then
if minetest.get_player_by_name(param) then
local badge = badges.get_badge(param) or "No badge"
return true, "badge of "..param..": "..badge
else
return false, "Invalid player \""..name.."\""
end
else
local badge = badges.get_badge(name) or "No badge"
return false, "Your badge: "..badge
end
end,
})
---
--- Overrides
---
local grant = minetest.registered_chatcommands["grant"].func
-- [override] /grant
minetest.registered_chatcommands["grant"].func = function(name, param)
local ok, msg = grant(name, param) -- Call original function
local grantname, grantprivstr = string.match(param, "([^ ]+) (.+)")
if grantname then
badges.update_privs(grantname, name) -- Update privileges
end
return ok, msg
end
local grantme = minetest.registered_chatcommands["grantme"].func
-- [override] /grantme
minetest.registered_chatcommands["grantme"].func = function(name, param)
local ok, msg = grantme(name, param) -- Call original function
badges.update_privs(name, name) -- Update privileges
return ok, msg
end
local revoke = minetest.registered_chatcommands["revoke"].func
-- [override] /revoke
minetest.registered_chatcommands["revoke"].func = function(name, param)
local ok, msg = revoke(name, param) -- Call original function
local revokename, revokeprivstr = string.match(param, "([^ ]+) (.+)")
if revokename then
badges.update_privs(revokename, name) -- Update privileges
end
return ok, msg
end
---
--- badges
---
-- Load default badges
dofile(minetest.get_modpath("badges").."/badges.lua")
local path = minetest.get_worldpath().."/badges.lua"
-- Attempt to load per-world badges
if io.open(path) then
dofile(path)
end
dofile(minetest.get_modpath("badges") .. "/bossbar.lua")