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