311a5a4e2605fd0343d863172fa77d58c837563e
[protector.git] / init.lua
1 minetest.register_privilege("delprotect","Ignore player protection")
2
3 protector = {}
4 protector.mod = "redo"
5 protector.radius = (tonumber(minetest.setting_get("protector_radius")) or 5)
6
7 protector.get_member_list = function(meta)
8
9 return meta:get_string("members"):split(" ")
10 end
11
12 protector.set_member_list = function(meta, list)
13
14 meta:set_string("members", table.concat(list, " "))
15 end
16
17 protector.is_member = function (meta, name)
18
19 for _, n in ipairs(protector.get_member_list(meta)) do
20
21 if n == name then
22 return true
23 end
24 end
25
26 return false
27 end
28
29 protector.add_member = function(meta, name)
30
31 if protector.is_member(meta, name) then
32 return
33 end
34
35 local list = protector.get_member_list(meta)
36
37 table.insert(list, name)
38
39 protector.set_member_list(meta, list)
40 end
41
42 protector.del_member = function(meta, name)
43
44 local list = protector.get_member_list(meta)
45
46 for i, n in ipairs(list) do
47
48 if n == name then
49 table.remove(list, i)
50 break
51 end
52 end
53
54 protector.set_member_list(meta, list)
55 end
56
57 -- Protector Interface
58
59 protector.generate_formspec = function(meta)
60
61 local formspec = "size[8,7]"
62 ..default.gui_bg..default.gui_bg_img..default.gui_slots
63 .."label[2.5,0;-- Protector interface --]"
64 .."label[0,1;PUNCH node to show protected area or USE for area check]"
65 .."label[0,2;Members: (type player name then press Enter to add)]"
66 .. "button_exit[2.5,6.2;3,0.5;close_me;Close]"
67
68 local members = protector.get_member_list(meta)
69 local npp = 12 -- max users added onto protector list
70 local i = 0
71
72 for _, member in ipairs(members) do
73
74 if i < npp then
75
76 -- show username
77 formspec = formspec .. "button[" .. (i % 4 * 2)
78 .. "," .. math.floor(i / 4 + 3)
79 .. ";1.5,.5;protector_member;" .. member .. "]"
80
81 -- username remove button
82 .. "button[" .. (i % 4 * 2 + 1.25) .. ","
83 .. math.floor(i / 4 + 3)
84 .. ";.75,.5;protector_del_member_" .. member .. ";X]"
85 end
86
87 i = i + 1
88 end
89
90 if i < npp then
91
92 -- user name entry field
93 formspec = formspec .. "field[" .. (i % 4 * 2 + 1 / 3) .. ","
94 .. (math.floor(i / 4 + 3) + 1 / 3)
95 .. ";1.433,.5;protector_add_member;;]"
96
97 -- username add button
98 .."button[" .. (i % 4 * 2 + 1.25) .. ","
99 .. math.floor(i / 4 + 3) .. ";.75,.5;protector_submit;+]"
100
101 end
102
103 return formspec
104 end
105
106 -- Infolevel:
107 -- 0 for no info
108 -- 1 for "This area is owned by <owner> !" if you can't dig
109 -- 2 for "This area is owned by <owner>.
110 -- 3 for checking protector overlaps
111
112 protector.can_dig = function(r, pos, digger, onlyowner, infolevel)
113
114 if not digger
115 or not pos then
116 return false
117 end
118
119 -- Delprotect privileged users can override protections
120
121 if minetest.check_player_privs(digger, {delprotect = true})
122 and infolevel == 1 then
123 return true
124 end
125
126 if infolevel == 3 then infolevel = 1 end
127
128 -- Find the protector nodes
129
130 local positions = minetest.find_nodes_in_area(
131 {x = pos.x - r, y = pos.y - r, z = pos.z - r},
132 {x = pos.x + r, y = pos.y + r, z = pos.z + r},
133 {"protector:protect", "protector:protect2"})
134
135 local meta, owner, members
136
137 for _, pos in ipairs(positions) do
138
139 meta = minetest.get_meta(pos)
140 owner = meta:get_string("owner")
141 members = meta:get_string("members")
142
143 if owner ~= digger then
144
145 if onlyowner
146 or not protector.is_member(meta, digger) then
147
148 if infolevel == 1 then
149
150 minetest.chat_send_player(digger,
151 "This area is owned by " .. owner .. " !")
152
153 elseif infolevel == 2 then
154
155 minetest.chat_send_player(digger,
156 "This area is owned by " .. owner .. ".")
157
158 minetest.chat_send_player(digger,
159 "Protection located at: " .. minetest.pos_to_string(pos))
160
161 if members ~= "" then
162
163 minetest.chat_send_player(digger,
164 "Members: " .. members .. ".")
165 end
166 end
167
168 return false
169 end
170 end
171
172 if infolevel == 2 then
173
174 minetest.chat_send_player(digger,
175 "This area is owned by " .. owner .. ".")
176
177 minetest.chat_send_player(digger,
178 "Protection located at: " .. minetest.pos_to_string(pos))
179
180 if members ~= "" then
181
182 minetest.chat_send_player(digger,
183 "Members: " .. members .. ".")
184 end
185
186 return false
187 end
188
189 end
190
191 if infolevel == 2 then
192
193 if #positions < 1 then
194
195 minetest.chat_send_player(digger,
196 "This area is not protected.")
197 end
198
199 minetest.chat_send_player(digger, "You can build here.")
200 end
201
202 return true
203 end
204
205 -- Can node be added or removed, if so return node else true (for protected)
206
207 protector.old_is_protected = minetest.is_protected
208
209 function minetest.is_protected(pos, digger)
210
211 if not protector.can_dig(protector.radius, pos, digger, false, 1) then
212
213 -- hurt player here if required
214 --player = minetest.get_player_by_name(digger)
215 --player:set_hp(player:get_hp() - 2)
216
217 return true
218 end
219
220 return protector.old_is_protected(pos, digger)
221
222 end
223
224 -- Make sure protection block doesn't overlap another protector's area
225
226 function protector.check_overlap(itemstack, placer, pointed_thing)
227
228 if pointed_thing.type ~= "node" then
229 return itemstack
230 end
231
232 if not protector.can_dig(protector.radius * 2, pointed_thing.under,
233 placer:get_player_name(), true, 3)
234 or not protector.can_dig(protector.radius * 2, pointed_thing.above,
235 placer:get_player_name(), true, 3) then
236
237 minetest.chat_send_player(placer:get_player_name(),
238 "Overlaps into above players protected area")
239
240 return
241 end
242
243 return minetest.item_place(itemstack, placer, pointed_thing)
244
245 end
246
247 --= Protection Block
248
249 minetest.register_node("protector:protect", {
250 description = "Protection Block",
251 drawtype = "nodebox",
252 tiles = {
253 "moreblocks_circle_stone_bricks.png",
254 "moreblocks_circle_stone_bricks.png",
255 "moreblocks_circle_stone_bricks.png^protector_logo.png"
256 },
257 sounds = default.node_sound_stone_defaults(),
258 groups = {dig_immediate = 2, unbreakable = 1},
259 is_ground_content = false,
260 paramtype = "light",
261 light_source = 4,
262
263 node_box = {
264 type = "fixed",
265 fixed = {
266 {-0.5 ,-0.5, -0.5, 0.5, 0.5, 0.5},
267 }
268 },
269
270 on_place = protector.check_overlap,
271
272 after_place_node = function(pos, placer)
273
274 local meta = minetest.get_meta(pos)
275
276 meta:set_string("owner", placer:get_player_name() or "")
277 meta:set_string("infotext", "Protection (owned by " .. meta:get_string("owner") .. ")")
278 meta:set_string("members", "")
279 end,
280
281 on_use = function(itemstack, user, pointed_thing)
282
283 if pointed_thing.type ~= "node" then
284 return
285 end
286
287 protector.can_dig(protector.radius, pointed_thing.under, user:get_player_name(), false, 2)
288 end,
289
290 on_rightclick = function(pos, node, clicker, itemstack)
291
292 local meta = minetest.get_meta(pos)
293
294 if protector.can_dig(1, pos,clicker:get_player_name(), true, 1) then
295 minetest.show_formspec(clicker:get_player_name(),
296 "protector:node_" .. minetest.pos_to_string(pos), protector.generate_formspec(meta))
297 end
298 end,
299
300 on_punch = function(pos, node, puncher)
301
302 if not protector.can_dig(1, pos, puncher:get_player_name(), true, 1) then
303 return
304 end
305
306 minetest.add_entity(pos, "protector:display")
307 end,
308
309 can_dig = function(pos, player)
310
311 return protector.can_dig(1, pos, player:get_player_name(), true, 1)
312 end,
313 })
314
315 minetest.register_craft({
316 output = "protector:protect",
317 recipe = {
318 {"default:stone", "default:stone", "default:stone"},
319 {"default:stone", "default:steel_ingot", "default:stone"},
320 {"default:stone", "default:stone", "default:stone"},
321 }
322 })
323
324 --= Protection Logo
325
326 minetest.register_node("protector:protect2", {
327 description = "Protection Logo",
328 tiles = {"protector_logo.png"},
329 wield_image = "protector_logo.png",
330 inventory_image = "protector_logo.png",
331 sounds = default.node_sound_stone_defaults(),
332 groups = {dig_immediate = 2, unbreakable = 1},
333 paramtype = 'light',
334 paramtype2 = "wallmounted",
335 legacy_wallmounted = true,
336 light_source = 4,
337 drawtype = "nodebox",
338 sunlight_propagates = true,
339 walkable = true,
340 node_box = {
341 type = "wallmounted",
342 wall_top = {-0.375, 0.4375, -0.5, 0.375, 0.5, 0.5},
343 wall_bottom = {-0.375, -0.5, -0.5, 0.375, -0.4375, 0.5},
344 wall_side = {-0.5, -0.5, -0.375, -0.4375, 0.5, 0.375},
345 },
346 selection_box = {type = "wallmounted"},
347
348 on_place = protector.check_overlap,
349
350 after_place_node = function(pos, placer)
351
352 local meta = minetest.get_meta(pos)
353
354 meta:set_string("owner", placer:get_player_name() or "")
355 meta:set_string("infotext", "Protection (owned by " .. meta:get_string("owner") .. ")")
356 meta:set_string("members", "")
357 end,
358
359 on_use = function(itemstack, user, pointed_thing)
360
361 if pointed_thing.type ~= "node" then
362 return
363 end
364
365 protector.can_dig(protector.radius, pointed_thing.under, user:get_player_name(), false, 2)
366 end,
367
368 on_rightclick = function(pos, node, clicker, itemstack)
369
370 local meta = minetest.get_meta(pos)
371
372 if protector.can_dig(1, pos, clicker:get_player_name(), true, 1) then
373
374 minetest.show_formspec(clicker:get_player_name(),
375 "protector:node_" .. minetest.pos_to_string(pos), protector.generate_formspec(meta))
376 end
377 end,
378
379 on_punch = function(pos, node, puncher)
380
381 if not protector.can_dig(1, pos, puncher:get_player_name(), true, 1) then
382 return
383 end
384
385 minetest.add_entity(pos, "protector:display")
386 end,
387
388 can_dig = function(pos, player)
389
390 return protector.can_dig(1, pos, player:get_player_name(), true, 1)
391 end,
392 })
393
394 minetest.register_craft({
395 output = "protector:protect2",
396 recipe = {
397 {"default:stone", "default:stone", "default:stone"},
398 {"default:stone", "default:copper_ingot", "default:stone"},
399 {"default:stone", "default:stone", "default:stone"},
400 }
401 })
402
403 -- If name entered or button press
404
405 minetest.register_on_player_receive_fields(function(player, formname, fields)
406
407 if string.sub(formname, 0, string.len("protector:node_")) == "protector:node_" then
408
409 local pos_s = string.sub(formname, string.len("protector:node_") + 1)
410 local pos = minetest.string_to_pos(pos_s)
411 local meta = minetest.get_meta(pos)
412
413 if not protector.can_dig(1, pos, player:get_player_name(), true, 1) then
414 return
415 end
416
417 if fields.protector_add_member then
418
419 for _, i in ipairs(fields.protector_add_member:split(" ")) do
420 protector.add_member(meta, i)
421 end
422 end
423
424 for field, value in pairs(fields) do
425
426 if string.sub(field, 0, string.len("protector_del_member_")) == "protector_del_member_" then
427 protector.del_member(meta, string.sub(field,string.len("protector_del_member_") + 1))
428 end
429 end
430
431 if not fields.close_me then
432 minetest.show_formspec(player:get_player_name(), formname, protector.generate_formspec(meta))
433 end
434
435 end
436
437 end)
438
439 -- Display entity shown when protector node is punched
440
441 minetest.register_entity("protector:display", {
442 physical = false,
443 collisionbox = {0, 0, 0, 0, 0, 0},
444 visual = "wielditem",
445 -- wielditem seems to be scaled to 1.5 times original node size
446 visual_size = {x = 1.0 / 1.5, y = 1.0 / 1.5},
447 textures = {"protector:display_node"},
448 timer = 0,
449
450 on_activate = function(self, staticdata)
451
452 -- Xanadu server only
453 if mobs and mobs.entity and mobs.entity == false then
454 self.object:remove()
455 end
456 end,
457
458 on_step = function(self, dtime)
459
460 self.timer = self.timer + dtime
461
462 if self.timer > 5 then
463 self.object:remove()
464 end
465 end,
466 })
467
468 -- Display-zone node, Do NOT place the display as a node,
469 -- it is made to be used as an entity (see above)
470
471 local x = protector.radius
472 minetest.register_node("protector:display_node", {
473 tiles = {"protector_display.png"},
474 use_texture_alpha = true,
475 walkable = false,
476 drawtype = "nodebox",
477 node_box = {
478 type = "fixed",
479 fixed = {
480 -- sides
481 {-(x+.55), -(x+.55), -(x+.55), -(x+.45), (x+.55), (x+.55)},
482 {-(x+.55), -(x+.55), (x+.45), (x+.55), (x+.55), (x+.55)},
483 {(x+.45), -(x+.55), -(x+.55), (x+.55), (x+.55), (x+.55)},
484 {-(x+.55), -(x+.55), -(x+.55), (x+.55), (x+.55), -(x+.45)},
485 -- top
486 {-(x+.55), (x+.45), -(x+.55), (x+.55), (x+.55), (x+.55)},
487 -- bottom
488 {-(x+.55), -(x+.55), -(x+.55), (x+.55), -(x+.45), (x+.55)},
489 -- middle (surround protector)
490 {-.55,-.55,-.55, .55,.55,.55},
491 },
492 },
493 selection_box = {
494 type = "regular",
495 },
496 paramtype = "light",
497 groups = {dig_immediate = 3, not_in_creative_inventory = 1},
498 drop = "",
499 })
500
501 dofile(minetest.get_modpath("protector") .. "/doors_chest.lua")
502 dofile(minetest.get_modpath("protector") .. "/pvp.lua")
503 dofile(minetest.get_modpath("protector") .. "/admin.lua")
504
505 print ("[MOD] Protector Redo loaded")