8a4f46fc5a48795d0fbd4d92eae686a3763ce4f7
[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 ipairs(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 ipairs(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 ipairs(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 ipairs(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 if protector.hurt > 0
218 and player then
219 player:set_hp(player:get_hp() - protector.hurt)
220 end
221
222 if protector.drop == true
223 and player then
224 -- drop tool/item if protection violated
225 local tool = player:get_wielded_item()
226 --local wear = tool:get_wear()
227 local num = player:get_wield_index()
228 local player_inv = player:get_inventory()
229 local inv = player_inv:get_stack("main", num)
230 local sta = inv:take_item(inv:get_count())
231 local obj = minetest.add_item(player:getpos(), sta)
232
233 if obj then
234 obj:setvelocity({x = 0, y = 5, z = 0})
235 player:set_wielded_item(nil)
236 minetest.after(0.2, function()
237 player_inv:set_stack("main", num, nil)
238 end)
239 end
240 end
241
242 return true
243 end
244
245 return protector.old_is_protected(pos, digger)
246
247 end
248
249 -- Make sure protection block doesn't overlap another protector's area
250
251 function protector.check_overlap(itemstack, placer, pointed_thing)
252
253 if pointed_thing.type ~= "node" then
254 return itemstack
255 end
256
257 if not protector.can_dig(protector.radius * 2, pointed_thing.under,
258 placer:get_player_name(), true, 3)
259 or not protector.can_dig(protector.radius * 2, pointed_thing.above,
260 placer:get_player_name(), true, 3) then
261
262 minetest.chat_send_player(placer:get_player_name(),
263 "Overlaps into above players protected area")
264
265 return
266 end
267
268 return minetest.item_place(itemstack, placer, pointed_thing)
269
270 end
271
272 --= Protection Block
273
274 minetest.register_node("protector:protect", {
275 description = "Protection Block",
276 drawtype = "nodebox",
277 tiles = {
278 "moreblocks_circle_stone_bricks.png",
279 "moreblocks_circle_stone_bricks.png",
280 "moreblocks_circle_stone_bricks.png^protector_logo.png"
281 },
282 sounds = default.node_sound_stone_defaults(),
283 groups = {dig_immediate = 2, unbreakable = 1},
284 is_ground_content = false,
285 paramtype = "light",
286 light_source = 4,
287
288 node_box = {
289 type = "fixed",
290 fixed = {
291 {-0.5 ,-0.5, -0.5, 0.5, 0.5, 0.5},
292 }
293 },
294
295 on_place = protector.check_overlap,
296
297 after_place_node = function(pos, placer)
298
299 local meta = minetest.get_meta(pos)
300
301 meta:set_string("owner", placer:get_player_name() or "")
302 meta:set_string("infotext", "Protection (owned by " .. meta:get_string("owner") .. ")")
303 meta:set_string("members", "")
304 end,
305
306 on_use = function(itemstack, user, pointed_thing)
307
308 if pointed_thing.type ~= "node" then
309 return
310 end
311
312 protector.can_dig(protector.radius, pointed_thing.under, user:get_player_name(), false, 2)
313 end,
314
315 on_rightclick = function(pos, node, clicker, itemstack)
316
317 local meta = minetest.get_meta(pos)
318
319 if protector.can_dig(1, pos,clicker:get_player_name(), true, 1) then
320 minetest.show_formspec(clicker:get_player_name(),
321 "protector:node_" .. minetest.pos_to_string(pos), protector.generate_formspec(meta))
322 end
323 end,
324
325 on_punch = function(pos, node, puncher)
326
327 if not protector.can_dig(1, pos, puncher:get_player_name(), true, 1) then
328 return
329 end
330
331 minetest.add_entity(pos, "protector:display")
332 end,
333
334 can_dig = function(pos, player)
335
336 return protector.can_dig(1, pos, player:get_player_name(), true, 1)
337 end,
338 })
339
340 minetest.register_craft({
341 output = "protector:protect",
342 recipe = {
343 {"default:stone", "default:stone", "default:stone"},
344 {"default:stone", "default:steel_ingot", "default:stone"},
345 {"default:stone", "default:stone", "default:stone"},
346 }
347 })
348
349 --= Protection Logo
350
351 minetest.register_node("protector:protect2", {
352 description = "Protection Logo",
353 tiles = {"protector_logo.png"},
354 wield_image = "protector_logo.png",
355 inventory_image = "protector_logo.png",
356 sounds = default.node_sound_stone_defaults(),
357 groups = {dig_immediate = 2, unbreakable = 1},
358 paramtype = 'light',
359 paramtype2 = "wallmounted",
360 legacy_wallmounted = true,
361 light_source = 4,
362 drawtype = "nodebox",
363 sunlight_propagates = true,
364 walkable = true,
365 node_box = {
366 type = "wallmounted",
367 wall_top = {-0.375, 0.4375, -0.5, 0.375, 0.5, 0.5},
368 wall_bottom = {-0.375, -0.5, -0.5, 0.375, -0.4375, 0.5},
369 wall_side = {-0.5, -0.5, -0.375, -0.4375, 0.5, 0.375},
370 },
371 selection_box = {type = "wallmounted"},
372
373 on_place = protector.check_overlap,
374
375 after_place_node = function(pos, placer)
376
377 local meta = minetest.get_meta(pos)
378
379 meta:set_string("owner", placer:get_player_name() or "")
380 meta:set_string("infotext", "Protection (owned by " .. meta:get_string("owner") .. ")")
381 meta:set_string("members", "")
382 end,
383
384 on_use = function(itemstack, user, pointed_thing)
385
386 if pointed_thing.type ~= "node" then
387 return
388 end
389
390 protector.can_dig(protector.radius, pointed_thing.under, user:get_player_name(), false, 2)
391 end,
392
393 on_rightclick = function(pos, node, clicker, itemstack)
394
395 local meta = minetest.get_meta(pos)
396
397 if protector.can_dig(1, pos, clicker:get_player_name(), true, 1) then
398
399 minetest.show_formspec(clicker:get_player_name(),
400 "protector:node_" .. minetest.pos_to_string(pos), protector.generate_formspec(meta))
401 end
402 end,
403
404 on_punch = function(pos, node, puncher)
405
406 if not protector.can_dig(1, pos, puncher:get_player_name(), true, 1) then
407 return
408 end
409
410 minetest.add_entity(pos, "protector:display")
411 end,
412
413 can_dig = function(pos, player)
414
415 return protector.can_dig(1, pos, player:get_player_name(), true, 1)
416 end,
417 })
418
419 minetest.register_craft({
420 output = "protector:protect2",
421 recipe = {
422 {"default:stone", "default:stone", "default:stone"},
423 {"default:stone", "default:copper_ingot", "default:stone"},
424 {"default:stone", "default:stone", "default:stone"},
425 }
426 })
427
428 -- If name entered or button press
429
430 minetest.register_on_player_receive_fields(function(player, formname, fields)
431
432 if string.sub(formname, 0, string.len("protector:node_")) == "protector:node_" then
433
434 local pos_s = string.sub(formname, string.len("protector:node_") + 1)
435 local pos = minetest.string_to_pos(pos_s)
436 local meta = minetest.get_meta(pos)
437
438 if not protector.can_dig(1, pos, player:get_player_name(), true, 1) then
439 return
440 end
441
442 if fields.protector_add_member then
443
444 for _, i in ipairs(fields.protector_add_member:split(" ")) do
445 protector.add_member(meta, i)
446 end
447 end
448
449 for field, value in pairs(fields) do
450
451 if string.sub(field, 0, string.len("protector_del_member_")) == "protector_del_member_" then
452 protector.del_member(meta, string.sub(field,string.len("protector_del_member_") + 1))
453 end
454 end
455
456 if not fields.close_me then
457 minetest.show_formspec(player:get_player_name(), formname, protector.generate_formspec(meta))
458 end
459
460 end
461
462 end)
463
464 -- Display entity shown when protector node is punched
465
466 minetest.register_entity("protector:display", {
467 physical = false,
468 collisionbox = {0, 0, 0, 0, 0, 0},
469 visual = "wielditem",
470 -- wielditem seems to be scaled to 1.5 times original node size
471 visual_size = {x = 1.0 / 1.5, y = 1.0 / 1.5},
472 textures = {"protector:display_node"},
473 timer = 0,
474
475 on_activate = function(self, staticdata)
476
477 -- Xanadu server only
478 if mobs and mobs.entity and mobs.entity == false then
479 self.object:remove()
480 end
481 end,
482
483 on_step = function(self, dtime)
484
485 self.timer = self.timer + dtime
486
487 if self.timer > 5 then
488 self.object:remove()
489 end
490 end,
491 })
492
493 -- Display-zone node, Do NOT place the display as a node,
494 -- it is made to be used as an entity (see above)
495
496 local x = protector.radius
497 minetest.register_node("protector:display_node", {
498 tiles = {"protector_display.png"},
499 use_texture_alpha = true,
500 walkable = false,
501 drawtype = "nodebox",
502 node_box = {
503 type = "fixed",
504 fixed = {
505 -- sides
506 {-(x+.55), -(x+.55), -(x+.55), -(x+.45), (x+.55), (x+.55)},
507 {-(x+.55), -(x+.55), (x+.45), (x+.55), (x+.55), (x+.55)},
508 {(x+.45), -(x+.55), -(x+.55), (x+.55), (x+.55), (x+.55)},
509 {-(x+.55), -(x+.55), -(x+.55), (x+.55), (x+.55), -(x+.45)},
510 -- top
511 {-(x+.55), (x+.45), -(x+.55), (x+.55), (x+.55), (x+.55)},
512 -- bottom
513 {-(x+.55), -(x+.55), -(x+.55), (x+.55), -(x+.45), (x+.55)},
514 -- middle (surround protector)
515 {-.55,-.55,-.55, .55,.55,.55},
516 },
517 },
518 selection_box = {
519 type = "regular",
520 },
521 paramtype = "light",
522 groups = {dig_immediate = 3, not_in_creative_inventory = 1},
523 drop = "",
524 })
525
526 dofile(minetest.get_modpath("protector") .. "/doors_chest.lua")
527 dofile(minetest.get_modpath("protector") .. "/pvp.lua")
528 dofile(minetest.get_modpath("protector") .. "/admin.lua")
529
530 print ("[MOD] Protector Redo loaded")